Ver código fonte

客户资料 单字段搜索

Lyyyi 1 mês atrás
pai
commit
ca118aa4b7

+ 119 - 3
OASystem/OASystem.Api/Controllers/SearchController.cs

@@ -1,10 +1,16 @@
-using Microsoft.AspNetCore.Http;
+using EyeSoft.Collections.Generic;
+using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
+using NPOI.SS.Formula.Functions;
 using OASystem.API.OAMethodLib.DeepSeekAPI;
 using OASystem.API.OAMethodLib.GenericSearch;
+using OASystem.Domain.AesEncryption;
+using OASystem.Domain.Entities.Customer;
 using OASystem.Domain.Entities.Financial;
 using OASystem.Domain.Entities.Groups;
+using OASystem.Domain.ViewModels.Search;
 using OASystem.Infrastructure.Repositories.System;
+using static iTextSharp.text.pdf.AcroFields;
 
 namespace OASystem.API.Controllers
 {
@@ -15,16 +21,22 @@ namespace OASystem.API.Controllers
     [ApiController]
     public class SearchController : ControllerBase
     {
+        private readonly SqlSugarClient _sqlSugar;
         private readonly DynamicSearchService<Grp_DelegationInfo> _groupSearchService;
+        private readonly DynamicSearchService<NewClientDataView> _clientSearchService;
         public SearchController(
-           DynamicSearchService<Grp_DelegationInfo> groupSearchService
+            SqlSugarClient sqlSugar,
+           DynamicSearchService<Grp_DelegationInfo> groupSearchService,
+           DynamicSearchService<NewClientDataView> clientSearchService
            )
         {
+            _sqlSugar = sqlSugar;
             _groupSearchService = groupSearchService;
+            _clientSearchService = clientSearchService;
         }
 
         /// <summary>
-        ///  接团信息  单字段(团组名称)关键字输入提示
+        ///  接团信息  关键字输入提示(单字段)
         /// </summary>
         /// <param name="keyword">关键字</param>
         /// <returns></returns>
@@ -88,5 +100,109 @@ namespace OASystem.API.Controllers
 
         }
 
+        /// <summary>
+        /// 客户资料  关键字输入提示(多字段)
+        /// </summary>
+        /// <param name="userId">关键字</param>
+        /// <param name="keyword">关键字</param>
+        /// <returns></returns>
+        [HttpGet("group/{userId}/{keyword}")]
+        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
+        public async Task<IActionResult> ClientKeywordSearch(int userId,string keyword)
+        {
+            try
+            {
+                // 验证请求参数
+                if (string.IsNullOrEmpty(keyword))
+                {
+                    return Ok(JsonView(true, $"暂无数据!"));
+                }
+
+                //查询有权限的数据
+                var clientIds = await _sqlSugar
+                    .Queryable<Crm_ClientDataAndUser>()
+                    .Where(x => x.IsDel == 0 && x.usersId == userId)
+                    .Select(x => x.NewClientDataId)
+                    .ToListAsync();
+                if (clientIds == null || clientIds.Count < 1)
+                {
+                    return Ok(JsonView(true, $"暂无数据!"));
+                }
+
+                var data = _sqlSugar.Queryable<Crm_NewClientData>()
+                    .Where(x => x.IsDel == 0 && clientIds.Contains(x.Id))
+                    .OrderByDescending(x => x.CreateTime)
+                    .Select(x => new NewClientDataView (){
+                        Id = x.Id,
+                        Client = x.Client,
+                        Contact = x.Contact,
+                        Job = x.Job,
+                        Telephone = x.Telephone,
+                        Location = x.Location,
+                    })
+                    .ToList();
+                int index = 0;
+                data.ForEach(x =>
+                {
+                    index++;
+                    x.RowNumber = index;
+                    x.Client = AesEncryptionHelper.Decrypt(x.Client);
+                    x.Contact = AesEncryptionHelper.Decrypt(x.Contact);
+                    x.Job = AesEncryptionHelper.Decrypt(x.Job);
+                    x.Telephone = AesEncryptionHelper.Decrypt(x.Telephone);
+                    x.Location = AesEncryptionHelper.Decrypt(x.Location);
+                });
+
+
+                var searchRequest = new DynamicSearchRequest
+                {
+                    Keyword = keyword,
+                    RequireAllSingleChars = true,
+                    PageIndex = 1,
+                    PageSize = 999999,
+                    FieldWeights = new Dictionary<string, int>
+                    {
+                        { "Client", 10 },
+                        { "Contact", 8 },
+                        { "Location", 6 }
+                    },
+                    OrderBy = "CreateTime"
+                };
+
+                // 验证字段配置
+                var validation = _clientSearchService.ValidateFieldConfig(
+                    searchRequest.FieldWeights,
+                    searchRequest.ReturnFields);
+
+                if (!validation.IsValid)
+                {
+                    return Ok(JsonView(true, $"暂无数据!{validation.Message}"));
+                }
+
+                var result = _clientSearchService.SearchDataSource(searchRequest,data);
+
+                if (result.Success)
+                {
+                    var view = result.Items.Select(x => x.Data).ToList();
+
+                    int resetIndex = 0;
+                    view.ForEach(x => {
+                        resetIndex++;
+                        x.RowNumber = resetIndex;
+                    });
+
+                    return Ok(JsonView(true, result.Message, view, view.Count));
+                }
+
+                return Ok(JsonView(true,"暂无数据"));
+            }
+            catch (Exception ex)
+            {
+                return Ok(JsonView(true, $"搜索服务暂时不可用!"));
+            }
+
+        }
+
+
     }
 }

