using DS.Module.Core;
using DS.Module.Core.Condition;
using DS.Module.Core.Data;
using DS.Module.Core.Extensions;
using DS.WMS.Core.Fee.Entity;
using DS.WMS.Core.Fee.Interface;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Op.Interface.TaskInteraction;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using SqlSugar;
namespace DS.WMS.Core.Fee.Method
{
///
/// 往来单位费用模板
///
public class FeeCustTemplateService : FeeServiceBase, IFeeCustTemplateService
{
Lazy actionService;
Lazy feeService;
///
/// 初始化
///
///
public FeeCustTemplateService(IServiceProvider serviceProvider) : base(serviceProvider)
{
actionService = new Lazy(serviceProvider.GetRequiredService());
feeService = new Lazy(serviceProvider.GetRequiredService());
}
///
/// 根据业务ID与类型生成费用
///
/// 业务ID
/// 业务类型
///
public async Task GenerateFeesAsync(long bsId, BusinessType businessType = BusinessType.OceanShippingExport)
{
var model = await actionService.Value.GetBusinessDataAsync(bsId, businessType, nameof(SeaExport.CustomerId));
long custId = model.CustomerId;
try
{
var list = await TenantDb.Queryable()
.Where(x => x.BusinessType == businessType && x.CustomerId == custId && !SqlFunc.IsNullOrEmpty(x.Condition))
.Select(x => new
{
x.Id,
x.CustomerId,
x.CustomerName,
x.CustomerType,
x.Condition,
Details = SqlFunc.Subqueryable().Where(y => y.TemplateId == x.Id).ToList(y => new
{
y.FeeId,
y.FeeCode,
y.FeeName,
y.FeeType,
y.Unit,
y.IsCtn,
y.Currency,
y.UnitPrice,
y.ExchangeRate,
y.TaxRate,
y.AccTaxRate,
y.Tax,
y.TaxUnitPrice,
y.IsInvoice,
y.IsAdvancedPay
})
}).ToListAsync();
if (list.Count == 0)
return;
var conditionModels = list.Select(x => new
{
x.Id,
ConditionModel = JsonConvert.DeserializeObject(x.Condition)
}).ToList();
var data = await actionService.Value.GetBusinessDataAsync(bsId, businessType, conditionModels.Select(x => x.ConditionModel));
if (data == null)
return;
List feeList = [];
foreach (var item in list)
{
var conditionModel = conditionModels.Find(x => x.Id == item.Id)?.ConditionModel;
if (actionService.Value.IsMatch(data, conditionModel))
{
var fees = item.Details.Select(x => new FeeRecord
{
BusinessId = bsId,
BusinessType = businessType,
FeeType = x.FeeType,
FeeId = x.FeeId,
FeeCode = x.FeeCode,
FeeName = x.FeeName,
CustomerId = item.CustomerId,
CustomerName = item.CustomerName,
CustomerType = item.CustomerType?.ToString(),
Unit = x.Unit,
UnitPrice = x.UnitPrice.GetValueOrDefault(),
Currency = x.Currency,
//IsCtn = x.IsCtn,
ExchangeRate = x.ExchangeRate,
TaxRate = x.TaxRate.GetValueOrDefault(),
AccTaxRate = x.AccTaxRate.GetValueOrDefault(),
Tax = x.Tax.GetValueOrDefault(),
TaxUnitPrice = x.TaxUnitPrice.GetValueOrDefault(),
IsInvoice = x.IsInvoice,
IsAdvancedPay = x.IsAdvancedPay,
Amount = x.UnitPrice.GetValueOrDefault()
});
feeList.AddRange(fees);
}
}
if (feeList.Count > 0)
{
var result = await feeService.Value.SaveAsync(feeList);
if (!result.Succeeded)
{
//记录失败日志
await new ApplicationException(result.Message).LogAsync(Db);
}
}
}
catch (Exception ex)
{
await ex.LogAsync(Db);
}
}
///
/// 列表
///
///
///
public async Task>> GetListAsync(PageRequest request)
{
var whereList = request.GetConditionalModels(Db);
return await TenantDb.Queryable().Select(x => new FeeCustTemplate
{
Id = x.Id,
BusinessType = x.BusinessType,
FeeType = x.FeeType,
Name = x.Name,
IsShared = x.IsShared,
Note = x.Note,
CreateBy = x.CreateBy,
CreateTime = x.CreateTime
}).Where(whereList).ToQueryPageAsync(request.PageCondition);
}
///
/// 详情
///
///
///
public async Task> GetAsync(long id)
{
var data = await TenantDb.Queryable().Where(x => x.Id == id)
.Includes(x => x.Details).FirstAsync();
return DataResult.Success(data, MultiLanguageConst.DataQuerySuccess);
}
///
/// 编辑
///
///
///
public async Task EditAsync(FeeCustTemplate model)
{
if (model.Details.Count > 0)
{
long userId = long.Parse(User.UserId);
DateTime dt = DateTime.Now;
foreach (var item in model.Details)
{
item.TemplateId = model.Id;
item.FeeType = model.FeeType;
item.CreateBy = userId;
item.CreateTime = dt;
}
}
bool flag;
if (model.Id == 0)
{
flag = await TenantDb.InsertNav(model).Include(x => x.Details).ExecuteCommandAsync();
}
else
{
flag = await TenantDb.Updateable(model).ExecuteCommandAsync() > 0;
flag = await TenantDb.Storageable(model.Details).DefaultAddElseUpdate().ExecuteCommandAsync() > 0;
}
return flag ? DataResult.Successed("添加成功!", model.Id, MultiLanguageConst.DataCreateSuccess)
: DataResult.FailedWithDesc(MultiLanguageConst.Operation_Failed);
}
///
/// 根据ID批量删除
///
///
///
public async Task DeleteAsync(IdModel model)
{
bool flag = await TenantDb.DeleteNav(x => model.Ids.Contains(x.Id))
.Include(x => x.Details).ExecuteCommandAsync();
return flag ? DataResult.Success : DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed));
}
}
}