LocalGuideDataRepository.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using AutoMapper;
  2. using OASystem.Domain;
  3. using OASystem.Domain.AesEncryption;
  4. using OASystem.Domain.Dtos.Resource;
  5. using OASystem.Domain.Entities.Groups;
  6. using OASystem.Domain.Entities.Resource;
  7. using OASystem.Domain.ViewModels.Resource;
  8. using Org.BouncyCastle.Utilities;
  9. using SqlSugar.Extensions;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. namespace OASystem.Infrastructure.Repositories.Resource
  16. {
  17. public class LocalGuideDataRepository : BaseRepository<Res_LocalGuideData, LocalGuideDataView>
  18. {
  19. private readonly IMapper _mapper;
  20. public LocalGuideDataRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
  21. {
  22. _mapper = mapper;
  23. }
  24. public async Task<Result> LocalGuideOperation(LocalGuideOperationDto dto)
  25. {
  26. var localGuideDat = _mapper.Map<Res_LocalGuideData>(dto);
  27. localGuideDat.LastUpdateTime = DateTime.Now;
  28. localGuideDat.LastUpdateUserId = dto.CreateUserId;
  29. EncryptionProcessor.EncryptProperties(localGuideDat);
  30. if (dto.Status == 1)//添加
  31. {
  32. string selectSql = string.Format(@"select * from Res_LocalGuideData where UnitArea='{0}' and UnitName='{1}' and Contact='{2}' and ContactTel='{3}'",
  33. AesEncryptionHelper.Encrypt(dto.UnitArea),
  34. AesEncryptionHelper.Encrypt(dto.UnitName),
  35. AesEncryptionHelper.Encrypt(dto.Contact),
  36. AesEncryptionHelper.Encrypt(dto.ContactTel));
  37. var LocalGuideData = await _sqlSugar.SqlQueryable<Res_LocalGuideData>(selectSql).FirstAsync();//查询是否存在
  38. if (LocalGuideData != null) return new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
  39. else//不存在,可添加
  40. {
  41. int id = await AddAsyncReturnId(localGuideDat);
  42. if (id == 0) return new Result() { Code = -1, Msg = "添加失败!" };
  43. return new Result() { Code = 0, Msg = "添加成功!", Data = new { Id = id } };
  44. }
  45. }
  46. else if (dto.Status == 2)//修改
  47. {
  48. var res = await _sqlSugar.Updateable(localGuideDat).IgnoreColumns(x => new { x.IsDel, x.CreateUserId, x.CreateTime, x.DeleteUserId, x.DeleteTime }).ExecuteCommandAsync();
  49. if (res < 1)
  50. {
  51. return new Result() { Code = -1, Msg = "修改失败!" };
  52. }
  53. return new Result() { Code = 0, Msg = "修改成功!" };
  54. }
  55. return new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  56. }
  57. public async Task<Result> QueryLocalGuide(QueryLocalGuide dto)
  58. {
  59. Result result = new Result() { Code = -2, Msg = "未知错误" };
  60. string unitName = dto.UnitName, unitArea = dto.UnitArea, contact = dto.Contact, contactTel = dto.ContactTel;
  61. int pageSize = dto.PageSize, pageIndex = dto.PageIndex;
  62. var localGuideDataSearch = await _sqlSugar
  63. .Queryable<Res_LocalGuideData>()
  64. .Where(x => x.IsDel == 0)
  65. .Select(x => new Res_LocalGuideData()
  66. {
  67. Id = x.Id,
  68. UnitName = x.UnitName,
  69. UnitArea = x.UnitArea,
  70. Contact = x.Contact,
  71. ContactTel = x.ContactTel
  72. })
  73. .ToListAsync();
  74. //处理要查询的字段解密
  75. foreach (var item in localGuideDataSearch)
  76. {
  77. item.UnitName = AesEncryptionHelper.Decrypt(item.UnitName);
  78. item.UnitArea = AesEncryptionHelper.Decrypt(item.UnitArea);
  79. item.Contact = AesEncryptionHelper.Decrypt(item.Contact);
  80. item.ContactTel = AesEncryptionHelper.Decrypt(item.ContactTel);
  81. }
  82. var localGuideIds = localGuideDataSearch
  83. .WhereIF(!string.IsNullOrEmpty(unitName), x => !string.IsNullOrEmpty(x.UnitName) && x.UnitName.Contains(unitName))
  84. .WhereIF(!string.IsNullOrEmpty(unitArea), x => !string.IsNullOrEmpty(x.UnitArea) && x.UnitArea.Contains(unitArea))
  85. .WhereIF(!string.IsNullOrEmpty(contact), x => !string.IsNullOrEmpty(x.Contact) && x.Contact.Contains(contact))
  86. .WhereIF(!string.IsNullOrEmpty(contactTel), x => !string.IsNullOrEmpty(x.ContactTel) && x.ContactTel.Contains(contactTel))
  87. .Select(x => x.Id)
  88. .ToList();
  89. var localGuideDataInfos = await _sqlSugar
  90. .Queryable<Res_LocalGuideData>()
  91. .WhereIF(localGuideIds.Any(), x => localGuideIds.Contains(x.Id))
  92. .ToListAsync();
  93. var localGuideDatas = _mapper.Map<List<LocalGuideDataView>>(localGuideDataInfos);
  94. if (dto.PortType == 1 || dto.PortType == 2)
  95. {
  96. if (localGuideDatas.Count == 0)
  97. {
  98. return result = new Result() { Code = 0, Msg = "暂无数据" };
  99. }
  100. localGuideDatas = localGuideDatas.OrderByDescending(x => x.CreateTime).ToList();
  101. if (dto.PageSize == 0 || dto.PageIndex == 0)
  102. {
  103. foreach (var item in localGuideDatas) EncryptionProcessor.DecryptProperties(item);
  104. return result = new Result()
  105. {
  106. Code = 0,
  107. Msg = "查询成功",
  108. Data = localGuideDatas,
  109. };
  110. }
  111. else
  112. {
  113. int totalItems = localGuideDatas.Count();
  114. int totalPages = (int)Math.Ceiling((double)totalItems / pageSize);
  115. int skip = (pageIndex - 1) * pageSize;
  116. var pageSource = localGuideDatas.Skip(skip).Take(pageSize).ToList();
  117. foreach (var item in pageSource) EncryptionProcessor.DecryptProperties(item);
  118. return result = new Result()
  119. {
  120. Code = 0,
  121. Msg = "查询成功",
  122. Data = new { pageCount = totalItems, totalPage = totalPages, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = pageSource },
  123. };
  124. }
  125. }
  126. else if (dto.PortType == 3)
  127. {
  128. if (localGuideDatas.Count == 0)
  129. {
  130. return result = new Result() { Code = 0, Msg = "暂无数据" };
  131. }
  132. localGuideDatas = localGuideDatas.OrderByDescending(x => x.CreateTime).ToList();
  133. if (dto.PageSize == 0 && dto.PageIndex == 0)
  134. {
  135. foreach (var item in localGuideDatas) EncryptionProcessor.DecryptProperties(item);
  136. return result = new Result()
  137. {
  138. Code = 0,
  139. Msg = "查询成功",
  140. Data = localGuideDatas,
  141. };
  142. }
  143. else
  144. {
  145. int totalItems = localGuideDatas.Count();
  146. int totalPages = (int)Math.Ceiling((double)totalItems / pageSize);
  147. int skip = (pageIndex - 1) * pageSize;
  148. var pageSource = localGuideDatas.Skip(skip).Take(pageSize).ToList();
  149. foreach (var item in pageSource) EncryptionProcessor.DecryptProperties(item); ;
  150. var pageSoure1 = _mapper.Map<Res_LocalGuideData_ListItemView>(pageSource);
  151. return result = new Result()
  152. {
  153. Code = 0,
  154. Msg = "查询成功",
  155. Data = new { pageCount = totalItems, totalPage = pageSource, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = pageSoure1 },
  156. };
  157. }
  158. }
  159. return result = new Result() { Code = -2, Msg = "请传入PortType参数!1:Web,2:Android,3:IOS" };
  160. }
  161. }
  162. }