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.

257 lines
10 KiB
C#

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
{
/// <summary>
/// 往来单位费用模板
/// </summary>
public class FeeCustTemplateService : FeeServiceBase, IFeeCustTemplateService
{
Lazy<IActionManagerService> actionService;
Lazy<IFeeRecordService> feeService;
/// <summary>
/// 初始化
/// </summary>
/// <param name="serviceProvider"></param>
public FeeCustTemplateService(IServiceProvider serviceProvider) : base(serviceProvider)
{
actionService = new Lazy<IActionManagerService>(serviceProvider.GetRequiredService<IActionManagerService>());
feeService = new Lazy<IFeeRecordService>(serviceProvider.GetRequiredService<IFeeRecordService>());
}
/// <summary>
/// 根据业务ID与类型生成费用
/// </summary>
/// <param name="bsId">业务ID</param>
/// <param name="businessType">业务类型</param>
/// <returns></returns>
public async Task GenerateFeesAsync(long bsId, BusinessType businessType = BusinessType.OceanShippingExport)
{
var model = await TenantDb.Queryable<SeaExport>().Where(x => x.Id == bsId).Select(x => new
{
x.CustomerId
}).FirstAsync();
if (model == null)
return;
//switch (businessType)
//{
// case BusinessType.OceanShippingExport:
// break;
// case BusinessType.OceanShippingImport:
// break;
//}
try
{
var list = await TenantDb.Queryable<FeeCustTemplate>()
.Where(x => x.BusinessType == businessType && x.CustomerId == model.CustomerId && !SqlFunc.IsNullOrEmpty(x.Condition))
.Select(x => new
{
x.Id,
x.CustomerId,
x.CustomerName,
x.CustomerType,
x.Condition,
Details = SqlFunc.Subqueryable<FeeCustTemplateDetail>().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<ConditionContent>(x.Condition)
}).ToList();
var data = await actionService.Value.GetBusinessDataAsync(bsId, businessType, conditionModels.Select(x => x.ConditionModel));
if (data == null)
return;
//获取订单箱型箱量
string bsNo = bsId.ToString();
var ctns = await TenantDb.Queryable<OpCtn>().Where(y => y.BSNO == bsNo).Select(x => new
{
x.CtnCode,
x.CtnNum
}).ToListAsync();
List<FeeRecord> 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(),
Quantity = x.IsCtn ? 0 : 1, //非箱型默认数量为1
Currency = x.Currency,
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()
});
//根据箱型箱量提取数量以计算总价
foreach (var fe in fees)
{
if (fe.Quantity > 0)
continue;
if (fe.Unit.IsNullOrEmpty())
{
fe.Quantity = 0;
}
else
{
var ctn = ctns.Find(x => x.CtnCode == fe.Unit);
fe.Quantity = ctn == null ? 0 : ctn.CtnNum.GetValueOrDefault();
}
}
//过滤掉数量为0的费用项
feeList.AddRange(fees.Where(x => x.Quantity > 0));
}
}
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);
}
}
/// <summary>
/// 列表
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<DataResult<List<FeeCustTemplate>>> GetListAsync(PageRequest request)
{
var whereList = request.GetConditionalModels(Db);
return await TenantDb.Queryable<FeeCustTemplate>().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);
}
/// <summary>
/// 详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<DataResult<FeeCustTemplate>> GetAsync(long id)
{
var data = await TenantDb.Queryable<FeeCustTemplate>().Where(x => x.Id == id)
.Includes(x => x.Details).FirstAsync();
return DataResult<FeeCustTemplate>.Success(data, MultiLanguageConst.DataQuerySuccess);
}
/// <summary>
/// 编辑
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public async Task<DataResult> 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);
}
/// <summary>
/// 根据ID批量删除
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public async Task<DataResult> DeleteAsync(IdModel model)
{
bool flag = await TenantDb.DeleteNav<FeeCustTemplate>(x => model.Ids.Contains(x.Id))
.Include(x => x.Details).ExecuteCommandAsync();
return flag ? DataResult.Success : DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed));
}
}
}