SearchController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. using EyeSoft.Collections.Generic;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using NPOI.SS.Formula.Functions;
  5. using OASystem.API.OAMethodLib.DeepSeekAPI;
  6. using OASystem.API.OAMethodLib.GenericSearch;
  7. using OASystem.Domain.AesEncryption;
  8. using OASystem.Domain.Entities.Customer;
  9. using OASystem.Domain.Entities.Financial;
  10. using OASystem.Domain.Entities.Groups;
  11. using OASystem.Domain.ViewModels.Search;
  12. using OASystem.Infrastructure.Repositories.System;
  13. using static iTextSharp.text.pdf.AcroFields;
  14. using OASystem.Infrastructure.Repositories.CRM;
  15. namespace OASystem.API.Controllers
  16. {
  17. /// <summary>
  18. /// 搜索
  19. /// </summary>
  20. [Route("api/search")]
  21. [ApiController]
  22. public class SearchController : ControllerBase
  23. {
  24. private readonly SqlSugarClient _sqlSugar;
  25. private readonly DynamicSearchService<Grp_DelegationInfo> _groupSearchService;
  26. private readonly DynamicSearchService<NewClientDataView> _clientSearchService;
  27. private readonly NewClientDataRepository _clientDataRepository;
  28. public SearchController(
  29. SqlSugarClient sqlSugar,
  30. DynamicSearchService<Grp_DelegationInfo> groupSearchService,
  31. DynamicSearchService<NewClientDataView> clientSearchService,
  32. NewClientDataRepository clientDataRepository
  33. )
  34. {
  35. _sqlSugar = sqlSugar;
  36. _groupSearchService = groupSearchService;
  37. _clientSearchService = clientSearchService;
  38. _clientDataRepository = clientDataRepository;
  39. }
  40. /// <summary>
  41. /// 接团信息 关键字输入提示(单字段)
  42. /// </summary>
  43. /// <param name="keyword">关键字</param>
  44. /// <returns></returns>
  45. [HttpGet("group/{keyword}")]
  46. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  47. public async Task<IActionResult> GroupKeywordSearch(string keyword)
  48. {
  49. try
  50. {
  51. // 验证请求参数
  52. if (string.IsNullOrEmpty(keyword))
  53. {
  54. return Ok(JsonView(true, $"暂无数据!"));
  55. }
  56. var searchRequest = new DynamicSearchRequest
  57. {
  58. Keyword = keyword,
  59. RequireAllSingleChars = true,
  60. PageIndex = 1,
  61. PageSize = 999999,
  62. FieldWeights = new Dictionary<string, int>
  63. {
  64. { "TeamName", 10 },
  65. //{ "ClientUnit", 8 },
  66. //{ "ClientName", 6 }
  67. },
  68. Filters = new List<SearchFilter>()
  69. {
  70. new(){Field = "IsDel",Operator="eq",Value="0" }
  71. },
  72. OrderBy = "TeamName",
  73. ReturnFields = new List<string>() { "TeamName" }
  74. };
  75. // 验证字段配置
  76. var validation = _groupSearchService.ValidateFieldConfig(
  77. searchRequest.FieldWeights,
  78. searchRequest.ReturnFields);
  79. if (!validation.IsValid)
  80. {
  81. return Ok(JsonView(true, $"暂无数据!{validation.Message}"));
  82. }
  83. var result = await _groupSearchService.SearchAsync(searchRequest);
  84. if (result.Success)
  85. {
  86. var data = result.Items.Select(x => new { x.Data.Id, x.Data.TeamName }).ToList();
  87. return Ok(JsonView(true, result.Message, data, data.Count));
  88. }
  89. return Ok(JsonView(true, result.Message));
  90. }
  91. catch (Exception ex)
  92. {
  93. return Ok(JsonView(true, $"搜索服务暂时不可用!"));
  94. }
  95. }
  96. /// <summary>
  97. /// 客户资料 关键字输入提示(多字段)
  98. /// </summary>
  99. /// <param name="userId">关键字</param>
  100. /// <param name="keyword">关键字</param>
  101. /// <returns></returns>
  102. [HttpGet("group/{userId}/{keyword}")]
  103. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  104. public async Task<IActionResult> ClientKeywordSearch(int userId, string keyword)
  105. {
  106. try
  107. {
  108. // 验证请求参数
  109. if (string.IsNullOrEmpty(keyword))
  110. {
  111. return Ok(JsonView(true, $"暂无数据!"));
  112. }
  113. //查询有权限的数据
  114. var clientIds = await _sqlSugar
  115. .Queryable<Crm_ClientDataAndUser>()
  116. .Where(x => x.IsDel == 0 && x.usersId == userId)
  117. .Select(x => x.NewClientDataId)
  118. .ToListAsync();
  119. if (clientIds == null || clientIds.Count < 1)
  120. {
  121. return Ok(JsonView(true, $"暂无数据!"));
  122. }
  123. var data = _sqlSugar.Queryable<Crm_NewClientData>()
  124. .Where(x => x.IsDel == 0 && clientIds.Contains(x.Id))
  125. .OrderByDescending(x => x.CreateTime)
  126. .Select(x => new NewClientDataView()
  127. {
  128. Id = x.Id,
  129. Client = x.Client,
  130. Contact = x.Contact,
  131. Job = x.Job,
  132. Telephone = x.Telephone,
  133. Location = x.Location,
  134. })
  135. .ToList();
  136. int index = 0;
  137. data.ForEach(x =>
  138. {
  139. index++;
  140. x.RowNumber = index;
  141. x.Client = AesEncryptionHelper.Decrypt(x.Client);
  142. x.Contact = AesEncryptionHelper.Decrypt(x.Contact);
  143. x.Job = AesEncryptionHelper.Decrypt(x.Job);
  144. x.Telephone = AesEncryptionHelper.Decrypt(x.Telephone);
  145. x.Location = AesEncryptionHelper.Decrypt(x.Location);
  146. });
  147. var searchRequest = new DynamicSearchRequest
  148. {
  149. Keyword = keyword,
  150. RequireAllSingleChars = true,
  151. PageIndex = 1,
  152. PageSize = 999999,
  153. FieldWeights = new Dictionary<string, int>
  154. {
  155. { "Client", 10 },
  156. { "Contact", 8 },
  157. { "Location", 6 }
  158. },
  159. OrderBy = "CreateTime"
  160. };
  161. // 验证字段配置
  162. var validation = _clientSearchService.ValidateFieldConfig(
  163. searchRequest.FieldWeights,
  164. searchRequest.ReturnFields);
  165. if (!validation.IsValid)
  166. {
  167. return Ok(JsonView(true, $"暂无数据!{validation.Message}"));
  168. }
  169. var result = _clientSearchService.SearchDataSource(searchRequest, data);
  170. if (result.Success)
  171. {
  172. var view = result.Items.Select(x => x.Data).ToList();
  173. int resetIndex = 0;
  174. view.ForEach(x =>
  175. {
  176. resetIndex++;
  177. x.RowNumber = resetIndex;
  178. });
  179. return Ok(JsonView(true, result.Message, view, view.Count));
  180. }
  181. return Ok(JsonView(true, "暂无数据"));
  182. }
  183. catch (Exception ex)
  184. {
  185. return Ok(JsonView(true, $"搜索服务暂时不可用!"));
  186. }
  187. }
  188. /// <summary>
  189. /// 客户资料
  190. /// </summary>
  191. /// <param name="userId">关键字</param>
  192. /// <param name="keyword">关键字</param>
  193. /// <returns></returns>
  194. [HttpGet("ClientKeywordSearch/{userId}/{keyword}")]
  195. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  196. public async Task<IActionResult> ClientKeywordSearchAll(int userId, string keyword)
  197. {
  198. try
  199. {
  200. // 验证请求参数
  201. if (string.IsNullOrEmpty(keyword))
  202. {
  203. return Ok(JsonView(true, $"暂无数据!"));
  204. }
  205. var userIds = _clientDataRepository.GetNewExistClient(userId).Select(x => x.Id).ToList();
  206. //查询有权限的数据(包括负责用户)
  207. var clientIds = await _sqlSugar
  208. .Queryable<Crm_ClientDataAndUser>()
  209. .Where(x => x.IsDel == 0 && userIds.Contains(x.usersId))
  210. .Select(x => x.NewClientDataId)
  211. .ToListAsync();
  212. if (clientIds == null || clientIds.Count < 1)
  213. {
  214. return Ok(JsonView(true, $"暂无数据!"));
  215. }
  216. var data = _sqlSugar.Queryable<Crm_NewClientData>()
  217. .Where(x => x.IsDel == 0 && clientIds.Contains(x.Id))
  218. .OrderByDescending(x => x.CreateTime)
  219. .Select(x => new NewClientDataView()
  220. {
  221. Id = x.Id,
  222. Client = x.Client,
  223. Contact = x.Contact,
  224. Job = x.Job,
  225. Telephone = x.Telephone,
  226. Location = x.Location,
  227. })
  228. .ToList();
  229. int index = 0;
  230. data.ForEach(x =>
  231. {
  232. index++;
  233. x.RowNumber = index;
  234. x.Client = AesEncryptionHelper.Decrypt(x.Client);
  235. x.Contact = AesEncryptionHelper.Decrypt(x.Contact);
  236. x.Job = AesEncryptionHelper.Decrypt(x.Job);
  237. x.Telephone = AesEncryptionHelper.Decrypt(x.Telephone);
  238. x.Location = AesEncryptionHelper.Decrypt(x.Location);
  239. });
  240. var searchRequest = new DynamicSearchRequest
  241. {
  242. Keyword = keyword,
  243. RequireAllSingleChars = true,
  244. PageIndex = 1,
  245. PageSize = 999999,
  246. FieldWeights = new Dictionary<string, int>
  247. {
  248. { "Client", 10 },
  249. { "Contact", 8 },
  250. { "Location", 6 }
  251. },
  252. OrderBy = "CreateTime"
  253. };
  254. // 验证字段配置
  255. var validation = _clientSearchService.ValidateFieldConfig(
  256. searchRequest.FieldWeights,
  257. searchRequest.ReturnFields);
  258. if (!validation.IsValid)
  259. {
  260. return Ok(JsonView(true, $"暂无数据!{validation.Message}"));
  261. }
  262. var result = _clientSearchService.SearchDataSource(searchRequest, data);
  263. if (result.Success)
  264. {
  265. var view = result.Items.Select(x => x.Data).ToList();
  266. int resetIndex = 0;
  267. view.ForEach(x =>
  268. {
  269. resetIndex++;
  270. x.RowNumber = resetIndex;
  271. });
  272. return Ok(JsonView(true, result.Message, view, view.Count));
  273. }
  274. return Ok(JsonView(true, "暂无数据"));
  275. }
  276. catch (Exception ex)
  277. {
  278. return Ok(JsonView(true, $"搜索服务暂时不可用!"));
  279. }
  280. }
  281. /// <summary>
  282. /// 团组、会务流程 关键字输入提示(智能版)
  283. /// </summary>
  284. /// <param name="keyword">关键字</param>
  285. /// <returns></returns>
  286. [HttpGet]
  287. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  288. public async Task<IActionResult> ProcessKeywordSearch(string keyword)
  289. {
  290. try
  291. {
  292. // 验证请求参数
  293. if (string.IsNullOrEmpty(keyword))
  294. {
  295. return Ok(JsonView(true, $"暂无数据!"));
  296. }
  297. var searchRequest = new DynamicSearchRequest
  298. {
  299. Keyword = keyword,
  300. RequireAllSingleChars = true,
  301. PageIndex = 1,
  302. PageSize = 999999,
  303. FieldWeights = new Dictionary<string, int>
  304. {
  305. { "TeamName", 10 }
  306. },
  307. Filters = new List<SearchFilter>()
  308. {
  309. new(){Field = "IsDel",Operator="eq",Value="0" }
  310. },
  311. OrderBy = "VisitDate",
  312. ReturnFields = new List<string>() { "TeamName" }
  313. };
  314. // 验证字段配置
  315. var validation = _groupSearchService.ValidateFieldConfig(
  316. searchRequest.FieldWeights,
  317. searchRequest.ReturnFields);
  318. if (!validation.IsValid)
  319. {
  320. return Ok(JsonView(true, $"暂无数据!{validation.Message}"));
  321. }
  322. var result = await _groupSearchService.SearchAsync(searchRequest);
  323. if (result.Success)
  324. {
  325. var data = new List<dynamic>();
  326. foreach (var item in result.Items)
  327. {
  328. data.Add(new
  329. {
  330. item.Data.Id,
  331. item.Data.TeamName,
  332. });
  333. }
  334. return Ok(JsonView(true, result.Message, data, data.Count));
  335. }
  336. return Ok(JsonView(true, result.Message));
  337. }
  338. catch (Exception ex)
  339. {
  340. return Ok(JsonView(true, $"搜索服务暂时不可用!"));
  341. }
  342. }
  343. }
  344. }