VisaPriceRepository.cs 23 KB

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