using AutoMapper; using OASystem.Domain; using OASystem.Domain.AesEncryption; using OASystem.Domain.Dtos.Resource; using OASystem.Domain.Entities.Groups; using OASystem.Domain.Entities.Resource; using OASystem.Domain.ViewModels.Resource; using SqlSugar.Extensions; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OASystem.Infrastructure.Repositories.Resource { public class LocalGuideDataRepository : BaseRepository { private readonly IMapper _mapper; public LocalGuideDataRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar) { _mapper = mapper; } public async Task LocalGuideOperation(LocalGuideOperationDto dto) { var localGuideDat = _mapper.Map(dto); localGuideDat.LastUpdate = DateTime.Now; EncryptionProcessor.EncryptProperties(localGuideDat); if (dto.Status == 1)//添加 { string selectSql = string.Format(@"select * from Res_LocalGuideData where UnitArea='{0}' and UnitName='{1}' and Contact='{2}' and ContactTel='{3}'", AesEncryptionHelper.Encrypt(dto.UnitArea), AesEncryptionHelper.Encrypt(dto.UnitName), AesEncryptionHelper.Encrypt(dto.Contact), AesEncryptionHelper.Encrypt(dto.ContactTel)); var LocalGuideData = await _sqlSugar.SqlQueryable(selectSql).FirstAsync();//查询是否存在 if (LocalGuideData != null) return new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" }; else//不存在,可添加 { int id = await AddAsyncReturnId(localGuideDat); if (id == 0) return new Result() { Code = -1, Msg = "添加失败!" }; return new Result() { Code = 0, Msg = "添加成功!", Data = new { Id = id } }; } } else if (dto.Status == 2)//修改 { var res = await _sqlSugar.Updateable(localGuideDat).IgnoreColumns(x => new { x.IsDel, x.CreateUserId, x.CreateTime, x.DeleteUserId, x.DeleteTime }).ExecuteCommandAsync(); if (res < 1) { return new Result() { Code = -1, Msg = "修改失败!" }; } return new Result() { Code = 0, Msg = "修改成功!" }; } return new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" }; } public async Task 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())) { Regex r = new Regex("And"); sqlWhere = r.Replace(sqlWhere, "Where", 1); } if (dto.PortType == 1) { string sql = string.Format(@"select * from Res_LocalGuideData {0}", sqlWhere); var localGuideData = await _sqlSugar.SqlQueryable(sql).ToListAsync(); if (localGuideData.Count == 0) { return result = new Result() { Code = 0, Msg = "暂无数据" }; } localGuideData = localGuideData.OrderByDescending(x => x.CreateTime).ToList(); if (dto.PageSize == 0 || dto.PageIndex == 0) { foreach (var item in localGuideData) EncryptionProcessor.DecryptProperties(item); return result = new Result() { Code = 0, Msg = "查询成功", Data = localGuideData, }; } else { int count = localGuideData.Count; float totalPage = (float)count / dto.PageSize;//总页数 if (totalPage == 0) totalPage = 1; else totalPage = (int)Math.Ceiling((double)totalPage); List ListData = new List(); 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(sql).ToListAsync(); //2024-05-11 修改,取消该判断,避免前端报错 //if (LocalGuideData.Count == 0) //{ // return result = new Result() { Code = 0, Msg = "暂无数据" }; //} localGuideData = localGuideData.OrderByDescending(x => x.CreateTime).ToList(); if (dto.PageSize == 0 && dto.PageIndex == 0) { foreach (var item in localGuideData) EncryptionProcessor.DecryptProperties(item); return result = new Result() { Code = 0, Msg = "查询成功", Data = localGuideData, }; } else { int count = localGuideData.Count; float totalPage = (float)count / dto.PageSize;//总页数 if (totalPage == 0) totalPage = 1; else totalPage = (int)Math.Ceiling((double)totalPage); List ListData = new List(); 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(sql).ToListAsync(); if (localGuideData.Count == 0) { return result = new Result() { Code = 0, Msg = "暂无数据" }; } localGuideData = localGuideData.OrderByDescending(x => x.CreateTime).ToList(); if (dto.PageSize == 0 && dto.PageIndex == 0) { foreach (var item in localGuideData) EncryptionProcessor.DecryptProperties(item); return result = new Result() { Code = 0, Msg = "查询成功", Data = localGuideData, }; } 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(); 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; } return result = new Result() { Code = 0, Msg = "查询成功", Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = ListData }, }; } } return result = new Result() { Code = -2, Msg = "请传入PortType参数!1:Web,2:Android,3:IOS" }; } } }