using DS.Module.Core;
using DS.Module.Core.Enums;
using DS.WMS.Core.Application.Entity;
using DS.WMS.Core.Application.Interface;
using DS.WMS.Core.Fee.Method;
using DS.WMS.Core.Settlement.Entity;
using DS.WMS.Core.Sys.Interface;
using Microsoft.Extensions.DependencyInjection;
namespace DS.WMS.Core.Settlement.Method
{
///
/// 结算基础实现
///
/// 实体的类型声明
public class SettlementService : FeeServiceBase, ISettlementService
where TEntity : SettlementBase, new()
{
protected readonly Lazy CommonService;
///
/// 初始化
///
/// DI容器
public SettlementService(IServiceProvider serviceProvider) : base(serviceProvider)
{
CommonService = new Lazy(serviceProvider.GetRequiredService());
}
#region 保存
public Task> SaveAsync(TEntity application)
{
throw new NotImplementedException();
}
#endregion
#region 删除
///
/// 删除结算单明细
///
/// 结算单明细ID
///
public async Task DeleteDetailAsync(params long[] ids)
{
var details = await TenantDb.Queryable().Where(x => ids.Contains(x.Id)).Select(
x => new ApplicationDetail
{
Id = x.Id,
ApplicationId = x.ApplicationId,
RecordId = x.RecordId,
DetailId = x.DetailId,
OriginalAmount = x.OriginalAmount,
ProcessedAmount = x.ProcessedAmount,
OriginalProcessedAmount = x.OriginalProcessedAmount
}).ToListAsync();
var appIds = details.Select(x => x.ApplicationId).Distinct().ToList();
var apps = await TenantDb.Queryable().Where(x => appIds.Contains(x.Id)).Select(x => new TEntity
{
Id = x.Id,
Mode = x.Mode
}).ToListAsync();
foreach (var app in apps)
app.Details = details.FindAll(x => x.ApplicationId == app.Id);
var result = PreDelete(apps);
if (!result.Succeeded)
return result;
await TenantDb.Ado.BeginTranAsync();
try
{
await OnDeleteDetailAsync(apps);
await TenantDb.Deleteable(details).ExecuteCommandAsync();
await TenantDb.Ado.CommitTranAsync();
return DataResult.Success;
}
catch (Exception ex)
{
await TenantDb.Ado.RollbackTranAsync();
await ex.LogAsync(Db);
return DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed));
}
}
///
/// 删除结算单
///
/// 结算单ID
///
public async Task DeleteAsync(params long[] ids)
{
var apps = await TenantDb.Queryable().Where(x => ids.Contains(x.Id)).Select(x => new TEntity
{
Id = x.Id,
Mode = x.Mode
}).ToListAsync();
var details = await TenantDb.Queryable().Where(x => ids.Contains(x.ApplicationId)).Select(
x => new ApplicationDetail
{
Id = x.Id,
ApplicationId = x.ApplicationId,
RecordId = x.RecordId,
DetailId = x.DetailId,
ApplyAmount = x.ApplyAmount,
OriginalAmount = x.OriginalAmount
}).ToListAsync();
foreach (var app in apps)
app.Details = details.FindAll(x => x.ApplicationId == app.Id); await TenantDb.Ado.BeginTranAsync();
var result = PreDelete(apps);
if (!result.Succeeded)
return result;
try
{
await OnDeleteDetailAsync(apps);
await TenantDb.DeleteNav(x => ids.Contains(x.Id)).Include(x => x.Details).ExecuteCommandAsync();
await TenantDb.Ado.CommitTranAsync();
return DataResult.Success;
}
catch (Exception ex)
{
await TenantDb.Ado.RollbackTranAsync();
await ex.LogAsync(Db);
return DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed));
}
}
///
/// 在删除结算单或其明细之前调用,用于检查状态
///
/// 结算单
///
protected virtual DataResult PreDelete(List settlements)
{
return DataResult.Success;
}
///
/// 在执行删除结算单或其明细时调用
///
/// 结算单及其明细
///
protected virtual async Task OnDeleteDetailAsync(List settlements)
{
//还原付费申请明细
var list = settlements.FindAll(x => x.Mode == SettlementMode.Payment);
if (list.Count > 0)
{
var items = list.SelectMany(x => x.Details);
var ids = items.Select(x => x.DetailId);
var details = TenantDb.Queryable().Where(x => ids.Contains(x.Id) && x.Category == FeeCategory.PaidApplication)
.Select(x => new ApplicationDetail
{
Id = x.Id,
ProcessedAmount = x.ProcessedAmount,
OriginalProcessedAmount = x.OriginalProcessedAmount
}).ToList();
foreach (var detail in details)
{
var item = items.FirstOrDefault(x => x.DetailId == detail.Id);
detail.ProcessedAmount -= item.ApplyAmount;
detail.OriginalProcessedAmount -= item.OriginalAmount;
}
await TenantDb.Updateable(details)
.UpdateColumns(x => new { x.ProcessedAmount, x.OriginalProcessedAmount })
.ExecuteCommandAsync();
}
}
#endregion
}
}