SearchController.cs 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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. }