|
|
@@ -0,0 +1,180 @@
|
|
|
+using AutoMapper;
|
|
|
+using OASystem.Domain.Entities.Financial;
|
|
|
+using OASystem.Domain.ViewModels.Financial;
|
|
|
+using OASystem.Domain.Entities.Groups;
|
|
|
+using OASystem.Domain;
|
|
|
+using OASystem.Infrastructure.Repositories.System;
|
|
|
+using OASystem.Domain.ViewModels.Groups;
|
|
|
+using Microsoft.Extensions.Logging;
|
|
|
+
|
|
|
+namespace OASystem.Infrastructure.Repositories.Financial
|
|
|
+{
|
|
|
+
|
|
|
+ public class GroupExtraPriceResult
|
|
|
+ {
|
|
|
+ /// <summary>
|
|
|
+ /// 剩余金额
|
|
|
+ /// </summary>
|
|
|
+ public decimal RemainingAmount { get; set; }
|
|
|
+ /// <summary>
|
|
|
+ /// 金额币种
|
|
|
+ /// </summary>
|
|
|
+ public int RemainingCurrency { get; set; }
|
|
|
+ /// <summary>
|
|
|
+ /// 剩余百分比
|
|
|
+ /// </summary>
|
|
|
+ public decimal RemainingPercentage { get; set; }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 已用金额
|
|
|
+ /// </summary>
|
|
|
+ public decimal UsedAmount { get; set; }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 财务 - 团组超支费用
|
|
|
+ /// </summary>
|
|
|
+ public class GroupExtraCostRepository : BaseRepository<Fin_GroupExtraCost, Fin_GroupExtraCostView>
|
|
|
+ {
|
|
|
+ private readonly IMapper _mapper;
|
|
|
+
|
|
|
+ private readonly SetDataRepository _setDataRepository;
|
|
|
+
|
|
|
+ //添加日志
|
|
|
+ private readonly ILogger<GroupExtraCostRepository> _logger;
|
|
|
+
|
|
|
+ public GroupExtraCostRepository(SqlSugarClient sqlSugar, IMapper mapper, SetDataRepository setDataRepository, ILogger<GroupExtraCostRepository> logger)
|
|
|
+ : base(sqlSugar)
|
|
|
+ {
|
|
|
+ _mapper = mapper;
|
|
|
+ _setDataRepository = setDataRepository;
|
|
|
+ _logger = logger;
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task<GroupExtraPriceResult> GetGroupExtraPriceByDiId(int diId)
|
|
|
+ {
|
|
|
+ var sumPriceList = await Query(x => x.DiId == diId && x.IsDel == 0)
|
|
|
+ .LeftJoin<Grp_CreditCardPayment>((x, y) => x.Id == y.CId && y.CTable == 1015)
|
|
|
+ .Where((x, y) => x.PriceSum > 0 && y.IsDel == 0)
|
|
|
+ .Select((x, y) => new { x.Id, x.PriceSum, x.PriceCurrency, x.PriceType, y.DayRate })
|
|
|
+ .ToListAsync();
|
|
|
+
|
|
|
+ var upperLimit = _sqlSugar.Queryable<Grp_DelegationInfo>()
|
|
|
+ .Where(x => x.Id == diId && x.IsDel == 0)
|
|
|
+ .Select(x => new { x.ExtOverLimit, x.ExtOverCurrency })
|
|
|
+ .First();
|
|
|
+
|
|
|
+ if (upperLimit == null)
|
|
|
+ {
|
|
|
+ _logger.LogError("团组超支费用查询失败,团组不存在 diId: {diId}", diId);
|
|
|
+ return new GroupExtraPriceResult()
|
|
|
+ {
|
|
|
+ RemainingAmount = 0,
|
|
|
+ RemainingCurrency = 836, //RMB
|
|
|
+ RemainingPercentage = 1,
|
|
|
+ UsedAmount = 0
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!sumPriceList.Any())
|
|
|
+ {
|
|
|
+ _logger.LogError("团组超支费用查询失败,团组超支费用为0 diId: {diId}", diId);
|
|
|
+ return new GroupExtraPriceResult()
|
|
|
+ {
|
|
|
+ RemainingAmount = upperLimit.ExtOverLimit,
|
|
|
+ RemainingCurrency = upperLimit.ExtOverCurrency,
|
|
|
+ RemainingPercentage = 1,
|
|
|
+ UsedAmount = 0
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ //剩余金额
|
|
|
+ decimal remainingAmount = 0;
|
|
|
+ //金额币种
|
|
|
+ int remainingCurrency = 0;
|
|
|
+ //剩余百分比
|
|
|
+ decimal remainingPercentage = 0;
|
|
|
+ //已用额度
|
|
|
+ decimal usedAmount = 0;
|
|
|
+
|
|
|
+ //获取超支部分的汇率
|
|
|
+ var currRate = await _setDataRepository.PostCurrencyByDiid(diId, 1015, upperLimit.ExtOverCurrency);
|
|
|
+ var currRateInfo = (currRate.Data as CurrencyInfo);
|
|
|
+ if (currRateInfo == null)
|
|
|
+ {
|
|
|
+ currRateInfo = new CurrencyInfo()
|
|
|
+ {
|
|
|
+ Rate = 1,
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ var currencyCount = sumPriceList
|
|
|
+ .GroupBy(x => x.PriceCurrency)
|
|
|
+ .ToList();
|
|
|
+
|
|
|
+ if (currencyCount.Count > 1)
|
|
|
+ {
|
|
|
+ //全部转为RMB处理
|
|
|
+ var rmbPrice = sumPriceList.Sum(x => x.PriceSum * x.DayRate);
|
|
|
+ var upperLimitRmbPrice = upperLimit.ExtOverLimit * currRateInfo.Rate;
|
|
|
+ remainingAmount = upperLimitRmbPrice - rmbPrice;
|
|
|
+ remainingCurrency = 836; //RMB
|
|
|
+ if (upperLimitRmbPrice > 0)
|
|
|
+ {
|
|
|
+ remainingPercentage = remainingAmount / upperLimitRmbPrice;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ remainingPercentage = 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ usedAmount = rmbPrice;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (upperLimit.ExtOverCurrency == currencyCount.First().Key)
|
|
|
+ {
|
|
|
+ remainingAmount = upperLimit.ExtOverLimit - sumPriceList.Sum(x => x.PriceSum);
|
|
|
+ remainingCurrency = upperLimit.ExtOverCurrency; //原币种
|
|
|
+ if (upperLimit.ExtOverLimit > 0)
|
|
|
+ {
|
|
|
+ remainingPercentage = remainingAmount / upperLimit.ExtOverLimit;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ remainingPercentage = 1;
|
|
|
+ }
|
|
|
+ usedAmount = sumPriceList.Sum(x => x.PriceSum);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //转换为RMB比较
|
|
|
+ var rmbPrice = sumPriceList.Sum(x => x.PriceSum * x.DayRate);
|
|
|
+ var upperLimitRmbPrice = upperLimit.ExtOverLimit * currRateInfo.Rate;
|
|
|
+ remainingAmount = upperLimitRmbPrice - rmbPrice;
|
|
|
+ remainingCurrency = 836; //RMB
|
|
|
+ if (upperLimitRmbPrice > 0)
|
|
|
+ {
|
|
|
+ remainingPercentage = remainingAmount / upperLimitRmbPrice;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ remainingPercentage = 1;
|
|
|
+ }
|
|
|
+ usedAmount = rmbPrice;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ remainingPercentage = Math.Round(remainingPercentage, 2);
|
|
|
+ remainingAmount = Math.Round(remainingAmount, 2);
|
|
|
+ usedAmount = Math.Round(usedAmount, 2);
|
|
|
+
|
|
|
+ return new GroupExtraPriceResult()
|
|
|
+ {
|
|
|
+ RemainingAmount = remainingAmount,
|
|
|
+ RemainingCurrency = remainingCurrency,
|
|
|
+ RemainingPercentage = remainingPercentage,
|
|
|
+ UsedAmount = usedAmount
|
|
|
+ };
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|