CountryFeeRepository.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. using AutoMapper;
  2. using MathNet.Numerics.Distributions;
  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 CountryFeeRepository : BaseRepository<Res_CountryFeeCost, CountryFeeCostView>
  9. {
  10. private readonly IMapper _mapper;
  11. public CountryFeeRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
  12. {
  13. _mapper = mapper;
  14. }
  15. public async Task<Res_CountryFeeCost> InfoByCountryName(string countryName)
  16. {
  17. if (string.IsNullOrEmpty(countryName)) return null;
  18. return await _sqlSugar.Queryable<Res_CountryFeeCost>()
  19. .FirstAsync(it => it.VisaCountry == countryName);
  20. }
  21. public async Task<JsonView> OperationCountryFeeCost(OperationCountryFeeCostDto dto)
  22. {
  23. var result = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "未知错误" };
  24. var countryFeeCost = _mapper.Map<Res_CountryFeeCost>(dto);
  25. countryFeeCost.LastUpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  26. if (dto.Status == 1)//添加
  27. {
  28. var exists = await _sqlSugar.Queryable<Res_CountryFeeCost>()
  29. .AnyAsync(x => x.IsDel == 0 &&
  30. x.VisaFeeType == dto.VisaFeeType &&
  31. x.VisaContinent == dto.VisaContinent &&
  32. x.VisaCountry == dto.VisaCountry);
  33. if (exists)
  34. {
  35. result.Msg = "该国家已存在,请勿重复添加!";
  36. return result;
  37. }
  38. else//不存在,可添加
  39. {
  40. int id = await AddAsyncReturnId(countryFeeCost);
  41. if (id == 0)
  42. {
  43. result.Msg = "添加失败!";
  44. return result;
  45. }
  46. result.Code = StatusCodes.Status200OK;
  47. result.Msg = "添加成功!";
  48. }
  49. }
  50. else if (dto.Status == 2)//修改
  51. {
  52. var update = await _sqlSugar
  53. .Updateable(countryFeeCost)
  54. .IgnoreColumns(it => new { it.Id, it.DeleteUserId, it.DeleteTime, it.CreateUserId, it.CreateTime, it.IsDel })
  55. .Where(it => it.Id == countryFeeCost.Id)
  56. .ExecuteCommandAsync();
  57. //bool res = await UpdateAsync(a => a.Id == dto.Id, a => _CountryFeeCost);
  58. if (update < 1)
  59. {
  60. result.Msg = "修改失败!";
  61. return result;
  62. }
  63. result.Code = StatusCodes.Status200OK;
  64. result.Msg = "修改成功!";
  65. }
  66. else
  67. {
  68. result.Msg = "请传入Status参数,1添加 2修改!";
  69. }
  70. return result;
  71. }
  72. #region New
  73. /// <summary>
  74. /// Page List Async
  75. /// </summary>
  76. /// <param name="pageIndex"></param>
  77. /// <param name="pageSize"></param>
  78. /// <param name="feeType"></param>
  79. /// <param name="countryName"></param>
  80. /// <returns></returns>
  81. public async Task<JsonView> PageListAsync(VisaFeeStandardListDto dto)
  82. {
  83. int pageIndex = dto.PageIndex <= 0 ? 1 : dto.PageIndex;
  84. int pageSize = dto.PageSize <= 0 ? 10 : dto.PageSize;
  85. int feeType = dto.VisaFeeType < 0 ? -1 : dto.VisaFeeType; // -1:全部 0:因公 1:因私
  86. string countryName = dto.CountryName?.Trim() ?? string.Empty;
  87. var query = _sqlSugar.Queryable<Res_VisaFeeStandard>()
  88. .LeftJoin<Sys_Users>((x, y) => x.LastUpdateUserId == y.Id)
  89. .Where((x, y) => x.IsDel == 0 && x.FeeType == feeType)
  90. .WhereIF(!string.IsNullOrEmpty(countryName), x => x.Country.Contains(countryName) || countryName.Contains(x.Country))
  91. .OrderByDescending((x, y) => x.LastUpdateTime)
  92. .Select((x, y) => new VisaFeeStandardListView
  93. {
  94. Id = x.Id,
  95. Continent = x.Continent,
  96. Country = x.Country,
  97. FeeType = x.FeeType,
  98. LastUpdateUserName = y.CnName,
  99. LastUpdateTime = x.LastUpdateTime
  100. });
  101. RefAsync<int> total = 0;
  102. var pageList = await query.ToPageListAsync(pageIndex, pageSize, total);
  103. if (!pageList.Any())
  104. {
  105. return new JsonView
  106. {
  107. Code = StatusCodes.Status200OK,
  108. Data = pageList,
  109. Count = total,
  110. Msg = "暂无数据!"
  111. };
  112. }
  113. var ids = pageList.Select(x => x.Id).ToList();
  114. var detailsList = await _sqlSugar.Queryable<Res_VisaFeeStandardDetails>()
  115. .Where(x => x.IsDel == 0 && ids.Contains(x.ParentId))
  116. .ToListAsync();
  117. var mappedDetails = _mapper.Map<List<VisaFeeStandardDetails>>(detailsList);
  118. var specifiedOrder = new List<string> { "四川", "重庆", "贵州", "云南" };
  119. foreach (var item in pageList)
  120. {
  121. var provinceDetails = mappedDetails.Where(x => x.ParentId == item.Id).ToList();
  122. if (provinceDetails.Any())
  123. provinceDetails = VisaFeeStandardDetails.SortByProvinces(provinceDetails, specifiedOrder);
  124. item.VisaFees = provinceDetails;
  125. }
  126. return new JsonView
  127. {
  128. Code = StatusCodes.Status200OK,
  129. Data = pageList,
  130. Count = total,
  131. Msg = "操作成功!"
  132. };
  133. }
  134. /// <summary>
  135. /// info Async
  136. /// </summary>
  137. /// <param name="id"></param>
  138. /// <returns></returns>
  139. public async Task<JsonView> InfoAsync(int id)
  140. {
  141. var view = await Query<Res_VisaFeeStandard>(x => x.Id == id)
  142. .Select(x => new VisaFeeStandardInfoView()
  143. {
  144. Id = x.Id,
  145. Continent = x.Continent,
  146. Country = x.Country,
  147. FeeType = x.FeeType,
  148. })
  149. .FirstAsync();
  150. if (view == null)
  151. {
  152. return new JsonView
  153. {
  154. Code = StatusCodes.Status200OK,
  155. Msg = "暂无数据!",
  156. Data = view
  157. };
  158. }
  159. var detailsList = await _sqlSugar.Queryable<Res_VisaFeeStandardDetails>()
  160. .LeftJoin<Sys_Cities>((x, y) => x.ProvinceId == y.Id && (y.Level == 1 || y.Level == 4))
  161. .Where((x, y) => x.ParentId == view.Id && x.IsDel == 0)
  162. .Select((x, y) => new VisaFeeStandardDetails {
  163. Id = x.Id,
  164. ParentId = x.ParentId,
  165. ProvinceId = x.ProvinceId,
  166. ProvinceName = y.Name_CN,
  167. VisaAddress = x.VisaAddress,
  168. IsVisaOnArrival = x.IsVisaOnArrival,
  169. IsElectronicSign = x.IsElectronicSign,
  170. VisaTime = x.VisaTime,
  171. IsVisaExemptionLarge = x.IsVisaExemptionLarge,
  172. LargeVisaPrice = x.LargeVisaPrice,
  173. LargeAgencyFee = x.LargeAgencyFee,
  174. IsVisaExemptionSmall = x.IsVisaExemptionSmall,
  175. SmallVisaPrice = x.SmallVisaPrice,
  176. SmallAgencyFee = x.SmallAgencyFee,
  177. NormExtFee = x.NormExtFee,
  178. UrgExtFee = x.UrgExtFee,
  179. IsUrgent = x.IsUrgent,
  180. UrgentTime = x.UrgentTime,
  181. UrgentPrice = x.UrgentPrice,
  182. UrgentPriceDesc = x.UrgentPriceDesc,
  183. Remark = y.Remark,
  184. })
  185. .ToListAsync();
  186. // 指定的省份顺序
  187. var specifiedOrder = new List<string> { "四川", "重庆", "贵州", "云南" };
  188. if (detailsList.Any())
  189. detailsList = VisaFeeStandardDetails.SortByProvinces(detailsList, specifiedOrder);
  190. view.VisaFees = detailsList;
  191. return new JsonView
  192. {
  193. Code = StatusCodes.Status200OK,
  194. Msg = "操作成功!",
  195. Data = view
  196. };
  197. }
  198. /// <summary>
  199. /// Save Async
  200. /// </summary>
  201. /// <param name="id"></param>
  202. /// <returns></returns>
  203. public async Task<JsonView> SaveAsync(VisaFeeStandardSaveDto dto)
  204. {
  205. var now = DateTime.Now;
  206. var standardInfo = _mapper.Map<Res_VisaFeeStandard>(dto);
  207. standardInfo.LastUpdateTime = now;
  208. standardInfo.LastUpdateUserId = dto.CurrUserId;
  209. standardInfo.CreateTime = now;
  210. standardInfo.CreateUserId = dto.CurrUserId;
  211. // 指定的省份顺序
  212. var specifiedOrder = new List<string> { "四川", "重庆", "贵州", "云南" };
  213. if (dto.VisaFees.Any())
  214. dto.VisaFees = VisaFeeStandardDetails.SortByProvinces(dto.VisaFees, specifiedOrder);
  215. var standardDetails = _mapper.Map<List<Res_VisaFeeStandardDetails>>(dto.VisaFees);
  216. standardDetails.ForEach(x =>
  217. {
  218. x.CreateUserId = dto.CurrUserId;
  219. x.CreateTime = now;
  220. });
  221. string msg = string.Empty;
  222. _sqlSugar.BeginTran();
  223. try
  224. {
  225. if (standardInfo.Id < 1) // 添加
  226. {
  227. var insertId = await _sqlSugar.Insertable(standardInfo).ExecuteReturnIdentityAsync();
  228. if (insertId < 1)
  229. {
  230. _sqlSugar.RollbackTran();
  231. return new JsonView { Code = StatusCodes.Status400BadRequest, Msg = "添加失败!" };
  232. }
  233. standardDetails.ForEach(x => x.ParentId = insertId);
  234. var detailsResult = await _sqlSugar.Insertable(standardDetails).ExecuteCommandAsync();
  235. if (detailsResult < 1)
  236. {
  237. _sqlSugar.RollbackTran();
  238. return new JsonView { Code = StatusCodes.Status400BadRequest, Msg = "添加失败!" };
  239. }
  240. msg = "添加成功!";
  241. }
  242. else // 修改
  243. {
  244. var updStatus = await _sqlSugar.Updateable(standardInfo)
  245. .IgnoreColumns(x => new { x.IsDel, x.CreateUserId, x.CreateTime, x.DeleteUserId, x.DeleteTime })
  246. .ExecuteCommandAsync();
  247. if (updStatus < 1)
  248. {
  249. _sqlSugar.RollbackTran();
  250. return new JsonView { Code = StatusCodes.Status400BadRequest, Msg = "修改失败!" };
  251. }
  252. await _sqlSugar.Deleteable<Res_VisaFeeStandardDetails>()
  253. .Where(x => x.ParentId == standardInfo.Id)
  254. .ExecuteCommandAsync();
  255. standardDetails.ForEach(x => x.ParentId = standardInfo.Id);
  256. var detailsResult = await _sqlSugar.Insertable(standardDetails).ExecuteCommandAsync();
  257. if (detailsResult < 1)
  258. {
  259. _sqlSugar.RollbackTran();
  260. return new JsonView { Code = StatusCodes.Status400BadRequest, Msg = "修改失败!" };
  261. }
  262. msg = "修改成功!";
  263. }
  264. _sqlSugar.CommitTran();
  265. return new JsonView { Code = StatusCodes.Status200OK, Msg = msg };
  266. }
  267. catch(Exception ex)
  268. {
  269. _sqlSugar.RollbackTran();
  270. msg = ex.Message;
  271. }
  272. return new JsonView { Code = StatusCodes.Status400BadRequest, Msg = msg };
  273. }
  274. /// <summary>
  275. /// SoftDel Async
  276. /// </summary>
  277. /// <param name="userId"></param>
  278. /// <param name="id"></param>
  279. /// <returns></returns>
  280. public async Task<JsonView> SoftDelAsync(int userId, int id)
  281. {
  282. _sqlSugar.BeginTran();
  283. try
  284. {
  285. var nowString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  286. // 主表软删除
  287. var delStatus = await _sqlSugar.Updateable<Res_VisaFeeStandard>()
  288. .SetColumns(x => x.DeleteUserId == userId)
  289. .SetColumns(x => x.DeleteTime == nowString)
  290. .SetColumns(x => x.IsDel == 1)
  291. .Where(x => x.Id == id)
  292. .ExecuteCommandAsync();
  293. if (delStatus < 1)
  294. {
  295. _sqlSugar.RollbackTran();
  296. return new JsonView { Code = StatusCodes.Status400BadRequest, Msg = "删除失败!" };
  297. }
  298. // 子表软删除
  299. var detailsDelStatus = await _sqlSugar.Updateable<Res_VisaFeeStandardDetails>()
  300. .SetColumns(x => x.DeleteUserId == userId)
  301. .SetColumns(x => x.DeleteTime == nowString)
  302. .SetColumns(x => x.IsDel == 1)
  303. .Where(x => x.ParentId == id)
  304. .ExecuteCommandAsync();
  305. if (detailsDelStatus < 1)
  306. {
  307. _sqlSugar.RollbackTran();
  308. return new JsonView { Code = StatusCodes.Status400BadRequest, Msg = "删除失败!" };
  309. }
  310. _sqlSugar.CommitTran();
  311. return new JsonView { Code = StatusCodes.Status200OK, Msg = "操作成功!" };
  312. }
  313. catch (Exception ex)
  314. {
  315. _sqlSugar.RollbackTran();
  316. return new JsonView { Code = StatusCodes.Status400BadRequest, Msg = ex.Message };
  317. }
  318. }
  319. #endregion
  320. }
  321. }