CarDataRepository.cs 5.8 KB

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