CarDataRepository.cs 6.5 KB

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