| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- 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 string RemainingCurrencyName { 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>()
- .LeftJoin<Sys_SetData>((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
- };
- }
- }
- }
|