SearchController.cs 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. using EyeSoft.Collections.Generic;
  2. using Humanizer;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.Mvc;
  5. using NPOI.SS.Formula.Functions;
  6. using NPOI.Util;
  7. using OASystem.API.OAMethodLib;
  8. using OASystem.API.OAMethodLib.DeepSeekAPI;
  9. using OASystem.API.OAMethodLib.GenericSearch;
  10. using OASystem.Domain.AesEncryption;
  11. using OASystem.Domain.Entities.Customer;
  12. using OASystem.Domain.Entities.Financial;
  13. using OASystem.Domain.Entities.Groups;
  14. using OASystem.Domain.ViewModels.Groups;
  15. using OASystem.Domain.ViewModels.Search;
  16. using OASystem.Infrastructure.Repositories.CRM;
  17. using OASystem.Infrastructure.Repositories.Groups;
  18. using OASystem.Infrastructure.Repositories.System;
  19. using System.Diagnostics;
  20. using static iTextSharp.text.pdf.AcroFields;
  21. namespace OASystem.API.Controllers
  22. {
  23. /// <summary>
  24. /// 搜索
  25. /// </summary>
  26. [Route("api/search")]
  27. [ApiController]
  28. public class SearchController : ControllerBase
  29. {
  30. private readonly SqlSugarClient _sqlSugar;
  31. private readonly DelegationInfoRepository _groupRep;
  32. private readonly DynamicSearchService<Grp_DelegationInfo> _groupSearchService;
  33. private readonly DynamicSearchService<NewClientDataView> _clientSearchService;
  34. private readonly DynamicSearchService<InvitationAIInvNameView> _invAISearchService;
  35. private readonly DynamicSearchService<AirInfo> _airSearchService;
  36. private readonly NewClientDataRepository _clientDataRepository;
  37. public SearchController(
  38. SqlSugarClient sqlSugar,
  39. DelegationInfoRepository groupRep,
  40. DynamicSearchService<Grp_DelegationInfo> groupSearchService,
  41. DynamicSearchService<NewClientDataView> clientSearchService,
  42. DynamicSearchService<InvitationAIInvNameView> invAISearchService,
  43. DynamicSearchService<AirInfo> airSearchService,
  44. NewClientDataRepository clientDataRepository
  45. )
  46. {
  47. _sqlSugar = sqlSugar;
  48. _groupRep = groupRep;
  49. _clientDataRepository = clientDataRepository;
  50. _groupSearchService = groupSearchService;
  51. _clientSearchService = clientSearchService;
  52. _airSearchService = airSearchService;
  53. _invAISearchService = invAISearchService;
  54. }
  55. /// <summary>
  56. /// 接团信息 关键字输入提示(单字段)
  57. /// </summary>
  58. /// <param name="keyword">关键字</param>
  59. /// <returns></returns>
  60. [HttpGet("group/{keyword}")]
  61. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  62. public async Task<IActionResult> GroupKeywordSearch(string keyword)
  63. {
  64. try
  65. {
  66. // 验证请求参数
  67. if (string.IsNullOrEmpty(keyword))
  68. {
  69. return Ok(JsonView(true, $"暂无数据!"));
  70. }
  71. var searchRequest = new DynamicSearchRequest
  72. {
  73. Keyword = keyword,
  74. RequireAllSingleChars = true,
  75. PageIndex = 1,
  76. PageSize = 999999,
  77. FieldWeights = new Dictionary<string, int>
  78. {
  79. { "TeamName", 10 },
  80. //{ "ClientUnit", 8 },
  81. //{ "ClientName", 6 }
  82. },
  83. Filters = new List<SearchFilter>()
  84. {
  85. new(){Field = "IsDel",Operator="eq",Value="0" }
  86. },
  87. OrderBy = "TeamName",
  88. ReturnFields = new List<string>() { "TeamName" }
  89. };
  90. // 验证字段配置
  91. var validation = _groupSearchService.ValidateFieldConfig(
  92. searchRequest.FieldWeights,
  93. searchRequest.ReturnFields);
  94. if (!validation.IsValid)
  95. {
  96. return Ok(JsonView(true, $"暂无数据!{validation.Message}"));
  97. }
  98. var result = await _groupSearchService.SearchAsync(searchRequest);
  99. if (result.Success)
  100. {
  101. var data = result.Items.Select(x => new { x.Data.Id, x.Data.TeamName }).ToList();
  102. return Ok(JsonView(true, result.Message, data, data.Count));
  103. }
  104. return Ok(JsonView(true, result.Message));
  105. }
  106. catch (Exception ex)
  107. {
  108. return Ok(JsonView(true, $"搜索服务暂时不可用!"));
  109. }
  110. }
  111. /// <summary>
  112. /// 客户资料 关键字输入提示(多字段)
  113. /// </summary>
  114. /// <param name="userId">关键字</param>
  115. /// <param name="keyword">关键字</param>
  116. /// <returns></returns>
  117. [HttpGet("group/{userId}/{keyword}")]
  118. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  119. public async Task<IActionResult> ClientKeywordSearch(int userId, string keyword)
  120. {
  121. try
  122. {
  123. // 验证请求参数
  124. if (string.IsNullOrEmpty(keyword))
  125. {
  126. return Ok(JsonView(true, $"暂无数据!"));
  127. }
  128. //查询有权限的数据
  129. var clientIds = await _sqlSugar
  130. .Queryable<Crm_ClientDataAndUser>()
  131. .Where(x => x.IsDel == 0 && x.usersId == userId)
  132. .Select(x => x.NewClientDataId)
  133. .ToListAsync();
  134. if (clientIds == null || clientIds.Count < 1)
  135. {
  136. return Ok(JsonView(true, $"暂无数据!"));
  137. }
  138. var data = _sqlSugar.Queryable<Crm_NewClientData>()
  139. .Where(x => x.IsDel == 0 && clientIds.Contains(x.Id))
  140. .OrderByDescending(x => x.CreateTime)
  141. .Select(x => new NewClientDataView()
  142. {
  143. Id = x.Id,
  144. Client = x.Client,
  145. Contact = x.Contact,
  146. Job = x.Job,
  147. Telephone = x.Telephone,
  148. Location = x.Location,
  149. })
  150. .ToList();
  151. int index = 0;
  152. data.ForEach(x =>
  153. {
  154. index++;
  155. x.RowNumber = index;
  156. x.Client = AesEncryptionHelper.Decrypt(x.Client);
  157. x.Contact = AesEncryptionHelper.Decrypt(x.Contact);
  158. x.Job = AesEncryptionHelper.Decrypt(x.Job);
  159. x.Telephone = AesEncryptionHelper.Decrypt(x.Telephone);
  160. x.Location = AesEncryptionHelper.Decrypt(x.Location);
  161. });
  162. var searchRequest = new DynamicSearchRequest
  163. {
  164. Keyword = keyword,
  165. RequireAllSingleChars = true,
  166. PageIndex = 1,
  167. PageSize = 999999,
  168. FieldWeights = new Dictionary<string, int>
  169. {
  170. { "Client", 10 },
  171. { "Contact", 8 },
  172. { "Location", 6 }
  173. },
  174. OrderBy = "CreateTime"
  175. };
  176. // 验证字段配置
  177. var validation = _clientSearchService.ValidateFieldConfig(
  178. searchRequest.FieldWeights,
  179. searchRequest.ReturnFields);
  180. if (!validation.IsValid)
  181. {
  182. return Ok(JsonView(true, $"暂无数据!{validation.Message}"));
  183. }
  184. var result = _clientSearchService.SearchDataSource(searchRequest, data);
  185. if (result.Success)
  186. {
  187. var view = result.Items.Select(x => x.Data).ToList();
  188. int resetIndex = 0;
  189. view.ForEach(x =>
  190. {
  191. resetIndex++;
  192. x.RowNumber = resetIndex;
  193. });
  194. return Ok(JsonView(true, result.Message, view, view.Count));
  195. }
  196. return Ok(JsonView(true, "暂无数据"));
  197. }
  198. catch (Exception ex)
  199. {
  200. return Ok(JsonView(true, $"搜索服务暂时不可用!"));
  201. }
  202. }
  203. /// <summary>
  204. /// 客户资料
  205. /// </summary>
  206. /// <param name="userId">关键字</param>
  207. /// <param name="keyword">关键字</param>
  208. /// <returns></returns>
  209. [HttpGet("ClientKeywordSearch/{userId}/{keyword}")]
  210. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  211. public async Task<IActionResult> ClientKeywordSearchAll(int userId, string keyword)
  212. {
  213. try
  214. {
  215. // 验证请求参数
  216. if (string.IsNullOrEmpty(keyword))
  217. {
  218. return Ok(JsonView(true, $"暂无数据!"));
  219. }
  220. var userIds = _clientDataRepository.GetNewExistClient(userId).Select(x => x.Id).ToList();
  221. //查询有权限的数据(包括负责用户)
  222. var clientIds = await _sqlSugar
  223. .Queryable<Crm_ClientDataAndUser>()
  224. .Where(x => x.IsDel == 0 && userIds.Contains(x.usersId))
  225. .Select(x => x.NewClientDataId)
  226. .ToListAsync();
  227. if (clientIds == null || clientIds.Count < 1)
  228. {
  229. return Ok(JsonView(true, $"暂无数据!"));
  230. }
  231. var data = _sqlSugar.Queryable<Crm_NewClientData>()
  232. .Where(x => x.IsDel == 0 && clientIds.Contains(x.Id))
  233. .OrderByDescending(x => x.CreateTime)
  234. .Select(x => new NewClientDataView()
  235. {
  236. Id = x.Id,
  237. Client = x.Client,
  238. Contact = x.Contact,
  239. Job = x.Job,
  240. Telephone = x.Telephone,
  241. Location = x.Location,
  242. })
  243. .ToList();
  244. int index = 0;
  245. data.ForEach(x =>
  246. {
  247. index++;
  248. x.RowNumber = index;
  249. x.Client = AesEncryptionHelper.Decrypt(x.Client);
  250. x.Contact = AesEncryptionHelper.Decrypt(x.Contact);
  251. x.Job = AesEncryptionHelper.Decrypt(x.Job);
  252. x.Telephone = AesEncryptionHelper.Decrypt(x.Telephone);
  253. x.Location = AesEncryptionHelper.Decrypt(x.Location);
  254. });
  255. var searchRequest = new DynamicSearchRequest
  256. {
  257. Keyword = keyword,
  258. RequireAllSingleChars = true,
  259. PageIndex = 1,
  260. PageSize = 999999,
  261. FieldWeights = new Dictionary<string, int>
  262. {
  263. { "Client", 10 },
  264. { "Contact", 8 },
  265. { "Location", 6 }
  266. },
  267. OrderBy = "CreateTime"
  268. };
  269. // 验证字段配置
  270. var validation = _clientSearchService.ValidateFieldConfig(
  271. searchRequest.FieldWeights,
  272. searchRequest.ReturnFields);
  273. if (!validation.IsValid)
  274. {
  275. return Ok(JsonView(true, $"暂无数据!{validation.Message}"));
  276. }
  277. var result = _clientSearchService.SearchDataSource(searchRequest, data);
  278. if (result.Success)
  279. {
  280. var view = result.Items.Select(x => x.Data).ToList();
  281. int resetIndex = 0;
  282. view.ForEach(x =>
  283. {
  284. resetIndex++;
  285. x.RowNumber = resetIndex;
  286. });
  287. return Ok(JsonView(true, result.Message, view, view.Count));
  288. }
  289. return Ok(JsonView(true, "暂无数据"));
  290. }
  291. catch (Exception ex)
  292. {
  293. return Ok(JsonView(true, $"搜索服务暂时不可用!"));
  294. }
  295. }
  296. /// <summary>
  297. /// 团组、会务流程 关键字输入提示(智能版)
  298. /// </summary>
  299. /// <param name="keyword">关键字</param>
  300. /// <returns></returns>
  301. [HttpGet]
  302. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  303. public async Task<IActionResult> ProcessKeywordSearch(string keyword)
  304. {
  305. try
  306. {
  307. // 验证请求参数
  308. if (string.IsNullOrEmpty(keyword))
  309. {
  310. return Ok(JsonView(true, $"暂无数据!"));
  311. }
  312. var searchRequest = new DynamicSearchRequest
  313. {
  314. Keyword = keyword,
  315. RequireAllSingleChars = true,
  316. PageIndex = 1,
  317. PageSize = 999999,
  318. FieldWeights = new Dictionary<string, int>
  319. {
  320. { "TeamName", 10 }
  321. },
  322. Filters = new List<SearchFilter>()
  323. {
  324. new(){Field = "IsDel",Operator="eq",Value="0" }
  325. },
  326. OrderBy = "VisitDate",
  327. ReturnFields = new List<string>() { "TeamName" }
  328. };
  329. // 验证字段配置
  330. var validation = _groupSearchService.ValidateFieldConfig(
  331. searchRequest.FieldWeights,
  332. searchRequest.ReturnFields);
  333. if (!validation.IsValid)
  334. {
  335. return Ok(JsonView(true, $"暂无数据!{validation.Message}"));
  336. }
  337. var result = await _groupSearchService.SearchAsync(searchRequest);
  338. if (result.Success)
  339. {
  340. var data = new List<dynamic>();
  341. foreach (var item in result.Items)
  342. {
  343. data.Add(new
  344. {
  345. item.Data.Id,
  346. item.Data.TeamName,
  347. });
  348. }
  349. return Ok(JsonView(true, result.Message, data, data.Count));
  350. }
  351. return Ok(JsonView(true, result.Message));
  352. }
  353. catch (Exception ex)
  354. {
  355. return Ok(JsonView(true, $"搜索服务暂时不可用!"));
  356. }
  357. }
  358. /// <summary>
  359. /// 团组会务成本 关键字输入提示(智能版)
  360. /// </summary>
  361. /// <param name="keyword">关键字</param>
  362. /// <returns></returns>
  363. [HttpGet("ConferenceAffairsKeywordSearch/{keyword}")]
  364. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  365. public async Task<IActionResult> ConferenceAffairsKeywordSearch(string keyword)
  366. {
  367. try
  368. {
  369. // 验证请求参数
  370. if (string.IsNullOrEmpty(keyword))
  371. {
  372. return Ok(JsonView(true, $"暂无数据!"));
  373. }
  374. var hwIds = _sqlSugar.Queryable<Sys_SetData>()
  375. .Where(x => x.IsDel == 0 && x.STid == 10 && x.Name.Contains("会务活动"))
  376. .Select(x => x.Id)
  377. .ToList();
  378. var object_hwIds = hwIds.ConvertAll<object>(x => x);
  379. var searchRequest = new DynamicSearchRequest
  380. {
  381. Keyword = keyword,
  382. RequireAllSingleChars = true,
  383. PageIndex = 1,
  384. PageSize = 999999,
  385. FieldWeights = new Dictionary<string, int>
  386. {
  387. { "TeamName", 10 }
  388. },
  389. Filters = new List<SearchFilter>()
  390. {
  391. new(){Field = "IsDel",Operator="eq",Value="0" },
  392. new(){Field = "TeamDid",Operator="in",Values=object_hwIds }
  393. },
  394. OrderBy = "VisitDate",
  395. ReturnFields = new List<string>() { "TeamName" }
  396. };
  397. // 验证字段配置
  398. var validation = _groupSearchService.ValidateFieldConfig(
  399. searchRequest.FieldWeights,
  400. searchRequest.ReturnFields);
  401. if (!validation.IsValid)
  402. {
  403. return Ok(JsonView(true, $"暂无数据!{validation.Message}"));
  404. }
  405. var result = await _groupSearchService.SearchAsync(searchRequest);
  406. if (result.Success)
  407. {
  408. var data = new List<dynamic>();
  409. foreach (var item in result.Items)
  410. {
  411. data.Add(new
  412. {
  413. item.Data.Id,
  414. item.Data.TeamName,
  415. });
  416. }
  417. return Ok(JsonView(true, result.Message, data, data.Count));
  418. }
  419. return Ok(JsonView(true, result.Message));
  420. }
  421. catch (Exception ex)
  422. {
  423. return Ok(JsonView(true, $"搜索服务暂时不可用!"));
  424. }
  425. }
  426. #region 团组费用-机票费用
  427. /// <summary>
  428. /// 团组机票费用基本信息
  429. /// </summary>
  430. /// <param name="groupIds"></param>
  431. /// <returns></returns>
  432. private async Task<List<AirInfo>> GetAirInfos(List<int> groupIds)
  433. {
  434. // 1. 查询
  435. var groupInfos = await _sqlSugar.Queryable<Grp_DelegationInfo>()
  436. .Where(x => x.IsDel == 0 && groupIds.Contains(x.Id))
  437. .Select(x => new { x.Id, x.TeamName })
  438. .ToListAsync();
  439. var airInfos = await _sqlSugar.Queryable<Grp_AirTicketReservations>()
  440. .Where(x => x.IsDel == 0 && groupIds.Contains(x.DIId) && !string.IsNullOrEmpty(x.TicketCode))
  441. .Select(x => new { x.DIId, x.TicketCode, x.ClientName })
  442. .ToListAsync();
  443. // 2. 解析客户名称ID
  444. var clientIds = new HashSet<int>();
  445. foreach (var item in airInfos)
  446. {
  447. if (string.IsNullOrWhiteSpace(item.ClientName))
  448. continue;
  449. var ids = item.ClientName.Split(',')
  450. .Select(s => s.Trim())
  451. .Where(s => int.TryParse(s, out _))
  452. .Select(int.Parse);
  453. foreach (var id in ids)
  454. {
  455. clientIds.Add(id);
  456. }
  457. }
  458. // 3. 批量查询客户名称
  459. var clientNames = new Dictionary<int, string>();
  460. if (clientIds.Any())
  461. {
  462. var nameInfos = await _sqlSugar.Queryable<Crm_DeleClient>()
  463. .Where(x => x.IsDel == 0 && clientIds.Contains(x.Id) &&
  464. !string.IsNullOrEmpty(x.FirstName) && !string.IsNullOrEmpty(x.LastName))
  465. .Select(x => new { x.Id, x.FirstName, x.LastName })
  466. .ToListAsync();
  467. clientNames = nameInfos.ToDictionary(
  468. x => x.Id,
  469. x => AesEncryptionHelper.Decrypt(x.LastName) + AesEncryptionHelper.Decrypt(x.FirstName)
  470. );
  471. }
  472. // 4. 构建 ClientName 到客户名称列表的映射(预解析)
  473. var clientNameCache = new Dictionary<string, List<string>>();
  474. foreach (var item in airInfos)
  475. {
  476. if (string.IsNullOrWhiteSpace(item.ClientName))
  477. continue;
  478. if (!clientNameCache.ContainsKey(item.ClientName))
  479. {
  480. var ids = item.ClientName.Split(',')
  481. .Select(s => s.Trim())
  482. .Where(s => int.TryParse(s, out _))
  483. .Select(int.Parse)
  484. .ToList();
  485. var names = ids.SelectMany(id =>
  486. clientNames.ContainsKey(id)
  487. ? new[] { clientNames[id] }
  488. : Array.Empty<string>()
  489. ).ToList();
  490. clientNameCache[item.ClientName] = names;
  491. }
  492. }
  493. // 5. 构建分组数据
  494. var airLookup = airInfos
  495. .GroupBy(x => x.DIId)
  496. .ToDictionary(g => g.Key, g => g.Select(x => x.TicketCode).Distinct().ToList());
  497. var clientLookup = airInfos
  498. .Where(x => !string.IsNullOrEmpty(x.ClientName) && clientNameCache.ContainsKey(x.ClientName))
  499. .GroupBy(x => x.DIId)
  500. .ToDictionary(g => g.Key, g => g.SelectMany(x => clientNameCache[x.ClientName]).Distinct().ToList());
  501. // 6. 组装结果
  502. var result = groupInfos.Select(group => new AirInfo
  503. {
  504. Id = group.Id,
  505. TeamName = group.TeamName,
  506. TicketCodes = airLookup.GetValueOrDefault(group.Id, new List<string>()),
  507. Clients = clientLookup.GetValueOrDefault(group.Id, new List<string>())
  508. }).ToList();
  509. return result;
  510. }
  511. public class AirInfo
  512. {
  513. public int Id { get; set; }
  514. public string TeamName { get; set; }
  515. public string TicketCodesLabel => string.Join("、", TicketCodes);
  516. public string ClientsLabel => string.Join("、", Clients);
  517. public List<string> TicketCodes { get; set; }
  518. public List<string> Clients { get; set; }
  519. }
  520. #endregion
  521. /// <summary>
  522. /// 团组各项费用录入 关键字输入提示(智能版)
  523. /// 76 酒店预订
  524. /// 79 车/导游地接
  525. /// 80 签证
  526. /// 81 邀请/公务活动
  527. /// 82 团组客户保险
  528. /// 85 机票预订
  529. /// 98 其他款项
  530. /// 285 收款退还
  531. /// 1015 超支费用
  532. /// 1081 文档下载
  533. /// 1466 会务相关
  534. /// </summary>
  535. /// <param name="userId">用户Id</param>
  536. /// <param name="feeType">费用类型</param>
  537. /// <param name="keyword">关键字</param>
  538. /// <returns></returns>
  539. [HttpGet("GroupFeeKeywordSearch/{userId}/{feeType}/{keyword}")]
  540. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  541. public async Task<IActionResult> GroupFeeKeywordSearch(int userId, int feeType, string keyword)
  542. {
  543. var stopwatch = Stopwatch.StartNew();
  544. try
  545. {
  546. #region 参数验证
  547. // 基本参数验证
  548. if (userId <= 0)
  549. return Ok(JsonView(false, "用户ID必须大于0!"));
  550. if (feeType <= 0)
  551. return Ok(JsonView(false, "费用类型必须大于0!"));
  552. // 验证用户是否存在
  553. var userExists = await _sqlSugar.Queryable<Sys_Users>()
  554. .Where(x => x.IsDel == 0 && x.Id == userId)
  555. .AnyAsync();
  556. if (!userExists) return Ok(JsonView(false, "用户不存在或已被删除"));
  557. // 验证费用类型是否有效
  558. var feeTypeExists = await _sqlSugar.Queryable<Sys_SetData>()
  559. .Where(x => x.IsDel == 0 && x.STid == 16 && x.Id == feeType)
  560. .AnyAsync();
  561. if (!feeTypeExists) return Ok(JsonView(false, "无效的费用类型"));
  562. // 验证关键字
  563. if (string.IsNullOrWhiteSpace(keyword)) return Ok(JsonView(false, "请输入搜索关键字"));
  564. #endregion
  565. #region 获取用户有权限的团组ID
  566. // 获取用户有权限访问的团组ID列表
  567. var authorizedGroupIds = await _sqlSugar.Queryable<Grp_GroupsTaskAssignment>()
  568. .Where(x => x.IsDel == 0 && x.UId == userId && x.CTId == feeType)
  569. .Select(x => x.DIId)
  570. .Distinct()
  571. .ToListAsync();
  572. if (!authorizedGroupIds.Any()) return Ok(JsonView(true, "暂无数据", new List<object>(), 0));
  573. #endregion
  574. // 类型等于 85 时单独处理
  575. if (feeType == 85)
  576. {
  577. var airInfos = await GetAirInfos(authorizedGroupIds);
  578. if (airInfos == null) return Ok(JsonView(false, "获取机票费用信息失败"));
  579. var airSearchReq = new DynamicSearchRequest()
  580. {
  581. Keyword = keyword.Trim(),
  582. RequireAllSingleChars = true,
  583. PageIndex = 1,
  584. PageSize = 20, // 限制返回数量,提高性能
  585. FieldWeights = new Dictionary<string, int>
  586. {
  587. { "TeamName", 10 },
  588. { "TicketCodesLabel", 10 },
  589. { "ClientsLabel", 10 }
  590. }
  591. };
  592. var airResult = _airSearchService.SearchDataSource(airSearchReq, airInfos);
  593. if (!airResult.Success)
  594. {
  595. return Ok(JsonView(false, airResult.Message ?? "搜索失败"));
  596. }
  597. if (airResult.Items == null || !airResult.Items.Any())
  598. {
  599. return Ok(JsonView(true, "未找到匹配的团组", new List<object>(), 0));
  600. }
  601. var airRespData = airResult.Items
  602. .Where(item => item.Data != null)
  603. .Select(item => new
  604. {
  605. item.Data.Id,
  606. item.Data.TeamName,
  607. item.Data.TicketCodes,
  608. item.Data.Clients,
  609. })
  610. .Where(x => !string.IsNullOrWhiteSpace(x.TeamName)) // 过滤空名称
  611. .Distinct() // 去重
  612. .ToList();
  613. stopwatch.Stop();
  614. return Ok(JsonView(true, $"搜索成功,耗时:{stopwatch.ElapsedMilliseconds}ms", airRespData, airRespData.Count));
  615. }
  616. #region 构建搜索请求
  617. var searchRequest = new DynamicSearchRequest
  618. {
  619. Keyword = keyword.Trim(),
  620. RequireAllSingleChars = true,
  621. PageIndex = 1,
  622. PageSize = 20, // 限制返回数量,提高性能
  623. FieldWeights = new Dictionary<string, int>
  624. {
  625. { "TeamName", 10 }
  626. },
  627. Filters = new List<SearchFilter>
  628. {
  629. new SearchFilter { Field = "IsDel", Operator = "eq", Value = "0" },
  630. new SearchFilter { Field = "Id", Operator = "in", Values = authorizedGroupIds.ConvertAll<object>(x => x) }
  631. },
  632. OrderBy = "VisitDate", // 添加排序方向
  633. ReturnFields = new List<string> { "Id", "TeamName" } // 添加ID字段
  634. };
  635. #endregion
  636. #region 字段配置验证
  637. var validation = _groupSearchService.ValidateFieldConfig(
  638. searchRequest.FieldWeights,
  639. searchRequest.ReturnFields);
  640. if (!validation.IsValid)
  641. {
  642. return Ok(JsonView(false, $"字段配置错误: {validation.Message}"));
  643. }
  644. #endregion
  645. #region 执行搜索
  646. var result = await _groupSearchService.SearchAsync(searchRequest);
  647. if (!result.Success)
  648. {
  649. return Ok(JsonView(false, result.Message ?? "搜索失败"));
  650. }
  651. if (result.Items == null || !result.Items.Any())
  652. {
  653. return Ok(JsonView(true, "未找到匹配的团组", new List<object>(), 0));
  654. }
  655. #endregion
  656. #region 构建返回数据
  657. var responseData = result.Items
  658. .Where(item => item.Data != null)
  659. .Select(item => new
  660. {
  661. Id = item.Data.Id,
  662. TeamName = item.Data.TeamName
  663. })
  664. .Where(x => !string.IsNullOrWhiteSpace(x.TeamName)) // 过滤空名称
  665. .Distinct() // 去重
  666. .ToList();
  667. #endregion
  668. stopwatch.Stop();
  669. return Ok(JsonView(true, $"搜索成功,耗时:{stopwatch.ElapsedMilliseconds} ms", responseData, responseData.Count));
  670. }
  671. catch (Exception ex)
  672. {
  673. // 记录日志(实际项目中应该使用日志框架)
  674. // _logger.LogError(ex, "团组费用关键字搜索失败,用户ID: {UserId}, 费用类型: {FeeType}", userId, feeType);
  675. // 生产环境中不要返回详细的错误信息
  676. return Ok(JsonView(false, "搜索服务暂时不可用,请稍后重试"));
  677. }
  678. }
  679. /// <summary>
  680. /// 三公-接团信息 关键字输入提示(单字段)
  681. /// </summary>
  682. /// <param name="currUserId">用户名称ID</param>
  683. /// <param name="keyword">关键字</param>
  684. /// <returns></returns>
  685. [HttpGet("GroupEnterExitCostKeywordSearch/{currUserId}/{keyword}")]
  686. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  687. public async Task<IActionResult> GroupEnterExitCostKeywordSearch(int currUserId,string keyword)
  688. {
  689. try
  690. {
  691. // 验证请求参数
  692. if (currUserId < 1)
  693. {
  694. return Ok(JsonView(true, $"请传入有效的用户ID!"));
  695. }
  696. if (string.IsNullOrEmpty(keyword))
  697. {
  698. return Ok(JsonView(true, $"暂无数据!"));
  699. }
  700. var searchRequest = new DynamicSearchRequest
  701. {
  702. Keyword = keyword,
  703. RequireAllSingleChars = true,
  704. PageIndex = 1,
  705. PageSize = 30,
  706. FieldWeights = new Dictionary<string, int>
  707. {
  708. { "TeamName", 10 },
  709. //{ "ClientUnit", 8 },
  710. //{ "ClientName", 6 }
  711. },
  712. Filters = new List<SearchFilter>()
  713. {
  714. new(){Field = "IsDel",Operator="eq",Value="0" }
  715. },
  716. OrderBy = "TeamName",
  717. ReturnFields = new List<string>() { "TeamName" }
  718. };
  719. // 验证字段配置
  720. var validation = _groupSearchService.ValidateFieldConfig(
  721. searchRequest.FieldWeights,
  722. searchRequest.ReturnFields);
  723. if (!validation.IsValid)
  724. {
  725. return Ok(JsonView(true, $"暂无数据!{validation.Message}"));
  726. }
  727. var result = await _groupSearchService.SearchAsync(searchRequest);
  728. if (result.Success)
  729. {
  730. var groupIds = result.Items.Select(x => x.Data.Id).ToList();
  731. var isNullDatas = _sqlSugar.Queryable<Grp_EnterExitCost>()
  732. .Where(x => x.IsDel == 0 && groupIds.Contains(x.DiId))
  733. .Select(x => x.DiId)
  734. .Distinct()
  735. .ToList();
  736. var isViewDatas = _sqlSugar.Queryable<Grp_EnterExitCostPermission>()
  737. .Where(x1 => x1.IsDel == 0 && x1.UserId == currUserId && groupIds.Contains(x1.GroupId))
  738. .Select(x1 => x1.GroupId)
  739. .Distinct()
  740. .ToList();
  741. var provCityDatas = await _groupRep.ProvinceCityBasicSource();
  742. var data = result.Items.Select(x =>
  743. {
  744. int groupId = x.Data.Id;
  745. int cityId = x.Data.CityId;
  746. bool isNull = !isNullDatas.Where(x => x == groupId).Any();
  747. bool isView = isViewDatas.Where(x => x == groupId).Any();
  748. var provinceId = 122; //默认四川
  749. if (provinceId > 0)
  750. {
  751. var parentId = _groupRep.FindParentIdByChildId(provCityDatas, cityId);
  752. if (parentId != null)
  753. {
  754. provinceId = (int)parentId;
  755. }
  756. }
  757. return new
  758. {
  759. x.Data.Id,
  760. groupName = x.Data.TeamName,
  761. provinceId,
  762. isNull,
  763. isView,
  764. };
  765. }).ToList();
  766. return Ok(JsonView(true, result.Message, data, data.Count));
  767. }
  768. return Ok(JsonView(true, result.Message));
  769. }
  770. catch (Exception ex)
  771. {
  772. return Ok(JsonView(true, $"搜索服务暂时不可用!"));
  773. }
  774. }
  775. /// <summary>
  776. /// 商邀AI invName关键字输入提示(单字段)
  777. /// </summary>
  778. /// <param name="keyword">关键字</param>
  779. /// <returns></returns>
  780. [HttpGet("InvAIKeywordSearch/{keyword}")]
  781. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  782. public async Task<IActionResult> InvAIKeywordSearch(string keyword)
  783. {
  784. try
  785. {
  786. // 验证请求参数 _invAISearchService
  787. if (string.IsNullOrEmpty(keyword))
  788. {
  789. return Ok(JsonView(true, $"暂无数据!"));
  790. }
  791. var invNames = await GeneralMethod.InvitationAIInvName();
  792. var searchRequest = new DynamicSearchRequest
  793. {
  794. Keyword = keyword,
  795. RequireAllSingleChars = true,
  796. PageIndex = 1,
  797. PageSize = 999999,
  798. FieldWeights = new Dictionary<string, int>
  799. {
  800. { "Name", 10 },
  801. },
  802. OrderBy = "SortTime"
  803. };
  804. // 验证字段配置
  805. var validation = _invAISearchService.ValidateFieldConfig(
  806. searchRequest.FieldWeights,
  807. searchRequest.ReturnFields);
  808. if (!validation.IsValid)
  809. {
  810. return Ok(JsonView(true, $"暂无数据!{validation.Message}"));
  811. }
  812. var result = _invAISearchService.SearchDataSource(searchRequest, invNames);
  813. if (result.Success)
  814. {
  815. var view = result.Items.Select(x => x.Data).ToList();
  816. return Ok(JsonView(true, result.Message, view, view.Count));
  817. }
  818. return Ok(JsonView(true, "暂无数据"));
  819. }
  820. catch (Exception ex)
  821. {
  822. return Ok(JsonView(true, $"搜索服务暂时不可用!"));
  823. }
  824. }
  825. /// <summary>
  826. /// 商邀AI ClientName 关键字输入提示(单字段)
  827. /// </summary>
  828. /// <param name="keyword">关键字</param>
  829. /// <returns></returns>
  830. [HttpGet("InvAIClientKeywordSearch/{keyword}")]
  831. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  832. public async Task<IActionResult> InvAIClientKeywordSearch(string keyword)
  833. {
  834. try
  835. {
  836. // 验证请求参数 _invAISearchService
  837. if (string.IsNullOrEmpty(keyword))
  838. {
  839. return Ok(JsonView(true, $"暂无数据!"));
  840. }
  841. var invNames = await GeneralMethod.InvitationAIClientName();
  842. var data = invNames.Select(x =>
  843. {
  844. return new InvitationAIInvNameView
  845. {
  846. Name = x,
  847. SortTime = DateTime.Now
  848. };
  849. }).ToList();
  850. var searchRequest = new DynamicSearchRequest
  851. {
  852. Keyword = keyword,
  853. RequireAllSingleChars = true,
  854. PageIndex = 1,
  855. PageSize = 999999,
  856. FieldWeights = new Dictionary<string, int>
  857. {
  858. { "Name", 10 },
  859. },
  860. OrderBy = "SortTime"
  861. };
  862. // 验证字段配置
  863. var validation = _invAISearchService.ValidateFieldConfig(
  864. searchRequest.FieldWeights,
  865. searchRequest.ReturnFields);
  866. if (!validation.IsValid)
  867. {
  868. return Ok(JsonView(true, $"暂无数据!{validation.Message}"));
  869. }
  870. var result = _invAISearchService.SearchDataSource(searchRequest, data);
  871. if (result.Success)
  872. {
  873. var view = result.Items.Select(x => x.Data.Name).ToList();
  874. if (view.Count == 0)
  875. {
  876. return Ok(JsonView(true, "暂无数据"));
  877. }
  878. return Ok(JsonView(true, result.Message, view, view.Count));
  879. }
  880. return Ok(JsonView(true, "暂无数据"));
  881. }
  882. catch (Exception ex)
  883. {
  884. return Ok(JsonView(true, $"搜索服务暂时不可用!"));
  885. }
  886. }
  887. /// <summary>
  888. /// 商邀AI CountryName 关键字输入提示(单字段)
  889. /// </summary>
  890. /// <param name="keyword">关键字</param>
  891. /// <returns></returns>
  892. [HttpGet("InvAICountryKeywordSearch/{keyword}")]
  893. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  894. public async Task<IActionResult> InvAICountryKeywordSearch(string keyword)
  895. {
  896. try
  897. {
  898. // 验证请求参数 _invAISearchService
  899. if (string.IsNullOrEmpty(keyword))
  900. {
  901. return Ok(JsonView(true, $"暂无数据!"));
  902. }
  903. var countryNames = await GeneralMethod.InvitationAICountryName();
  904. var data = countryNames.Select(x =>
  905. {
  906. return new InvitationAIInvNameView
  907. {
  908. Name = x,
  909. SortTime = DateTime.Now
  910. };
  911. }).ToList();
  912. var searchRequest = new DynamicSearchRequest
  913. {
  914. Keyword = keyword,
  915. RequireAllSingleChars = true,
  916. PageIndex = 1,
  917. PageSize = 999999,
  918. FieldWeights = new Dictionary<string, int>
  919. {
  920. { "Name", 10 },
  921. },
  922. OrderBy = "SortTime"
  923. };
  924. // 验证字段配置
  925. var validation = _invAISearchService.ValidateFieldConfig(
  926. searchRequest.FieldWeights,
  927. searchRequest.ReturnFields);
  928. if (!validation.IsValid)
  929. {
  930. return Ok(JsonView(true, $"暂无数据!{validation.Message}"));
  931. }
  932. var result = _invAISearchService.SearchDataSource(searchRequest, data);
  933. if (result.Success)
  934. {
  935. var view = result.Items.Select(x => x.Data.Name).ToList();
  936. if (view.Count == 0)
  937. {
  938. return Ok(JsonView(true, "暂无数据"));
  939. }
  940. return Ok(JsonView(true, result.Message, view, view.Count));
  941. }
  942. return Ok(JsonView(true, "暂无数据"));
  943. }
  944. catch (Exception ex)
  945. {
  946. return Ok(JsonView(true, $"搜索服务暂时不可用!"));
  947. }
  948. }
  949. }
  950. }