GroupExtraCostRepository.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using AutoMapper;
  2. using OASystem.Domain.Entities.Financial;
  3. using OASystem.Domain.ViewModels.Financial;
  4. using OASystem.Domain.Entities.Groups;
  5. using OASystem.Domain;
  6. using OASystem.Infrastructure.Repositories.System;
  7. using OASystem.Domain.ViewModels.Groups;
  8. using Microsoft.Extensions.Logging;
  9. namespace OASystem.Infrastructure.Repositories.Financial
  10. {
  11. public class GroupExtraPriceResult
  12. {
  13. /// <summary>
  14. /// 剩余金额
  15. /// </summary>
  16. public decimal RemainingAmount { get; set; }
  17. /// <summary>
  18. /// 金额币种
  19. /// </summary>
  20. public int RemainingCurrency { get; set; }
  21. /// <summary>
  22. /// 剩余百分比
  23. /// </summary>
  24. public decimal RemainingPercentage { get; set; }
  25. /// <summary>
  26. /// 已用金额
  27. /// </summary>
  28. public decimal UsedAmount { get; set; }
  29. }
  30. /// <summary>
  31. /// 财务 - 团组超支费用
  32. /// </summary>
  33. public class GroupExtraCostRepository : BaseRepository<Fin_GroupExtraCost, Fin_GroupExtraCostView>
  34. {
  35. private readonly IMapper _mapper;
  36. private readonly SetDataRepository _setDataRepository;
  37. //添加日志
  38. private readonly ILogger<GroupExtraCostRepository> _logger;
  39. public GroupExtraCostRepository(SqlSugarClient sqlSugar, IMapper mapper, SetDataRepository setDataRepository, ILogger<GroupExtraCostRepository> logger)
  40. : base(sqlSugar)
  41. {
  42. _mapper = mapper;
  43. _setDataRepository = setDataRepository;
  44. _logger = logger;
  45. }
  46. public async Task<GroupExtraPriceResult> GetGroupExtraPriceByDiId(int diId)
  47. {
  48. var sumPriceList = await Query(x => x.DiId == diId && x.IsDel == 0)
  49. .LeftJoin<Grp_CreditCardPayment>((x, y) => x.Id == y.CId && y.CTable == 1015)
  50. .Where((x, y) => x.PriceSum > 0 && y.IsDel == 0)
  51. .Select((x, y) => new { x.Id, x.PriceSum, x.PriceCurrency, x.PriceType, y.DayRate })
  52. .ToListAsync();
  53. var upperLimit = _sqlSugar.Queryable<Grp_DelegationInfo>()
  54. .Where(x => x.Id == diId && x.IsDel == 0)
  55. .Select(x => new { x.ExtOverLimit, x.ExtOverCurrency })
  56. .First();
  57. if (upperLimit == null)
  58. {
  59. _logger.LogError("团组超支费用查询失败,团组不存在 diId: {diId}", diId);
  60. return new GroupExtraPriceResult()
  61. {
  62. RemainingAmount = 0,
  63. RemainingCurrency = 836, //RMB
  64. RemainingPercentage = 1,
  65. UsedAmount = 0
  66. };
  67. }
  68. if (!sumPriceList.Any())
  69. {
  70. _logger.LogError("团组超支费用查询失败,团组超支费用为0 diId: {diId}", diId);
  71. return new GroupExtraPriceResult()
  72. {
  73. RemainingAmount = upperLimit.ExtOverLimit,
  74. RemainingCurrency = upperLimit.ExtOverCurrency,
  75. RemainingPercentage = 1,
  76. UsedAmount = 0
  77. };
  78. }
  79. //剩余金额
  80. decimal remainingAmount = 0;
  81. //金额币种
  82. int remainingCurrency = 0;
  83. //剩余百分比
  84. decimal remainingPercentage = 0;
  85. //已用额度
  86. decimal usedAmount = 0;
  87. //获取超支部分的汇率
  88. var currRate = await _setDataRepository.PostCurrencyByDiid(diId, 1015, upperLimit.ExtOverCurrency);
  89. var currRateInfo = (currRate.Data as CurrencyInfo);
  90. if (currRateInfo == null)
  91. {
  92. currRateInfo = new CurrencyInfo()
  93. {
  94. Rate = 1,
  95. };
  96. }
  97. var currencyCount = sumPriceList
  98. .GroupBy(x => x.PriceCurrency)
  99. .ToList();
  100. if (currencyCount.Count > 1)
  101. {
  102. //全部转为RMB处理
  103. var rmbPrice = sumPriceList.Sum(x => x.PriceSum * x.DayRate);
  104. var upperLimitRmbPrice = upperLimit.ExtOverLimit * currRateInfo.Rate;
  105. remainingAmount = upperLimitRmbPrice - rmbPrice;
  106. remainingCurrency = 836; //RMB
  107. if (upperLimitRmbPrice > 0)
  108. {
  109. remainingPercentage = remainingAmount / upperLimitRmbPrice;
  110. }
  111. else
  112. {
  113. remainingPercentage = 1;
  114. }
  115. usedAmount = rmbPrice;
  116. }
  117. else
  118. {
  119. if (upperLimit.ExtOverCurrency == currencyCount.First().Key)
  120. {
  121. remainingAmount = upperLimit.ExtOverLimit - sumPriceList.Sum(x => x.PriceSum);
  122. remainingCurrency = upperLimit.ExtOverCurrency; //原币种
  123. if (upperLimit.ExtOverLimit > 0)
  124. {
  125. remainingPercentage = remainingAmount / upperLimit.ExtOverLimit;
  126. }
  127. else
  128. {
  129. remainingPercentage = 1;
  130. }
  131. usedAmount = sumPriceList.Sum(x => x.PriceSum);
  132. }
  133. else
  134. {
  135. //转换为RMB比较
  136. var rmbPrice = sumPriceList.Sum(x => x.PriceSum * x.DayRate);
  137. var upperLimitRmbPrice = upperLimit.ExtOverLimit * currRateInfo.Rate;
  138. remainingAmount = upperLimitRmbPrice - rmbPrice;
  139. remainingCurrency = 836; //RMB
  140. if (upperLimitRmbPrice > 0)
  141. {
  142. remainingPercentage = remainingAmount / upperLimitRmbPrice;
  143. }
  144. else
  145. {
  146. remainingPercentage = 1;
  147. }
  148. usedAmount = rmbPrice;
  149. }
  150. }
  151. remainingPercentage = Math.Round(remainingPercentage, 2);
  152. remainingAmount = Math.Round(remainingAmount, 2);
  153. usedAmount = Math.Round(usedAmount, 2);
  154. return new GroupExtraPriceResult()
  155. {
  156. RemainingAmount = remainingAmount,
  157. RemainingCurrency = remainingCurrency,
  158. RemainingPercentage = remainingPercentage,
  159. UsedAmount = usedAmount
  160. };
  161. }
  162. }
  163. }