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.

568 lines
24 KiB
C#

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;
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public CheckBillAutoService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
saasService = _serviceProvider.GetRequiredService<ISaasDbService>();
commonService = _serviceProvider.GetRequiredService<ICommonService>();
}
public DataResult EditCheckBillAutoInfo(CheckBillAutoReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
if (req.Id == 0)
{
var data = req.Adapt<CheckBillAuto>();
var sequence = commonService.GetSequenceNext<CheckBillAuto>();
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<CheckBillAuto>().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<List<CheckBillAutoDetailRes>> GetCheckBillAutoDetailList(PageRequest request)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = tenantDb.Queryable<CheckBillAutoDetail>()
.Where(whereList).Select<CheckBillAutoDetailRes>()
.ToQueryPage(request.PageCondition);
return data;
}
public DataResult<CheckBillAutoRes> GetCheckBillAutoInfo(string id)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var data = tenantDb.Queryable<CheckBillAuto>()
.Where(a => a.Id == long.Parse(id))
.Select<CheckBillAutoRes>()
.First();
return DataResult<CheckBillAutoRes>.Success(data, MultiLanguageConst.DataQuerySuccess);
}
public DataResult<List<CheckBillAutoRes>> GetCheckBillAutoList(PageRequest request)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = tenantDb.Queryable<CheckBillAuto>()
.Where(whereList).Select<CheckBillAutoRes>()
.ToQueryPage(request.PageCondition);
return data;
}
public async Task<DataResult> CreateCheckBillAutoByExcel(string id, List<CheckBillAutoExcelReq> req)
{
if (req.IsNull() || req.Count == 0)
return await Task.FromResult(DataResult.Failed("导入excel无数据"));
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var info = tenantDb.Queryable<CheckBillAuto>().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<VW_Op_Business>().Where(x => nums.Contains(x.MBLNO)).Any())//|| nums.Contains(x.HBLNO)
{
return await Task.FromResult(DataResult.Failed("请检查导入提单号信息"));
}
var details = new List<CheckBillAutoDetail>();
foreach (var item in req)
{
var mblno = item.;
var bizList = tenantDb.Queryable<VW_Op_Business>().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<FeeRecord>().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<FeeRecord>().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("导入成功!"));
}
/// <summary>
/// 对账明细批量删除
/// </summary>
/// <param name="req">主表Id及明细业务Ids</param>
/// <returns></returns>
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<CheckBillAuto>().First(x => x.Id == checkId);
if (info.IsNull())
return DataResult.Failed("客户对账信息不存在");
if (req.Ids.Length == 0)
return DataResult.Failed("明细业务Ids不能为空");
var businessList = tenantDb.Queryable<CheckBillAutoDetail>().Where(x => x.CheckId == checkId && req.Ids.Contains(x.Id)).ToList();
if (businessList.Count > 0)
5 months ago
tenantDb.Deleteable(businessList).ExecuteCommand();
var details = tenantDb.Queryable<CheckBillAutoDetail>().Where(x => x.CheckId == checkId && !req.Ids.Contains(x.Id)).ToList();
if (details.Count > 0)
{
5 months ago
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);
}
5 months ago
/// <summary>
/// 自动对账批量删除
/// </summary>
/// <param name="req">主表Ids</param>
/// <returns></returns>
public DataResult BatchDelCheckBillAuto(IdModel req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
if (req.Ids.Length == 0)
return DataResult.Failed("业务Ids不能为空");
if (tenantDb.Queryable<CheckBillAuto>().Where(x => req.Ids.Contains(x.Id) && (x.BillNo != null && x.BillNo != "")).Any())
{
return DataResult.Failed("存在已生成的对账信息!");
}
var billLists = tenantDb.Queryable<CheckBillAuto>().Where(x => req.Ids.Contains(x.Id)).ToList();
if (billLists.Count > 0)
{
var details = tenantDb.Queryable<CheckBillAutoDetail>().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);
}
/// <summary>
/// 自动对账重新匹配
/// </summary>
/// <param name="req">主表Id</param>
5 months ago
/// <returns></returns>
public DataResult RecountCheckBillAuto(IdModel req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
if (req.Id.IsNull())
return DataResult.Failed("业务Id不能为空");
var info = tenantDb.Queryable<CheckBillAuto>().Where(x => x.Id == long.Parse(req.Id)).First();
if (info.IsNotNull())
5 months ago
{
if (info.BillNo.IsNotNull())
5 months ago
{
return DataResult.Failed("存在已生成的对账信息!");
5 months ago
}
var list = tenantDb.Queryable<CheckBillAutoDetail>().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<VW_Op_Business>().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<FeeRecord>().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();
5 months ago
}
else
{
5 months ago
return DataResult.Failed("自动对账主表信息不存在");
5 months ago
}
return DataResult.Successed("重新匹配成功!", MultiLanguageConst.DataUpdateSuccess);
}
/// <summary>
/// 生成对账单
/// </summary>
/// <param name="req">主表Id</param>
/// <returns></returns>
public async Task<DataResult> 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<CheckBillAuto>().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<CheckBillAutoDetail>().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<CheckBill>();
var sequence = commonService.GetSequenceNext<CheckBill>();
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<CheckBillBusiness>();
var feeList = new List<CheckBillFeeRecords>();
foreach (var bill in list)
{
var mblno = bill.MBLNO;
var bizList = tenantDb.Queryable<VW_Op_Business>().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<FeeRecord>().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));
}
}
/// <summary>
/// 取消生成的对账单
/// </summary>
/// <param name="req">主表Id</param>
/// <returns></returns>
public async Task<DataResult> 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<CheckBillAuto>().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<CheckBill>().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<CheckBillBusiness>().Where(x => x.CheckId == bill.Id).ToListAsync();
var feelist = await tenantDb.Queryable<CheckBillFeeRecords>().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));
}
5 months ago
}
}
}