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.

291 lines
12 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;
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.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<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)
tenantDb.Deleteable(businessList).ExecuteCommand();
return DataResult.Successed("删除成功!", MultiLanguageConst.DataDelSuccess);
}
}
}