Browse Source

游地接资料 加密模糊查询

LEIYI 3 months ago
parent
commit
cd28840cff

+ 6 - 3
OASystem/OASystem.Api/Controllers/ResourceController.cs

@@ -16,6 +16,7 @@ using OASystem.Domain.ViewModels.QiYeWeChat;
 using OASystem.Infrastructure.Repositories.Groups;
 using Org.BouncyCastle.Utilities.Encoders;
 using System.Data;
+using System.Diagnostics;
 using static OASystem.API.OAMethodLib.JWTHelper;
 using static OpenAI.GPT3.ObjectModels.SharedModels.IOpenAiModels;
 
@@ -259,13 +260,15 @@ namespace OASystem.API.Controllers
         [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
         public async Task<IActionResult> QueryLocalGuide(QueryLocalGuide dto)
         {
+            Stopwatch stopwatch = Stopwatch.StartNew(); 
             Result LocalGuide = await _localGuideDataRep.QueryLocalGuide(dto);
             if (LocalGuide.Code == 0)
             {
-                return Ok(JsonView(true, "查询成功", LocalGuide.Data));
+                stopwatch.Stop();
+                return Ok(JsonView(true, $"查询成功!耗时:{stopwatch.ElapsedMilliseconds} 毫秒", LocalGuide.Data));
             }
-            
-            return Ok(JsonView(false, LocalGuide.Msg));
+            stopwatch.Stop();
+            return Ok(JsonView(false, LocalGuide.Msg + $"  耗时:{stopwatch.ElapsedMilliseconds} 毫秒"));
         }
 
         /// <summary>

+ 1 - 0
OASystem/OASystem.Domain/AutoMappers/_baseMappingProfile.cs

@@ -204,6 +204,7 @@ namespace OASystem.Domain.AutoMappers
             #region 导游地接资料
             CreateMap<Res_LocalGuideData, LocalGuideDataView>();
             CreateMap<LocalGuideOperationDto, Res_LocalGuideData>();
+            CreateMap<Res_LocalGuideData, Res_LocalGuideData_ListItemView>();
             #endregion
             #region 机场三字码资料
             CreateMap<Res_ThreeCode, ThreeCodeView>();

+ 47 - 114
OASystem/OASystem.Infrastructure/Repositories/Resource/LocalGuideDataRepository.cs

@@ -5,6 +5,7 @@ using OASystem.Domain.Dtos.Resource;
 using OASystem.Domain.Entities.Groups;
 using OASystem.Domain.Entities.Resource;
 using OASystem.Domain.ViewModels.Resource;
+using Org.BouncyCastle.Utilities;
 using SqlSugar.Extensions;
 using System;
 using System.Collections.Generic;
@@ -60,168 +61,100 @@ namespace OASystem.Infrastructure.Repositories.Resource
         public async Task<Result> QueryLocalGuide(QueryLocalGuide dto)
         {
             Result result = new Result() { Code = -2, Msg = "未知错误" };
-            string sqlWhere = string.Empty;
-            if (!string.IsNullOrWhiteSpace(dto.UnitName)) sqlWhere += string.Format(@" And UnitName like '%{0}%'", AesEncryptionHelper.Encrypt(dto.UnitName));
-            if (!string.IsNullOrWhiteSpace(dto.UnitArea) && dto.UnitArea != "全部") sqlWhere += string.Format(@" And UnitArea like '%{0}%'", AesEncryptionHelper.Encrypt(dto.UnitArea));
-            if (!string.IsNullOrWhiteSpace(dto.Contact)) sqlWhere += string.Format(@" And Contact like '%{0}%'", AesEncryptionHelper.Encrypt(dto.Contact));
-            if (!string.IsNullOrWhiteSpace(dto.ContactTel)) sqlWhere += string.Format(@" And ContactTel like '%{0}%'", AesEncryptionHelper.Encrypt(dto.ContactTel));
-            sqlWhere += string.Format(@" And IsDel={0}", 0);
-            if (!string.IsNullOrEmpty(sqlWhere.Trim()))
+
+            var localGuideDatas = await _sqlSugar.Queryable<Res_LocalGuideData>().Where(x => x.IsDel == 0).ToListAsync();
+
+            string unitName = dto.UnitName, unitArea = dto.UnitArea, contact = dto.Contact, contactTel = dto.ContactTel;
+
+            int pageSize = dto.PageSize, pageIndex = dto.PageIndex;
+
+            //处理要查询的字段解密
+            foreach (var item in localGuideDatas)
             {
-                Regex r = new Regex("And");
-                sqlWhere = r.Replace(sqlWhere, "Where", 1);
+                if (!string.IsNullOrEmpty(unitName)) AesEncryptionHelper.Decrypt(unitName);
+                if (!string.IsNullOrEmpty(unitArea)) AesEncryptionHelper.Decrypt(unitName);
+                if (!string.IsNullOrEmpty(contact)) AesEncryptionHelper.Decrypt(unitName);
+                if (!string.IsNullOrEmpty(contactTel)) AesEncryptionHelper.Decrypt(unitName);
             }
-            if (dto.PortType == 1)
+            localGuideDatas = localGuideDatas
+                .WhereIF(!string.IsNullOrEmpty(unitName),x => x.UnitName.Contains(unitName))
+                .WhereIF(!string.IsNullOrEmpty(unitArea), x => x.UnitArea.Contains(unitArea))
+                .WhereIF(!string.IsNullOrEmpty(contact), x => x.Contact.Contains(contact))
+                .WhereIF(!string.IsNullOrEmpty(contactTel), x => x.ContactTel.Contains(contactTel))
+                .ToList();
+
+            if (dto.PortType == 1 || dto.PortType == 2)
             {
-                string sql = string.Format(@"select * from Res_LocalGuideData {0}", sqlWhere);
-                var localGuideData = await _sqlSugar.SqlQueryable<Res_LocalGuideData>(sql).ToListAsync();
-                if (localGuideData.Count == 0)
+                if (localGuideDatas.Count == 0)
                 {
                     return result = new Result() { Code = 0, Msg = "暂无数据" };
                 }
-                localGuideData = localGuideData.OrderByDescending(x => x.CreateTime).ToList();
+                localGuideDatas = localGuideDatas.OrderByDescending(x => x.CreateTime).ToList();
 
                 if (dto.PageSize == 0 || dto.PageIndex == 0)
                 {
-                    foreach (var item in localGuideData) EncryptionProcessor.DecryptProperties(item);
+                    foreach (var item in localGuideDatas) EncryptionProcessor.DecryptProperties(item);
 
                     return result = new Result()
                     {
                         Code = 0,
                         Msg = "查询成功",
-                        Data = localGuideData,
+                        Data = localGuideDatas,
                     };
                 }
                 else
                 {
-                    int count = localGuideData.Count;
-                    float totalPage = (float)count / dto.PageSize;//总页数
-                    if (totalPage == 0) totalPage = 1;
-                    else totalPage = (int)Math.Ceiling((double)totalPage);
-
-                    List<Res_LocalGuideData> ListData = new List<Res_LocalGuideData>();
-                    for (int i = 0; i < dto.PageSize; i++)
-                    {
-                        var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
-                        if (RowIndex < localGuideData.Count)
-                        {
-                            EncryptionProcessor.DecryptProperties(localGuideData[RowIndex]);
-                            ListData.Add(localGuideData[RowIndex]);
-                        }
-                        else break;
-                    }
-                    return result = new Result()
-                    {
-                        Code = 0,
-                        Msg = "查询成功",
-                        Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = ListData },
-                    };
-                }
-
-            }
-            else if (dto.PortType == 2)
-            {
-                string sql = string.Format(@"select * from Res_LocalGuideData {0}", sqlWhere);
-                var localGuideData = await _sqlSugar.SqlQueryable<Res_LocalGuideData>(sql).ToListAsync();
-                //2024-05-11 修改,取消该判断,避免前端报错
-                //if (LocalGuideData.Count == 0)
-                //{
-                //    return result = new Result() { Code = 0, Msg = "暂无数据" };
-                //}
-                localGuideData = localGuideData.OrderByDescending(x => x.CreateTime).ToList();
+                    int totalItems = localGuideDatas.Count(); 
+                    int totalPages = (int)Math.Ceiling((double)totalItems / pageSize);
+                    int skip = (pageIndex - 1) * pageSize;
 
-                if (dto.PageSize == 0 && dto.PageIndex == 0)
-                {
-                    foreach (var item in localGuideData) EncryptionProcessor.DecryptProperties(item);
+                    var pageSource = localGuideDatas.Skip(skip).Take(pageSize).ToList();
+                    foreach (var item in pageSource) EncryptionProcessor.DecryptProperties(item);
                     return result = new Result()
                     {
                         Code = 0,
                         Msg = "查询成功",
-                        Data = localGuideData,
+                        Data = new { pageCount = totalItems, totalPage = totalPages, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = pageSource },
                     };
                 }
-                else
-                {
-                    int count = localGuideData.Count;
-                    float totalPage = (float)count / dto.PageSize;//总页数
-                    if (totalPage == 0) totalPage = 1;
-                    else totalPage = (int)Math.Ceiling((double)totalPage);
 
-                    List<Res_LocalGuideData> ListData = new List<Res_LocalGuideData>();
-                    for (int i = 0; i < dto.PageSize; i++)
-                    {
-                        var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
-                        if (RowIndex < localGuideData.Count)
-                        {
-                            EncryptionProcessor.DecryptProperties(localGuideData[RowIndex]);
-                            ListData.Add(localGuideData[RowIndex]);
-                        }
-                        else
-                        {
-                            break;
-                        }
-                    }
-                    return result = new Result()
-                    {
-                        Code = 0,
-                        Msg = "查询成功",
-                        Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = ListData },
-                    };
-                }
             }
             else if (dto.PortType == 3)
             {
-                string sql = string.Format(@"select Id,UnitArea,UnitName,Contact,ContactTel,Score,LastUpdate from Res_LocalGuideData {0}", sqlWhere);
-                var localGuideData = await _sqlSugar.SqlQueryable<Res_LocalGuideData>(sql).ToListAsync();
-                if (localGuideData.Count == 0)
+                if (localGuideDatas.Count == 0)
                 {
                     return result = new Result() { Code = 0, Msg = "暂无数据" };
                 }
-                localGuideData = localGuideData.OrderByDescending(x => x.CreateTime).ToList();
+                localGuideDatas = localGuideDatas.OrderByDescending(x => x.CreateTime).ToList();
 
                 if (dto.PageSize == 0 && dto.PageIndex == 0)
                 {
-                    foreach (var item in localGuideData) EncryptionProcessor.DecryptProperties(item);
+                    foreach (var item in localGuideDatas) EncryptionProcessor.DecryptProperties(item);
                     return result = new Result()
                     {
                         Code = 0,
                         Msg = "查询成功",
-                        Data = localGuideData,
+                        Data = localGuideDatas,
                     };
                 }
                 else
                 {
-                    int count = localGuideData.Count;
-                    float totalPage = (float)count / dto.PageSize;//总页数
-                    if (totalPage == 0) totalPage = 1;
-                    else totalPage = (int)Math.Ceiling((double)totalPage);
 
-                    var ListData = new List<Res_LocalGuideData_ListItemView>();
-                    for (int i = 0; i < dto.PageSize; i++)
-                    {
-                        var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
-                        if (RowIndex < localGuideData.Count)
-                        {
-                            var temp = new Res_LocalGuideData_ListItemView()
-                            {
-                                Contact = localGuideData[RowIndex].Contact,
-                                ContactTel = localGuideData[RowIndex].ContactTel,
-                                Id = localGuideData[RowIndex].Id,
-                                Score = localGuideData[RowIndex].Score,
-                                UnitArea = localGuideData[RowIndex].UnitArea,
-                                UnitName = localGuideData[RowIndex].UnitName
-                            };
-                            temp.LastUpdateStr = localGuideData[RowIndex].CreateTime.ToString("yyyy-MM-dd");
-                            EncryptionProcessor.DecryptProperties(temp);
-                            ListData.Add(temp);
-                        }
-                        else break;
-                    }
+                    int totalItems = localGuideDatas.Count();
+                    int totalPages = (int)Math.Ceiling((double)totalItems / pageSize);
+                    int skip = (pageIndex - 1) * pageSize;
+
+                    var pageSource = localGuideDatas.Skip(skip).Take(pageSize).ToList();
+
+                    foreach (var item in pageSource) EncryptionProcessor.DecryptProperties(item); ;
+
+                    var pageSoure1 = _mapper.Map<Res_LocalGuideData_ListItemView>(pageSource);
+
                     return result = new Result()
                     {
                         Code = 0,
                         Msg = "查询成功",
-                        Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = ListData },
+                        Data = new { pageCount = totalItems, totalPage = pageSource, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = pageSoure1 },
                     };
                 }
             }