DecreasePaymentsRepository.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. using AutoMapper;
  2. using NPOI.SS.Formula.Functions;
  3. using OASystem.Domain;
  4. using OASystem.Domain.Dtos.Groups;
  5. using OASystem.Domain.Entities.Financial;
  6. using OASystem.Domain.Entities.Groups;
  7. using OASystem.Domain.Entities.Resource;
  8. using OASystem.Domain.ViewModels.Financial;
  9. using OASystem.Domain.ViewModels.Groups;
  10. using OASystem.Domain.ViewModels.QiYeWeChat;
  11. using OASystem.Domain.ViewModels.Resource;
  12. using OASystem.Infrastructure.Repositories.System;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Runtime.Intrinsics.Arm;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. namespace OASystem.Infrastructure.Repositories.Groups
  20. {
  21. public class DecreasePaymentsRepository : BaseRepository<Grp_DecreasePayments, Grp_DecreasePayments>
  22. {
  23. private readonly IMapper _mapper;
  24. private readonly TeamRateRepository _teamRateRep;
  25. public DecreasePaymentsRepository(SqlSugarClient sqlSugar, IMapper mapper, TeamRateRepository teamRateRep)
  26. : base(sqlSugar)
  27. {
  28. this._mapper = mapper;
  29. _teamRateRep = teamRateRep;
  30. }
  31. /// <summary>
  32. /// 根据团组Id查询数据
  33. /// </summary>
  34. /// <param name="dto"></param>
  35. /// <returns></returns>
  36. public async Task<Result> DecreasePaymentsList(DecreasePaymentsListDto dto)
  37. {
  38. Result result = new Result() { Code = -2, Msg = "未知错误" };
  39. try
  40. {
  41. //string UserId = "0";
  42. //List<Grp_GroupsTaskAssignment> gtaUIdList = _sqlSugar.Queryable<Grp_GroupsTaskAssignment>().Where(a => a.DIId == dto.DiId && a.IsDel == 0 && a.CTId == 98).ToList();
  43. //UserId = string.Join(",", gtaUIdList.Select(it => it.UId).ToList());
  44. //string sql = string.Format(@"select Id,DiId,PriceName,Price,(select name from Sys_SetData where id=s.Currency) as Currency,FilePath,
  45. // (select CnName from Sys_Users where Id=s.CreateUserId) as 'CreateUserName',CreateTime,(select IsAuditGM from
  46. // Grp_CreditCardPayment where CTable=98 and CId=s.Id and DiId = {0} and IsDel=0) as 'isAudit' from Grp_DecreasePayments s
  47. // where DIID={0} and IsDel=0 and s.CreateUserId in ({1}) ", dto.DiId, dto.UserId);
  48. string sqlWhere = "";
  49. if (dto.IsPaySign != -1) {
  50. sqlWhere += string.Format(@" And ccp.IsPay = {0} ", dto.IsPaySign);
  51. }
  52. //雷怡 2024-5-8 15:14 新增View字段 CreateTime
  53. string sql = string.Format(@" Select gdp.Id,gdp.DiId,gdp.PriceName,gdp.Price,sd1.[Name] as Currency,gdp.FilePath,su.CnName as CreateUserName,
  54. ccp.IsAuditGM as isAudit,ccp.IsPay,gdp.CreateTime
  55. From Grp_DecreasePayments as gdp With(Nolock) Left Join Grp_CreditCardPayment as ccp With(Nolock) On gdp.Id = ccp.CId
  56. Left Join Sys_SetData as sd1 On gdp.Currency = sd1.Id
  57. Left Join Sys_Users as su On gdp.CreateUserId = su.Id
  58. Where gdp.DiId = {0} And ccp.CTable = 98 {2} And ccp.IsDel = 0 And gdp.IsDel = 0 And gdp.CreateUserId in ({1}) ", dto.DiId, dto.UserId, sqlWhere);
  59. List<DecreasePaymentsView> _DecreasePayments = await _sqlSugar.SqlQueryable<DecreasePaymentsView>(sql).ToListAsync();
  60. if (_DecreasePayments.Count > 0)
  61. {
  62. result = new Result() { Code = 0, Msg = "查询成功!", Data = _DecreasePayments };
  63. }
  64. else
  65. {
  66. result = new Result() { Code = 0, Msg = "暂无数据!", Data = _DecreasePayments };
  67. }
  68. }
  69. catch (Exception ex)
  70. {
  71. result = new Result() { Code = -2, Msg = ex.Message };
  72. }
  73. return result;
  74. }
  75. public async Task<Result> DecreasePaymentsSelect(DecreasePaymentsDto dto)
  76. {
  77. Result result = new Result() { Code = -2, Msg = "未知错误" };
  78. #region 团组下拉框
  79. List<Grp_GroupsTaskAssignment> grp_GroupsTaskAssignment = Query<Grp_GroupsTaskAssignment>(a => a.IsDel == 0 && a.UId == dto.UserId && a.CTId == dto.CTId).ToList();
  80. string DiId = "";
  81. List<DecreasePaymentGroupView> _Delegations = new List<DecreasePaymentGroupView>();
  82. if (grp_GroupsTaskAssignment.Count > 0)
  83. {
  84. foreach (var item in grp_GroupsTaskAssignment)
  85. {
  86. DiId += item.DIId + ",";
  87. }
  88. if (DiId != "0")
  89. {
  90. DiId = DiId.Substring(0, DiId.Length - 1);
  91. }
  92. string sql = string.Format($@"Select
  93. di.Id,
  94. di.TeamName,
  95. di.ClientUnit,
  96. di.ClientName,
  97. di.TourCode,
  98. di.TeamDid,
  99. sd.[Name] As TeamTypeName,
  100. di.VisitCountry,
  101. di.VisitStartDate,
  102. di.VisitEndDate,
  103. di.VisitDays,
  104. di.VisitPNumber,
  105. di.CreateTime
  106. From Grp_DelegationInfo di With(NoLock)
  107. Left Join Sys_SetData sd On di.TeamDid = sd.Id
  108. Where di.Id in({DiId}) and di.IsDel=0
  109. Order By di.CreateTime Desc");
  110. //DecreasePaymentGroupView
  111. _Delegations = _sqlSugar.SqlQueryable<DecreasePaymentGroupView>(sql).ToList();
  112. }
  113. #endregion
  114. #region 其他下拉框查询
  115. //支付方式
  116. List<Sys_SetData> Payment = _sqlSugar.Queryable<Sys_SetData>().Where(a => a.STid == 14 && a.IsDel == 0).ToList();
  117. List<SetDataInfoView> _Payment = _mapper.Map<List<SetDataInfoView>>(Payment);
  118. //供应商 supplier
  119. List<Sys_SetData> supplier = _sqlSugar.Queryable<Sys_SetData>().Where(a => a.STid == 21 && a.IsDel == 0).ToList();
  120. List<SetDataInfoView> _supplier = _mapper.Map<List<SetDataInfoView>>(supplier);
  121. if (_supplier.Count > 0)
  122. {
  123. var d1 = _supplier.Find(it => it.Name.Equals("策划 公司"));
  124. if (d1 != null) _supplier.Remove(d1);
  125. var d2 = _supplier.Find(it => it.Name.Equals("礼仪"));
  126. if (d2 != null) _supplier.Remove(d2);
  127. var d3 = _supplier.Find(it => it.Name.Equals("日常办公用品"));
  128. if (d3 != null) _supplier.Remove(d3);
  129. var d4 = _supplier.Find(it => it.Name.Equals("其他"));
  130. if (d4 != null)
  131. {
  132. _supplier.Remove(d4);
  133. _supplier.Add(d4);
  134. }
  135. }
  136. List<dynamic> _supplierArea = new List<dynamic>() {
  137. new { id=0,name = "未选择" },
  138. new { id=1,name = "国内" },
  139. new { id=2,name = "国外" },
  140. };
  141. #endregion
  142. var data = new
  143. {
  144. payment = _Payment,
  145. groupName = _Delegations,
  146. supplier = _supplier,
  147. supplierArea = _supplierArea
  148. };
  149. return result = new Result() { Code = 0, Msg = "查询成功!", Data = data };
  150. }
  151. public async Task<Result> OpDecreasePayments(DecreasePaymentsOpDto dto)
  152. {
  153. Result result = new Result() { Code = -2, Msg = "未知错误" };
  154. BeginTran();
  155. try
  156. {
  157. int id = 0;
  158. Grp_DecreasePayments grp_Decrease = _mapper.Map<Grp_DecreasePayments>(dto);
  159. //处理费用总计
  160. if (grp_Decrease.FeeTotal == 0.00M)
  161. {
  162. grp_Decrease.FeeTotal = grp_Decrease.Price * grp_Decrease.Price;
  163. }
  164. List<TeamRateDescAddCurrencyIdView> teamRates = await _teamRateRep.PostGroupTeamRateItemByDiIdAndCTableId(1, dto.DiId, 98);
  165. if (dto.Status == 1)//添加
  166. {
  167. string selectSql = string.Format(@"select * from Grp_DecreasePayments where PriceName='{0}' and IsDel={1} and DiId={2}"
  168. , dto.PriceName, 0, dto.DiId);
  169. var DecreasePayments = await _sqlSugar.SqlQueryable<Grp_DecreasePayments>(selectSql).FirstAsync();//查询是否存在
  170. if (DecreasePayments != null)
  171. {
  172. return new Result() { Code = -1, Msg = "该数据已存在,请勿重复添加!" };
  173. }
  174. else//不存在,可添加
  175. {
  176. id = await AddAsyncReturnId(grp_Decrease);
  177. if (id < 1)
  178. {
  179. RollbackTran();
  180. return new Result() { Code = -1, Msg = "添加失败!" };
  181. }
  182. //进行C表添加
  183. Grp_CreditCardPayment C = new Grp_CreditCardPayment();
  184. C.PayDId = dto.PayDId;
  185. C.ConsumptionPatterns = "";
  186. C.ConsumptionDate = "";
  187. C.CTDId = 0;
  188. C.BankNo = "";
  189. C.CardholderName = "";
  190. C.PayMoney = grp_Decrease.FeeTotal;
  191. C.PaymentCurrency = grp_Decrease.Currency;
  192. //当天汇率
  193. //if (!string.IsNullOrEmpty(hfRate.Value))
  194. // C.DayRate = hfRate.Value;
  195. //else
  196. //C.DayRate = "";
  197. C.CompanyBankNo = "";
  198. C.OtherBankName = "";
  199. C.OtherSideNo = "";
  200. C.OtherSideName = "";
  201. C.Remark = "";
  202. C.CreateUserId = grp_Decrease.CreateUserId;
  203. C.MFOperator = 0;
  204. C.MFOperatorDate = "";
  205. C.IsAuditDM = 0;
  206. C.AuditDMOperate = 0;
  207. C.AuditDMDate = "";
  208. C.IsAuditMF = 0;
  209. C.AuditMFOperate = 0;
  210. C.AuditMFDate = "";
  211. C.IsAuditGM = 0;
  212. C.AuditGMOperate = 21;
  213. C.AuditGMDate = "";
  214. if (C.PayDId == 72) C.IsPay = 1;
  215. else C.IsPay = 0;
  216. C.DIId = grp_Decrease.DiId;
  217. C.CId = id;
  218. C.CTable = 98;
  219. C.PayPercentage = 100;
  220. C.PayThenMoney = 0;
  221. C.PayPercentageOld = 0;
  222. C.PayThenMoneyOld = 0;
  223. C.UpdateDate = "";
  224. C.Payee = dto.Payee;
  225. C.OrbitalPrivateTransfer = dto.OrbitalPrivateTransfer;
  226. C.ExceedBudget = 0;
  227. //C.RMBPrice = 0.00f;
  228. //设置该团组的汇率
  229. TeamRateDescAddCurrencyIdView teamRate = teamRates.Where(it => it.CurrencyId == grp_Decrease.Currency).FirstOrDefault();
  230. //Grp_TeamRate _TeamRate = _sqlSugar.Queryable<Grp_TeamRate>().First(a => a.DiId == grp_Decrease.DiId && a.IsDel == 0 && a.CTable == 98);
  231. if (teamRate != null)
  232. {
  233. if (teamRate.CurrencyCode.Equals("CNY"))
  234. {
  235. C.DayRate = 1.0000M;
  236. C.RMBPrice = C.PayMoney;
  237. }
  238. else
  239. {
  240. C.DayRate = teamRate.Rate;
  241. C.RMBPrice = C.PayMoney * C.DayRate;
  242. }
  243. }
  244. else
  245. {
  246. RollbackTran();
  247. return new Result() { Code = -1, Msg = $"添加失败!团组汇率未设置{_sqlSugar.Queryable<Sys_SetData>().First(it => it.IsDel == 0 && it.Id == grp_Decrease.Currency)?.Name ?? ""}该币种汇率!" };
  248. }
  249. int cId = await _sqlSugar.Insertable(C).ExecuteReturnIdentityAsync();
  250. if (cId != 0)
  251. {
  252. CommitTran();
  253. var data = new { ccpId = cId, sign = 1 };
  254. return new Result() { Code = 0, Msg = "添加成功!", Data = data };
  255. }
  256. else
  257. {
  258. RollbackTran();
  259. return new Result() { Code = -1, Msg = "添加失败!" };
  260. }
  261. }
  262. }
  263. else if (dto.Status == 2)
  264. {
  265. bool res = await UpdateAsync(a => a.Id == grp_Decrease.Id, a => new Grp_DecreasePayments
  266. {
  267. DiId = grp_Decrease.DiId,
  268. SupplierArea = grp_Decrease.SupplierArea,
  269. SupplierTypeId = grp_Decrease.SupplierTypeId,
  270. PriceName = grp_Decrease.PriceName,
  271. Price = grp_Decrease.Price,
  272. Quantity = grp_Decrease.Quantity,
  273. FeeTotal = grp_Decrease.FeeTotal,
  274. Currency = grp_Decrease.Currency,
  275. FilePath = grp_Decrease.FilePath,
  276. Remark = grp_Decrease.Remark,
  277. });
  278. if (res)
  279. {
  280. Grp_CreditCardPayment grp_CreditCardPayment = _sqlSugar.Queryable<Grp_CreditCardPayment>().First(a => a.CId == grp_Decrease.Id && a.CTable == 98 && a.CId == grp_Decrease.Id && a.IsDel == 0);
  281. if (grp_CreditCardPayment != null)
  282. {
  283. //设置该团组的汇率
  284. TeamRateDescAddCurrencyIdView teamRate = teamRates.Where(it => it.CurrencyId == grp_Decrease.Currency).FirstOrDefault();
  285. if (teamRate != null)
  286. {
  287. if (teamRate.CurrencyCode.Equals("CNY"))
  288. {
  289. grp_CreditCardPayment.DayRate = 1.0000M;
  290. grp_CreditCardPayment.RMBPrice = grp_CreditCardPayment.PayMoney;
  291. }
  292. else
  293. {
  294. grp_CreditCardPayment.DayRate = teamRate.Rate;
  295. grp_CreditCardPayment.RMBPrice = grp_CreditCardPayment.PayMoney * grp_CreditCardPayment.DayRate;
  296. }
  297. }
  298. else
  299. {
  300. RollbackTran();
  301. return new Result() { Code = -1, Msg = "修改失败!" };
  302. }
  303. int ispay = 0;
  304. if (dto.PayDId == 72) ispay = 1;
  305. int CTable = await _sqlSugar.Updateable<Grp_CreditCardPayment>()
  306. .Where(a => a.Id == grp_CreditCardPayment.Id)
  307. .SetColumns(a => new Grp_CreditCardPayment
  308. {
  309. PayDId = dto.PayDId,
  310. IsPay = ispay,
  311. PayMoney = grp_Decrease.FeeTotal,
  312. PaymentCurrency = grp_Decrease.Currency,
  313. Payee = dto.Payee,
  314. OrbitalPrivateTransfer = dto.OrbitalPrivateTransfer,
  315. DayRate = grp_CreditCardPayment.DayRate,
  316. RMBPrice = grp_CreditCardPayment.RMBPrice,
  317. }).ExecuteCommandAsync();
  318. if (CTable > 0)
  319. {
  320. CommitTran();
  321. var data = new { ccpId = grp_CreditCardPayment.Id, sign = 2 };
  322. return new Result() { Code = 0, Msg = "修改成功!", Data = data };
  323. }
  324. else
  325. {
  326. RollbackTran();
  327. return new Result() { Code = -1, Msg = "修改失败!" };
  328. }
  329. }
  330. else
  331. {
  332. RollbackTran();
  333. return new Result() { Code = -1, Msg = "修改失败!" };
  334. }
  335. }
  336. else
  337. {
  338. RollbackTran();
  339. return new Result() { Code = -1, Msg = "修改失败!" };
  340. }
  341. }
  342. }
  343. catch (Exception ex)
  344. {
  345. RollbackTran();
  346. return new Result() { Code = -2, Msg = ex.Message };
  347. }
  348. return result;
  349. }
  350. public async Task<Result> PostGroupNameAndEasy(DecreasePaymentsDto dto)
  351. {
  352. Result result = new Result() { Code = -2, Msg = "未知错误" };
  353. try
  354. {
  355. #region 团组下拉框
  356. List<Grp_GroupsTaskAssignment> grp_GroupsTaskAssignment = Query<Grp_GroupsTaskAssignment>(a => a.IsDel == 0 && a.UId == dto.UserId && a.CTId == dto.CTId).ToList();
  357. string DiId = "";
  358. foreach (var item in grp_GroupsTaskAssignment)
  359. {
  360. DiId += item.DIId + ",";
  361. }
  362. if (!string.IsNullOrWhiteSpace(DiId))
  363. {
  364. DiId = DiId.Substring(0, DiId.Length - 1);
  365. }
  366. else
  367. {
  368. DiId = "0";
  369. }
  370. string sql = string.Format(@"select Id,TourCode,TeamName,ClientName,VisitStartDate,VisitEndDate,VisitCountry,VisitDays,VisitPNumber from Grp_DelegationInfo where Id in({0}) and IsDel={1}", DiId, 0);
  371. List<DelegationEasyView> grp_Delegations = _sqlSugar.SqlQueryable<DelegationEasyView>(sql).ToList();
  372. if (grp_Delegations.Count != 0)
  373. {
  374. int count = grp_Delegations.Count;
  375. float totalPage = (float)count / dto.PageSize;//总页数
  376. if (totalPage == 0) totalPage = 1;
  377. else totalPage = (int)Math.Ceiling((double)totalPage);
  378. List<DelegationEasyView> delegationEasyViews = new List<DelegationEasyView>();
  379. for (int i = 0; i < dto.PageSize; i++)
  380. {
  381. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  382. if (RowIndex < grp_Delegations.Count)
  383. {
  384. delegationEasyViews.Add(grp_Delegations[RowIndex]);
  385. }
  386. else
  387. {
  388. break;
  389. }
  390. }
  391. ListViewBase<DelegationEasyView> rst = new ListViewBase<DelegationEasyView>();
  392. rst.DataList = delegationEasyViews;
  393. rst.DataCount = count;
  394. rst.CurrPageIndex = dto.PageIndex;
  395. rst.CurrPageSize = dto.PageSize;
  396. return result = new Result() { Code = 0, Msg = "查询成功!", Data = rst };
  397. }
  398. else
  399. {
  400. ListViewBase<DelegationEasyView> rst = new ListViewBase<DelegationEasyView>();
  401. rst.DataList = new List<DelegationEasyView>();
  402. return result = new Result() { Code = 0, Msg = "暂无数据!", Data = rst };
  403. }
  404. #endregion
  405. }
  406. catch (Exception ex)
  407. {
  408. return result = new Result() { Code = -2, Msg = "程序错误" };
  409. throw;
  410. }
  411. }
  412. /// <summary>
  413. /// 根据团组增减款项表Id查询数据
  414. /// </summary>
  415. /// <param name="dto"></param>
  416. /// <returns></returns>
  417. /// <exception cref="NotImplementedException"></exception>
  418. public async Task<Result> QueryDecreasePaymentsById(DecreasePaymentsByIdDto dto)
  419. {
  420. Result result = new Result() { Code = -2, Msg = "程序错误" };
  421. try
  422. {
  423. Grp_DecreasePayments grp_Decrease = _sqlSugar.Queryable<Grp_DecreasePayments>().First(a => a.Id == dto.Id && a.IsDel == 0);
  424. Grp_CreditCardPayment grp_CreditCard = _sqlSugar.Queryable<Grp_CreditCardPayment>().First(a => a.CId == dto.Id && a.IsDel == 0 && a.CTable == 98);
  425. var data = new
  426. {
  427. _Decrease = grp_Decrease,
  428. _CreditCard = grp_CreditCard,
  429. };
  430. result = new Result() { Code = 0, Msg = "查询成功!", Data = data };
  431. }
  432. catch (Exception ex)
  433. {
  434. result = new Result() { Code = -2, Msg = "程序错误" };
  435. throw;
  436. }
  437. return result;
  438. }
  439. /// <summary>
  440. /// Del
  441. /// </summary>
  442. /// <param name="dto"></param>
  443. /// <returns></returns>
  444. /// <exception cref="NotImplementedException"></exception>
  445. public async Task<Result> _Del(int id, int userId)
  446. {
  447. Result result = new Result() { Code = -2, Msg = "删除失败!" };
  448. _sqlSugar.BeginTran();
  449. var del = await _sqlSugar.Updateable<Grp_DecreasePayments>()
  450. .SetColumns(it => new Grp_DecreasePayments()
  451. {
  452. IsDel = 1,
  453. DeleteUserId = userId,
  454. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  455. }).Where(it => it.Id == id)
  456. .ExecuteCommandAsync();
  457. if (del > 0)
  458. {
  459. var del1 = await _sqlSugar.Updateable<Grp_CreditCardPayment>()
  460. .SetColumns(a => new Grp_CreditCardPayment()
  461. {
  462. IsDel = 1,
  463. DeleteUserId = userId,
  464. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  465. })
  466. .Where(a => a.CId == id && a.CTable == 98)
  467. .ExecuteCommandAsync();
  468. if (del1 > 0)
  469. {
  470. _sqlSugar.CommitTran();
  471. result.Code = 0;
  472. result.Msg = "删除成功!";
  473. return result;
  474. }
  475. }
  476. _sqlSugar.RollbackTran();
  477. return result;
  478. }
  479. }
  480. }