CarDataRepository.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using AutoMapper;
  2. using OASystem.Domain;
  3. using OASystem.Domain.AesEncryption;
  4. using OASystem.Domain.Dtos.Resource;
  5. using OASystem.Domain.Entities.Resource;
  6. using OASystem.Domain.ViewModels.Resource;
  7. namespace OASystem.Infrastructure.Repositories.Resource
  8. {
  9. public class CarDataRepository: BaseRepository<Res_CarData, CarDataView>
  10. {
  11. private readonly IMapper _mapper;
  12. public CarDataRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
  13. {
  14. _mapper = mapper;
  15. }
  16. /// <summary>
  17. /// 查询用车资料
  18. /// </summary>
  19. /// <param name="dto"></param>
  20. /// <returns></returns>
  21. public async Task<Result> QueryCarData(QueryCarDataDto dto)
  22. {
  23. string sqlWhere = string.Empty;
  24. if (!string.IsNullOrWhiteSpace(dto.UnitName)) sqlWhere += string.Format(@" And UnitName like '%{0}%'", AesEncryptionHelper.Encrypt(dto.UnitName));
  25. if (!string.IsNullOrWhiteSpace(dto.UnitArea) && dto.UnitArea != "全部") sqlWhere += string.Format(@" And UnitArea like '%{0}%'", AesEncryptionHelper.Encrypt(dto.UnitArea));
  26. if (!string.IsNullOrWhiteSpace(dto.Contact)) sqlWhere += string.Format(@" And Contact like '%{0}%'", AesEncryptionHelper.Encrypt(dto.Contact));
  27. if (!string.IsNullOrWhiteSpace(dto.ContactTel)) sqlWhere += string.Format(@" And ContactTel like '%{0}%'", AesEncryptionHelper.Encrypt(dto.ContactTel));
  28. sqlWhere += string.Format(@" And IsDel={0}", 0);
  29. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  30. {
  31. Regex r = new Regex("And");
  32. sqlWhere = r.Replace(sqlWhere, "Where", 1);
  33. }
  34. if (dto.PortType == 1)
  35. {
  36. string sql = string.Format(@"select * from Res_CarData {0}", sqlWhere);
  37. var carDataView = await _sqlSugar.SqlQueryable<Res_CarData>(sql).ToListAsync();
  38. if (carDataView.Count == 0) return new Result() { Code = -1, Msg = "暂无数据" };
  39. carDataView = carDataView.OrderByDescending(x => x.CreateTime).ToList();
  40. var carDatas = _mapper.Map<List<CarDataView>>(carDataView);
  41. if (dto.PageSize == 0 && dto.PageIndex == 0)
  42. {
  43. foreach (var item in carDatas) EncryptionProcessor.DecryptProperties(item);
  44. return new Result()
  45. {
  46. Code = 0,
  47. Msg = "查询成功",
  48. Data = carDatas,
  49. };
  50. }
  51. else
  52. {
  53. int count = carDatas.Count;
  54. float totalPage = (float)count / dto.PageSize;//总页数
  55. if (totalPage == 0) totalPage = 1;
  56. else totalPage = (int)Math.Ceiling((double)totalPage);
  57. var ListData = new List<Res_CarData>();
  58. for (int i = 0; i < dto.PageSize; i++)
  59. {
  60. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  61. if (RowIndex < carDatas.Count) {
  62. EncryptionProcessor.DecryptProperties(carDatas[RowIndex]);
  63. ListData.Add(carDatas[RowIndex]);
  64. }
  65. else break;
  66. }
  67. return new Result()
  68. {
  69. Code = 0,
  70. Msg = "查询成功",
  71. Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = ListData },
  72. };
  73. }
  74. }
  75. else if (dto.PortType == 2 || dto.PortType == 3)
  76. {
  77. string sql = string.Format(@"select * from Res_CarData {0}", sqlWhere);
  78. var carDatas = await _sqlSugar.SqlQueryable<Res_CarData>(sql).ToListAsync();
  79. if (carDatas.Count == 0) return new Result() { Code = -1, Msg = "暂无数据" };
  80. carDatas = carDatas.OrderByDescending(x => x.CreateTime).ToList();
  81. if (dto.PageSize == 0 || dto.PageIndex == 0)
  82. {
  83. foreach (var item in carDatas) EncryptionProcessor.DecryptProperties(item);
  84. return new Result()
  85. {
  86. Code = 0,
  87. Msg = "查询成功",
  88. Data = carDatas,
  89. };
  90. }
  91. else
  92. {
  93. int count = carDatas.Count;
  94. float totalPage = (float)count / dto.PageSize;//总页数
  95. if (totalPage == 0) totalPage = 1;
  96. else totalPage = (int)Math.Ceiling((double)totalPage);
  97. var ListData = new List<Res_CarData>();
  98. for (int i = 0; i < dto.PageSize; i++)
  99. {
  100. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  101. if (RowIndex < carDatas.Count)
  102. {
  103. EncryptionProcessor.DecryptProperties(carDatas[RowIndex]);
  104. ListData.Add(carDatas[RowIndex]);
  105. }
  106. else break;
  107. }
  108. return new Result()
  109. {
  110. Code = 0,
  111. Msg = "查询成功",
  112. Data = new { pageCount = count, totalPage = ((int)totalPage).ToString(), pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = ListData },
  113. };
  114. }
  115. }
  116. else return new Result() { Code = -2, Msg = MsgTips.Port };
  117. }
  118. }
  119. }