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 { /// /// 剩余金额 /// public decimal RemainingAmount { get; set; } /// /// 金额币种 /// public int RemainingCurrency { get; set; } /// /// 剩余百分比 /// public decimal RemainingPercentage { get; set; } /// /// 已用金额 /// public decimal UsedAmount { get; set; } /// /// 金额币种名称 /// public string RemainingCurrencyName { get; set; } } /// /// 财务 - 团组超支费用 /// public class GroupExtraCostRepository : BaseRepository { private readonly IMapper _mapper; private readonly SetDataRepository _setDataRepository; //添加日志 private readonly ILogger _logger; public GroupExtraCostRepository(SqlSugarClient sqlSugar, IMapper mapper, SetDataRepository setDataRepository, ILogger logger) : base(sqlSugar) { _mapper = mapper; _setDataRepository = setDataRepository; _logger = logger; } public async Task GetGroupExtraPriceByDiId(int diId) { var sumPriceList = await Query(x => x.DiId == diId && x.IsDel == 0) .LeftJoin((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() .LeftJoin((x, y) => x.ExtOverCurrency == y.Id && y.IsDel == 0) .Where((x, y) => x.Id == diId && x.IsDel == 0) .Select((x, y) => new { x.ExtOverLimit, x.ExtOverCurrency, CurrencyName = y.Name }) .First(); if (upperLimit == null) { _logger.LogError("团组超支费用查询失败,团组不存在 diId: {diId}", diId); return new GroupExtraPriceResult() { RemainingAmount = 0, RemainingCurrency = 836, //RMB RemainingPercentage = 1, UsedAmount = 0, RemainingCurrencyName = "人民币" }; } if (!sumPriceList.Any()) { _logger.LogError("团组超支费用查询失败,团组超支费用为0 diId: {diId}", diId); return new GroupExtraPriceResult() { RemainingAmount = upperLimit.ExtOverLimit, RemainingCurrency = upperLimit.ExtOverCurrency, RemainingPercentage = 1, UsedAmount = 0, RemainingCurrencyName = upperLimit.CurrencyName }; } //剩余金额 decimal remainingAmount = 0; //金额币种 int remainingCurrency = 0; //剩余百分比 decimal remainingPercentage = 0; //已用额度 decimal usedAmount = 0; //币种名称 string remainingCurrencyName = string.Empty; //获取超支部分的汇率 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; remainingCurrencyName = "人民币"; } 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); remainingCurrencyName = upperLimit.CurrencyName; } 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; remainingCurrencyName = "人民币"; } } 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, RemainingCurrencyName = remainingCurrencyName }; } } }