IFinance_ReconcileDataRepository.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using SMP.Model.Finance;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using XYY.Core.Standard.Data.Infrastructure;
  8. using Dapper;
  9. using SMP.Model.Dto.Reconcile;
  10. using XYY.Common.Standard;
  11. using SMP.Model.Enums;
  12. namespace SMP.Data.Finance
  13. {
  14. public interface IFinance_ReconcileDataRepository : IBaseRepository<Finance_ReconcileData>
  15. {
  16. Task<int> InsertConcileData(string billName, int mappingSolutionId);
  17. Task<int> RemoveReconcileData(string billName, int mappingSolutionId);
  18. Task<int> RefreshConcileData(string billName, int mappingSolutionId);
  19. Task<IEnumerable<ReconcileSumAmount>> GetSumAmount(string billName, int reconcileSolutionId);
  20. Task<PageResult<Finance_ReconcileData>> QueryServiceBillExpensePage(string billName, int mappingSolutionId, ReconcileDiffType diffType, decimal diffAccurate, List<string> numbers, int pageIndex, int pageLength);
  21. }
  22. public class Finance_ReconcileDataRepository : BaseRepository<Finance_ReconcileData>, IFinance_ReconcileDataRepository
  23. {
  24. public Finance_ReconcileDataRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
  25. {
  26. }
  27. public Task<IEnumerable<ReconcileSumAmount>> GetSumAmount(string billName, int mappingSolutionId)
  28. {
  29. string sql = "select BillDataName,sum(isnull(BillData,0)) as BillSumAmount,SystemDataName as ExpenseName,sum(SystemData) as ExpenseSumAmount from Finance_ReconcileData where BillName=@BillName and MappingSolutionId=@MappingSolutionId group by BillDataName,SystemDataName";
  30. return _unitOfWork.Connection.QueryAsync<ReconcileSumAmount>(sql, new { BillName = billName, MappingSolutionId = mappingSolutionId }, transaction: _unitOfWork.Transaction);
  31. }
  32. public Task<int> RemoveReconcileData(string billName, int mappingSolutionId)
  33. {
  34. string sql = "delete from Finance_ReconcileData where BillName=@BillName and MappingSolutionId=@MappingSolutionId";
  35. return _unitOfWork.Connection.ExecuteAsync(sql, new { BillName = billName, MappingSolutionId = mappingSolutionId }, transaction: _unitOfWork.Transaction);
  36. }
  37. public Task<int> InsertConcileData(string billName, int mappingSolutionId)
  38. {
  39. string sql = @"insert into Finance_ReconcileData(BillName,MappingSolutionId,Number,BillDataName,BillData,ExpenseId,SystemDataName ,SystemData,Sort,Diff,CreateTime,Version)
  40. select BillName,MappingSolutionId,BillNumber as Number,BillDataName,BillData,ExpenseId, ExpenseName as SystemDataName,AmountinOriginalCurrency as SystemData,Sort,(isnull(BillData,0) - isnull(AmountinOriginalCurrency,0)) as Diff,getdate() as CreateTime,'2022-05-17' as Version
  41. from View_ServiceBillOrderExpense
  42. where BillName=@BillName and MappingSolutionId=@MappingSolutionId
  43. union
  44. select BillName,MappingSolutionId,Number,BillDataName,BillData,ExpenseId,ExpenseName as SystemDataName,SumOriginAmount as SystemData,Sort,(isnull(BillData,0) - isnull(SumOriginAmount,0)) as Diff,getdate() as CreateTime,'2022-05-17' as Version
  45. from View_ServiceBillLadingBillExpense
  46. where BillName=@BillName and MappingSolutionId=@MappingSolutionId and ExpenseName is not null";
  47. return _unitOfWork.Connection.ExecuteAsync(sql, new { BillName = billName, MappingSolutionId = mappingSolutionId }, transaction: _unitOfWork.Transaction);
  48. }
  49. public async Task<PageResult<Finance_ReconcileData>> QueryServiceBillExpensePage(string billName, int mappingSolutionId, ReconcileDiffType diffType, decimal diffAccurate, List<string> numbers, int pageIndex, int pageLength)
  50. {
  51. PageResult<Finance_ReconcileData> pageResult = new PageResult<Finance_ReconcileData>();
  52. pageResult.PageIndex = pageIndex;
  53. pageResult.PageSize = pageLength;
  54. string diffCondition = null;
  55. if (diffType == ReconcileDiffType.无差异)
  56. {
  57. diffCondition = " and abs(Diff) < @DiffAccurate ";
  58. }
  59. else if (diffType == ReconcileDiffType.有差异)
  60. {
  61. diffCondition = " and abs(Diff) >= @DiffAccurate ";
  62. }
  63. else if (diffType == ReconcileDiffType.仅正差异)
  64. {
  65. diffCondition = " and Diff >= @DiffAccurate ";
  66. }
  67. else if (diffType == ReconcileDiffType.仅负差异)
  68. {
  69. diffCondition = " and Diff <= @DiffAccurate ";
  70. }
  71. if (numbers?.Any() == true)
  72. {
  73. string sql = $"select * from Finance_ReconcileData where BillName=@BillName and MappingSolutionId=@MappingSolutionId {diffCondition} and Number in @Numbers";
  74. var result = await base._unitOfWork.Connection.QueryAsync<Finance_ReconcileData>(sql, param: new { BillName = billName, MappingSolutionId = mappingSolutionId, Numbers = numbers, DiffAccurate = diffAccurate }, transaction: base._unitOfWork.Transaction);
  75. if (result?.Any() == true)
  76. {
  77. pageResult.TotalCount = result.GroupBy(i => i.Number).Count();
  78. pageResult.Result = result;
  79. return pageResult;
  80. }
  81. else
  82. {
  83. pageResult.TotalCount = 0;
  84. pageResult.Result = new List<Finance_ReconcileData>();
  85. return pageResult;
  86. }
  87. }
  88. else
  89. {
  90. if (pageIndex <= 0)
  91. pageIndex = 1;
  92. if (pageLength <= 0)
  93. pageLength = 20;
  94. if (pageLength >= 2000)
  95. throw new Exception("最多支持2000条");
  96. int start = (pageIndex - 1) * pageLength;
  97. int stop = pageIndex * pageLength;
  98. string numberSql = @"with t as(
  99. select ROW_NUMBER() over( order by number) rid, number from Finance_ServiceBillResolveResult where BillName=@BillName group by number )
  100. select number from t
  101. where rid>=@Start and rid<=@Stop";
  102. var sortedNumbers = await base._unitOfWork.Connection.QueryAsync<string>(numberSql, param: new { BillName = billName, Start = start, Stop = stop }, transaction: base._unitOfWork.Transaction);
  103. if (sortedNumbers?.Any() == true)
  104. {
  105. string sql = $"select * from Finance_ReconcileData where BillName=@BillName and MappingSolutionId=@MappingSolutionId {diffCondition} and Number in @Numbers";
  106. var result = await base._unitOfWork.Connection.QueryAsync<Finance_ReconcileData>(sql, param: new { BillName = billName, Numbers = sortedNumbers, MappingSolutionId = mappingSolutionId, DiffAccurate = diffAccurate }, transaction: base._unitOfWork.Transaction);
  107. string countSql = @$"with t as(
  108. select Number from Finance_ReconcileData where BillName=@BillName {diffCondition} group by Number
  109. )
  110. select count(1) from t";
  111. var totalCount = await base._unitOfWork.Connection.ExecuteScalarAsync<int>(countSql, param: new { BillName = billName, MappingSolutionId = mappingSolutionId, DiffAccurate = diffAccurate }, transaction: base._unitOfWork.Transaction);
  112. pageResult.TotalCount = totalCount;
  113. pageResult.Result = result;
  114. return pageResult;
  115. }
  116. else
  117. {
  118. pageResult.TotalCount = 0;
  119. pageResult.Result = new List<Finance_ReconcileData>();
  120. return pageResult;
  121. }
  122. }
  123. }
  124. public async Task<int> RefreshConcileData(string billName, int mappingSolutionId)
  125. {
  126. await RemoveReconcileData(billName, mappingSolutionId);
  127. int count = await InsertConcileData(billName, mappingSolutionId);
  128. return count;
  129. }
  130. }
  131. }