VisaPriceRepository.cs 23 KB

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