using DS.Module.Core; using DS.Module.SqlSugar; using DS.Module.UserModule; using DS.WMS.Core.Check.Dtos; using DS.WMS.Core.Check.Interface; using DS.WMS.Core.Fee.Entity; using DS.WMS.Core.Sys.Interface; using Microsoft.Extensions.DependencyInjection; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using DS.Module.Core.Extensions; using DS.WMS.Core.Check.Entity; using Mapster; using DS.WMS.Core.Op.View; using MathNet.Numerics.LinearAlgebra.Factorization; using NPOI.SS.Formula.Functions; using NPOI.Util; using Quartz.Logging; using DS.Module.Core.Data; namespace DS.WMS.Core.Check.Method { public class CheckBillAutoService : ICheckBillAutoService { private readonly IServiceProvider _serviceProvider; private readonly ISqlSugarClient db; private readonly IUser user; private readonly ISaasDbService saasService; private readonly ICommonService commonService; /// /// /// /// public CheckBillAutoService(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; db = _serviceProvider.GetRequiredService(); user = _serviceProvider.GetRequiredService(); saasService = _serviceProvider.GetRequiredService(); commonService = _serviceProvider.GetRequiredService(); } public DataResult EditCheckBillAutoInfo(CheckBillAutoReq req) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); if (req.Id == 0) { var data = req.Adapt(); var sequence = commonService.GetSequenceNext(); if (!sequence.Succeeded) { return DataResult.Failed(sequence.Message, MultiLanguageConst.SequenceSetNotExist); } data.AutoBillNo = sequence.Data; data.CheckStatus = 0; data.CheckStatusName = "未对账"; var entity = tenantDb.Insertable(data).ExecuteReturnEntity(); return DataResult.Successed("添加成功!", entity.Id, MultiLanguageConst.DataCreateSuccess); } else { var info = tenantDb.Queryable().Where(x => x.Id == req.Id).First(); info = req.Adapt(info); tenantDb.Updateable(info).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand(); return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess); } } public DataResult> GetCheckBillAutoDetailList(PageRequest request) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); //序列化查询条件 var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition); var data = tenantDb.Queryable() .Where(whereList).Select() .ToQueryPage(request.PageCondition); return data; } public DataResult GetCheckBillAutoInfo(string id) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); var data = tenantDb.Queryable() .Where(a => a.Id == long.Parse(id)) .Select() .First(); return DataResult.Success(data, MultiLanguageConst.DataQuerySuccess); } public DataResult> GetCheckBillAutoList(PageRequest request) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); //序列化查询条件 var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition); var data = tenantDb.Queryable() .Where(whereList).Select() .ToQueryPage(request.PageCondition); return data; } public async Task CreateCheckBillAutoByExcel(string id, List req) { if (req.IsNull() || req.Count == 0) return await Task.FromResult(DataResult.Failed("导入excel无数据")); var tenantDb = saasService.GetBizDbScopeById(user.TenantId); var info = tenantDb.Queryable().First(x => x.Id == long.Parse(id)); if (info.IsNull()) { return await Task.FromResult(DataResult.Failed("自动对账主表信息不存在!")); } if (info.CustomerId == 0 || info.CustomerName.IsNull()) { return await Task.FromResult(DataResult.Failed("请维护主单结算对象!")); } var nums = req.Select(x => x.提单号); if (!tenantDb.Queryable().Where(x => nums.Contains(x.MBLNO)).Any())//|| nums.Contains(x.HBLNO) { return await Task.FromResult(DataResult.Failed("请检查导入提单号信息")); } var details = new List(); foreach (var item in req) { var mblno = item.提单号; var bizList = tenantDb.Queryable().Where(x => x.CustomerId == info.CustomerId && x.MBLNO.Contains(mblno)).ToList(); if (bizList.Count > 1) { var mblnos = bizList.Select(x => x.MBLNO).Distinct(); foreach (var temp in mblnos) { var mblno1 = temp; var usdAmount = 0M; var rmbAmount = 0M; var bizlist1 = bizList.Where(x => x.MBLNO == mblno1).ToList(); var mainBiz = bizlist1[0]; foreach (var biz in bizlist1) { var feeList = tenantDb.Queryable().Where(x => x.BusinessId == biz.BusinessId && x.CustomerId == info.CustomerId && x.FeeType == FeeType.Payable).ToList(); foreach (var record in feeList) { if (record.Currency == "RMB") { rmbAmount += record.Amount; } if (record.Currency == "USD") { usdAmount += record.Amount; } } } var detail = new CheckBillAutoDetail() { CheckId = info.Id, MBLNO = mainBiz.MBLNO, RMBLocalAmount = rmbAmount, USDLocalAmount = usdAmount, RMBCheckAmount = item.RMB金额, USDCheckAmount = item.USD金额, RMBDifferenceAmount = rmbAmount - item.RMB金额, USDDifferenceAmount = usdAmount - item.USD金额, CustomerId = info.CustomerId, CustomerName = info.CustomerName, ETD = mainBiz.ETD, Vessel = mainBiz.Vessel, VesselId = mainBiz.VesselId, Voyno = mainBiz.Voyno, OperatorId = mainBiz.OperatorId, OperatorName = mainBiz.OperatorName, SaleId = mainBiz.SaleId, SaleName = mainBiz.SaleName, IsEqual = (rmbAmount - item.RMB金额 == 0 && usdAmount - item.USD金额 == 0) ? true : false, }; details.Add(detail); } } else if (bizList.Count == 1) { var biz = bizList[0]; var feeList = tenantDb.Queryable().Where(x => x.BusinessId == biz.BusinessId && x.CustomerId == info.CustomerId && x.FeeType == FeeType.Payable).ToList(); var usdAmount = 0M; var rmbAmount = 0M; foreach (var record in feeList) { if (record.Currency == "RMB") { rmbAmount += record.Amount; } if (record.Currency == "USD") { usdAmount += record.Amount; } } var detail = new CheckBillAutoDetail() { CheckId = info.Id, MBLNO = biz.MBLNO, RMBLocalAmount = rmbAmount, USDLocalAmount = usdAmount, RMBCheckAmount = item.RMB金额, USDCheckAmount = item.USD金额, RMBDifferenceAmount = rmbAmount - item.RMB金额, USDDifferenceAmount = usdAmount - item.USD金额, CustomerId = info.CustomerId, CustomerName = info.CustomerName, ETD = biz.ETD, Vessel = biz.Vessel, VesselId = biz.VesselId, Voyno = biz.Voyno, OperatorId = biz.OperatorId, OperatorName = biz.OperatorName, SaleId = biz.SaleId, SaleName = biz.SaleName, IsEqual = (rmbAmount - item.RMB金额 == 0 && usdAmount - item.USD金额 == 0) ? true : false, }; details.Add(detail); } else { continue; } } if (details.Count > 0) { await tenantDb.Insertable(details).ExecuteCommandAsync(); if (details.Where(x => x.IsEqual == false).Any()) { info.CheckStatus = 2; info.CheckStatusName = "对账不一致"; } else { info.CheckStatus = 1; info.CheckStatusName = "对账一致"; } await tenantDb.Updateable(info).ExecuteCommandAsync(); } else { return await Task.FromResult(DataResult.Failed("无匹配的数据!")); } return await Task.FromResult(DataResult.Successed("导入成功!")); } /// /// 对账明细批量删除 /// /// 主表Id及明细业务Ids /// public DataResult BatchDelCheckBillAutoDetail(IdModel req) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); var checkId = long.Parse(req.Id); if (req.Id.IsNull()) return DataResult.Failed("主表Id不能为空"); var info = tenantDb.Queryable().First(x => x.Id == checkId); if (info.IsNull()) return DataResult.Failed("客户对账信息不存在"); if (req.Ids.Length == 0) return DataResult.Failed("明细业务Ids不能为空"); var businessList = tenantDb.Queryable().Where(x => x.CheckId == checkId && req.Ids.Contains(x.Id)).ToList(); if (businessList.Count > 0) tenantDb.Deleteable(businessList).ExecuteCommand(); return DataResult.Successed("删除成功!", MultiLanguageConst.DataDelSuccess); } } }