You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.8 KiB
C#

using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core.Fee.Entity;
using DS.WMS.Core.Flow.Entity;
using LanguageExt.Pipes;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.Fee.Method
{
/// <summary>
/// 费用服务基类
/// </summary>
public abstract class FeeServiceBase
{
/// <summary>
/// //默认本币
/// </summary>
protected const string DefaultCurrency = "CNY";
/// <summary>
/// 获取用户相关信息
/// </summary>
protected IUser User { get; private set; }
/// <summary>
/// 获取主库访问对象
/// </summary>
protected ISqlSugarClient Db { get; private set; }
/// <summary>
/// 获取业务库访问对象
/// </summary>
protected ISaasDbService SaasService { get; private set; }
SqlSugarScopeProvider? _tenantDb;
/// <summary>
/// 获取业务库访问对象
/// </summary>
protected SqlSugarScopeProvider TenantDb
{
get
{
if (_tenantDb == null)
_tenantDb = SaasService.GetBizDbScopeById(User.TenantId);
return _tenantDb;
}
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="serviceProvider">服务提供程序</param>
protected FeeServiceBase(IServiceProvider serviceProvider)
{
User = serviceProvider.GetRequiredService<IUser>();
Db = serviceProvider.GetRequiredService<ISqlSugarClient>();
SaasService = serviceProvider.GetRequiredService<ISaasDbService>();
}
/// <summary>
/// 查找模板
/// </summary>
/// <param name="auditType">审批类型</param>
/// <returns></returns>
protected async Task<FlowTemplateTenant> FindTemplateAsync(AuditType auditType)
{
string typeStr = auditType.ToString();
return await Db.Queryable<FlowTemplateTenant>().FirstAsync(x =>
x.Status == StatusEnum.Enable && x.AuditType == typeStr);
}
/// <summary>
/// 获取汇率
/// </summary>
/// <param name="items"></param>
/// <returns></returns>
protected async Task FetchExchangeRateAsync(IEnumerable<FeeRecord> items)
{
var exRecords = items.Where(x => !x.ExchangeRate.HasValue && x.Currency != DefaultCurrency).ToList();
if (exRecords.Count > 0)
{
var codes = exRecords.Select(x => x.Currency).Distinct().ToList();
var currencies = await TenantDb.Queryable<FeeCurrency>().Where(x => codes.Contains(x.CodeName)).Includes(x => x.Exchanges).ToListAsync();
DateTime dtNow = DateTime.Now;
foreach (var item in exRecords)
{
var currency = currencies.Find(x => x.CodeName == item.Currency);
if (currency != null)
{
item.ExchangeRate = currency.DefaultRate;
if (currency.Exchanges != null && currency.Exchanges.Count > 0)
{
//取当前时间范围内的最新一条
var exchange = currency.Exchanges.FindAll(x => x.Status == StatusEnum.Enable &&
x.StartDate >= dtNow && x.EndDate <= dtNow).OrderByDescending(x => x.UpdateTime).FirstOrDefault();
if (exchange != null)
item.ExchangeRate = item.FeeType == FeeType.Receivable ? exchange.DRValue : exchange.CRValue;
}
}
}
}
}
}
}