+ 1 - 0
OASystem/OASystem.Api/OAMethodLib/GenericSearch/DynamicSearchRequest.cs

@@ -50,6 +50,7 @@
         /// 是否要求所有单字必须完全出现(新增参数)
         /// </summary>
         public bool RequireAllSingleChars { get; set; } = false;
+
     }
 
     /// <summary>

+ 49 - 48
OASystem/OASystem.Api/OAMethodLib/GenericSearch/DynamicSearchService.cs

@@ -1,7 +1,4 @@
-using SqlSugar;
-using System.Diagnostics;
-using System.DirectoryServices;
-using System.Linq;
+using System.Diagnostics;
 using System.Linq.Expressions;
 
 namespace OASystem.API.OAMethodLib.GenericSearch
@@ -81,6 +78,54 @@ namespace OASystem.API.OAMethodLib.GenericSearch
             }
         }
 
+        /// <summary>
+        /// 执行动态搜索(应用层统计匹配度)
+        /// 基于数据源
+        /// </summary>
+        /// <param name="request">搜索请求参数</param>
+        /// <param name="dataSource">数据源</param>
+        /// <returns>包含搜索结果和匹配度信息的结果对象</returns>
+        public SearchResult<T> SearchDataSource(DynamicSearchRequest request, List<T> dataSource)
+        {
+            var resultView = new SearchResult<T>() { Success = false, Message = "异常错误" };
+
+            var stopwatch = Stopwatch.StartNew();
+            var searchId = Guid.NewGuid().ToString("N")[..8];
+
+            if (dataSource == null) return new SearchResult<T>();
+
+            try
+            {
+                List<T> data = dataSource;
+                int totalCount = dataSource.Count;
+
+                // 在应用层计算匹配度
+                var scoredItems = CalculateMatchScore(data, request);
+
+                stopwatch.Stop();
+
+                return new SearchResult<T>
+                {
+                    Message = $"搜索成功!耗时:{stopwatch.ElapsedMilliseconds}ms",
+                    Items = scoredItems,
+                    TotalCount = totalCount,
+                    Keyword = request.Keyword,
+                    FieldWeights = request.FieldWeights,
+                    ReturnFields = request.ReturnFields,
+                    PageIndex = request.PageIndex,
+                    PageSize = request.PageSize,
+                    ResponseTime = stopwatch.ElapsedMilliseconds,
+                    SearchId = searchId
+                };
+            }
+            catch (Exception ex)
+            {
+                stopwatch.Stop();
+                resultView.Message = string.Format("【{SearchId}】动态搜索失败: {ErrorMessage}", searchId, ex.Message);
+                return resultView;
+            }
+        }
+
         /// <summary>
         /// 轻量级搜索 - 只返回指定字段,提升性能(应用层统计匹配度)
         /// </summary>
@@ -367,50 +412,6 @@ namespace OASystem.API.OAMethodLib.GenericSearch
                     whereConditions.Add($"({string.Join(" OR ", keywordConditions)})");
                 }
                 #endregion
-
-                #region or 构建
-                //var searchAnalysis = AnalyzeSearchPattern(request.Keyword);
-                //var keywordConditions = new List<string>();
-
-                //// 统一处理所有搜索词
-                //var allSearchTerms = new List<string>();
-
-                //// 添加符号分割的片段(去除特殊字符)
-                //allSearchTerms.AddRange(searchAnalysis.SymbolSegments
-                //    .Select(segment => Regex.Replace(segment, @"[^\u4e00-\u9fa5a-zA-Z0-9]", ""))
-                //    .Where(segment => !string.IsNullOrEmpty(segment)));
-
-                //// 添加单字(排除重复)
-                //foreach (var singleChar in searchAnalysis.SingleChars)
-                //{
-                //    var charStr = singleChar.ToString();
-                //    // 只有当单字不在任何符号片段中时才添加
-                //    if (!allSearchTerms.Any(term => term.Contains(charStr)))
-                //    {
-                //        allSearchTerms.Add(charStr);
-                //    }
-                //}
-
-                //// 处理每个搜索词
-                //foreach (var term in allSearchTerms.Distinct())
-                //{
-                //    var fieldConditions = validFields.Select(field =>
-                //    {
-                //        var paramName = $"@term{parameters.Count}";
-                //        parameters.Add(new SugarParameter(paramName, $"%{term}%"));
-                //        return $"{field} LIKE {paramName}";
-                //    });
-                //    keywordConditions.Add($"({string.Join(" OR ", fieldConditions)})");
-                //}
-
-                //// 所有搜索条件使用 AND 连接
-                //if (keywordConditions.Any())
-                //{
-                //    whereConditions.Add($"({string.Join(" AND ", keywordConditions)})");
-                //}
-
-
-                #endregion
             }
 
             // 构建过滤条件

+ 19 - 0
OASystem/OASystem.Domain/ViewModels/Search/NewClientDataView.cs

@@ -0,0 +1,19 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace OASystem.Domain.ViewModels.Search
+{
+    public class NewClientDataView
+    {
+        public int RowNumber { get; set; }
+        public int Id { get; set; }
+        public string Client { get; set; }
+        public string Contact { get; set; }
+        public string Job { get; set; }
+        public string Telephone { get; set; }
+        public string Location { get; set; }
+    }
+}