CountryFeeRepository.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. .LeftJoin<Sys_Cities>((x, y) => x.ProvinceId == y.Id && (y.Level == 1 || y.Level == 4))
  116. .Where((x, y) => ids.Contains( x.ParentId ) && x.IsDel == 0)
  117. .Select((x, y) => new VisaFeeStandardDetails
  118. {
  119. Id = x.Id,
  120. ParentId = x.ParentId,
  121. ProvinceId = x.ProvinceId,
  122. ProvinceName = y.Name_CN,
  123. VisaAddress = x.VisaAddress,
  124. IsVisaOnArrival = x.IsVisaOnArrival,
  125. IsElectronicSign = x.IsElectronicSign,
  126. VisaTime = x.VisaTime,
  127. IsVisaExemptionLarge = x.IsVisaExemptionLarge,
  128. LargeVisaPrice = x.LargeVisaPrice,
  129. LargeAgencyFee = x.LargeAgencyFee,
  130. IsVisaExemptionSmall = x.IsVisaExemptionSmall,
  131. SmallVisaPrice = x.SmallVisaPrice,
  132. SmallAgencyFee = x.SmallAgencyFee,
  133. NormExtFee = x.NormExtFee,
  134. UrgExtFee = x.UrgExtFee,
  135. IsUrgent = x.IsUrgent,
  136. UrgentTime = x.UrgentTime,
  137. UrgentPrice = x.UrgentPrice,
  138. UrgentPriceDesc = x.UrgentPriceDesc,
  139. Remark = y.Remark,
  140. })
  141. .ToListAsync();
  142. var specifiedOrder = new List<string> { "四川", "重庆", "贵州", "云南" };
  143. foreach (var item in pageList)
  144. {
  145. var provinceDetails = detailsList.Where(x => x.ParentId == item.Id).ToList();
  146. if (provinceDetails.Any())
  147. provinceDetails = VisaFeeStandardDetails.SortByProvinces(provinceDetails, specifiedOrder);
  148. item.VisaFees = provinceDetails;
  149. }
  150. return new JsonView
  151. {
  152. Code = StatusCodes.Status200OK,
  153. Data = pageList,
  154. Count = total,
  155. Msg = "操作成功!"
  156. };
  157. }
  158. /// <summary>
  159. /// info Async
  160. /// </summary>
  161. /// <param name="id"></param>
  162. /// <returns></returns>
  163. public async Task<JsonView> InfoAsync(int id)
  164. {
  165. var view = await Query<Res_VisaFeeStandard>(x => x.Id == id)
  166. .Select(x => new VisaFeeStandardInfoView()
  167. {
  168. Id = x.Id,
  169. Continent = x.Continent,
  170. Country = x.Country,
  171. FeeType = x.FeeType,
  172. })
  173. .FirstAsync();
  174. if (view == null)
  175. {
  176. return new JsonView
  177. {
  178. Code = StatusCodes.Status200OK,
  179. Msg = "暂无数据!",
  180. Data = view
  181. };
  182. }
  183. var detailsList = await _sqlSugar.Queryable<Res_VisaFeeStandardDetails>()
  184. .LeftJoin<Sys_Cities>((x, y) => x.ProvinceId == y.Id && (y.Level == 1 || y.Level == 4))
  185. .Where((x, y) => x.ParentId == view.Id && x.IsDel == 0)
  186. .Select((x, y) => new VisaFeeStandardDetails {
  187. Id = x.Id,
  188. ParentId = x.ParentId,
  189. ProvinceId = x.ProvinceId,
  190. ProvinceName = y.Name_CN,
  191. VisaAddress = x.VisaAddress,
  192. IsVisaOnArrival = x.IsVisaOnArrival,
  193. IsElectronicSign = x.IsElectronicSign,
  194. VisaTime = x.VisaTime,
  195. IsVisaExemptionLarge = x.IsVisaExemptionLarge,
  196. LargeVisaPrice = x.LargeVisaPrice,
  197. LargeAgencyFee = x.LargeAgencyFee,
  198. IsVisaExemptionSmall = x.IsVisaExemptionSmall,
  199. SmallVisaPrice = x.SmallVisaPrice,
  200. SmallAgencyFee = x.SmallAgencyFee,
  201. NormExtFee = x.NormExtFee,
  202. UrgExtFee = x.UrgExtFee,
  203. IsUrgent = x.IsUrgent,
  204. UrgentTime = x.UrgentTime,
  205. UrgentPrice = x.UrgentPrice,
  206. UrgentPriceDesc = x.UrgentPriceDesc,
  207. Remark = y.Remark,
  208. })
  209. .ToListAsync();
  210. // 指定的省份顺序
  211. var specifiedOrder = new List<string> { "四川", "重庆", "贵州", "云南" };
  212. if (detailsList.Any())
  213. detailsList = VisaFeeStandardDetails.SortByProvinces(detailsList, specifiedOrder);
  214. view.VisaFees = detailsList;
  215. return new JsonView
  216. {
  217. Code = StatusCodes.Status200OK,
  218. Msg = "操作成功!",
  219. Data = view
  220. };
  221. }
  222. /// <summary>
  223. /// Save Async
  224. /// </summary>
  225. /// <param name="id"></param>
  226. /// <returns></returns>
  227. public async Task<JsonView> SaveAsync(VisaFeeStandardSaveDto dto)
  228. {
  229. var now = DateTime.Now;
  230. var standardInfo = _mapper.Map<Res_VisaFeeStandard>(dto);
  231. standardInfo.LastUpdateTime = now;
  232. standardInfo.LastUpdateUserId = dto.CurrUserId;
  233. standardInfo.CreateTime = now;
  234. standardInfo.CreateUserId = dto.CurrUserId;
  235. // 指定的省份顺序
  236. var specifiedOrder = new List<string> { "四川", "重庆", "贵州", "云南" };
  237. if (dto.VisaFees.Any())
  238. dto.VisaFees = VisaFeeStandardDetails.SortByProvinces(dto.VisaFees, specifiedOrder);
  239. var standardDetails = _mapper.Map<List<Res_VisaFeeStandardDetails>>(dto.VisaFees);
  240. standardDetails.ForEach(x =>
  241. {
  242. x.CreateUserId = dto.CurrUserId;
  243. x.CreateTime = now;
  244. });
  245. string msg = string.Empty;
  246. _sqlSugar.BeginTran();
  247. try
  248. {
  249. if (standardInfo.Id < 1) // 添加
  250. {
  251. var insertId = await _sqlSugar.Insertable(standardInfo).ExecuteReturnIdentityAsync();
  252. if (insertId < 1)
  253. {
  254. _sqlSugar.RollbackTran();
  255. return new JsonView { Code = StatusCodes.Status400BadRequest, Msg = "添加失败!" };
  256. }
  257. standardDetails.ForEach(x => x.ParentId = insertId);
  258. var detailsResult = await _sqlSugar.Insertable(standardDetails).ExecuteCommandAsync();
  259. if (detailsResult < 1)
  260. {
  261. _sqlSugar.RollbackTran();
  262. return new JsonView { Code = StatusCodes.Status400BadRequest, Msg = "添加失败!" };
  263. }
  264. msg = "添加成功!";
  265. }
  266. else // 修改
  267. {
  268. var updStatus = await _sqlSugar.Updateable(standardInfo)
  269. .IgnoreColumns(x => new { x.IsDel, x.CreateUserId, x.CreateTime, x.DeleteUserId, x.DeleteTime })
  270. .ExecuteCommandAsync();
  271. if (updStatus < 1)
  272. {
  273. _sqlSugar.RollbackTran();
  274. return new JsonView { Code = StatusCodes.Status400BadRequest, Msg = "修改失败!" };
  275. }
  276. await _sqlSugar.Deleteable<Res_VisaFeeStandardDetails>()
  277. .Where(x => x.ParentId == standardInfo.Id)
  278. .ExecuteCommandAsync();
  279. standardDetails.ForEach(x => x.ParentId = standardInfo.Id);
  280. var detailsResult = await _sqlSugar.Insertable(standardDetails).ExecuteCommandAsync();
  281. if (detailsResult < 1)
  282. {
  283. _sqlSugar.RollbackTran();
  284. return new JsonView { Code = StatusCodes.Status400BadRequest, Msg = "修改失败!" };
  285. }
  286. msg = "修改成功!";
  287. }
  288. _sqlSugar.CommitTran();
  289. return new JsonView { Code = StatusCodes.Status200OK, Msg = msg };
  290. }
  291. catch(Exception ex)
  292. {
  293. _sqlSugar.RollbackTran();
  294. msg = ex.Message;
  295. }
  296. return new JsonView { Code = StatusCodes.Status400BadRequest, Msg = msg };
  297. }
  298. /// <summary>
  299. /// SoftDel Async
  300. /// </summary>
  301. /// <param name="userId"></param>
  302. /// <param name="id"></param>
  303. /// <returns></returns>
  304. public async Task<JsonView> SoftDelAsync(int userId, int id)
  305. {
  306. _sqlSugar.BeginTran();
  307. try
  308. {
  309. var nowString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  310. // 主表软删除
  311. var delStatus = await _sqlSugar.Updateable<Res_VisaFeeStandard>()
  312. .SetColumns(x => x.DeleteUserId == userId)
  313. .SetColumns(x => x.DeleteTime == nowString)
  314. .SetColumns(x => x.IsDel == 1)
  315. .Where(x => x.Id == id)
  316. .ExecuteCommandAsync();
  317. if (delStatus < 1)
  318. {
  319. _sqlSugar.RollbackTran();
  320. return new JsonView { Code = StatusCodes.Status400BadRequest, Msg = "删除失败!" };
  321. }
  322. // 子表软删除
  323. var detailsDelStatus = await _sqlSugar.Updateable<Res_VisaFeeStandardDetails>()
  324. .SetColumns(x => x.DeleteUserId == userId)
  325. .SetColumns(x => x.DeleteTime == nowString)
  326. .SetColumns(x => x.IsDel == 1)
  327. .Where(x => x.ParentId == id)
  328. .ExecuteCommandAsync();
  329. if (detailsDelStatus < 1)
  330. {
  331. _sqlSugar.RollbackTran();
  332. return new JsonView { Code = StatusCodes.Status400BadRequest, Msg = "删除失败!" };
  333. }
  334. _sqlSugar.CommitTran();
  335. return new JsonView { Code = StatusCodes.Status200OK, Msg = "操作成功!" };
  336. }
  337. catch (Exception ex)
  338. {
  339. _sqlSugar.RollbackTran();
  340. return new JsonView { Code = StatusCodes.Status400BadRequest, Msg = ex.Message };
  341. }
  342. }
  343. #endregion
  344. }
  345. }