From 4b5a39305ebc6188dcbd0068e2f3339258224146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B5=87=E6=96=87=E9=BE=99?= Date: Fri, 11 Oct 2024 16:53:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BE=80=E6=9D=A5=E5=8D=95?= =?UTF-8?q?=E4=BD=8D=E5=B9=B2=E7=B3=BB=E4=BA=BA=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Fee/Method/FeeCustTemplateService.cs | 1 + .../Method/FeeCustTemplateJobService.cs | 318 +++++++++++++++++- .../Info/Method/ClientInfoService.cs | 14 +- .../Info/Method/ClientStakeholderService.cs | 5 +- 4 files changed, 329 insertions(+), 9 deletions(-) diff --git a/ds-wms-service/DS.WMS.Core/Fee/Method/FeeCustTemplateService.cs b/ds-wms-service/DS.WMS.Core/Fee/Method/FeeCustTemplateService.cs index 791a0bfd..a159581e 100644 --- a/ds-wms-service/DS.WMS.Core/Fee/Method/FeeCustTemplateService.cs +++ b/ds-wms-service/DS.WMS.Core/Fee/Method/FeeCustTemplateService.cs @@ -365,6 +365,7 @@ namespace DS.WMS.Core.Fee.Method PODCode = g.Key.POD, CreateBy = userId, CreateTime = dtNow, + Note = "系统导入", Details = [] }; list.Add(template); diff --git a/ds-wms-service/DS.WMS.Core/HangfireJob/Method/FeeCustTemplateJobService.cs b/ds-wms-service/DS.WMS.Core/HangfireJob/Method/FeeCustTemplateJobService.cs index 224ba38f..e195766f 100644 --- a/ds-wms-service/DS.WMS.Core/HangfireJob/Method/FeeCustTemplateJobService.cs +++ b/ds-wms-service/DS.WMS.Core/HangfireJob/Method/FeeCustTemplateJobService.cs @@ -1,5 +1,13 @@ -using DS.Module.SqlSugar; +using DS.Module.Core; +using DS.Module.Core.Condition; +using DS.Module.Core.Data; +using DS.Module.SqlSugar; +using DS.WMS.Core.Fee.Entity; +using DS.WMS.Core.Invoice.Dtos; +using DS.WMS.Core.Op.Dtos; +using DS.WMS.Core.Op.Entity; using Microsoft.Extensions.DependencyInjection; +using Newtonsoft.Json; using SqlSugar; namespace DS.WMS.Core.HangfireJob.Method @@ -9,7 +17,7 @@ namespace DS.WMS.Core.HangfireJob.Method /// public class FeeCustTemplateJobService { - ISqlSugarClient db; + ISqlSugarClient? db; /// /// 初始化 @@ -24,13 +32,313 @@ namespace DS.WMS.Core.HangfireJob.Method /// 生成费用 /// /// - public async Task GenerateFeesAsync() + public async Task GenerateFeesAsync() { var dbLinks = await db.Queryable().ToListAsync(); - foreach (var dbLink in dbLinks) + SqlSugarClient? tenantDb = null; + try { - + foreach (var dbLink in dbLinks) + { + string configId = dbLinks.IndexOf(dbLink).ToString(); + //if (db.IsAnyConnection(configId)) + //{ + // var config = new ConnectionConfig + // { + // ConfigId = configId, + // DbType = dbLink.DbType, + // IsAutoCloseConnection = true, + // ConnectionString = dbLink.Connection + // }; + // db.AddConnection(config); + //} + tenantDb = new SqlSugarClient(new ConnectionConfig + { + ConfigId = configId, + DbType = dbLink.DbType, + IsAutoCloseConnection = true, + ConnectionString = dbLink.Connection + }); + tenantDb.QueryFilter.Clear(); + tenantDb.QueryFilter.Clear(); + tenantDb.QueryFilter.Clear(); + + + } + } + finally + { + tenantDb?.Dispose(); + } + + } + + /// + /// 根据开船日生成费用 + /// + /// + /// 开船日 + /// + internal static async Task GenerateFeesCoreAsync(SqlSugarClient tenantDb, DateTime etd) + { + DateTime dt = DateTime.Now; + var list = await tenantDb.Queryable() + .Where(x => !x.IsDisabled && SqlFunc.Between(dt, x.StartTime, x.EndTime) && (x.IsShared || x.CustomerId != null)) + .Select(x => new FeeCustTemplate + { + Id = x.Id, + CustomerId = x.CustomerId, + FeeType = x.FeeType, + FeeCategoryId = x.FeeCategoryId, + FeeCategoryName = x.FeeCategoryName, + Priority = x.Priority, + IsShared = x.IsShared, + POLCode = x.POLCode, + PODCode = x.PODCode, + LaneId = x.LaneId, + SourceId = x.SourceId, + CarrierId = x.CarrierId, + ForwarderId = x.ForwarderId, + MBLFrtCode = x.MBLFrtCode, + Condition = x.Condition + }).ToListAsync(); + if (list.Count == 0) + return; + if (etd == default) + etd = DateTime.Now.Date; + + var tids = list.Select(x => x.Id); + var orders = await tenantDb.Queryable().Where(x => SqlFunc.DateIsSame(x.ETD, etd) && + SqlFunc.Subqueryable().Where(y => y.BusinessId == x.Id && y.BusinessType == BusinessType.OceanShippingExport && tids.Contains(y.TemplateId)).NotAny()) + .Select().ToListAsync(); + if (orders.Count == 0) + return; + + List feeList = []; + await tenantDb.Ado.BeginTranAsync(); + try + { + foreach (var order in orders) + { + var custList = list.Where(x => x.CustomerId == order.CustomerId).OrderBy(x => x.Priority); + foreach (var template in custList) //遍历客户费用模板,查找匹配项 + { + var fees = await CreateFeesIfMatchAsync(tenantDb,order, template); + if (fees != null) + { + feeList.AddRange(fees); + + var record = new FeeCustTemplateRecord + { + BusinessId = order.CustomerId, + BusinessType = BusinessType.OceanShippingExport, + CreateTime = dt, + FeeCategoryId = template.FeeCategoryId, + FeeType = template.FeeType, + TemplateId = template.Id + }; + await tenantDb.Insertable(record).ExecuteCommandAsync(); + } + } + + //未找到客户模板,开始匹配共享模板 + if (feeList.Count == 0) + { + var sharedList = list.Where(x => x.IsShared).OrderBy(x => x.Priority).ToList(); + foreach (var template in sharedList) + { + var fees = await CreateFeesIfMatchAsync(tenantDb, order, template); + if (fees != null) + { + feeList.AddRange(fees); + + var record = new FeeCustTemplateRecord + { + BusinessId = order.CustomerId, + BusinessType = BusinessType.OceanShippingExport, + CreateTime = dt, + FeeCategoryId = template.FeeCategoryId, + FeeType = template.FeeType, + TemplateId = template.Id + }; + await tenantDb.Insertable(record).ExecuteCommandAsync(); + } + } + } + + //写入当前业务的费用 + if (feeList.Count > 0) + { + //var result = await feeService.Value.SaveAsync(tenantDb, feeList, true, false); + //if (!result.Succeeded) + //{ + // //记录失败日志 + // //await new ApplicationException(result.Message).LogAsync(Db); + //} + + feeList.Clear(); + } + } + + await tenantDb.Ado.CommitTranAsync(); + } + catch (Exception ex) + { + await tenantDb.Ado.RollbackTranAsync(); + //await ex.LogAsync(Db); + } + } + + static async Task?> CreateFeesIfMatchAsync(SqlSugarClient tenantDb, SeaExportRes order, FeeCustTemplate template) + { + if (!string.IsNullOrEmpty(template.POLCode) && template.POLCode != order.LoadPortCode) + return null; + + if (!string.IsNullOrEmpty(template.PODCode) && template.PODCode != order.DischargePortCode) + return null; + + if (!string.IsNullOrEmpty(template.MBLFrtCode) && template.MBLFrtCode != order.MBLFrtCode) + return null; + + if (template.LaneId.HasValue && template.LaneId != order.LaneId) + return null; + + if (template.CarrierId.HasValue && template.CarrierId != order.CarrierId) + return null; + + if (template.SourceId.HasValue && template.SourceId != order.SourceId) + return null; + + if (template.ForwarderId.HasValue && template.ForwarderId != order.ForwarderId) + return null; + + if (!string.IsNullOrEmpty(template.Condition)) //设置了自定义匹配条件 + { + var conditionModel = JsonConvert.DeserializeObject(template.Condition); + if (!IsMatch(order, conditionModel)) + return null; + } + + if (await tenantDb.Queryable().AnyAsync(x => x.BusinessId == order.Id && x.BusinessType == BusinessType.OceanShippingExport && + x.FeeType == template.FeeType && x.FeeCategoryId == template.FeeCategoryId)) + return null; + + var details = await tenantDb.Queryable().Where(y => y.TemplateId == template.Id) + .Select(x => new FeeRecord + { + BusinessId = order.Id, + BusinessType = BusinessType.OceanShippingExport, + FeeType = template.FeeType, + FeeId = x.FeeId, + FeeCode = x.FeeCode, + FeeName = x.FeeName, + CustomerId = x.CustomerId, + CustomerName = x.CustomerName, + CustomerType = x.CustomerType, + Unit = x.Unit, + UnitPrice = SqlFunc.IsNull(x.UnitPrice.Value, 0), + //Quantity = x.IsCtn ? 1 : 0, + Currency = x.Currency, + ExchangeRate = x.ExchangeRate, + TaxRate = SqlFunc.IsNull(x.TaxRate.Value, 0), + AccTaxRate = SqlFunc.IsNull(x.AccTaxRate.Value, 0), + Tax = SqlFunc.IsNull(x.Tax.Value, 0), + TaxUnitPrice = SqlFunc.IsNull(x.TaxUnitPrice.Value, 0), + IsInvoice = x.IsInvoice, + IsAdvancedPay = x.IsAdvancedPay, + Remark = template.FeeCategoryName, + TemplateId = x.TemplateId, + InputMethod = InputMethod.Automatic + }).ToListAsync(); + + foreach (var detail in details) + { + if (detail.CustomerId == 0) + { + switch (detail.CustomerType) + { + case "controller": + detail.CustomerId = order.CustomerId; + detail.CustomerName = order.CustomerName; + break; + + case "yard": + detail.CustomerId = order.YardId; + detail.CustomerName = order.Yard; + break; + + case "custom": + detail.CustomerId = order.CustomserId; + detail.CustomerName = order.Customser; + break; + + case "contract": + detail.CustomerId = order.ContractClientId; + detail.CustomerName = order.ContractClientName; + break; + + case "shipagency": + detail.CustomerId = order.ShipAgencyId; + detail.CustomerName = order.ShipAgency; + break; + + case "shipper": + detail.CustomerId = order.ShipperId.GetValueOrDefault(); + detail.CustomerName = order.Shipper; + break; + + case "truck": + detail.CustomerId = order.TruckerId; + detail.CustomerName = order.Trucker; + break; + + case "booking": + detail.CustomerId = order.ForwarderId; + detail.CustomerName = order.Forwarder; + break; + + case "carrier": + detail.CustomerId = order.CarrierId; + detail.CustomerName = order.Carrier; + break; + + case "wareHouse": + detail.CustomerId = order.WareHouseId; + detail.CustomerName = order.WareHouse; + break; + + case "shippercn": + detail.CustomerId = order.ShipperCnId.GetValueOrDefault(); + detail.CustomerName = order.ShipperCn; + break; + + case "agent": + detail.CustomerId = order.AgentId.GetValueOrDefault(); + detail.CustomerName = order.Agent; + break; + } + } } + + return details; + } + + /// + /// 根据指定条件返回是否匹配的结果 + /// + /// 要对比的数据源 + /// 匹配条件 + /// + static bool IsMatch(object source, ConditionContent condition) + { + if (source == null || condition == null) + return false; + + TaskFlowDataContext dataContext = new((TaskFlowDataNameConst.Business, source)); + if (string.IsNullOrEmpty(condition.SourceName)) + condition.SourceName = TaskFlowDataNameConst.Business; + + return ConditionHelper.IsPass(condition, dataContext); } } } diff --git a/ds-wms-service/DS.WMS.Core/Info/Method/ClientInfoService.cs b/ds-wms-service/DS.WMS.Core/Info/Method/ClientInfoService.cs index 058379d6..bfc72a5a 100644 --- a/ds-wms-service/DS.WMS.Core/Info/Method/ClientInfoService.cs +++ b/ds-wms-service/DS.WMS.Core/Info/Method/ClientInfoService.cs @@ -558,8 +558,18 @@ public class ClientInfoService : ServiceBase, IClientInfoService .And(x => x.ShortName == req.ShortName.Trim() || x.Description == req.Description.Trim()) .AndIF(req.Id > 0, x => x.Id != req.Id); - if (await TenantDb.Queryable().Where(expr.ToExpression()).AnyAsync()) - return DataResult.Failed("客户信息已存在,请检查客户全称/简称是否重复!", MultiLanguageConst.ClientInfoExist); + var client = await TenantDb.Queryable().Where(expr.ToExpression()).Select(x => new + { + ClientId = x.Id, + x.ShortName, + x.Description + }).FirstAsync(); + if (client != null) + { + var result = DataResult.Failed("客户信息已存在,请检查客户全称/简称是否重复!", MultiLanguageConst.ClientInfoExist); + result.Code = ResultCode.Fail; + result.Data = client; + } return DataResult.Success; } diff --git a/ds-wms-service/DS.WMS.Core/Info/Method/ClientStakeholderService.cs b/ds-wms-service/DS.WMS.Core/Info/Method/ClientStakeholderService.cs index 6130e96f..78b6110e 100644 --- a/ds-wms-service/DS.WMS.Core/Info/Method/ClientStakeholderService.cs +++ b/ds-wms-service/DS.WMS.Core/Info/Method/ClientStakeholderService.cs @@ -266,9 +266,10 @@ namespace DS.WMS.Core.Info.Method public async Task EditAsync(InfoClientStakeholder stakeholder) { await TenantDb.Storageable(stakeholder).DefaultAddElseUpdate().ExecuteCommandAsync(); - return DataResult.Success; + var result = DataResult.Success; + result.Data = stakeholder; + return result; } - } }