VisaFeeInfoRepository.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. using AutoMapper;
  2. using OASystem.Domain;
  3. using OASystem.Domain.Dtos.Groups;
  4. using OASystem.Domain.Entities.Groups;
  5. using OASystem.Domain.Entities.Resource;
  6. using OASystem.Domain.ViewModels.Groups;
  7. using OASystem.Domain.ViewModels.Resource;
  8. using OASystem.Infrastructure.Repositories.Resource;
  9. namespace OASystem.Infrastructure.Repositories.Groups
  10. {
  11. /// <summary>
  12. /// 团组签证费用详情 info
  13. /// 仓储
  14. /// </summary>
  15. public class VisaFeeInfoRepository:BaseRepository<Grp_VisaFeeInfo,VisaInfoView>
  16. {
  17. private readonly IMapper _mapper;
  18. private Result _result;
  19. private readonly CountryFeeRepository _countryFeeRep;
  20. private readonly DelegationInfoRepository _groupRep;
  21. public VisaFeeInfoRepository(SqlSugarClient sqlSugar, IMapper mapper, CountryFeeRepository countryFeeRep, DelegationInfoRepository groupRep)
  22. : base(sqlSugar)
  23. {
  24. _mapper = mapper;
  25. _result = new Result() { Code = -2,Msg = "操作失败!"};
  26. _countryFeeRep = countryFeeRep;
  27. _groupRep = groupRep;
  28. }
  29. /// <summary>
  30. /// Init
  31. /// </summary>
  32. /// <param name="portType"></param>
  33. /// <param name="diId"></param>
  34. /// <returns></returns>
  35. public async Task<Result> Init()
  36. {
  37. var data = await _sqlSugar.Queryable<Res_CountryFeeCost>().Where(it => it.IsDel == 0).ToListAsync();
  38. _result.Code = 0;
  39. _result.Data = data;
  40. _result.Msg = "操作成功!";
  41. return _result;
  42. }
  43. /// <summary>
  44. /// List
  45. /// </summary>
  46. /// <param name="portType"></param>
  47. /// <param name="diId"></param>
  48. /// <returns></returns>
  49. public async Task<JsonView> List(int portType, int diId)
  50. {
  51. if (diId < 0)
  52. return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "请传入有效的DiId参数" };
  53. if (portType < 1 || portType > 3)
  54. return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "请传入有效的portType参数" };
  55. //防SQL注入
  56. string sql = @"Select vfi.Id,vfi.IsChecked,cfc.VisaCountry AS Country,cfc.VisaPrice As VisaFee,
  57. vfi.OBType,vfi.AgencyFee,vfi.OtherFee,vfi.Remark
  58. From Grp_VisaFeeInfo vfi
  59. Left Join Res_CountryFeeCost cfc On vfi.CountryVisaFeeId = cfc.Id
  60. Where vfi.Isdel = 0 And vfi.Diid = @diId";
  61. var data = await _sqlSugar.SqlQueryable<VisaFeeInfosView>(sql)
  62. .AddParameters(new { diId })
  63. .ToListAsync();
  64. if (data.Count == 0)
  65. {
  66. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>()
  67. .FirstAsync(x => x.IsDel == 0 && x.Id == diId);
  68. if (groupInfo == null)
  69. {
  70. return new JsonView() { Code = StatusCodes.Status200OK, Data = data, Msg = "团组信息不存在!" };
  71. }
  72. var countrys = _groupRep.GroupSplitCountry(groupInfo.VisitCountry ?? "");
  73. if (countrys.Count > 0)
  74. {
  75. var visaFeeCountrys = await _sqlSugar.Queryable<Res_VisaFeeStandard>()
  76. .Where(x => x.IsDel == 0 && x.FeeType == 0 && countrys.Contains(x.Country))
  77. .Select(x => new VisaFeeStandardInfoView
  78. {
  79. Id = x.Id,
  80. Continent = x.Continent,
  81. Country = x.Country,
  82. FeeType = x.FeeType,
  83. })
  84. .ToListAsync();
  85. if (visaFeeCountrys.Count > 0)
  86. {
  87. var visaFeeCountryIds = visaFeeCountrys.Select(x => x.Id).ToList();
  88. var visaFeeDetails = await _sqlSugar.Queryable<Res_VisaFeeStandardDetails>()
  89. .Where(x => x.IsDel == 0 && visaFeeCountryIds.Contains(x.ParentId))
  90. .ToListAsync();
  91. // 建立ParentId到Details的字典,避免多次Where查找
  92. var detailsDict = visaFeeDetails
  93. .GroupBy(x => x.ParentId)
  94. .ToDictionary(g => g.Key, g => g.ToList());
  95. foreach (var countryInfo in visaFeeCountrys)
  96. {
  97. if (detailsDict.TryGetValue(countryInfo.Id, out var details))
  98. {
  99. // 只做映射,无副作用可省略变量
  100. countryInfo.VisaFees = _mapper.Map<List<VisaFeeStandardDetails>>(details);
  101. }
  102. }
  103. var provCityDatas = await _groupRep.ProvinceCityBasicSource();
  104. int provinceId = _groupRep.FindParentIdByChildId(provCityDatas, groupInfo.CityId) ?? 0;
  105. foreach (var country in countrys)
  106. {
  107. var countryInfo = visaFeeCountrys.FirstOrDefault(x => x.Country == country);
  108. if (countryInfo != null && countryInfo.VisaFees != null)
  109. {
  110. //如果未找到对应的城市则默认四川的费用标准
  111. var cityVisaFeeInfo = countryInfo.VisaFees.FirstOrDefault(x => x.ProvinceId == provinceId)
  112. ?? countryInfo.VisaFees.FirstOrDefault(x => x.ProvinceId == 122); // 122 四川省ID
  113. if (cityVisaFeeInfo != null)
  114. {
  115. data.Add(new VisaFeeInfosView()
  116. {
  117. IsChecked = 0,
  118. Country = country,
  119. //大公务签证费用为0时,使用小公务签证费用
  120. VisaFee = cityVisaFeeInfo.LargeVisaPrice == 0.00M
  121. ? cityVisaFeeInfo.SmallVisaPrice
  122. : cityVisaFeeInfo.LargeVisaPrice,
  123. OBType = 1,
  124. AgencyFee = cityVisaFeeInfo.LargeAgencyFee
  125. });
  126. data.Add(new VisaFeeInfosView()
  127. {
  128. IsChecked = 0,
  129. Country = country,
  130. //小公务签证费用为0时,使用大公务签证费用
  131. VisaFee = cityVisaFeeInfo.SmallVisaPrice == 0.00M
  132. ? cityVisaFeeInfo.LargeVisaPrice
  133. : cityVisaFeeInfo.SmallVisaPrice,
  134. OBType = 2,
  135. AgencyFee = cityVisaFeeInfo.SmallAgencyFee
  136. });
  137. continue;
  138. }
  139. }
  140. // 没有费用信息时补空行
  141. data.Add(new VisaFeeInfosView()
  142. {
  143. IsChecked = 0,
  144. Country = country,
  145. OBType = 1,
  146. });
  147. data.Add(new VisaFeeInfosView()
  148. {
  149. IsChecked = 0,
  150. Country = country,
  151. OBType = 2,
  152. });
  153. }
  154. }
  155. AddDefaultRows(data, 10);
  156. }
  157. else
  158. {
  159. AddDefaultRows(data, 10);
  160. }
  161. }
  162. else if (data.Count <= 10)
  163. {
  164. AddDefaultRows(data, 10);
  165. }
  166. return new JsonView() { Code = StatusCodes.Status200OK, Data = data, Msg = "操作成功!" };
  167. }
  168. /// <summary>
  169. /// 补齐默认行
  170. /// </summary>
  171. /// <param name="list"></param>
  172. /// <param name="totalRows"></param>
  173. private void AddDefaultRows(List<VisaFeeInfosView> list, int totalRows)
  174. {
  175. int defaultRow = totalRows - list.Count;
  176. for (int i = 0; i < defaultRow; i++)
  177. {
  178. list.Add(new VisaFeeInfosView { Id = 0, OBType = 1 });
  179. }
  180. }
  181. /// <summary>
  182. /// List
  183. /// </summary>
  184. /// <param name="dto"></param>
  185. /// <returns></returns>
  186. public async Task<Result> Update(VisaFeeAddAndUpdateDto dto)
  187. {
  188. if (dto.VisaFeeInfos.Count < 1) return _result = new Result() { Code = -1, Msg = "请传入有效的签证费用集合参数" };
  189. if (dto.PortType < 1 || dto.PortType > 3) return _result = new Result() { Code = -1, Msg = "请传入有效的portType参数" };
  190. var visaInfos = new List<Grp_VisaFeeInfo>();
  191. BeginTran();
  192. //bool visaFeeUpdate = false;
  193. var countrys = dto.VisaFeeInfos.GroupBy(x => x.Country);
  194. foreach (var country in countrys)
  195. {
  196. int countryVisaFeeId = 0;
  197. decimal _grandBusinessAgencyFee = 0;
  198. decimal _pettyBusinessAgencyFee = 0;
  199. decimal _visaFee = 0;
  200. foreach (var item in country.ToList())
  201. {
  202. _visaFee = item.VisaFee;
  203. if (item.OBType == 1) _grandBusinessAgencyFee = item.AgencyFee;
  204. else if (item.OBType == 2) _pettyBusinessAgencyFee = item.AgencyFee;
  205. }
  206. var info = await _countryFeeRep.InfoByCountryName(country.Key);
  207. if (info == null) //添加
  208. {
  209. int addId = _sqlSugar.Insertable(
  210. new Res_CountryFeeCost()
  211. {
  212. VisaCountry = country.Key,
  213. VisaPrice = _visaFee,
  214. GrandBusinessAgencyFee = _grandBusinessAgencyFee,
  215. PettyBusinessAgencyFee = _pettyBusinessAgencyFee,
  216. LastUpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  217. CreateUserId = dto.OpUserId,
  218. }).ExecuteReturnIdentity();
  219. if (addId > 0) countryVisaFeeId = addId;
  220. }
  221. else //修改
  222. {
  223. countryVisaFeeId = info.Id;
  224. if (_visaFee != info.VisaPrice) //价格不同的时候执行修改
  225. {
  226. var _CountryFeeCost = new Res_CountryFeeCost()
  227. {
  228. Id = info.Id,
  229. VisaPrice = _visaFee,
  230. GrandBusinessAgencyFee = _grandBusinessAgencyFee,
  231. PettyBusinessAgencyFee = _pettyBusinessAgencyFee,
  232. LastUpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  233. };
  234. int update1 = _sqlSugar.Updateable(_CountryFeeCost).UpdateColumns(it => new { it.VisaPrice, it.LastUpdateTime }).WhereColumns(it => it.Id).ExecuteCommand();
  235. //if (update1 > 0)
  236. //{
  237. // visaFeeUpdate = true;
  238. //}
  239. }
  240. }
  241. foreach (var item in country.ToList())
  242. {
  243. visaInfos.Add(new Grp_VisaFeeInfo()
  244. {
  245. Id = item.Id,
  246. DiId = dto.DiId,
  247. IsChecked = item.IsChecked,
  248. CountryVisaFeeId = countryVisaFeeId,
  249. OBType = item.OBType,
  250. AgencyFee = item.AgencyFee,
  251. OtherFee = item.OtherFee
  252. });
  253. }
  254. }
  255. //执行删除
  256. var update = _sqlSugar.Updateable<Grp_VisaFeeInfo>().SetColumns(it => new Grp_VisaFeeInfo()
  257. {
  258. DeleteUserId = dto.OpUserId,
  259. DeleteTime = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss"),
  260. IsDel = 1
  261. }).Where(it => it.DiId == dto.DiId).ExecuteCommand();
  262. var add = _sqlSugar.Insertable(visaInfos).ExecuteCommand();
  263. if (add > 0)
  264. {
  265. CommitTran();
  266. return new Result() {Code = 0 ,Msg = "操作成功!" };
  267. }
  268. RollbackTran();
  269. return _result;
  270. }
  271. /// <summary>
  272. /// 出入境费用签证费用提示
  273. /// </summary>
  274. /// <param name="portType"></param>
  275. /// <param name="diId"></param>
  276. /// <returns></returns>
  277. public async Task<Result> EnterExitCostVisaTips(int diId)
  278. {
  279. if (diId < 1) return _result = new Result() { Code = -1, Msg = "请传入有效的DiId参数" };
  280. var visaData = await _sqlSugar.Queryable<Grp_VisaFeeInfo>().Where(it => it.IsDel == 0 && it.DiId == diId).ToListAsync();
  281. List<int> ids = visaData.Select (it => it.CountryVisaFeeId).ToList();
  282. var visaCountryDatas = await _sqlSugar.Queryable<Res_CountryFeeCost>().Where(it => ids.Contains(it.Id)).ToListAsync();
  283. var datas = new List<dynamic>();
  284. string remark = "";
  285. decimal feeTotal = 0.00M;
  286. visaData = visaData.Where(it => it.IsChecked == 1).ToList();
  287. var visaData1 = visaData.GroupBy(it => it.CountryVisaFeeId);
  288. //费用报价系数
  289. decimal priceCoeff = 1.00M;
  290. foreach (var kvp in visaData1)
  291. {
  292. var countryData = visaCountryDatas.Find(it => it.Id == kvp.Key);
  293. decimal _otherFee = kvp.FirstOrDefault()?.OtherFee * priceCoeff ?? 0;
  294. decimal _visaFee = Convert.ToDecimal(countryData?.VisaPrice ?? 0.00M) * priceCoeff;
  295. decimal visaFeeTotal = _visaFee + _otherFee;
  296. decimal _agencyFee = 0;
  297. decimal _GrandBusinessAgencyFee = 0;
  298. decimal _PettyBusinessAgencyFee = 0;
  299. string remark1 = $"签证费:{_visaFee:#0.00}元、";
  300. foreach (var item in kvp.ToList())
  301. {
  302. if (item.OBType == 1)
  303. {
  304. if (item.AgencyFee > 0)
  305. {
  306. remark1 += $@"大公务代办费:{item.AgencyFee:#0.00}元、";
  307. _agencyFee += item.AgencyFee;
  308. _GrandBusinessAgencyFee = item.AgencyFee * priceCoeff;
  309. }
  310. }
  311. else if (item.OBType == 1)
  312. {
  313. if (item.AgencyFee > 0)
  314. {
  315. remark1 += $@"小公务代办费:{item.AgencyFee.ToString("#0.00")}元、";
  316. _agencyFee += item.AgencyFee;
  317. _PettyBusinessAgencyFee = item.AgencyFee * priceCoeff;
  318. }
  319. }
  320. }
  321. if (remark1.Length > 0) remark1 = remark1.Substring(0, remark1.Length - 1);
  322. if (_otherFee > 0) remark1 += $@"其他费用:{_otherFee:#0.00}元";
  323. visaFeeTotal += (_GrandBusinessAgencyFee + _PettyBusinessAgencyFee);
  324. remark += $@"{countryData?.VisaCountry ?? ""}:签证总费用:{visaFeeTotal}元/人 其中({remark1});";
  325. feeTotal += visaFeeTotal;
  326. datas.Add(new
  327. {
  328. Country = countryData?.VisaCountry ?? "",
  329. visaFeeTotal,
  330. VisaFee = countryData?.VisaPrice ?? 0.00M,
  331. GrandBusinessAgencyFee = _GrandBusinessAgencyFee,
  332. PettyBusinessAgencyFee = _PettyBusinessAgencyFee,
  333. OtherFee = _otherFee
  334. });
  335. }
  336. _result.Code = 0;
  337. _result.Data = new {
  338. feeTotal,
  339. data = datas,
  340. remark
  341. };
  342. return _result;
  343. }
  344. /// <summary>
  345. /// 出入境费用草稿签证费用提示
  346. /// </summary>
  347. /// <param name="portType"></param>
  348. /// <param name="diId"></param>
  349. /// <returns></returns>
  350. public async Task<(decimal,string)> EnterExitCostDraftVisaTips(string[] countrys)
  351. {
  352. var visaCountryDatas = await _sqlSugar.Queryable<Res_CountryFeeCost>().Where(it => countrys.Contains(it.VisaCountry)).ToListAsync();
  353. string remark = "";
  354. decimal feeTotal = 0.00M;
  355. //费用报价系数
  356. decimal priceCoeff = 1.00M;
  357. foreach (var kvp in visaCountryDatas)
  358. {
  359. var countryName = kvp?.VisaCountry ?? "";
  360. //免签
  361. if (kvp.IsVisaExemption == 0)
  362. {
  363. remark += $@"{countryName}:免签;";
  364. continue;
  365. }
  366. decimal visaFee = Convert.ToDecimal(kvp?.VisaPrice ?? 0.00M) * priceCoeff;
  367. decimal gbAgencyFee = kvp.GrandBusinessAgencyFee;
  368. decimal pbAgencyFee = kvp.PettyBusinessAgencyFee;
  369. string remark1 = $"签证费:{visaFee:#0.00}元、";
  370. if (gbAgencyFee > 0.00M)
  371. {
  372. remark1 += $@"大公务代办费:{gbAgencyFee:#0.00}元、";
  373. gbAgencyFee *= priceCoeff;
  374. }
  375. if (pbAgencyFee > 0.00M)
  376. {
  377. remark1 += $@"小公务代办费:{pbAgencyFee:#0.00}元、";
  378. pbAgencyFee *= priceCoeff;
  379. }
  380. if (remark1.Length > 0) remark1 = remark1.Substring(0, remark1.Length - 1);
  381. decimal visaFeeTotal = visaFee + gbAgencyFee + pbAgencyFee;
  382. remark += $@"{countryName}:签证总费用:{visaFeeTotal}元/人 其中({remark1});";
  383. feeTotal += visaFeeTotal;
  384. }
  385. return (feeTotal,remark);
  386. }
  387. }
  388. }