LocalGuideDataRepository.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.LastUpdate = DateTime.Now;
  28. EncryptionProcessor.EncryptProperties(localGuideDat);
  29. if (dto.Status == 1)//添加
  30. {
  31. string selectSql = string.Format(@"select * from Res_LocalGuideData where UnitArea='{0}' and UnitName='{1}' and Contact='{2}' and ContactTel='{3}'",
  32. AesEncryptionHelper.Encrypt(dto.UnitArea),
  33. AesEncryptionHelper.Encrypt(dto.UnitName),
  34. AesEncryptionHelper.Encrypt(dto.Contact),
  35. AesEncryptionHelper.Encrypt(dto.ContactTel));
  36. var LocalGuideData = await _sqlSugar.SqlQueryable<Res_LocalGuideData>(selectSql).FirstAsync();//查询是否存在
  37. if (LocalGuideData != null) return new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
  38. else//不存在,可添加
  39. {
  40. int id = await AddAsyncReturnId(localGuideDat);
  41. if (id == 0) return new Result() { Code = -1, Msg = "添加失败!" };
  42. return new Result() { Code = 0, Msg = "添加成功!", Data = new { Id = id } };
  43. }
  44. }
  45. else if (dto.Status == 2)//修改
  46. {
  47. var res = await _sqlSugar.Updateable(localGuideDat).IgnoreColumns(x => new { x.IsDel, x.CreateUserId, x.CreateTime, x.DeleteUserId, x.DeleteTime }).ExecuteCommandAsync();
  48. if (res < 1)
  49. {
  50. return new Result() { Code = -1, Msg = "修改失败!" };
  51. }
  52. return new Result() { Code = 0, Msg = "修改成功!" };
  53. }
  54. return new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  55. }
  56. public async Task<Result> QueryLocalGuide(QueryLocalGuide dto)
  57. {
  58. Result result = new Result() { Code = -2, Msg = "未知错误" };
  59. var localGuideDatas = await _sqlSugar.Queryable<Res_LocalGuideData>().Where(x => x.IsDel == 0).ToListAsync();
  60. string unitName = dto.UnitName, unitArea = dto.UnitArea, contact = dto.Contact, contactTel = dto.ContactTel;
  61. int pageSize = dto.PageSize, pageIndex = dto.PageIndex;
  62. //处理要查询的字段解密
  63. foreach (var item in localGuideDatas)
  64. {
  65. if (!string.IsNullOrEmpty(unitName)) AesEncryptionHelper.Decrypt(unitName);
  66. if (!string.IsNullOrEmpty(unitArea)) AesEncryptionHelper.Decrypt(unitName);
  67. if (!string.IsNullOrEmpty(contact)) AesEncryptionHelper.Decrypt(unitName);
  68. if (!string.IsNullOrEmpty(contactTel)) AesEncryptionHelper.Decrypt(unitName);
  69. }
  70. localGuideDatas = localGuideDatas
  71. .WhereIF(!string.IsNullOrEmpty(unitName),x => x.UnitName.Contains(unitName))
  72. .WhereIF(!string.IsNullOrEmpty(unitArea), x => x.UnitArea.Contains(unitArea))
  73. .WhereIF(!string.IsNullOrEmpty(contact), x => x.Contact.Contains(contact))
  74. .WhereIF(!string.IsNullOrEmpty(contactTel), x => x.ContactTel.Contains(contactTel))
  75. .ToList();
  76. if (dto.PortType == 1 || dto.PortType == 2)
  77. {
  78. if (localGuideDatas.Count == 0)
  79. {
  80. return result = new Result() { Code = 0, Msg = "暂无数据" };
  81. }
  82. localGuideDatas = localGuideDatas.OrderByDescending(x => x.CreateTime).ToList();
  83. if (dto.PageSize == 0 || dto.PageIndex == 0)
  84. {
  85. foreach (var item in localGuideDatas) EncryptionProcessor.DecryptProperties(item);
  86. return result = new Result()
  87. {
  88. Code = 0,
  89. Msg = "查询成功",
  90. Data = localGuideDatas,
  91. };
  92. }
  93. else
  94. {
  95. int totalItems = localGuideDatas.Count();
  96. int totalPages = (int)Math.Ceiling((double)totalItems / pageSize);
  97. int skip = (pageIndex - 1) * pageSize;
  98. var pageSource = localGuideDatas.Skip(skip).Take(pageSize).ToList();
  99. foreach (var item in pageSource) EncryptionProcessor.DecryptProperties(item);
  100. return result = new Result()
  101. {
  102. Code = 0,
  103. Msg = "查询成功",
  104. Data = new { pageCount = totalItems, totalPage = totalPages, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = pageSource },
  105. };
  106. }
  107. }
  108. else if (dto.PortType == 3)
  109. {
  110. if (localGuideDatas.Count == 0)
  111. {
  112. return result = new Result() { Code = 0, Msg = "暂无数据" };
  113. }
  114. localGuideDatas = localGuideDatas.OrderByDescending(x => x.CreateTime).ToList();
  115. if (dto.PageSize == 0 && dto.PageIndex == 0)
  116. {
  117. foreach (var item in localGuideDatas) EncryptionProcessor.DecryptProperties(item);
  118. return result = new Result()
  119. {
  120. Code = 0,
  121. Msg = "查询成功",
  122. Data = localGuideDatas,
  123. };
  124. }
  125. else
  126. {
  127. int totalItems = localGuideDatas.Count();
  128. int totalPages = (int)Math.Ceiling((double)totalItems / pageSize);
  129. int skip = (pageIndex - 1) * pageSize;
  130. var pageSource = localGuideDatas.Skip(skip).Take(pageSize).ToList();
  131. foreach (var item in pageSource) EncryptionProcessor.DecryptProperties(item); ;
  132. var pageSoure1 = _mapper.Map<Res_LocalGuideData_ListItemView>(pageSource);
  133. return result = new Result()
  134. {
  135. Code = 0,
  136. Msg = "查询成功",
  137. Data = new { pageCount = totalItems, totalPage = pageSource, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = pageSoure1 },
  138. };
  139. }
  140. }
  141. return result = new Result() { Code = -2, Msg = "请传入PortType参数!1:Web,2:Android,3:IOS" };
  142. }
  143. }
  144. }