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; using System.Collections; using LanguageExt.ClassInstances.Pred; using DS.WMS.Core.Fee.Dtos; using Microsoft.EntityFrameworkCore.Metadata.Internal; 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.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(); var details = tenantDb.Queryable().Where(x => x.CheckId == checkId && !req.Ids.Contains(x.Id)).ToList(); if (details.Count > 0) { if (details.Where(x => x.IsEqual == false).Any()) { info.CheckStatus = 2; info.CheckStatusName = "对账不一致"; } else { info.CheckStatus = 1; info.CheckStatusName = "对账一致"; } tenantDb.Updateable(info).ExecuteCommand(); } return DataResult.Successed("删除成功!", MultiLanguageConst.DataDelSuccess); } /// /// 自动对账批量删除 /// /// 主表Ids /// public DataResult BatchDelCheckBillAuto(IdModel req) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); if (req.Ids.Length == 0) return DataResult.Failed("业务Ids不能为空"); if (tenantDb.Queryable().Where(x => req.Ids.Contains(x.Id) && (x.BillNo != null && x.BillNo != "")).Any()) { return DataResult.Failed("存在已生成的对账信息!"); } var billLists = tenantDb.Queryable().Where(x => req.Ids.Contains(x.Id)).ToList(); if (billLists.Count > 0) { var details = tenantDb.Queryable().Where(x => req.Ids.Contains(x.CheckId)).ToList(); if (details.Count > 0) { tenantDb.Deleteable(details).ExecuteCommand(); } tenantDb.Deleteable(billLists).ExecuteCommand(); } return DataResult.Successed("删除成功!", MultiLanguageConst.DataDelSuccess); } /// /// 自动对账重新匹配 /// /// 主表Id /// public DataResult RecountCheckBillAuto(IdModel req) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); if (req.Id.IsNull()) return DataResult.Failed("业务Id不能为空"); var info = tenantDb.Queryable().Where(x => x.Id == long.Parse(req.Id)).First(); if (info.IsNotNull()) { if (info.BillNo.IsNotNull()) { return DataResult.Failed("存在已生成的对账信息!"); } var list = tenantDb.Queryable().Where(x => x.CheckId == long.Parse(req.Id)).ToList(); foreach (var bill in list) { var mblno = bill.MBLNO; var usdAmount = 0M; var rmbAmount = 0M; var bizList = tenantDb.Queryable().Where(x => x.MBLNO.Contains(mblno)).ToList(); var mblnos = bizList.Select(x => x.MBLNO).Distinct(); foreach (var temp in mblnos) { var mblno1 = temp; 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; } } } } bill.RMBLocalAmount = rmbAmount; bill.USDLocalAmount = usdAmount; bill.RMBDifferenceAmount = rmbAmount - bill.RMBCheckAmount; bill.USDDifferenceAmount = usdAmount - bill.USDCheckAmount; bill.IsEqual = (rmbAmount - bill.RMBCheckAmount == 0 && usdAmount - bill.USDCheckAmount == 0) ? true : false; } tenantDb.Updateable(list).ExecuteCommand(); //更新主表 if (list.Where(x => x.IsEqual == false).Any()) { info.CheckStatus = 2; info.CheckStatusName = "对账不一致"; } else { info.CheckStatus = 1; info.CheckStatusName = "对账一致"; } tenantDb.Updateable(info).ExecuteCommand(); } else { return DataResult.Failed("自动对账主表信息不存在"); } return DataResult.Successed("重新匹配成功!", MultiLanguageConst.DataUpdateSuccess); } /// /// 生成对账单 /// /// 主表Id /// public async Task CreateCheckBill(IdModel req) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); if (req.Id.IsNull()) return await Task.FromResult(DataResult.Failed("业务Id不能为空")); var info = await tenantDb.Queryable().Where(x => x.Id == long.Parse(req.Id)).FirstAsync(); if (info.IsNull()) return await Task.FromResult(DataResult.Failed("自动对账主表信息不存在")); if (info.BillNo.IsNotNull()) return await Task.FromResult(DataResult.Failed("对账信息已生成!")); var list = await tenantDb.Queryable().Where(x => x.CheckId == long.Parse(req.Id) && x.IsEqual == true).ToListAsync(); if (list.Count == 0) return await Task.FromResult(DataResult.Failed("自动对账明细无一致的明细!")); await tenantDb.Ado.BeginTranAsync(); try { var data = info.Adapt(); var sequence = commonService.GetSequenceNext(); if (!sequence.Succeeded) { return await Task.FromResult(DataResult.Failed(sequence.Message, MultiLanguageConst.SequenceSetNotExist)); } data.BillNo = sequence.Data; var entity = await tenantDb.Insertable(data).ExecuteReturnEntityAsync(); var businessList = new List(); var feeList = new List(); foreach (var bill in list) { var mblno = bill.MBLNO; var bizList = tenantDb.Queryable().Where(x => x.MBLNO.Contains(mblno)).ToList(); var mblnos = bizList.Select(x => x.MBLNO).Distinct(); foreach (var temp in mblnos) { var mblno1 = temp; var bizlist1 = bizList.Where(x => x.MBLNO == mblno1).ToList(); //var mainBiz = bizlist1[0]; foreach (var biz in bizlist1) { var feeRecordList = tenantDb.Queryable().Where(x => x.BusinessId == biz.BusinessId && x.CustomerId == info.CustomerId && x.FeeType == FeeType.Payable).ToList(); if (feeRecordList.Count > 0) { businessList.Add(new CheckBillBusiness() { CheckId = entity.Id, BusinessId = biz.BusinessId }); foreach (var record in feeRecordList) { feeList.Add(new CheckBillFeeRecords() { CheckId = entity.Id, BusinessId = biz.BusinessId, FeeRecordId = record.Id, CheckBillAmount = record.Amount, }); //对账标识 record.IsDebit = true; record.DebitNo = entity.Id.ToString(); } //更新费用记录 await tenantDb.Updateable(feeRecordList).ExecuteCommandAsync(); } } } } if (businessList.Count > 0) { await tenantDb.Insertable(businessList).ExecuteCommandAsync(); if (feeList.Count > 0) { await tenantDb.Insertable(feeList).ExecuteCommandAsync(); } } info.BillNo = sequence.Data; await tenantDb.Updateable(info).ExecuteCommandAsync(); await tenantDb.Ado.CommitTranAsync(); return await Task.FromResult(DataResult.Successed("生成对账单成功!", MultiLanguageConst.DataUpdateSuccess)); } catch (Exception ex) { await tenantDb.Ado.RollbackTranAsync(); //await ex.LogAsync(tenantDb); return DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed)); } } /// /// 取消生成的对账单 /// /// 主表Id /// public async Task CancelCheckBill(IdModel req) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); if (req.Id.IsNull()) return await Task.FromResult(DataResult.Failed("业务Id不能为空")); var info = await tenantDb.Queryable().Where(x => x.Id == long.Parse(req.Id)).FirstAsync(); if (info.IsNull()) return await Task.FromResult(DataResult.Failed("自动对账主表信息不存在")); if (info.BillNo.IsNull()) return await Task.FromResult(DataResult.Failed("对账信息未生成!")); var bill = await tenantDb.Queryable().Where(x => x.BillNo == info.BillNo).FirstAsync(); if (bill.IsNull()) return await Task.FromResult(DataResult.Failed("对账信息不存在!")); if ((bool)bill.IsLocking) return await Task.FromResult(DataResult.Failed("对账信息已锁定!")); await tenantDb.Ado.BeginTranAsync(); try { var billlist = await tenantDb.Queryable().Where(x => x.CheckId == bill.Id).ToListAsync(); var feelist = await tenantDb.Queryable().Where(x => x.CheckId == bill.Id).ToListAsync(); await tenantDb.Deleteable(feelist).ExecuteCommandAsync(); await tenantDb.Deleteable(billlist).ExecuteCommandAsync(); await tenantDb.Deleteable(bill).ExecuteCommandAsync(); info.BillNo = ""; await tenantDb.Updateable(info).ExecuteCommandAsync(); await tenantDb.Ado.CommitTranAsync(); return await Task.FromResult(DataResult.Successed("取消对账单成功!", MultiLanguageConst.DataUpdateSuccess)); } catch (Exception ex) { await tenantDb.Ado.RollbackTranAsync(); //await ex.LogAsync(tenantDb); return DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed)); } } } }