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.

296 lines
12 KiB
C#

using System.Linq.Expressions;
using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core.Fee.Dtos;
using DS.WMS.Core.Fee.Entity;
using DS.WMS.Core.Flow.Entity;
using DS.WMS.Core.Op.Entity;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.Fee.Method
{
/// <summary>
/// 费用服务基类
/// </summary>
public abstract class FeeServiceBase
{
/// <summary>
/// 人民币代码
/// </summary>
public const string RMB_CODE = "CNY";
/// <summary>
/// 美元代码
/// </summary>
public const string USD_CODE = "USD";
/// <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="expr1">关联条件1</param>
/// <returns>查询对象</returns>
public ISugarQueryable<BusinessFeeDto> CreateFeeQuery(
Expression<Func<SeaExport, FeeRecord, bool>>? expr1 = null)
{
//海运出口
var query1 = TenantDb.Queryable<SeaExport>()
.InnerJoin<FeeRecord>((s, f) => s.Id == f.BusinessId && f.BusinessType == BusinessType.OceanShippingExport)
.WhereIF(expr1 != null, expr1)
.Select((s, f) => new BusinessFeeDto
{
BusinessId = s.Id,
BusinessType = BusinessType.OceanShippingExport,
AccountDate = s.AccountDate,
CntrTotal = s.CntrTotal,
CreateBy = s.CreateBy,
CustomerNo = s.CustomerNo,
ClientName = s.CustomerName, //委托单位
DischargePort = s.DischargePort,
ETD = s.ETD,
HBLNO = s.HBLNO,
LoadPort = s.LoadPort,
MBLNO = s.MBLNO,
OperatorId = s.OperatorId,
SaleDeptId = s.SaleDeptId,
SaleId = s.SaleId,
Sale = s.Sale,//揽货人
Vessel = s.Vessel,//船名
Voyage = s.Voyno,//航次
BookingNo = s.BookingNo,
StlName = s.StlName,
InvoiceNo = s.InvoiceNo,
RecordId = f.Id,
FeeType = f.FeeType,
FeeStatus = f.FeeStatus,
CustomerId = f.CustomerId,//费用对象
CustomerName = f.CustomerName,
FeeId = f.FeeId,
FeeName = f.FeeName,
Currency = f.Currency,
Amount = f.Amount,
ExchangeRate = f.ExchangeRate,
OrderAmount = f.OrderAmount,
InvoiceAmount = f.InvoiceAmount,
SettlementAmount = f.SettlementAmount,
OrderSettlementAmount = f.OrderSettlementAmount,
OrderInvSettlementAmount = f.OrderInvSettlementAmount,
TaxRate = f.TaxRate,
AccTaxRate = f.AccTaxRate,
IsDebit = f.IsDebit,
DebitNo = f.DebitNo,
IsInvoice = f.IsInvoice,
SaleOrgId = f.SaleOrgId,
});
//海运进口
return TenantDb.UnionAll(new List<ISugarQueryable<BusinessFeeDto>> { query1 });
}
/// <summary>
/// 更新费用及其业务的费用状态
/// </summary>
/// <param name="ids">费用记录ID</param>
/// <remarks>此方法内部将始终异步执行,请确保在调用前已提交数据库事务等必要的操作。</remarks>
protected internal void UpdateFeeStatus(IEnumerable<long> ids)
{
var task1 = Task.Factory.StartNew(UpdateFeeStatusCore, ids, CancellationToken.None);
task1.ContinueWith(t => UpdateBizStatusCore(t.Result), TaskContinuationOptions.OnlyOnRanToCompletion);
}
private List<FeeRecord> UpdateFeeStatusCore(object? state)
{
if (state == null)
return [];
var ids = (IEnumerable<long>)state;
var fees = TenantDb.Queryable<FeeRecord>().Where(x => ids.Contains(x.Id))
.Select(x => new FeeRecord
{
Id = x.Id,
BusinessId = x.BusinessId,
BusinessType = x.BusinessType,
FeeStatus = x.FeeStatus,
Amount = x.Amount,
SettlementAmount = x.SettlementAmount,
OrderAmount = x.OrderAmount,
OrderSettlementAmount = x.OrderSettlementAmount
}).ToList();
if (fees.Count == 0)
return [];
List<FeeRecord> list = new(fees.Count);
foreach (var item in fees)
{
var restAmount = item.Amount - item.SettlementAmount - item.OrderAmount + item.OrderSettlementAmount;
if (restAmount == 0)
{
item.FeeStatus = FeeStatus.SettlementCompleted;
list.Add(item);
}
else if (restAmount != item.Amount)
{
item.FeeStatus = FeeStatus.PartialSettlement;
list.Add(item);
}
else if (item.SettlementAmount == 0)
{
item.FeeStatus = FeeStatus.AuditPassed;
list.Add(item);
}
}
TenantDb.Updateable(list).UpdateColumns(x => new { x.FeeStatus }).ExecuteCommand();
return list;
}
private void UpdateBizStatusCore(List<FeeRecord> list)
{
var bizIds = list.Select(x => x.BusinessId);
var types = list.Select(x => x.BusinessType).Distinct();
var fees2 = TenantDb.Queryable<FeeRecord>().Where(x => bizIds.Contains(x.BusinessId) && types.Contains(x.BusinessType))
.Select(x => new { x.BusinessId, x.BusinessType, x.FeeType, x.FeeStatus }).ToList();
if (fees2.Count == 0)
return;
var gpList = fees2.GroupBy(x => new { x.BusinessId, x.BusinessType });
foreach (var gp in gpList)
{
BusinessFeeStatus biz = new() { BusinessId = gp.Key.BusinessId, BusinessType = gp.Key.BusinessType };
var upt = TenantDb.Updateable(biz).WhereColumns(x => new { x.BusinessId, x.BusinessType });
//应收
var arList = gp.Where(x => x.FeeType == FeeType.Receivable).ToList();
if (arList.Count > 0)
{
if (arList.All(x => x.FeeStatus == FeeStatus.SettlementCompleted))
{
biz.ARFeeStatus = BillFeeStatus.SettlementCompleted;
upt = upt.UpdateColumns(x => x.ARFeeStatus);
}
else if (arList.Any(x => x.FeeStatus == FeeStatus.PartialSettlement))
{
biz.ARFeeStatus = BillFeeStatus.PartialSettlement;
upt = upt.UpdateColumns(x => x.ARFeeStatus);
}
}
//应付
var apList = gp.Where(x => x.FeeType == FeeType.Payable).ToList();
if (apList.Count > 0)
{
if (apList.All(x => x.FeeStatus == FeeStatus.SettlementCompleted))
{
biz.APFeeStatus = BillFeeStatus.SettlementCompleted;
upt = upt.UpdateColumns(x => x.APFeeStatus);
}
else if (apList.Any(x => x.FeeStatus == FeeStatus.PartialSettlement))
{
biz.APFeeStatus = BillFeeStatus.PartialSettlement;
upt = upt.UpdateColumns(x => x.APFeeStatus);
}
}
upt.ExecuteCommand();
}
}
/// <summary>
/// 查找模板
/// </summary>
/// <param name="auditType">审批类型</param>
/// <returns></returns>
protected internal async Task<FlowTemplateTenant> FindTemplateAsync(AuditType auditType)
{
return await Db.Queryable<FlowTemplateTenant>().FirstAsync(x => x.Status == StatusEnum.Enable && x.AuditType == auditType);
}
/// <summary>
/// 获取汇率
/// </summary>
/// <param name="items"></param>
/// <returns></returns>
protected internal async Task FetchExchangeRateAsync(IEnumerable<FeeRecord> 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<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;
}
}
}
}
}
/// <summary>
/// 获取当前登录用户的待审批工作流的查询对象
/// </summary>
/// <param name="auditTypes">审核类型</param>
/// <returns></returns>
protected internal ISugarQueryable<FlowInstance> GetCurrentFlowsQuery(AuditType[] auditTypes)
{
return Db.Queryable<FlowInstance>().Where(x => x.FlowStatus == FlowStatusEnum.Running
&& SqlFunc.SplitIn(x.MakerList, User.UserId) && auditTypes.Contains(x.Type.Value))
.Select(x => new FlowInstance { Id = x.Id, BusinessId = x.BusinessId, BusinessType = x.BusinessType });
}
}
}