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) { //switch (businessType) //{ // case BusinessType.OceanShippingExport: // break; // case BusinessType.OceanShippingImport: // break; //} var order = await TenantDb.Queryable().Where(x => x.Id == bsId).Select( x => new { x.CustomerId }).FirstAsync(); if (order == null) return; try { var list = await TenantDb.Queryable() .Where(x => x.BusinessType == businessType && !SqlFunc.IsNullOrEmpty(x.Condition) && (x.IsShared || x.CustomerId == order.CustomerId)) .Select(x => new { x.Id, x.Condition, Details = SqlFunc.Subqueryable().Where(y => y.TemplateId == x.Id && y.CustomerId == order.CustomerId).ToList(y => new { y.CustomerId, y.CustomerName, y.CustomerType, 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 = x.CustomerId, CustomerName = x.CustomerName, CustomerType = x.CustomerType?.ToString(), Unit = x.Unit, UnitPrice = x.UnitPrice.GetValueOrDefault(), Quantity = x.IsCtn ? 1 : 0, Note = x.IsCtn.ToString().ToLowerInvariant(), //临时存储 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, }); feeList.AddRange(fees); } } if (feeList.Count > 0) { var result = await feeService.Value.SaveAsync(feeList, true); 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, CustomerId = x.CustomerId, CustomerName = x.CustomerName, CustomerType = x.CustomerType, 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).FirstAsync(); if (data != null) data.Details = await TenantDb.Queryable().Where(x => x.TemplateId == data.Id).ToListAsync(); 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; if (item.CustomerId == 0 && model.CustomerId.HasValue) item.CustomerId = model.CustomerId.Value; if (item.CustomerName.IsNullOrEmpty() && !model.CustomerName.IsNullOrEmpty()) item.CustomerName = model.CustomerName; item.CustomerType = model.CustomerType; item.CreateBy = userId; item.CreateTime = dt; } } await TenantDb.Ado.BeginTranAsync(); try { if (model.Id == 0) { await TenantDb.InsertNav(model).Include(x => x.Details).ExecuteCommandAsync(); } else { await TenantDb.Updateable(model).ExecuteCommandAsync(); if (model.Details.Count > 0) await TenantDb.Storageable(model.Details).DefaultAddElseUpdate().ExecuteCommandAsync(); } await TenantDb.Ado.CommitTranAsync(); return DataResult.Successed("提交成功", model.Id, MultiLanguageConst.DataCreateSuccess); } catch (Exception ex) { await TenantDb.Ado.RollbackTranAsync(); await ex.LogAsync(Db); return DataResult.FailedWithDesc(nameof(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)); } } }