SearchController.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. namespace OASystem.API.Controllers
  15. {
  16. /// <summary>
  17. /// 搜索
  18. /// </summary>
  19. [Route("api/search")]
  20. [ApiController]
  21. public class SearchController : ControllerBase
  22. {
  23. private readonly SqlSugarClient _sqlSugar;
  24. private readonly DynamicSearchService<Grp_DelegationInfo> _groupSearchService;
  25. private readonly DynamicSearchService<NewClientDataView> _clientSearchService;
  26. public SearchController(
  27. SqlSugarClient sqlSugar,
  28. DynamicSearchService<Grp_DelegationInfo> groupSearchService,
  29. DynamicSearchService<NewClientDataView> clientSearchService
  30. )
  31. {
  32. _sqlSugar = sqlSugar;
  33. _groupSearchService = groupSearchService;
  34. _clientSearchService = clientSearchService;
  35. }
  36. /// <summary>
  37. /// 接团信息 关键字输入提示(单字段)
  38. /// </summary>
  39. /// <param name="keyword">关键字</param>
  40. /// <returns></returns>
  41. [HttpGet("group/{keyword}")]
  42. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  43. public async Task<IActionResult> GroupKeywordSearch(string keyword)
  44. {
  45. try
  46. {
  47. // 验证请求参数
  48. if (string.IsNullOrEmpty(keyword))
  49. {
  50. return Ok(JsonView(true, $"暂无数据!"));
  51. }
  52. var searchRequest = new DynamicSearchRequest
  53. {
  54. Keyword = keyword,
  55. RequireAllSingleChars = true,
  56. PageIndex = 1,
  57. PageSize = 999999,
  58. FieldWeights = new Dictionary<string, int>
  59. {
  60. { "TeamName", 10 },
  61. //{ "ClientUnit", 8 },
  62. //{ "ClientName", 6 }
  63. },
  64. Filters = new List<SearchFilter>()
  65. {
  66. new(){Field = "IsDel",Operator="eq",Value="0" }
  67. },
  68. OrderBy = "TeamName",
  69. ReturnFields = new List<string>() { "TeamName" }
  70. };
  71. // 验证字段配置
  72. var validation = _groupSearchService.ValidateFieldConfig(
  73. searchRequest.FieldWeights,
  74. searchRequest.ReturnFields);
  75. if (!validation.IsValid)
  76. {
  77. return Ok(JsonView(true, $"暂无数据!{validation.Message}"));
  78. }
  79. var result = await _groupSearchService.SearchAsync(searchRequest);
  80. if (result.Success)
  81. {
  82. var data = result.Items.Select(x => new { x.Data.Id, x.Data.TeamName }).ToList();
  83. return Ok(JsonView(true, result.Message, data, data.Count));
  84. }
  85. return Ok(JsonView(true, result.Message));
  86. }
  87. catch (Exception ex)
  88. {
  89. return Ok(JsonView(true, $"搜索服务暂时不可用!"));
  90. }
  91. }
  92. /// <summary>
  93. /// 客户资料 关键字输入提示(多字段)
  94. /// </summary>
  95. /// <param name="userId">关键字</param>
  96. /// <param name="keyword">关键字</param>
  97. /// <returns></returns>
  98. [HttpGet("group/{userId}/{keyword}")]
  99. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  100. public async Task<IActionResult> ClientKeywordSearch(int userId,string keyword)
  101. {
  102. try
  103. {
  104. // 验证请求参数
  105. if (string.IsNullOrEmpty(keyword))
  106. {
  107. return Ok(JsonView(true, $"暂无数据!"));
  108. }
  109. //查询有权限的数据
  110. var clientIds = await _sqlSugar
  111. .Queryable<Crm_ClientDataAndUser>()
  112. .Where(x => x.IsDel == 0 && x.usersId == userId)
  113. .Select(x => x.NewClientDataId)
  114. .ToListAsync();
  115. if (clientIds == null || clientIds.Count < 1)
  116. {
  117. return Ok(JsonView(true, $"暂无数据!"));
  118. }
  119. var data = _sqlSugar.Queryable<Crm_NewClientData>()
  120. .Where(x => x.IsDel == 0 && clientIds.Contains(x.Id))
  121. .OrderByDescending(x => x.CreateTime)
  122. .Select(x => new NewClientDataView (){
  123. Id = x.Id,
  124. Client = x.Client,
  125. Contact = x.Contact,
  126. Job = x.Job,
  127. Telephone = x.Telephone,
  128. Location = x.Location,
  129. })
  130. .ToList();
  131. int index = 0;
  132. data.ForEach(x =>
  133. {
  134. index++;
  135. x.RowNumber = index;
  136. x.Client = AesEncryptionHelper.Decrypt(x.Client);
  137. x.Contact = AesEncryptionHelper.Decrypt(x.Contact);
  138. x.Job = AesEncryptionHelper.Decrypt(x.Job);
  139. x.Telephone = AesEncryptionHelper.Decrypt(x.Telephone);
  140. x.Location = AesEncryptionHelper.Decrypt(x.Location);
  141. });
  142. var searchRequest = new DynamicSearchRequest
  143. {
  144. Keyword = keyword,
  145. RequireAllSingleChars = true,
  146. PageIndex = 1,
  147. PageSize = 999999,
  148. FieldWeights = new Dictionary<string, int>
  149. {
  150. { "Client", 10 },
  151. { "Contact", 8 },
  152. { "Location", 6 }
  153. },
  154. OrderBy = "CreateTime"
  155. };
  156. // 验证字段配置
  157. var validation = _clientSearchService.ValidateFieldConfig(
  158. searchRequest.FieldWeights,
  159. searchRequest.ReturnFields);
  160. if (!validation.IsValid)
  161. {
  162. return Ok(JsonView(true, $"暂无数据!{validation.Message}"));
  163. }
  164. var result = _clientSearchService.SearchDataSource(searchRequest,data);
  165. if (result.Success)
  166. {
  167. var view = result.Items.Select(x => x.Data).ToList();
  168. int resetIndex = 0;
  169. view.ForEach(x => {
  170. resetIndex++;
  171. x.RowNumber = resetIndex;
  172. });
  173. return Ok(JsonView(true, result.Message, view, view.Count));
  174. }
  175. return Ok(JsonView(true,"暂无数据"));
  176. }
  177. catch (Exception ex)
  178. {
  179. return Ok(JsonView(true, $"搜索服务暂时不可用!"));
  180. }
  181. }
  182. }
  183. }