CarDataRepository.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 carDatas = await _sqlSugar.SqlQueryable<Res_CarData>(sql).ToListAsync();
  39. if (carDatas.Count == 0) return new Result() { Code = -1, Msg = "暂无数据" };
  40. carDatas = carDatas.OrderByDescending(x => x.CreateTime).ToList();
  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. }