DynamicSearchService.cs 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. using SqlSugar;
  2. using System.Diagnostics;
  3. using System.DirectoryServices;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. namespace OASystem.API.OAMethodLib.GenericSearch
  7. {
  8. /// <summary>
  9. /// 动态检索服务
  10. /// 支持动态字段权重配置、返回字段筛选、智能搜索等功能
  11. /// </summary>
  12. /// <typeparam name="T">实体类型</typeparam>
  13. public class DynamicSearchService<T> where T : class, new()
  14. {
  15. private readonly SqlSugarClient _db;
  16. private readonly ILogger<DynamicSearchService<T>> _logger;
  17. public DynamicSearchService(SqlSugarClient db, ILogger<DynamicSearchService<T>> logger)
  18. {
  19. _db = db;
  20. _logger = logger;
  21. }
  22. /// <summary>
  23. /// 执行动态搜索(应用层统计匹配度)
  24. /// </summary>
  25. /// <param name="request">搜索请求参数</param>
  26. /// <returns>包含搜索结果和匹配度信息的结果对象</returns>
  27. public async Task<SearchResult<T>> SearchAsync(DynamicSearchRequest request)
  28. {
  29. var resultView = new SearchResult<T>() { Success = false, Message = "异常错误" };
  30. var stopwatch = Stopwatch.StartNew();
  31. var searchId = Guid.NewGuid().ToString("N")[..8];
  32. try
  33. {
  34. List<T> data;
  35. int totalCount;
  36. // 使用原生SQL方式构建查询
  37. if (!string.IsNullOrWhiteSpace(request.Keyword))
  38. {
  39. var result = await SearchWithNativeSqlAsync(request);
  40. data = result.Data;
  41. totalCount = result.TotalCount;
  42. }
  43. else
  44. {
  45. // 无关键词时使用简单查询
  46. var query = BuildBaseQuery(request);
  47. totalCount = await query.CountAsync();
  48. data = await query.ToPageListAsync(request.PageIndex, request.PageSize);
  49. }
  50. // 在应用层计算匹配度
  51. var scoredItems = CalculateMatchScore(data, request);
  52. stopwatch.Stop();
  53. return new SearchResult<T>
  54. {
  55. Items = scoredItems,
  56. TotalCount = totalCount,
  57. Keyword = request.Keyword,
  58. FieldWeights = request.FieldWeights,
  59. ReturnFields = request.ReturnFields,
  60. PageIndex = request.PageIndex,
  61. PageSize = request.PageSize,
  62. ResponseTime = stopwatch.ElapsedMilliseconds,
  63. SearchId = searchId
  64. };
  65. }
  66. catch (Exception ex)
  67. {
  68. stopwatch.Stop();
  69. resultView.Message = string.Format("【{SearchId}】动态搜索失败: {ErrorMessage}", searchId, ex.Message);
  70. return resultView;
  71. }
  72. }
  73. /// <summary>
  74. /// 轻量级搜索 - 只返回指定字段,提升性能(应用层统计匹配度)
  75. /// </summary>
  76. /// <typeparam name="TResult">返回的结果类型</typeparam>
  77. /// <param name="request">搜索请求参数</param>
  78. /// <param name="selector">字段选择表达式</param>
  79. /// <returns>包含指定字段和匹配度信息的搜索结果</returns>
  80. public async Task<SearchResult<TResult>> LightweightSearchAsync<TResult>(
  81. DynamicSearchRequest request,
  82. Expression<Func<T, TResult>> selector) where TResult : class, new()
  83. {
  84. var stopwatch = Stopwatch.StartNew();
  85. var searchId = Guid.NewGuid().ToString("N")[..8];
  86. _logger.LogInformation("【{SearchId}】开始轻量级搜索: 实体{Entity}, 返回类型{ResultType}",
  87. searchId, typeof(T).Name, typeof(TResult).Name);
  88. try
  89. {
  90. // 构建基础查询
  91. var baseQuery = _db.Queryable<T>();
  92. // 应用过滤条件
  93. baseQuery = ApplyFilters(baseQuery, request.Filters);
  94. // 应用搜索条件
  95. if (!string.IsNullOrWhiteSpace(request.Keyword))
  96. {
  97. var searchAnalysis = AnalyzeSearchPattern(request.Keyword);
  98. if (searchAnalysis.HasSearchContent)
  99. {
  100. var searchFields = request.FieldWeights?.Keys.ToList() ?? GetDefaultSearchFields();
  101. var searchConditions = BuildSearchConditions(searchAnalysis, searchFields);
  102. if (searchConditions.Any())
  103. {
  104. baseQuery = baseQuery.Where(searchConditions);
  105. }
  106. }
  107. }
  108. // 应用字段选择 - 在数据库层面进行字段选择
  109. var finalQuery = baseQuery.Select(selector);
  110. // 应用排序
  111. finalQuery = ApplyOrderByForLightweight(finalQuery, request.OrderBy, request.IsDescending);
  112. // 执行查询获取轻量级数据
  113. var totalCount = await finalQuery.CountAsync();
  114. var lightweightData = await finalQuery.ToPageListAsync(request.PageIndex, request.PageSize);
  115. // 为了计算匹配度,需要查询完整的实体数据
  116. List<T> fullDataForScoring;
  117. if (!string.IsNullOrWhiteSpace(request.Keyword))
  118. {
  119. var fullResult = await SearchWithNativeSqlAsync(request);
  120. fullDataForScoring = fullResult.Data;
  121. }
  122. else
  123. {
  124. var fullQuery = BuildBaseQuery(request);
  125. fullDataForScoring = await fullQuery.ToPageListAsync(request.PageIndex, request.PageSize);
  126. }
  127. // 计算匹配度
  128. var scoredItems = CalculateMatchScore(fullDataForScoring, request);
  129. // 将匹配度信息与轻量级数据关联
  130. var lightweightItems = AssociateMatchScores(lightweightData, scoredItems, selector);
  131. stopwatch.Stop();
  132. _logger.LogInformation("【{SearchId}】轻量级搜索完成: 找到 {Count} 条记录, 耗时 {TotalTime}ms",
  133. searchId, lightweightItems.Count, stopwatch.ElapsedMilliseconds);
  134. return new SearchResult<TResult>
  135. {
  136. Items = lightweightItems,
  137. TotalCount = totalCount,
  138. Keyword = request.Keyword,
  139. FieldWeights = request.FieldWeights,
  140. PageIndex = request.PageIndex,
  141. PageSize = request.PageSize,
  142. ResponseTime = stopwatch.ElapsedMilliseconds,
  143. SearchId = searchId
  144. };
  145. }
  146. catch (Exception ex)
  147. {
  148. stopwatch.Stop();
  149. _logger.LogError(ex, "【{SearchId}】轻量级搜索失败", searchId);
  150. throw;
  151. }
  152. }
  153. /// <summary>
  154. /// 将匹配度信息与轻量级数据关联
  155. /// </summary>
  156. private List<SearchResultItem<TResult>> AssociateMatchScores<TResult>(
  157. List<TResult> lightweightData,
  158. List<SearchResultItem<T>> scoredItems,
  159. Expression<Func<T, TResult>> selector) where TResult : class, new()
  160. {
  161. var result = new List<SearchResultItem<TResult>>();
  162. // 构建一个字典来快速查找匹配度信息
  163. var scoreDict = new Dictionary<int, SearchResultItem<T>>();
  164. foreach (var scoredItem in scoredItems)
  165. {
  166. var id = GetEntityId(scoredItem.Data);
  167. if (id > 0)
  168. {
  169. scoreDict[id] = scoredItem;
  170. }
  171. }
  172. // 关联匹配度信息
  173. foreach (var lightItem in lightweightData)
  174. {
  175. var id = GetEntityId(lightItem);
  176. if (id > 0 && scoreDict.TryGetValue(id, out var scoredItem))
  177. {
  178. result.Add(new SearchResultItem<TResult>
  179. {
  180. Data = lightItem,
  181. MatchScore = scoredItem.MatchScore,
  182. MatchFields = scoredItem.MatchFields
  183. });
  184. }
  185. else
  186. {
  187. result.Add(new SearchResultItem<TResult>
  188. {
  189. Data = lightItem,
  190. MatchScore = 0,
  191. MatchFields = new List<MatchFieldInfo>()
  192. });
  193. }
  194. }
  195. return result.OrderByDescending(x => x.MatchScore).ToList();
  196. }
  197. /// <summary>
  198. /// 获取实体ID(通过反射)
  199. /// </summary>
  200. private int GetEntityId<TEntity>(TEntity entity)
  201. {
  202. if (entity == null) return 0;
  203. var idProperty = typeof(TEntity).GetProperty("Id");
  204. if (idProperty != null && idProperty.PropertyType == typeof(int))
  205. {
  206. return (int)(idProperty.GetValue(entity) ?? 0);
  207. }
  208. return 0;
  209. }
  210. /// <summary>
  211. /// 获取实体可搜索字段信息
  212. /// </summary>
  213. /// <returns>可搜索字段信息列表,按权重降序排列</returns>
  214. public List<FieldInfo> GetSearchableFields()
  215. {
  216. var entityType = typeof(T);
  217. var properties = entityType.GetProperties();
  218. var searchableFields = new List<FieldInfo>();
  219. foreach (var prop in properties)
  220. {
  221. var fieldInfo = new FieldInfo
  222. {
  223. FieldName = prop.Name,
  224. DisplayName = GetDisplayName(prop),
  225. DataType = prop.PropertyType.Name,
  226. IsSearchable = prop.PropertyType == typeof(string),
  227. DefaultWeight = GetDefaultWeight(prop.Name),
  228. Description = GetFieldDescription(prop),
  229. CanFilter = true,
  230. CanSort = true
  231. };
  232. searchableFields.Add(fieldInfo);
  233. }
  234. return searchableFields
  235. .OrderByDescending(f => f.DefaultWeight)
  236. .ThenBy(f => f.FieldName)
  237. .ToList();
  238. }
  239. /// <summary>
  240. /// 验证字段配置
  241. /// </summary>
  242. /// <param name="fieldWeights">字段权重配置</param>
  243. /// <param name="returnFields">返回字段列表</param>
  244. /// <returns>验证结果</returns>
  245. public (bool IsValid, string Message) ValidateFieldConfig(
  246. Dictionary<string, int> fieldWeights,
  247. List<string> returnFields)
  248. {
  249. var allFields = GetSearchableFields();
  250. var validFieldNames = allFields.Select(f => f.FieldName).ToList();
  251. // 验证搜索字段
  252. if (fieldWeights != null)
  253. {
  254. var invalidSearchFields = fieldWeights.Keys.Except(validFieldNames).ToList();
  255. if (invalidSearchFields.Any())
  256. {
  257. return (false, $"无效的搜索字段: {string.Join(", ", invalidSearchFields)}");
  258. }
  259. }
  260. // 验证返回字段
  261. if (returnFields != null)
  262. {
  263. var invalidReturnFields = returnFields.Except(validFieldNames).ToList();
  264. if (invalidReturnFields.Any())
  265. {
  266. return (false, $"无效的返回字段: {string.Join(", ", invalidReturnFields)}");
  267. }
  268. }
  269. return (true, "字段配置有效");
  270. }
  271. #region 私有方法 - 搜索逻辑
  272. /// <summary>
  273. /// 使用原生SQL进行搜索
  274. /// </summary>
  275. private async Task<(List<T> Data, int TotalCount)> SearchWithNativeSqlAsync(DynamicSearchRequest request)
  276. {
  277. var whereConditions = new List<string>();
  278. var parameters = new List<SugarParameter>();
  279. // 获取搜索字段
  280. var searchFields = request.FieldWeights?.Keys.ToList() ?? GetDefaultSearchFields();
  281. var validFields = ValidateSearchFields(searchFields);
  282. // 构建搜索条件
  283. if (!string.IsNullOrWhiteSpace(request.Keyword))
  284. {
  285. var searchAnalysis = AnalyzeSearchPattern(request.Keyword);
  286. // 符号分割的关键字条件
  287. foreach (var segment in searchAnalysis.SymbolSegments)
  288. {
  289. var cleanSegment = Regex.Replace(segment, @"[^\u4e00-\u9fa5a-zA-Z0-9]", "");
  290. if (!string.IsNullOrEmpty(cleanSegment))
  291. {
  292. var fieldConditions = validFields.Select(field =>
  293. {
  294. var paramName = $"@segment{parameters.Count}";
  295. parameters.Add(new SugarParameter(paramName, $"%{cleanSegment}%"));
  296. return $"{field} LIKE {paramName}";
  297. });
  298. whereConditions.Add($"({string.Join(" OR ", fieldConditions)})");
  299. }
  300. }
  301. // 单字检索条件
  302. foreach (var singleChar in searchAnalysis.SingleChars)
  303. {
  304. var charStr = singleChar.ToString();
  305. var fieldConditions = validFields.Select(field =>
  306. {
  307. var paramName = $"@char{parameters.Count}";
  308. parameters.Add(new SugarParameter(paramName, $"%{charStr}%"));
  309. return $"{field} LIKE {paramName}";
  310. });
  311. whereConditions.Add($"({string.Join(" OR ", fieldConditions)})");
  312. }
  313. }
  314. // 构建过滤条件
  315. var filterConditions = BuildNativeFilterConditions(request.Filters, parameters);
  316. whereConditions.AddRange(filterConditions);
  317. // 构建完整SQL
  318. var whereClause = whereConditions.Any()
  319. ? "WHERE " + string.Join(" AND ", whereConditions)
  320. : "";
  321. var orderByClause = BuildNativeOrderByClause(request.OrderBy, request.IsDescending);
  322. var tableName = _db.EntityMaintenance.GetTableName(typeof(T));
  323. // 构建返回字段
  324. //var returnFields = BuildReturnFields(request.ReturnFields);
  325. // 先查询总数
  326. var countSql = $"SELECT COUNT(1) FROM {tableName} {whereClause}";
  327. var totalCount = await _db.Ado.GetIntAsync(countSql, parameters);
  328. // 再查询数据
  329. var offset = (request.PageIndex - 1) * request.PageSize;
  330. var dataSql = $@"
  331. SELECT * FROM (
  332. SELECT *, ROW_NUMBER() OVER ({orderByClause}) AS RowNumber
  333. FROM {tableName}
  334. {whereClause}
  335. ) AS Paginated
  336. WHERE Paginated.RowNumber > {offset} AND Paginated.RowNumber <= {offset + request.PageSize}
  337. {orderByClause}";
  338. var data = await _db.Ado.SqlQueryAsync<T>(dataSql, parameters);
  339. return (data, totalCount);
  340. }
  341. /// <summary>
  342. /// 构建返回字段列表
  343. /// </summary>
  344. private string BuildReturnFields(List<string> returnFields)
  345. {
  346. if (returnFields == null || !returnFields.Any())
  347. {
  348. return "*";
  349. }
  350. // 验证字段是否存在
  351. var validFields = ValidateReturnFields(returnFields);
  352. if (!validFields.Any())
  353. {
  354. return "*";
  355. }
  356. // 构建字段列表,处理可能的SQL注入和字段名转义
  357. var escapedFields = validFields.Select(field =>
  358. {
  359. // 如果字段包含空格、特殊字符或关键字,用方括号括起来
  360. if (field.Contains(" ") || IsSqlKeyword(field) || field.Contains("."))
  361. {
  362. return $"[{field}]";
  363. }
  364. return field;
  365. });
  366. return string.Join(", ", escapedFields);
  367. }
  368. /// <summary>
  369. /// 验证返回字段的有效性
  370. /// </summary>
  371. private List<string> ValidateReturnFields(List<string> returnFields)
  372. {
  373. if (returnFields == null) return new List<string>();
  374. var entityType = typeof(T);
  375. var entityProperties = entityType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
  376. .Select(p => p.Name)
  377. .ToList();
  378. // 过滤出实体类中存在的字段
  379. var validFields = returnFields
  380. .Where(field => entityProperties.Contains(field, StringComparer.OrdinalIgnoreCase))
  381. .ToList();
  382. // 记录无效字段警告
  383. var invalidFields = returnFields.Except(validFields, StringComparer.OrdinalIgnoreCase).ToList();
  384. if (invalidFields.Any())
  385. {
  386. _logger.LogWarning("发现无效的返回字段: {InvalidFields}", string.Join(", ", invalidFields));
  387. }
  388. return validFields;
  389. }
  390. /// <summary>
  391. /// 检查是否为SQL关键字
  392. /// </summary>
  393. private bool IsSqlKeyword(string word)
  394. {
  395. var sqlKeywords = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
  396. {
  397. "SELECT", "FROM", "WHERE", "INSERT", "UPDATE", "DELETE", "CREATE", "DROP",
  398. "ALTER", "TABLE", "VIEW", "INDEX", "PRIMARY", "KEY", "FOREIGN", "REFERENCES",
  399. "AND", "OR", "NOT", "LIKE", "IN", "BETWEEN", "IS", "NULL", "ORDER", "BY",
  400. "GROUP", "HAVING", "JOIN", "INNER", "LEFT", "RIGHT", "OUTER", "ON", "AS",
  401. "DISTINCT", "TOP", "COUNT", "SUM", "AVG", "MIN", "MAX", "UNION", "ALL"
  402. };
  403. return sqlKeywords.Contains(word);
  404. }
  405. /// <summary>
  406. /// 构建基础查询
  407. /// </summary>
  408. private ISugarQueryable<T> BuildBaseQuery(DynamicSearchRequest request)
  409. {
  410. var query = _db.Queryable<T>();
  411. // 应用过滤条件
  412. query = ApplyFilters(query, request.Filters);
  413. // 应用排序
  414. query = ApplyOrderBy(query, request.OrderBy, request.IsDescending);
  415. return query;
  416. }
  417. /// <summary>
  418. /// 构建原生过滤条件
  419. /// </summary>
  420. private List<string> BuildNativeFilterConditions(List<SearchFilter> filters, List<SugarParameter> parameters)
  421. {
  422. var conditions = new List<string>();
  423. if (filters == null) return conditions;
  424. foreach (var filter in filters)
  425. {
  426. var condition = filter.Operator?.ToLower() switch
  427. {
  428. "eq" => BuildNativeCondition(filter, "=", parameters),
  429. "neq" => BuildNativeCondition(filter, "!=", parameters),
  430. "contains" => BuildNativeLikeCondition(filter, "%", "%", parameters),
  431. "startswith" => BuildNativeLikeCondition(filter, "", "%", parameters),
  432. "endswith" => BuildNativeLikeCondition(filter, "%", "", parameters),
  433. "gt" => BuildNativeCondition(filter, ">", parameters),
  434. "gte" => BuildNativeCondition(filter, ">=", parameters),
  435. "lt" => BuildNativeCondition(filter, "<", parameters),
  436. "lte" => BuildNativeCondition(filter, "<=", parameters),
  437. "in" => BuildNativeInCondition(filter, parameters),
  438. _ => null
  439. };
  440. if (!string.IsNullOrEmpty(condition))
  441. {
  442. conditions.Add(condition);
  443. }
  444. }
  445. return conditions;
  446. }
  447. private string BuildNativeCondition(SearchFilter filter, string op, List<SugarParameter> parameters)
  448. {
  449. var paramName = $"@filter{parameters.Count}";
  450. parameters.Add(new SugarParameter(paramName, filter.Value));
  451. return $"{filter.Field} {op} {paramName}";
  452. }
  453. private string BuildNativeLikeCondition(SearchFilter filter, string prefix, string suffix, List<SugarParameter> parameters)
  454. {
  455. var paramName = $"@filter{parameters.Count}";
  456. parameters.Add(new SugarParameter(paramName, $"{prefix}{filter.Value}{suffix}"));
  457. return $"{filter.Field} LIKE {paramName}";
  458. }
  459. private string BuildNativeInCondition(SearchFilter filter, List<SugarParameter> parameters)
  460. {
  461. if (filter.Values == null || !filter.Values.Any())
  462. return null;
  463. var paramNames = new List<string>();
  464. foreach (var value in filter.Values)
  465. {
  466. var paramName = $"@filter{parameters.Count}";
  467. parameters.Add(new SugarParameter(paramName, value));
  468. paramNames.Add(paramName);
  469. }
  470. return $"{filter.Field} IN ({string.Join(",", paramNames)})";
  471. }
  472. #endregion
  473. #region 私有方法 - 匹配度计算
  474. /// <summary>
  475. /// 在应用层计算匹配度
  476. /// </summary>
  477. private List<SearchResultItem<T>> CalculateMatchScore(List<T> data, DynamicSearchRequest request)
  478. {
  479. if (string.IsNullOrWhiteSpace(request.Keyword))
  480. {
  481. // 无关键词时,所有记录匹配度为0
  482. return data.Select(item => new SearchResultItem<T>
  483. {
  484. Data = item,
  485. MatchScore = 0
  486. }).ToList();
  487. }
  488. var searchAnalysis = AnalyzeSearchPattern(request.Keyword);
  489. var searchFields = request.FieldWeights?.Keys.ToList() ?? GetDefaultSearchFields();
  490. var fieldWeights = request.FieldWeights ?? GetDefaultFieldWeights(searchFields);
  491. var scoredItems = data.Select(item =>
  492. {
  493. var matchResult = CalculateItemMatchScore(item, searchAnalysis, searchFields, fieldWeights);
  494. return new SearchResultItem<T>
  495. {
  496. Data = item,
  497. MatchScore = matchResult.TotalScore,
  498. MatchFields = matchResult.MatchFields
  499. };
  500. })
  501. .Where(item => item.MatchScore > 0) // 只保留有匹配的记录
  502. .OrderByDescending(item => item.MatchScore)
  503. .ThenByDescending(item => GetCreateTime(item.Data))
  504. .ToList();
  505. return scoredItems;
  506. }
  507. /// <summary>
  508. /// 计算单个项的匹配度详情
  509. /// </summary>
  510. private (int TotalScore, List<MatchFieldInfo> MatchFields) CalculateItemMatchScore(
  511. T item,
  512. SearchAnalysis analysis,
  513. List<string> searchFields,
  514. Dictionary<string, int> fieldWeights)
  515. {
  516. int totalScore = 0;
  517. var matchFields = new List<MatchFieldInfo>();
  518. foreach (var field in searchFields)
  519. {
  520. var fieldValue = GetFieldValue(item, field);
  521. if (string.IsNullOrEmpty(fieldValue))
  522. continue;
  523. var weight = fieldWeights.ContainsKey(field) ? fieldWeights[field] : GetDefaultWeight(field);
  524. int fieldScore = 0;
  525. var fieldMatchReasons = new List<string>();
  526. // 符号分割关键字匹配
  527. foreach (var segment in analysis.SymbolSegments)
  528. {
  529. var cleanSegment = Regex.Replace(segment, @"[^\u4e00-\u9fa5a-zA-Z0-9]", "");
  530. if (!string.IsNullOrEmpty(cleanSegment) && fieldValue.Contains(cleanSegment))
  531. {
  532. int segmentScore = weight;
  533. if (fieldValue.Equals(cleanSegment))
  534. {
  535. segmentScore += 15;
  536. fieldMatchReasons.Add($"完全匹配 '{cleanSegment}'");
  537. }
  538. else if (fieldValue.StartsWith(cleanSegment))
  539. {
  540. segmentScore += 10;
  541. fieldMatchReasons.Add($"开头匹配 '{cleanSegment}'");
  542. }
  543. else if (fieldValue.EndsWith(cleanSegment))
  544. {
  545. segmentScore += 5;
  546. fieldMatchReasons.Add($"结尾匹配 '{cleanSegment}'");
  547. }
  548. else
  549. {
  550. fieldMatchReasons.Add($"包含 '{cleanSegment}'");
  551. }
  552. fieldScore += segmentScore;
  553. }
  554. }
  555. // 单字匹配
  556. foreach (var singleChar in analysis.SingleChars)
  557. {
  558. int count = fieldValue.Count(c => c == singleChar);
  559. if (count > 0)
  560. {
  561. int charScore = count * (int)(weight * 0.3);
  562. if (fieldValue.StartsWith(singleChar.ToString()))
  563. {
  564. charScore += weight;
  565. fieldMatchReasons.Add($"开头单字 '{singleChar}'");
  566. }
  567. else
  568. {
  569. fieldMatchReasons.Add($"包含单字 '{singleChar}'({count}次)");
  570. }
  571. fieldScore += charScore;
  572. }
  573. }
  574. if (fieldScore > 0)
  575. {
  576. totalScore += fieldScore;
  577. matchFields.Add(new MatchFieldInfo
  578. {
  579. FieldName = field,
  580. FieldValue = GetDisplayFieldValue(fieldValue),
  581. Score = fieldScore,
  582. MatchReason = string.Join("; ", fieldMatchReasons)
  583. });
  584. }
  585. }
  586. // 按分数排序匹配字段
  587. matchFields = matchFields.OrderByDescending(m => m.Score).ToList();
  588. return (totalScore, matchFields);
  589. }
  590. /// <summary>
  591. /// 获取显示用的字段值(截断过长的内容)
  592. /// </summary>
  593. private string GetDisplayFieldValue(string fieldValue)
  594. {
  595. if (string.IsNullOrEmpty(fieldValue))
  596. return fieldValue;
  597. // 如果字段值过长,截断显示
  598. return fieldValue.Length > 50 ? fieldValue.Substring(0, 50) + "..." : fieldValue;
  599. }
  600. #endregion
  601. #region 私有方法 - 辅助功能
  602. /// <summary>
  603. /// 应用过滤条件
  604. /// </summary>
  605. private ISugarQueryable<T> ApplyFilters(ISugarQueryable<T> query, List<SearchFilter> filters)
  606. {
  607. if (filters == null || !filters.Any())
  608. return query;
  609. foreach (var filter in filters)
  610. {
  611. query = filter.Operator?.ToLower() switch
  612. {
  613. "eq" => query.Where($"{filter.Field} = @Value", new { filter.Value }),
  614. "neq" => query.Where($"{filter.Field} != @Value", new { filter.Value }),
  615. "contains" => query.Where($"{filter.Field} LIKE '%' + @Value + '%'", new { filter.Value }),
  616. "startswith" => query.Where($"{filter.Field} LIKE @Value + '%'", new { filter.Value }),
  617. "endswith" => query.Where($"{filter.Field} LIKE '%' + @Value", new { filter.Value }),
  618. "gt" => query.Where($"{filter.Field} > @Value", new { filter.Value }),
  619. "gte" => query.Where($"{filter.Field} >= @Value", new { filter.Value }),
  620. "lt" => query.Where($"{filter.Field} < @Value", new { filter.Value }),
  621. "lte" => query.Where($"{filter.Field} <= @Value", new { filter.Value }),
  622. "in" => ApplyInFilter(query, filter),
  623. _ => query
  624. };
  625. }
  626. return query;
  627. }
  628. /// <summary>
  629. /// 使用SqlSugar条件构建器构建搜索条件
  630. /// </summary>
  631. private List<IConditionalModel> BuildSearchConditions(SearchAnalysis analysis, List<string> searchFields)
  632. {
  633. var conditionalModels = new List<IConditionalModel>();
  634. // 获取有效的搜索字段
  635. var validFields = ValidateSearchFields(searchFields);
  636. if (!validFields.Any())
  637. return conditionalModels;
  638. // 1. 符号分割的关键字条件
  639. foreach (var segment in analysis.SymbolSegments)
  640. {
  641. var cleanSegment = Regex.Replace(segment, @"[^\u4e00-\u9fa5a-zA-Z0-9]", "");
  642. if (!string.IsNullOrEmpty(cleanSegment))
  643. {
  644. var segmentGroup = new List<IConditionalModel>();
  645. foreach (var field in validFields)
  646. {
  647. segmentGroup.Add(new ConditionalModel
  648. {
  649. FieldName = field,
  650. ConditionalType = ConditionalType.Like,
  651. FieldValue = $"%{cleanSegment}%"
  652. });
  653. }
  654. if (segmentGroup.Count > 1)
  655. {
  656. conditionalModels.Add(new ConditionalCollections
  657. {
  658. ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>(
  659. segmentGroup.Select((model, index) =>
  660. new KeyValuePair<WhereType, ConditionalModel>(
  661. index == 0 ? WhereType.And : WhereType.Or,
  662. (ConditionalModel)model))
  663. )
  664. });
  665. }
  666. else if (segmentGroup.Count == 1)
  667. {
  668. conditionalModels.Add(segmentGroup[0]);
  669. }
  670. }
  671. }
  672. return conditionalModels;
  673. }
  674. /// <summary>
  675. /// 应用IN过滤条件
  676. /// </summary>
  677. private ISugarQueryable<T> ApplyInFilter(ISugarQueryable<T> query, SearchFilter filter)
  678. {
  679. if (filter.Values == null || !filter.Values.Any())
  680. return query;
  681. var valueList = string.Join(",", filter.Values.Select(v => $"'{v}'"));
  682. return query.Where($"{filter.Field} IN ({valueList})");
  683. }
  684. /// <summary>
  685. /// 应用排序
  686. /// </summary>
  687. private ISugarQueryable<T> ApplyOrderBy(ISugarQueryable<T> query, string orderBy, bool isDescending)
  688. {
  689. if (string.IsNullOrWhiteSpace(orderBy))
  690. {
  691. // 默认按主键或创建时间排序
  692. var entityType = typeof(T);
  693. var idProperty = entityType.GetProperty("Id") ?? entityType.GetProperty("CreateTime");
  694. if (idProperty != null)
  695. {
  696. orderBy = idProperty.Name;
  697. }
  698. }
  699. if (!string.IsNullOrWhiteSpace(orderBy))
  700. {
  701. return isDescending
  702. ? query.OrderBy($"{orderBy} DESC")
  703. : query.OrderBy($"{orderBy} ASC");
  704. }
  705. return query;
  706. }
  707. /// <summary>
  708. /// 为轻量级搜索应用排序
  709. /// </summary>
  710. private ISugarQueryable<TResult> ApplyOrderByForLightweight<TResult>(
  711. ISugarQueryable<TResult> query,
  712. string orderBy,
  713. bool isDescending) where TResult : class, new()
  714. {
  715. if (string.IsNullOrWhiteSpace(orderBy))
  716. {
  717. // 检查结果类型是否有Id或CreateTime字段
  718. var resultType = typeof(TResult);
  719. var idProperty = resultType.GetProperty("Id") ?? resultType.GetProperty("CreateTime");
  720. if (idProperty != null)
  721. {
  722. orderBy = idProperty.Name;
  723. }
  724. else
  725. {
  726. // 如果没有默认排序字段,返回原查询
  727. return query;
  728. }
  729. }
  730. if (!string.IsNullOrWhiteSpace(orderBy))
  731. {
  732. // 验证排序字段是否存在于结果类型中
  733. var resultType = typeof(TResult);
  734. var orderByProperty = resultType.GetProperty(orderBy);
  735. if (orderByProperty != null)
  736. {
  737. return isDescending
  738. ? query.OrderBy($"{orderBy} DESC")
  739. : query.OrderBy($"{orderBy} ASC");
  740. }
  741. else
  742. {
  743. _logger.LogWarning("排序字段 {OrderBy} 在返回类型 {ResultType} 中不存在", orderBy, resultType.Name);
  744. }
  745. }
  746. return query;
  747. }
  748. /// <summary>
  749. /// 构建原生排序子句
  750. /// </summary>
  751. private string BuildNativeOrderByClause(string orderBy, bool isDescending)
  752. {
  753. if (string.IsNullOrWhiteSpace(orderBy))
  754. //return "ORDER BY Id DESC";
  755. return "";
  756. return $"ORDER BY {orderBy} {(isDescending ? "DESC" : "ASC")}";
  757. }
  758. /// <summary>
  759. /// 分析搜索模式
  760. /// </summary>
  761. private SearchAnalysis AnalyzeSearchPattern(string keyword)
  762. {
  763. var analysis = new SearchAnalysis { OriginalKeyword = keyword };
  764. // 检查是否包含特殊符号
  765. var specialSymbols = new[] { ' ', ',', ',', ';', ';', '、', '/', '\\', '|', '-', '_' };
  766. if (keyword.Any(c => specialSymbols.Contains(c)))
  767. {
  768. analysis.HasSpecialSymbols = true;
  769. analysis.SymbolSegments = keyword.Split(specialSymbols, StringSplitOptions.RemoveEmptyEntries)
  770. .Select(s => s.Trim())
  771. .Where(s => !string.IsNullOrEmpty(s))
  772. .ToList();
  773. }
  774. // 清理关键词并提取单字
  775. var cleanKeyword = Regex.Replace(keyword, @"[^\u4e00-\u9fa5a-zA-Z0-9]", "");
  776. if (!string.IsNullOrEmpty(cleanKeyword))
  777. {
  778. foreach (char c in cleanKeyword)
  779. {
  780. analysis.SingleChars.Add(c);
  781. }
  782. analysis.IsSingleChar = true;
  783. // 如果没有特殊符号但有关键词,也作为符号分割段
  784. if (cleanKeyword.Length > 1 && !analysis.HasSpecialSymbols)
  785. {
  786. analysis.SymbolSegments.Add(cleanKeyword);
  787. }
  788. }
  789. // 去重
  790. analysis.SingleChars = analysis.SingleChars.Distinct().ToList();
  791. analysis.SymbolSegments = analysis.SymbolSegments.Distinct().ToList();
  792. return analysis;
  793. }
  794. /// <summary>
  795. /// 获取创建时间(用于排序)
  796. /// </summary>
  797. private DateTime GetCreateTime(T item)
  798. {
  799. var createTimeProperty = typeof(T).GetProperty("CreateTime");
  800. if (createTimeProperty != null)
  801. {
  802. return (DateTime)(createTimeProperty.GetValue(item) ?? DateTime.MinValue);
  803. }
  804. return DateTime.MinValue;
  805. }
  806. /// <summary>
  807. /// 获取字段值
  808. /// </summary>
  809. private string GetFieldValue(T item, string fieldName)
  810. {
  811. var property = typeof(T).GetProperty(fieldName);
  812. return property?.GetValue(item) as string;
  813. }
  814. /// <summary>
  815. /// 获取默认搜索字段
  816. /// </summary>
  817. private List<string> GetDefaultSearchFields()
  818. {
  819. return typeof(T).GetProperties()
  820. .Where(p => p.PropertyType == typeof(string))
  821. .Select(p => p.Name)
  822. .Take(5)
  823. .ToList();
  824. }
  825. /// <summary>
  826. /// 获取默认字段权重
  827. /// </summary>
  828. private Dictionary<string, int> GetDefaultFieldWeights(List<string> fields)
  829. {
  830. var weights = new Dictionary<string, int>();
  831. foreach (var field in fields)
  832. {
  833. weights[field] = GetDefaultWeight(field);
  834. }
  835. return weights;
  836. }
  837. /// <summary>
  838. /// 验证字段有效性
  839. /// </summary>
  840. private List<string> ValidateFields(List<string> fields)
  841. {
  842. var validFields = typeof(T).GetProperties()
  843. .Select(p => p.Name)
  844. .ToList();
  845. return fields.Intersect(validFields).ToList();
  846. }
  847. /// <summary>
  848. /// 验证搜索字段
  849. /// </summary>
  850. private List<string> ValidateSearchFields(List<string> searchFields)
  851. {
  852. var validFields = typeof(T).GetProperties()
  853. .Where(p => p.PropertyType == typeof(string))
  854. .Select(p => p.Name)
  855. .ToList();
  856. return searchFields.Intersect(validFields).ToList();
  857. }
  858. /// <summary>
  859. /// 获取默认权重
  860. /// </summary>
  861. private int GetDefaultWeight(string fieldName)
  862. {
  863. return fieldName.ToLower() switch
  864. {
  865. var name when name.Contains("name") => 10,
  866. var name when name.Contains("title") => 10,
  867. var name when name.Contains("code") => 8,
  868. var name when name.Contains("no") => 8,
  869. var name when name.Contains("desc") => 6,
  870. var name when name.Contains("content") => 6,
  871. var name when name.Contains("remark") => 5,
  872. var name when name.Contains("phone") => 4,
  873. var name when name.Contains("tel") => 4,
  874. var name when name.Contains("address") => 3,
  875. var name when name.Contains("email") => 3,
  876. _ => 5
  877. };
  878. }
  879. /// <summary>
  880. /// 获取显示名称
  881. /// </summary>
  882. private string GetDisplayName(System.Reflection.PropertyInfo property)
  883. {
  884. var displayAttr = property.GetCustomAttribute<System.ComponentModel.DataAnnotations.DisplayAttribute>();
  885. return displayAttr?.Name ?? property.Name;
  886. }
  887. /// <summary>
  888. /// 获取字段描述
  889. /// </summary>
  890. private string GetFieldDescription(System.Reflection.PropertyInfo property)
  891. {
  892. var descriptionAttr = property.GetCustomAttribute<System.ComponentModel.DescriptionAttribute>();
  893. return descriptionAttr?.Description ?? string.Empty;
  894. }
  895. #endregion
  896. }
  897. }