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
{
///
/// 费用服务基类
///
public abstract class FeeServiceBase
{
///
/// 人民币代码
///
public const string RMB_CODE = "CNY";
///
/// 美元代码
///
public const string USD_CODE = "USD";
///
/// 工作流运行状态值
///
public static readonly int RunningStatus = (int)FlowStatusEnum.Running;
///
/// 获取用户相关信息
///
protected IUser User { get; private set; }
///
/// 获取主库访问对象
///
protected ISqlSugarClient Db { get; private set; }
///
/// 获取业务库访问对象
///
protected ISaasDbService SaasService { get; private set; }
SqlSugarScopeProvider? _tenantDb;
///
/// 获取业务库访问对象
///
protected SqlSugarScopeProvider TenantDb
{
get
{
if (_tenantDb == null)
_tenantDb = SaasService.GetBizDbScopeById(User.TenantId);
return _tenantDb;
}
}
///
/// 初始化
///
/// 服务提供程序
protected FeeServiceBase(IServiceProvider serviceProvider)
{
User = serviceProvider.GetRequiredService();
Db = serviceProvider.GetRequiredService();
SaasService = serviceProvider.GetRequiredService();
}
///
/// 查找模板
///
/// 审批类型
///
protected async Task FindTemplateAsync(AuditType auditType)
{
string typeStr = auditType.ToString();
return await Db.Queryable().FirstAsync(x =>
x.Status == StatusEnum.Enable && x.AuditType == typeStr);
}
///
/// 获取汇率
///
///
///
protected async Task FetchExchangeRateAsync(IEnumerable items)
{
var exRecords = items.Where(x => !x.ExchangeRate.HasValue && x.Currency != x.LocalCurrency).ToList();
if (exRecords.Count > 0)
{
var codes = exRecords.Select(x => x.Currency).Distinct().ToList();
var currencies = await TenantDb.Queryable().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;
}
}
}
}
}
///
/// 获取当前登录用户的待审批工作流的查询对象
///
/// 审核类型
///
protected ISugarQueryable GetCurrentFlowsQuery(string[] auditTypes)
{
return Db.Queryable().Where(x => x.FlowStatus == RunningStatus
&& SqlFunc.SplitIn(x.MakerList, User.UserId) && auditTypes.Contains(x.AuditType))
.Select(x => new FlowInstance { Id = x.Id, BusinessId = x.BusinessId, BusinessType = x.BusinessType });
}
}
}