GroupExtraCostRepository.cs 7.1 KB

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