ThreeCodeRepository.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. using SqlSugar;
  7. namespace OASystem.Infrastructure.Repositories.Resource
  8. {
  9. public class ThreeCodeRepository:BaseRepository<Res_ThreeCode,ThreeCodeView>
  10. {
  11. private readonly IMapper _mapper;
  12. public ThreeCodeRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
  13. {
  14. _mapper = mapper;
  15. }
  16. /// <summary>
  17. /// sqlSugar分页查询
  18. /// </summary>
  19. /// <param name="PageIndex"></param>
  20. /// <param name="PageSize"></param>
  21. /// <returns></returns>
  22. public async Task<Result> QueryThreeCode(QueryThreeCodeDto dto)
  23. {
  24. Result result = new Result() { Code = -2, Msg = "未知错误" };
  25. try
  26. {
  27. string sqlWhere = string.Empty;
  28. if (!string.IsNullOrWhiteSpace(dto.City) && dto.City!="全部")
  29. {
  30. sqlWhere += string.Format(@" And City like '%{0}%'", dto.City);
  31. }
  32. if (!string.IsNullOrWhiteSpace(dto.Three))
  33. {
  34. sqlWhere += string.Format(@" And Three like '%{0}%'", dto.Three);
  35. }
  36. sqlWhere += string.Format(@" And IsDel={0}", 0);
  37. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  38. {
  39. Regex r = new Regex("And");
  40. sqlWhere = r.Replace(sqlWhere, "Where", 1);
  41. }
  42. if (dto.PortType == 1)
  43. {
  44. string sql = string.Format(@"select * from Res_ThreeCode {0}", sqlWhere);
  45. List<Res_ThreeCode> ThreeCode = await _sqlSugar.SqlQueryable<Res_ThreeCode>(sql).ToListAsync();
  46. if (ThreeCode.Count == 0)
  47. {
  48. return result = new Result() { Code = -1, Msg = "暂无数据" };
  49. }
  50. ThreeCode = ThreeCode.OrderByDescending(x => x.CreateTime).ToList();
  51. if (dto.PageSize == 0 && dto.PageIndex == 0)
  52. {
  53. return result = new Result()
  54. {
  55. Code = 0,
  56. Msg = "查询成功",
  57. Data = ThreeCode,
  58. };
  59. }
  60. else
  61. {
  62. int count = ThreeCode.Count;
  63. float totalPage = (float)count / dto.PageSize;//总页数
  64. if (totalPage == 0) totalPage = 1;
  65. else totalPage = (int)Math.Ceiling((double)totalPage);
  66. List<Res_ThreeCode> _ThreeCode = new List<Res_ThreeCode>();
  67. for (int i = 0; i < dto.PageSize; i++)
  68. {
  69. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  70. if (RowIndex < ThreeCode.Count)
  71. {
  72. _ThreeCode.Add(ThreeCode[RowIndex]);
  73. }
  74. else
  75. {
  76. break;
  77. }
  78. }
  79. return result = new Result()
  80. {
  81. Code = 0,
  82. Msg = "查询成功",
  83. Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = _ThreeCode },
  84. };
  85. }
  86. }
  87. else if (dto.PortType == 2)
  88. {
  89. string sql = string.Format(@"select * from Res_ThreeCode {0}", sqlWhere);
  90. List<Res_ThreeCode> ThreeCode = await _sqlSugar.SqlQueryable<Res_ThreeCode>(sql).ToListAsync();
  91. if (ThreeCode.Count == 0)
  92. {
  93. return result = new Result() { Code = -1, Msg = "暂无数据" };
  94. }
  95. ThreeCode = ThreeCode.OrderByDescending(x => x.CreateTime).ToList();
  96. if (dto.PageSize == 0 && dto.PageIndex == 0)
  97. {
  98. return result = new Result()
  99. {
  100. Code = 0,
  101. Msg = "查询成功",
  102. Data = ThreeCode,
  103. };
  104. }
  105. else
  106. {
  107. int count = ThreeCode.Count;
  108. float totalPage = (float)count / dto.PageSize;//总页数
  109. if (totalPage == 0) totalPage = 1;
  110. else totalPage = (int)Math.Ceiling((double)totalPage);
  111. List<Res_ThreeCode> _ThreeCode = new List<Res_ThreeCode>();
  112. for (int i = 0; i < dto.PageSize; i++)
  113. {
  114. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  115. if (RowIndex < ThreeCode.Count)
  116. {
  117. _ThreeCode.Add(ThreeCode[RowIndex]);
  118. }
  119. else
  120. {
  121. break;
  122. }
  123. }
  124. return result = new Result()
  125. {
  126. Code = 0,
  127. Msg = "查询成功",
  128. Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = _ThreeCode },
  129. };
  130. }
  131. }
  132. else
  133. {
  134. return result = new Result() { Code = -2, Msg = "请传入PortType参数!1:Web,2:Android,3:IOS" };
  135. }
  136. }
  137. catch (Exception)
  138. {
  139. return result;
  140. throw;
  141. }
  142. }
  143. /// <summary>
  144. /// 添加、编辑操作
  145. /// </summary>
  146. /// <param name="dto"></param>
  147. /// <returns></returns>
  148. public async Task<Result> ThreeCodeOperation(ThreeCodeOperationDto dto)
  149. {
  150. Result result = new Result() { Code = -2, Msg = "未知错误" };
  151. try
  152. {
  153. if (dto.Status == 1)//添加
  154. {
  155. string selectSql = string.Format(@"select * from Res_ThreeCode where Three='{0}' and Country='{1}' and City='{2}' and AirPort='{3}'"
  156. , dto.Three, dto.Country, dto.City, dto.AirPort);
  157. var ThreeCode = await _sqlSugar.SqlQueryable<Res_ThreeCode>(selectSql).FirstAsync();//查询是否存在
  158. if (ThreeCode != null)
  159. {
  160. return result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
  161. }
  162. else//不存在,可添加
  163. {
  164. Res_ThreeCode _ThreeCodeDto = _mapper.Map<Res_ThreeCode>(dto);
  165. int id = await AddAsyncReturnId(_ThreeCodeDto);
  166. if (id == 0)
  167. {
  168. return result = new Result() { Code = -1, Msg = "添加失败!" };
  169. }
  170. result = new Result() { Code = 0, Msg = "添加成功!", Data = new { Id = id } };
  171. }
  172. }
  173. else if (dto.Status == 2)//修改
  174. {
  175. bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Res_ThreeCode
  176. {
  177. Three=dto.Three,
  178. Four=dto.Four,
  179. Country=dto.Country,
  180. City = dto.City,
  181. AirPort = dto.AirPort,
  182. AirPort_En = dto.AirPort_En,
  183. Remark = dto.Remark,
  184. });
  185. if (!res)
  186. {
  187. return result = new Result() { Code = -1, Msg = "修改失败!" };
  188. }
  189. result = new Result() { Code = 0, Msg = "修改成功!" };
  190. }
  191. else
  192. {
  193. return result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  194. }
  195. }
  196. catch (Exception ex)
  197. {
  198. return result = new Result() { Code = -2, Msg = "程序错误!" };
  199. }
  200. return result;
  201. }
  202. /// <summary>
  203. /// 三字码数组查询
  204. /// </summary>
  205. /// <param name="codeArr">需要查询的三字码数组</param>
  206. /// <returns>查询到的键值对数组</returns>
  207. public Dictionary<string,Res_ThreeCode> QueryThreeCodeArray(List<string> codeArr)
  208. {
  209. Dictionary<string, Res_ThreeCode> dic = new Dictionary<string, Res_ThreeCode>();
  210. codeArr.ForEach(code =>
  211. {
  212. code = code.ToUpper();
  213. });
  214. var queryData = Query(x => codeArr.Contains(x.Three.ToUpper()) && x.IsDel == 0).ToList();
  215. var groupData = queryData.GroupBy(x => x.Three).ToList();
  216. foreach (var item in groupData)
  217. {
  218. dic.Add(item.Key, item.ToList()[0]);
  219. }
  220. return dic;
  221. }
  222. }
  223. }