VisaPriceRepository.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. 
  2. using AutoMapper;
  3. using NPOI.SS.Formula.Functions;
  4. using OASystem.Domain;
  5. using OASystem.Domain.AesEncryption;
  6. using OASystem.Domain.Dtos.Financial;
  7. using OASystem.Domain.Dtos.Groups;
  8. using OASystem.Domain.Entities.Customer;
  9. using OASystem.Domain.Entities.Financial;
  10. using OASystem.Domain.Entities.Groups;
  11. using OASystem.Domain.Entities.Resource;
  12. using OASystem.Domain.ViewModels.Financial;
  13. using OASystem.Domain.ViewModels.Groups;
  14. using OASystem.Infrastructure.Repositories.System;
  15. using OASystem.Infrastructure.Tools;
  16. using Org.BouncyCastle.Asn1.Cms;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Drawing;
  20. using System.Linq;
  21. using System.Security.Cryptography;
  22. using System.Text;
  23. using System.Threading.Tasks;
  24. namespace OASystem.Infrastructure.Repositories.Groups
  25. {
  26. public class VisaPriceRepository : BaseRepository<Grp_VisaInfo, VisaPriceDto>
  27. {
  28. private readonly IMapper _mapper;
  29. private readonly DelegationInfoRepository _groupRep;
  30. public VisaPriceRepository(SqlSugarClient sqlSugar, IMapper mapper, DelegationInfoRepository groupRep)
  31. : base(sqlSugar)
  32. {
  33. _mapper = mapper;
  34. _groupRep = groupRep;
  35. }
  36. /// <summary>
  37. /// 根据diid查询签证费用列表
  38. /// </summary>
  39. /// <param name="dto"></param>
  40. /// <returns></returns>
  41. public async Task<Result> PostVisaByDiId(VisaPriceDto dto)
  42. {
  43. Result result = new Result() { Code = -2, Msg = "未知错误" };
  44. if (dto.PortType == 0 || string.IsNullOrWhiteSpace(dto.PortType.ToString()))
  45. {
  46. return result = new Result() { Code = -1, Msg = "请传入PortType参数,请求端口分类 1 Web 2 Android 3 IOS" };
  47. }
  48. try
  49. {
  50. #region SQL条件拼接
  51. string sqlWhere = string.Empty;
  52. if (!string.IsNullOrWhiteSpace(dto.VisaClient))
  53. {
  54. sqlWhere += string.Format(@" And v.VisaClient like '%{0}%'", dto.VisaClient);
  55. }
  56. sqlWhere += string.Format(@"And v.DIId={0} and v.isdel={1}", dto.DiID, 0);
  57. string UserId = "";
  58. List<Grp_GroupsTaskAssignment> gtaUIdList = _sqlSugar.Queryable<Grp_GroupsTaskAssignment>().Where(a => a.DIId == dto.DiID && a.IsDel == 0 && a.CTId == 80).ToList();
  59. foreach (Grp_GroupsTaskAssignment gta in gtaUIdList)
  60. UserId += gta.UId + ",";
  61. if (!string.IsNullOrWhiteSpace(UserId))
  62. {
  63. UserId = UserId.Substring(0, UserId.Length - 1);
  64. }
  65. else
  66. {
  67. UserId = "0";
  68. }
  69. sqlWhere += string.Format(@" And v.CreateUserId in ({0})", UserId);
  70. if (dto.IsPaySign != -1)
  71. {
  72. sqlWhere += string.Format(@" And c.IsPay = {0} ", dto.IsPaySign);
  73. }
  74. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  75. {
  76. Regex r = new Regex("And");
  77. sqlWhere = r.Replace(sqlWhere, "Where", 1);
  78. }
  79. #endregion
  80. int startIndex = (dto.PageIndex - 1) * dto.PageSize + 1;
  81. int endIndex = startIndex + dto.PageSize - 1;
  82. if (dto.PortType == 1)
  83. {
  84. //string sql = string.Format(@"select *,(select IsAuditGM from Grp_CreditCardPayment where CTable=80 and CId=v.Id and IsDel=0) IsAuditGM,
  85. // (select Name from Sys_SetData where isdel=0 and v.VisaCurrency=Id) VisaCurrencyStr from
  86. // Grp_VisaInfo v {0} order by CreateTime desc",sqlWhere);
  87. string sql = string.Format(@" Select v.*, c.IsAuditGM,c.IsPay,sd.Name As 'PayName' From Grp_VisaInfo as v With(Nolock)
  88. left Join Grp_CreditCardPayment as c With(Nolock) On v.Id = c.CId
  89. Left Join Sys_SetData as sd With(Nolock) On c.PayDId = sd.Id
  90. {0}
  91. And c.CTable = 80 And c.IsDel = 0 ", sqlWhere);
  92. List<VisaInfoView> infoViews = _sqlSugar.SqlQueryable<VisaInfoView>(sql).ToList();
  93. var parseIntValue = 0;
  94. var CrmIds = (from item in infoViews
  95. where item.VisaClient.Contains(",") || int.TryParse(item.VisaClient, out parseIntValue)
  96. select item.VisaClient).ToList();
  97. var strCrmIds = string.Join(',', CrmIds);
  98. List<Crm_DeleClient> clietArr = new List<Crm_DeleClient>();
  99. if (!string.IsNullOrWhiteSpace(strCrmIds))
  100. {
  101. sql = string.Format("SELECT * FROM Crm_DeleClient cdc where id in ({0}) and IsDel = 0", strCrmIds);
  102. clietArr = _sqlSugar.SqlQueryable<Crm_DeleClient>(sql).ToList();
  103. foreach (var item in clietArr)
  104. {
  105. EncryptionProcessor.DecryptProperties(item);
  106. }
  107. }
  108. Dictionary<int, string> dic_Currency = new Dictionary<int, string>();
  109. foreach (VisaInfoView view in infoViews)
  110. {
  111. if (dic_Currency.ContainsKey(view.VisaCurrency))
  112. {
  113. view.VisaCurrencyStr = dic_Currency[view.VisaCurrency];
  114. }
  115. else
  116. {
  117. List<Sys_SetData> temp_sdList = Query<Sys_SetData>(s => s.Id == view.VisaCurrency).ToList();
  118. if (temp_sdList.Count > 0)
  119. {
  120. view.VisaCurrencyStr = temp_sdList[0].Name;
  121. dic_Currency.Add(view.VisaCurrency, temp_sdList[0].Name);
  122. }
  123. else
  124. {
  125. view.VisaCurrencyStr = "未知";
  126. dic_Currency.Add(view.VisaCurrency, "未知");
  127. }
  128. }
  129. if (view.IsAuditGM == 0) view.IsAuditGMStr = "未审核";
  130. else if (view.IsAuditGM == 1) view.IsAuditGMStr = "已通过";
  131. else if (view.IsAuditGM == 2) view.IsAuditGMStr = "未通过";
  132. else if (view.IsAuditGM == 3) view.IsAuditGMStr = "自动审核";
  133. {
  134. if (view.VisaClient.Contains(","))
  135. {
  136. var splitArr = view.VisaClient.Split(',');
  137. var name = string.Empty;
  138. var i = 0;
  139. while (i < splitArr.Count())
  140. {
  141. if (int.TryParse(splitArr[i], out parseIntValue))
  142. {
  143. var clientObject = clietArr.Find(x => x.Id == parseIntValue);
  144. name += (clientObject?.LastName + clientObject?.FirstName);
  145. }
  146. i++;
  147. if (i < splitArr.Count())
  148. {
  149. name += " ";
  150. }
  151. }
  152. if (!string.IsNullOrWhiteSpace(name))
  153. {
  154. view.VisaClient = name.TrimEnd();
  155. }
  156. continue;
  157. }
  158. }
  159. if (int.TryParse(view.VisaClient, out parseIntValue))
  160. {
  161. var name = string.Empty;
  162. var clientObject = clietArr.Find(x => x.Id == parseIntValue);
  163. name += (clientObject?.LastName + clientObject?.FirstName);
  164. if (!string.IsNullOrWhiteSpace(name))
  165. {
  166. view.VisaClient = name;
  167. }
  168. continue;
  169. }
  170. }
  171. return result = new Result() { Code = 0, Msg = "查询成功!", Data = infoViews };
  172. }
  173. else if (dto.PortType == 2 || dto.PortType == 3)
  174. {
  175. string sql = string.Format(@"Select * From (
  176. Select row_number() over (order by v.Id Desc) as RowNumber,v.*,c.IsAuditGM,s.Name as VisaCurrencyStr,s1.Name As 'PayName'
  177. From Grp_VisaInfo v
  178. Left Join Grp_CreditCardPayment c On v.Id = c.CId and CTable=80 and c.isdel=0
  179. Left Join Sys_SetData s On v.VisaCurrency = s.Id
  180. Left Join Sys_SetData s1 On c.PayDId = s1.Id {0}
  181. ) temp Where RowNumber Between {1} and {2}", sqlWhere, startIndex, endIndex);
  182. List<VisaInfoView> infoViews = _sqlSugar.SqlQueryable<VisaInfoView>(sql).ToList();
  183. var parseIntValue = 0;
  184. var CrmIds = (from item in infoViews
  185. where item.VisaClient.Contains(",") || int.TryParse(item.VisaClient, out parseIntValue)
  186. select item.VisaClient).ToList();
  187. var strCrmIds = string.Join(',', CrmIds);
  188. List<Crm_DeleClient> clietArr = new List<Crm_DeleClient>();
  189. if (!string.IsNullOrWhiteSpace(strCrmIds))
  190. {
  191. sql = string.Format("SELECT * FROM Crm_DeleClient cdc where id in ({0}) and IsDel = 0", strCrmIds);
  192. clietArr = _sqlSugar.SqlQueryable<Crm_DeleClient>(sql).ToList();
  193. }
  194. foreach (VisaInfoView view in infoViews)
  195. {
  196. if (view.IsAuditGM == 0) view.IsAuditGMStr = "未审核";
  197. else if (view.IsAuditGM == 1) view.IsAuditGMStr = "已通过";
  198. else if (view.IsAuditGM == 2) view.IsAuditGMStr = "未通过";
  199. else if (view.IsAuditGM == 3) view.IsAuditGMStr = "自动审核";
  200. {
  201. if (view.VisaClient.Contains(","))
  202. {
  203. var splitArr = view.VisaClient.Split(',');
  204. var name = string.Empty;
  205. var i = 0;
  206. while (i < splitArr.Count())
  207. {
  208. if (int.TryParse(splitArr[i], out parseIntValue))
  209. {
  210. var clientObject = clietArr.Find(x => x.Id == parseIntValue);
  211. name += (clientObject?.LastName + clientObject?.FirstName);
  212. }
  213. i++;
  214. if (i < splitArr.Count())
  215. {
  216. name += "[*TuT*]";
  217. }
  218. }
  219. if (!string.IsNullOrWhiteSpace(name))
  220. {
  221. view.VisaClient = name.TrimEnd();
  222. }
  223. continue;
  224. }
  225. }
  226. if (int.TryParse(view.VisaClient, out parseIntValue))
  227. {
  228. var name = string.Empty;
  229. var clientObject = clietArr.Find(x => x.Id == parseIntValue);
  230. name += (clientObject?.LastName + clientObject?.FirstName);
  231. if (!string.IsNullOrWhiteSpace(name))
  232. {
  233. view.VisaClient = name;
  234. }
  235. continue;
  236. }
  237. }
  238. string CountSql = string.Format(@"Select COUNT(1) as Count From (
  239. Select v.*,c.IsAuditGM,s.Name as VisaCurrencyStr
  240. From Grp_VisaInfo v
  241. left Join Grp_CreditCardPayment c On v.Id = c.CId and CTable=80 and c.isdel=0
  242. Left Join Sys_SetData s On v.VisaCurrency = s.Id {0}
  243. ) temp", sqlWhere);
  244. DataCount dataCount = _sqlSugar.SqlQueryable<DataCount>(CountSql).First();
  245. if (dataCount != null)
  246. {
  247. int count = dataCount.Count;
  248. float totalPage = (float)count / dto.PageSize;//总页数
  249. if (totalPage == 0) totalPage = 1;
  250. else totalPage = (int)Math.Ceiling((double)totalPage);
  251. ListViewBase<VisaInfoView> rst = new ListViewBase<VisaInfoView>();
  252. rst.DataList = infoViews;
  253. rst.DataCount = count;
  254. rst.CurrPageIndex = dto.PageIndex;
  255. rst.CurrPageSize = dto.PageSize;
  256. return result = new Result() { Code = 0, Msg = "查询成功!", Data = rst };
  257. }
  258. }
  259. }
  260. catch (Exception ex)
  261. {
  262. return result = new Result() { Code = -2, Msg = "未知错误" };
  263. throw;
  264. }
  265. return result;
  266. }
  267. /// <summary>
  268. /// 根据签证费用Id查询单条数据及c表数据
  269. /// </summary>
  270. /// <param name="dto"></param>
  271. /// <returns></returns>
  272. public async Task<Result> PostVisaById(PostVisaByIdDto dto)
  273. {
  274. var result = new Result() { Code = -2, Msg = "未知错误" };
  275. Grp_VisaInfo _VisaInfo = _sqlSugar.Queryable<Grp_VisaInfo>().First(a => a.Id == dto.Id && a.IsDel == 0);
  276. Grp_CreditCardPayment _CreditCardPayment = _sqlSugar.Queryable<Grp_CreditCardPayment>().First(a => a.CId == dto.Id && a.IsDel == 0 && a.CTable == 80);
  277. var data = new
  278. {
  279. VisaInfo = _VisaInfo,
  280. CreditCardPayment = _CreditCardPayment
  281. };
  282. return result = new Result() { Code = 0, Msg = "暂无数据!", Data = data };
  283. }
  284. /// <summary>
  285. /// 签证费用录入操作(Status:1.新增,2.修改)
  286. /// </summary>
  287. /// <param name="dto"></param>
  288. /// <returns></returns>
  289. public async Task<Result> OpVisaPrice(OpVisaPriceDto dto)
  290. {
  291. Result result = new Result() { Code = -2, Msg = "未知错误" };
  292. BeginTran();
  293. int id = 0;
  294. Grp_VisaInfo grp_Visa1 = _mapper.Map<Grp_VisaInfo>(dto);
  295. grp_Visa1.Area = dto.Area;
  296. Grp_CreditCardPayment c = _mapper.Map<Grp_CreditCardPayment>(dto);
  297. c.PayMoney = dto.VisaPrice;
  298. c.PaymentCurrency = dto.VisaCurrency;
  299. c.Remark = dto.CRemark;
  300. c.PayPercentage = 100;
  301. c.CTable = 80;
  302. c.CId = dto.Id;
  303. c.IsAuditGM = 0;
  304. //2025-04-07 第四次更改 PayDId == 72(刷卡) IsPay == 1
  305. if (c.PayDId == 72) c.IsPay = 1;
  306. else c.IsPay = 0;
  307. c.RMBPrice = c.PayMoney;
  308. c.DayRate = 1.0000M;
  309. // 按汇率计算人名币费用
  310. var _TeamRate = _sqlSugar.Queryable<Grp_TeamRate>().First(a => a.DiId == dto.DiId && a.IsDel == 0 && a.CTable == 80);
  311. var currencyInfos = new List<CurrencyInfo>();
  312. if (_TeamRate != null)
  313. {
  314. Sys_SetData _SetData = _sqlSugar.Queryable<Sys_SetData>().First(a => a.IsDel == 0 && a.Id == c.PaymentCurrency);
  315. if (_SetData != null)
  316. {
  317. currencyInfos = CommonFun.GetCurrencyChinaToList(_TeamRate.Remark);
  318. CurrencyInfo CurrencyRate = currencyInfos.FirstOrDefault(a => a.CurrencyCode == _SetData.Name);
  319. if (CurrencyRate != null)
  320. {
  321. c.RMBPrice = c.PayMoney * Convert.ToDecimal(CurrencyRate.Rate);
  322. c.DayRate = CurrencyRate.Rate;
  323. }
  324. }
  325. }
  326. //todo:费用标准需更改为 Res_VisaFeeStandardDetails
  327. #region 自动审核处理
  328. var isAutoAudit = false;
  329. if (dto.IsThird == 0)
  330. {
  331. var area = dto.Area;
  332. var feeType = 0; // 0 因公 1 因私
  333. if (area.Contains("(因公)"))
  334. {
  335. feeType = 0;
  336. area = area.Replace("(因公)", "");
  337. }
  338. else if (area.Contains("(因私)"))
  339. {
  340. feeType = 1;
  341. area = area.Replace("(因私)", "");
  342. }
  343. var visaFeeInfo = _sqlSugar.Queryable<Res_VisaFeeStandard>()
  344. .Where(x => x.IsDel == 0 && x.FeeType == feeType && x.Country.Equals(area))
  345. .First();
  346. if (visaFeeInfo != null)
  347. {
  348. //因公默认四川省
  349. int provinceId = 122;
  350. var groupNames = await _groupRep.EnterExitCostGroupNameInfoAsync(dto.DiId);
  351. if (groupNames != null)
  352. {
  353. provinceId = groupNames.ProvinceId;
  354. }
  355. var visaDetails = _sqlSugar.Queryable<Res_VisaFeeStandardDetails>()
  356. .Where(x => x.IsDel == 0 && x.ParentId == visaFeeInfo.Id && x.ProvinceId == provinceId)
  357. .First();
  358. decimal visaFeePrice = 0.00M;
  359. if (dto.AgencyFeeType == 1)
  360. {
  361. if (!visaDetails.IsVisaExemptionSmall) visaFeePrice += visaDetails.SmallVisaPrice;
  362. visaFeePrice += visaDetails.SmallAgencyFee;
  363. }
  364. else if (dto.AgencyFeeType == 2)
  365. {
  366. if (!visaDetails.IsVisaExemptionLarge) visaFeePrice += visaDetails.LargeVisaPrice;
  367. visaFeePrice += visaDetails.LargeAgencyFee;
  368. }
  369. var visaFeeTotalPrice = visaFeePrice * dto.VisaNumber;
  370. if (c.RMBPrice <= visaFeeTotalPrice)
  371. {
  372. isAutoAudit = true;
  373. c.IsAuditGM = 3;
  374. c.AuditGMOperate = 4; //系统管理员
  375. c.AuditGMDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  376. }
  377. }
  378. }
  379. #endregion
  380. if (dto.Status == 1)//添加
  381. {
  382. var grp_Visa = _sqlSugar.Queryable<Grp_VisaInfo>()
  383. .First(a => a.IsDel == 0 &&
  384. a.DIId == dto.DiId &&
  385. a.VisaDescription.Equals(dto.VisaDescription) &&
  386. a.VisaCurrency == dto.VisaCurrency &&
  387. a.VisaPrice == dto.VisaPrice &&
  388. a.VisaClient == dto.VisaClient
  389. );
  390. if (grp_Visa != null)
  391. {
  392. return result = new Result() { Code = -1, Msg = "该笔费用已存在,请勿重复添加!" };
  393. }
  394. else
  395. {
  396. id = await AddAsyncReturnId(grp_Visa1);
  397. if (id != 0)
  398. {
  399. c.CId = id;
  400. int cId = await _sqlSugar.Insertable(c).ExecuteReturnIdentityAsync();
  401. if (cId != 0)
  402. {
  403. var data = new { ccpId = cId, sign = 1 };
  404. result = new Result() { Code = 0, Msg = "添加成功!", Data = data };
  405. }
  406. else
  407. {
  408. RollbackTran();
  409. result = new Result() { Code = -1, Msg = "添加失败!" };
  410. }
  411. }
  412. else
  413. {
  414. RollbackTran();
  415. result = new Result() { Code = -1, Msg = "添加失败,请稍后重试!" };
  416. }
  417. }
  418. }
  419. else if (dto.Status == 2)//修改
  420. {
  421. bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Grp_VisaInfo
  422. {
  423. VisaClient = grp_Visa1.VisaClient,
  424. Area = grp_Visa1.Area,
  425. AgencyFeeType = grp_Visa1.AgencyFeeType,
  426. VisaPrice = grp_Visa1.VisaPrice,
  427. VisaCurrency = grp_Visa1.VisaCurrency,
  428. IsThird = grp_Visa1.IsThird,
  429. PassengerType = grp_Visa1.PassengerType,
  430. VisaNumber = grp_Visa1.VisaNumber,
  431. VisaFreeNumber = grp_Visa1.VisaFreeNumber,
  432. Remark = dto.Remark,
  433. VisaDescription = dto.VisaDescription
  434. });
  435. if (res)
  436. {
  437. int CTable = await _sqlSugar.Updateable<Grp_CreditCardPayment>()
  438. .Where(a => a.CId == grp_Visa1.Id && a.CTable == 80)
  439. .SetColumns(a => new Grp_CreditCardPayment
  440. {
  441. PayDId = dto.PayDId,
  442. IsPay = c.IsPay,
  443. PayMoney = c.PayMoney,
  444. PaymentCurrency = c.PaymentCurrency,
  445. Payee = c.Payee,
  446. OrbitalPrivateTransfer = c.OrbitalPrivateTransfer,
  447. DayRate = c.DayRate,
  448. RMBPrice = c.RMBPrice,
  449. ConsumptionPatterns = c.ConsumptionPatterns,
  450. ConsumptionDate = c.ConsumptionDate,
  451. CTDId = c.CTDId,
  452. CompanyBankNo = c.CompanyBankNo,
  453. OtherBankName = c.OtherBankName,
  454. OtherSideNo = c.OtherSideNo,
  455. OtherSideName = c.OtherSideName,
  456. BankNo = c.BankNo,
  457. CardholderName = c.CardholderName,
  458. Remark = c.Remark,
  459. })
  460. .SetColumnsIF(isAutoAudit, x => x.IsAuditGM == 3)
  461. .SetColumnsIF(isAutoAudit, x => x.AuditGMOperate == 4)
  462. .SetColumnsIF(isAutoAudit, x => x.AuditGMDate == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
  463. .ExecuteCommandAsync();
  464. var ccp = Query<Grp_CreditCardPayment>(a => a.CId == grp_Visa1.Id && a.CTable == 80).First();
  465. var data = new { ccpId = ccp.Id, sign = 2 };
  466. result = new Result() { Code = 0, Msg = "修改成功!", Data = data };
  467. }
  468. else
  469. {
  470. RollbackTran();
  471. result = new Result() { Code = -1, Msg = "修改失败,请稍后重试!" };
  472. }
  473. }
  474. CommitTran();
  475. return result;
  476. }
  477. }
  478. }