DecreasePaymentsRepository.cs 23 KB

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