SearchController.cs 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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))
  441. .Select(x => new { x.DIId, x.TicketCode, x.ClientName,x.CustTicketInfos })
  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 airLookupOld = airInfos
  495. .GroupBy(x => x.DIId)
  496. .ToDictionary(
  497. g => g.Key,
  498. g => g.Select(x => x?.TicketCode?.Trim())
  499. .Where(code => !string.IsNullOrWhiteSpace(code))
  500. .Distinct()
  501. .ToList()
  502. );
  503. var airLookupNew = airInfos
  504. .GroupBy(x => x.DIId)
  505. .ToDictionary(
  506. g => g.Key,
  507. g => g.SelectMany(x => x.CustTicketInfos?.Select(y => y.TicketCode.Trim()) ?? Enumerable.Empty<string>())
  508. .Where(code => !string.IsNullOrWhiteSpace(code))
  509. .Distinct()
  510. .ToList()
  511. );
  512. var airLookup = airLookupOld
  513. .Concat(airLookupNew)
  514. .GroupBy(kvp => kvp.Key)
  515. .ToDictionary(
  516. g => g.Key,
  517. g => g.SelectMany(kvp => kvp.Value)
  518. .Distinct()
  519. .ToList()
  520. );
  521. var clientLookup = airInfos
  522. .Where(x => !string.IsNullOrEmpty(x.ClientName) && clientNameCache.ContainsKey(x.ClientName))
  523. .GroupBy(x => x.DIId)
  524. .ToDictionary(g => g.Key, g => g.SelectMany(x => clientNameCache[x.ClientName]).Distinct().ToList());
  525. // 6. 组装结果
  526. var result = groupInfos.Select(group => new AirInfo
  527. {
  528. Id = group.Id,
  529. TeamName = group.TeamName,
  530. TicketCodes = airLookup.GetValueOrDefault(group.Id, new List<string>()),
  531. Clients = clientLookup.GetValueOrDefault(group.Id, new List<string>())
  532. }).ToList();
  533. return result;
  534. }
  535. public class AirInfo
  536. {
  537. public int Id { get; set; }
  538. public string TeamName { get; set; }
  539. public string TicketCodesLabel => string.Join("、", TicketCodes);
  540. public string ClientsLabel => string.Join("、", Clients);
  541. public List<string> TicketCodes { get; set; }
  542. public List<string> Clients { get; set; }
  543. }
  544. #endregion
  545. /// <summary>
  546. /// 团组各项费用录入 关键字输入提示(智能版)
  547. /// 76 酒店预订
  548. /// 79 车/导游地接
  549. /// 80 签证
  550. /// 81 邀请/公务活动
  551. /// 82 团组客户保险
  552. /// 85 机票预订
  553. /// 98 其他款项
  554. /// 285 收款退还
  555. /// 1015 超支费用
  556. /// 1081 文档下载
  557. /// 1466 会务相关
  558. /// </summary>
  559. /// <param name="userId">用户Id</param>
  560. /// <param name="feeType">费用类型</param>
  561. /// <param name="keyword">关键字</param>
  562. /// <returns></returns>
  563. [HttpGet("GroupFeeKeywordSearch/{userId}/{feeType}/{keyword}")]
  564. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  565. public async Task<IActionResult> GroupFeeKeywordSearch(int userId, int feeType, string keyword)
  566. {
  567. var stopwatch = Stopwatch.StartNew();
  568. try
  569. {
  570. #region 参数验证
  571. // 基本参数验证
  572. if (userId <= 0)
  573. return Ok(JsonView(false, "用户ID必须大于0!"));
  574. if (feeType <= 0)
  575. return Ok(JsonView(false, "费用类型必须大于0!"));
  576. // 验证用户是否存在
  577. var userExists = await _sqlSugar.Queryable<Sys_Users>()
  578. .Where(x => x.IsDel == 0 && x.Id == userId)
  579. .AnyAsync();
  580. if (!userExists) return Ok(JsonView(false, "用户不存在或已被删除"));
  581. // 验证费用类型是否有效
  582. var feeTypeExists = await _sqlSugar.Queryable<Sys_SetData>()
  583. .Where(x => x.IsDel == 0 && x.STid == 16 && x.Id == feeType)
  584. .AnyAsync();
  585. if (!feeTypeExists) return Ok(JsonView(false, "无效的费用类型"));
  586. // 验证关键字
  587. if (string.IsNullOrWhiteSpace(keyword)) return Ok(JsonView(false, "请输入搜索关键字"));
  588. #endregion
  589. #region 获取用户有权限的团组ID
  590. // 获取用户有权限访问的团组ID列表
  591. var authorizedGroupIds = await _sqlSugar.Queryable<Grp_GroupsTaskAssignment>()
  592. .Where(x => x.IsDel == 0 && x.UId == userId && x.CTId == feeType)
  593. .Select(x => x.DIId)
  594. .Distinct()
  595. .ToListAsync();
  596. if (!authorizedGroupIds.Any()) return Ok(JsonView(true, "暂无数据", new List<object>(), 0));
  597. #endregion
  598. // 类型等于 85 时单独处理
  599. if (feeType == 85)
  600. {
  601. var airInfos = await GetAirInfos(authorizedGroupIds);
  602. if (airInfos == null) return Ok(JsonView(false, "获取机票费用信息失败"));
  603. var airSearchReq = new DynamicSearchRequest()
  604. {
  605. Keyword = keyword.Trim(),
  606. RequireAllSingleChars = true,
  607. PageIndex = 1,
  608. PageSize = 20, // 限制返回数量,提高性能
  609. FieldWeights = new Dictionary<string, int>
  610. {
  611. { "TeamName", 10 },
  612. { "TicketCodesLabel", 20 },
  613. { "ClientsLabel", 10 }
  614. }
  615. };
  616. var airResult = _airSearchService.SearchDataSource(airSearchReq, airInfos);
  617. if (!airResult.Success)
  618. {
  619. return Ok(JsonView(false, airResult.Message ?? "搜索失败"));
  620. }
  621. if (airResult.Items == null || !airResult.Items.Any())
  622. {
  623. return Ok(JsonView(true, "未找到匹配的团组", new List<object>(), 0));
  624. }
  625. var airRespData = airResult.Items
  626. .Where(item => item.Data != null)
  627. .Select(item => new
  628. {
  629. item.Data.Id,
  630. item.Data.TeamName,
  631. item.Data.TicketCodes,
  632. item.Data.Clients,
  633. })
  634. .Where(x => !string.IsNullOrWhiteSpace(x.TeamName)) // 过滤空名称
  635. .Distinct() // 去重
  636. .ToList();
  637. stopwatch.Stop();
  638. return Ok(JsonView(true, $"搜索成功,耗时:{stopwatch.ElapsedMilliseconds}ms", airRespData, airRespData.Count));
  639. }
  640. #region 构建搜索请求
  641. var searchRequest = new DynamicSearchRequest
  642. {
  643. Keyword = keyword.Trim(),
  644. RequireAllSingleChars = true,
  645. PageIndex = 1,
  646. PageSize = 20, // 限制返回数量,提高性能
  647. FieldWeights = new Dictionary<string, int>
  648. {
  649. { "TeamName", 10 }
  650. },
  651. Filters = new List<SearchFilter>
  652. {
  653. new SearchFilter { Field = "IsDel", Operator = "eq", Value = "0" },
  654. new SearchFilter { Field = "Id", Operator = "in", Values = authorizedGroupIds.ConvertAll<object>(x => x) }
  655. },
  656. OrderBy = "VisitDate", // 添加排序方向
  657. ReturnFields = new List<string> { "Id", "TeamName" } // 添加ID字段
  658. };
  659. #endregion
  660. #region 字段配置验证
  661. var validation = _groupSearchService.ValidateFieldConfig(
  662. searchRequest.FieldWeights,
  663. searchRequest.ReturnFields);
  664. if (!validation.IsValid)
  665. {
  666. return Ok(JsonView(false, $"字段配置错误: {validation.Message}"));
  667. }
  668. #endregion
  669. #region 执行搜索
  670. var result = await _groupSearchService.SearchAsync(searchRequest);
  671. if (!result.Success)
  672. {
  673. return Ok(JsonView(false, result.Message ?? "搜索失败"));
  674. }
  675. if (result.Items == null || !result.Items.Any())
  676. {
  677. return Ok(JsonView(true, "未找到匹配的团组", new List<object>(), 0));
  678. }
  679. #endregion
  680. #region 构建返回数据
  681. var responseData = result.Items
  682. .Where(item => item.Data != null)
  683. .Select(item => new
  684. {
  685. Id = item.Data.Id,
  686. TeamName = item.Data.TeamName
  687. })
  688. .Where(x => !string.IsNullOrWhiteSpace(x.TeamName)) // 过滤空名称
  689. .Distinct() // 去重
  690. .ToList();
  691. #endregion
  692. stopwatch.Stop();
  693. return Ok(JsonView(true, $"搜索成功,耗时:{stopwatch.ElapsedMilliseconds} ms", responseData, responseData.Count));
  694. }
  695. catch (Exception ex)
  696. {
  697. // 记录日志(实际项目中应该使用日志框架)
  698. // _logger.LogError(ex, "团组费用关键字搜索失败,用户ID: {UserId}, 费用类型: {FeeType}", userId, feeType);
  699. // 生产环境中不要返回详细的错误信息
  700. return Ok(JsonView(false, "搜索服务暂时不可用,请稍后重试"));
  701. }
  702. }
  703. /// <summary>
  704. /// 三公-接团信息 关键字输入提示(单字段)
  705. /// </summary>
  706. /// <param name="currUserId">用户名称ID</param>
  707. /// <param name="keyword">关键字</param>
  708. /// <returns></returns>
  709. [HttpGet("GroupEnterExitCostKeywordSearch/{currUserId}/{keyword}")]
  710. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  711. public async Task<IActionResult> GroupEnterExitCostKeywordSearch(int currUserId,string keyword)
  712. {
  713. try
  714. {
  715. // 验证请求参数
  716. if (currUserId < 1)
  717. {
  718. return Ok(JsonView(true, $"请传入有效的用户ID!"));
  719. }
  720. if (string.IsNullOrEmpty(keyword))
  721. {
  722. return Ok(JsonView(true, $"暂无数据!"));
  723. }
  724. var searchRequest = new DynamicSearchRequest
  725. {
  726. Keyword = keyword,
  727. RequireAllSingleChars = true,
  728. PageIndex = 1,
  729. PageSize = 30,
  730. FieldWeights = new Dictionary<string, int>
  731. {
  732. { "TeamName", 10 },
  733. //{ "ClientUnit", 8 },
  734. //{ "ClientName", 6 }
  735. },
  736. Filters = new List<SearchFilter>()
  737. {
  738. new(){Field = "IsDel",Operator="eq",Value="0" }
  739. },
  740. OrderBy = "TeamName",
  741. ReturnFields = new List<string>() { "TeamName" }
  742. };
  743. // 验证字段配置
  744. var validation = _groupSearchService.ValidateFieldConfig(
  745. searchRequest.FieldWeights,
  746. searchRequest.ReturnFields);
  747. if (!validation.IsValid)
  748. {
  749. return Ok(JsonView(true, $"暂无数据!{validation.Message}"));
  750. }
  751. var result = await _groupSearchService.SearchAsync(searchRequest);
  752. if (result.Success)
  753. {
  754. var groupIds = result.Items.Select(x => x.Data.Id).ToList();
  755. var isNullDatas = _sqlSugar.Queryable<Grp_EnterExitCost>()
  756. .Where(x => x.IsDel == 0 && groupIds.Contains(x.DiId))
  757. .Select(x => x.DiId)
  758. .Distinct()
  759. .ToList();
  760. var isViewDatas = _sqlSugar.Queryable<Grp_EnterExitCostPermission>()
  761. .Where(x1 => x1.IsDel == 0 && x1.UserId == currUserId && groupIds.Contains(x1.GroupId))
  762. .Select(x1 => x1.GroupId)
  763. .Distinct()
  764. .ToList();
  765. var provCityDatas = await _groupRep.ProvinceCityBasicSource();
  766. var data = result.Items.Select(x =>
  767. {
  768. int groupId = x.Data.Id;
  769. int cityId = x.Data.CityId;
  770. bool isNull = !isNullDatas.Where(x => x == groupId).Any();
  771. bool isView = isViewDatas.Where(x => x == groupId).Any();
  772. var provinceId = 122; //默认四川
  773. if (provinceId > 0)
  774. {
  775. var parentId = _groupRep.FindParentIdByChildId(provCityDatas, cityId);
  776. if (parentId != null)
  777. {
  778. provinceId = (int)parentId;
  779. }
  780. }
  781. return new
  782. {
  783. x.Data.Id,
  784. groupName = x.Data.TeamName,
  785. provinceId,
  786. isNull,
  787. isView,
  788. };
  789. }).ToList();
  790. return Ok(JsonView(true, result.Message, data, data.Count));
  791. }
  792. return Ok(JsonView(true, result.Message));
  793. }
  794. catch (Exception ex)
  795. {
  796. return Ok(JsonView(true, $"搜索服务暂时不可用!"));
  797. }
  798. }
  799. /// <summary>
  800. /// 商邀AI invName关键字输入提示(单字段)
  801. /// </summary>
  802. /// <param name="keyword">关键字</param>
  803. /// <returns></returns>
  804. [HttpGet("InvAIKeywordSearch/{keyword}")]
  805. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  806. public async Task<IActionResult> InvAIKeywordSearch(string keyword)
  807. {
  808. try
  809. {
  810. // 验证请求参数 _invAISearchService
  811. if (string.IsNullOrEmpty(keyword))
  812. {
  813. return Ok(JsonView(true, $"暂无数据!"));
  814. }
  815. var invNames = await GeneralMethod.InvitationAIInvName();
  816. var searchRequest = new DynamicSearchRequest
  817. {
  818. Keyword = keyword,
  819. RequireAllSingleChars = true,
  820. PageIndex = 1,
  821. PageSize = 999999,
  822. FieldWeights = new Dictionary<string, int>
  823. {
  824. { "Name", 10 },
  825. },
  826. OrderBy = "SortTime"
  827. };
  828. // 验证字段配置
  829. var validation = _invAISearchService.ValidateFieldConfig(
  830. searchRequest.FieldWeights,
  831. searchRequest.ReturnFields);
  832. if (!validation.IsValid)
  833. {
  834. return Ok(JsonView(true, $"暂无数据!{validation.Message}"));
  835. }
  836. var result = _invAISearchService.SearchDataSource(searchRequest, invNames);
  837. if (result.Success)
  838. {
  839. var view = result.Items.Select(x => x.Data).ToList();
  840. return Ok(JsonView(true, result.Message, view, view.Count));
  841. }
  842. return Ok(JsonView(true, "暂无数据"));
  843. }
  844. catch (Exception ex)
  845. {
  846. return Ok(JsonView(true, $"搜索服务暂时不可用!"));
  847. }
  848. }
  849. /// <summary>
  850. /// 商邀AI ClientName 关键字输入提示(单字段)
  851. /// </summary>
  852. /// <param name="keyword">关键字</param>
  853. /// <returns></returns>
  854. [HttpGet("InvAIClientKeywordSearch/{keyword}")]
  855. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  856. public async Task<IActionResult> InvAIClientKeywordSearch(string keyword)
  857. {
  858. try
  859. {
  860. // 验证请求参数 _invAISearchService
  861. if (string.IsNullOrEmpty(keyword))
  862. {
  863. return Ok(JsonView(true, $"暂无数据!"));
  864. }
  865. var invNames = await GeneralMethod.InvitationAIClientName();
  866. var data = invNames.Select(x =>
  867. {
  868. return new InvitationAIInvNameView
  869. {
  870. Name = x,
  871. SortTime = DateTime.Now
  872. };
  873. }).ToList();
  874. var searchRequest = new DynamicSearchRequest
  875. {
  876. Keyword = keyword,
  877. RequireAllSingleChars = true,
  878. PageIndex = 1,
  879. PageSize = 999999,
  880. FieldWeights = new Dictionary<string, int>
  881. {
  882. { "Name", 10 },
  883. },
  884. OrderBy = "SortTime"
  885. };
  886. // 验证字段配置
  887. var validation = _invAISearchService.ValidateFieldConfig(
  888. searchRequest.FieldWeights,
  889. searchRequest.ReturnFields);
  890. if (!validation.IsValid)
  891. {
  892. return Ok(JsonView(true, $"暂无数据!{validation.Message}"));
  893. }
  894. var result = _invAISearchService.SearchDataSource(searchRequest, data);
  895. if (result.Success)
  896. {
  897. var view = result.Items.Select(x => x.Data.Name).ToList();
  898. if (view.Count == 0)
  899. {
  900. return Ok(JsonView(true, "暂无数据"));
  901. }
  902. return Ok(JsonView(true, result.Message, view, view.Count));
  903. }
  904. return Ok(JsonView(true, "暂无数据"));
  905. }
  906. catch (Exception ex)
  907. {
  908. return Ok(JsonView(true, $"搜索服务暂时不可用!"));
  909. }
  910. }
  911. /// <summary>
  912. /// 商邀AI CountryName 关键字输入提示(单字段)
  913. /// </summary>
  914. /// <param name="keyword">关键字</param>
  915. /// <returns></returns>
  916. [HttpGet("InvAICountryKeywordSearch/{keyword}")]
  917. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  918. public async Task<IActionResult> InvAICountryKeywordSearch(string keyword)
  919. {
  920. try
  921. {
  922. // 验证请求参数 _invAISearchService
  923. if (string.IsNullOrEmpty(keyword))
  924. {
  925. return Ok(JsonView(true, $"暂无数据!"));
  926. }
  927. var countryNames = await GeneralMethod.InvitationAICountryName();
  928. var data = countryNames.Select(x =>
  929. {
  930. return new InvitationAIInvNameView
  931. {
  932. Name = x,
  933. SortTime = DateTime.Now
  934. };
  935. }).ToList();
  936. var searchRequest = new DynamicSearchRequest
  937. {
  938. Keyword = keyword,
  939. RequireAllSingleChars = true,
  940. PageIndex = 1,
  941. PageSize = 999999,
  942. FieldWeights = new Dictionary<string, int>
  943. {
  944. { "Name", 10 },
  945. },
  946. OrderBy = "SortTime"
  947. };
  948. // 验证字段配置
  949. var validation = _invAISearchService.ValidateFieldConfig(
  950. searchRequest.FieldWeights,
  951. searchRequest.ReturnFields);
  952. if (!validation.IsValid)
  953. {
  954. return Ok(JsonView(true, $"暂无数据!{validation.Message}"));
  955. }
  956. var result = _invAISearchService.SearchDataSource(searchRequest, data);
  957. if (result.Success)
  958. {
  959. var view = result.Items.Select(x => x.Data.Name).ToList();
  960. if (view.Count == 0)
  961. {
  962. return Ok(JsonView(true, "暂无数据"));
  963. }
  964. return Ok(JsonView(true, result.Message, view, view.Count));
  965. }
  966. return Ok(JsonView(true, "暂无数据"));
  967. }
  968. catch (Exception ex)
  969. {
  970. return Ok(JsonView(true, $"搜索服务暂时不可用!"));
  971. }
  972. }
  973. }
  974. }