411 lines
20 KiB
C#
411 lines
20 KiB
C#
using AutoMapper;
|
||
using Microsoft.EntityFrameworkCore.Storage;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using WMS.Web.Core.Dto;
|
||
using WMS.Web.Core.Dto.Erp.OutStock;
|
||
using WMS.Web.Core.Dto.Login;
|
||
using WMS.Web.Core.Help;
|
||
using WMS.Web.Core.Internal.Results;
|
||
using WMS.Web.Domain.Entitys;
|
||
using WMS.Web.Domain.Infrastructure;
|
||
using WMS.Web.Domain.IService;
|
||
using WMS.Web.Domain.IService.Public;
|
||
using WMS.Web.Domain.Values;
|
||
|
||
namespace WMS.Web.Domain.Services
|
||
{
|
||
/// <summary>
|
||
/// 出库任务
|
||
/// </summary>
|
||
public class OutStockTaskService : IOutStockTaskService
|
||
{
|
||
private readonly IMapper _mapper;
|
||
private readonly IErpService _erpService;
|
||
private readonly ILoginService _loginService;
|
||
private readonly IBasicsRepositories _transactionRepositories;
|
||
private readonly IOutStockRepositories _outStockRepositories;
|
||
private readonly IOutStockTaskRepositories _outStockTaskRepositories;
|
||
private readonly IErpOpsSyncDateRepositories _erpOpsSyncDateRepositories;
|
||
public OutStockTaskService(IMapper mapper, IErpService erpService, ILoginService loginService,
|
||
IBasicsRepositories transactionRepositories,
|
||
IOutStockRepositories outStockRepositories, IOutStockTaskRepositories outStockTaskRepositories, IErpOpsSyncDateRepositories erpOpsSyncDateRepositories)
|
||
{
|
||
_mapper = mapper;
|
||
_erpService = erpService;
|
||
_loginService = loginService;
|
||
_transactionRepositories = transactionRepositories;
|
||
_outStockRepositories = outStockRepositories;
|
||
_outStockTaskRepositories = outStockTaskRepositories;
|
||
_erpOpsSyncDateRepositories = erpOpsSyncDateRepositories;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 出库任务作废
|
||
/// </summary>
|
||
/// <param name="dto"></param>
|
||
/// <returns></returns>
|
||
public async Task<Result> Repeal(OperateRequest dto, LoginInDto loginInfo)
|
||
{
|
||
var list = await _outStockTaskRepositories.GetEntityList(dto.Ids);
|
||
foreach (var entity in list)
|
||
{
|
||
//作废
|
||
entity.Repeal(loginInfo.UserInfo.StaffId);
|
||
}
|
||
var isSuccess = await _outStockTaskRepositories.EditEntityList(list, true);
|
||
if (!isSuccess) return Result.ReFailure(ResultCodes.DateWriteError);
|
||
|
||
return Result.ReSuccess();
|
||
}
|
||
/// <summary>
|
||
/// 出库任务合并
|
||
/// </summary>
|
||
/// <param name="dto"></param>
|
||
/// <returns></returns>
|
||
public async Task<Result> merge(OperateRequest dto, LoginInDto loginInfo)
|
||
{
|
||
var list = await _outStockTaskRepositories.GetEntityList(dto.Ids);
|
||
// 2:符合合并数据逻辑:出库状态为”待拣货”+出库类型为:销售出库+发货组织一致+收货客户一致+发货仓库一致
|
||
OutStockTask entity = new OutStockTask();
|
||
var res = entity.Merge(list, loginInfo.UserInfo.StaffId);
|
||
if (!res.IsSuccess) return res;
|
||
|
||
IDbContextTransaction _transaction = _transactionRepositories.GetTransaction();
|
||
bool isRollback = false;
|
||
bool isSuccess = true;
|
||
entity = await _outStockTaskRepositories.Add(entity, false);
|
||
if (entity == null) isRollback = true;
|
||
isSuccess = await _outStockTaskRepositories.DeleteEntityList(list.Select(s => s.Id).ToList(), false);
|
||
if (isSuccess == false) isRollback = true;
|
||
//提交事务
|
||
isSuccess = _transactionRepositories.CommitTransaction(isRollback, _transaction);
|
||
if (!isSuccess)
|
||
return Result.ReFailure(ResultCodes.DateWriteError);
|
||
|
||
return Result.ReSuccess();
|
||
}
|
||
|
||
/// <summary>
|
||
/// erp数据转化wms 执行数据库操作
|
||
/// </summary>
|
||
/// <param name="erp_list"></param>
|
||
/// <param name="isTransaction"></param>
|
||
/// <returns></returns>
|
||
public async Task<Result> SsynDate(List<ErpDeliveryNoticeOutStockResultDto> erp_list, bool isTransaction)
|
||
{
|
||
//2.通过单据编号找到wms系统现有的任务单;并修改
|
||
var erp_removeList = new List<ErpDeliveryNoticeOutStockResultDto>();
|
||
var SourceBillNo_list = erp_list.GroupBy(x => x.SourceBillNo).Select(x => x.Key).ToList();
|
||
var data_list = await _outStockTaskRepositories.GetListBySourceBillNo(SourceBillNo_list);
|
||
if (data_list.Count != 0)
|
||
{
|
||
//2.1提取出wms任务单明细信息
|
||
foreach (var outStockTask in data_list)
|
||
{
|
||
var sourcNos = outStockTask.Details.SelectMany(s => s.ErpDetails).Select(s => s.SourceBillNo).ToList();
|
||
//仓库不同 拆分成不同的任务单
|
||
var erps = erp_list.Where(w => sourcNos.Contains(w.SourceBillNo) && w.StockCode.Equals(outStockTask.StockCode)).ToList();
|
||
//比对数据
|
||
SsynDateComparison(outStockTask, erps);
|
||
foreach (var erp in erps)
|
||
{
|
||
//仓库不同 拆分成不同的
|
||
var detail = outStockTask.Details.FirstOrDefault(w => w.MaterialNumber == erp.MaterialNumber);
|
||
if (detail == null)
|
||
{
|
||
//添加一条物料明细
|
||
detail = _mapper.Map<OutStockTaskDetails>(erp);
|
||
var erpDetail = _mapper.Map<OutStockTaskErpDetails>(erp);
|
||
detail.ErpDetails.Add(erpDetail);
|
||
outStockTask.Details.Add(detail);
|
||
}
|
||
else
|
||
{
|
||
//找到物料明细下面对应的金蝶明细Id 然后修改(跟金蝶明细一一对应)
|
||
var erpDetail = detail.ErpDetails.FirstOrDefault(f => f.Erp_DetailId == erp.Erp_DetailId);
|
||
if (erpDetail == null)
|
||
{
|
||
erpDetail = _mapper.Map<OutStockTaskErpDetails>(erp);
|
||
detail.ErpDetails.Add(erpDetail);
|
||
}
|
||
else
|
||
erpDetail.AccruedQty = erp.AccruedQty;
|
||
|
||
detail.AccruedQty = detail.ErpDetails.Sum(s => s.AccruedQty);
|
||
}
|
||
//操作完后剔除
|
||
erp_removeList.Add(erp);
|
||
}
|
||
//执行完后重新计算一下状态
|
||
outStockTask.GenerateStatus();
|
||
}
|
||
}
|
||
//2.2.提交修改
|
||
var isSuccess = await _outStockTaskRepositories.EditEntityList(data_list, isTransaction);
|
||
if (!isSuccess)
|
||
return Result.ReFailure(ResultCodes.DateWriteError);
|
||
|
||
//2.3剔除:已修改的单据
|
||
foreach (var item in erp_removeList)
|
||
erp_list.Remove(item);
|
||
|
||
//3.wms任务单的来源单据编号不存在于erp中,那么就新增
|
||
if (erp_list.Count != 0)
|
||
{
|
||
var add_entitys = new List<OutStockTask>();
|
||
//根据来源订单号和仓库分组 一个来源订单号和一个仓库 对应一个任务单
|
||
var billNos = erp_list.GroupBy(x => (x.SourceBillNo, x.StockCode)).Select(x => x.Key).ToList();
|
||
foreach (var item in billNos)
|
||
{
|
||
var eList = erp_list.Where(f => f.SourceBillNo == item.SourceBillNo && f.StockCode == item.StockCode).ToList();
|
||
var e = eList.First();
|
||
var entity = new OutStockTask();
|
||
entity.Create((OutStockType)e.Type, e.StockCode, e.OrgCode, e.DeliveryOrgId, e.ReceiptCustomerId, (DateTime)e.CreateTime);
|
||
|
||
//找到当前对应来源单据编号的集合数据
|
||
var mIds = eList.GroupBy(g => g.MaterialNumber).Select(s => s.Key).ToList();
|
||
//给到dto的实体明细中
|
||
foreach (var mid in mIds)
|
||
{
|
||
var emList = eList.Where(w => w.MaterialNumber == mid).ToList();
|
||
var detail = _mapper.Map<OutStockTaskDetails>(emList.First());
|
||
var erpDetail = _mapper.Map<List<OutStockTaskErpDetails>>(emList);
|
||
detail.ErpDetails.AddRange(erpDetail);
|
||
|
||
detail.AccruedQty = detail.ErpDetails.Sum(s => s.AccruedQty);
|
||
entity.Details.Add(detail);
|
||
}
|
||
add_entitys.Add(entity);
|
||
}
|
||
//3.1提交新增
|
||
isSuccess = await _outStockTaskRepositories.AddRange(add_entitys, isTransaction);
|
||
if (!isSuccess)
|
||
return Result.ReFailure(ResultCodes.DateWriteError);
|
||
}
|
||
|
||
return Result.ReSuccess();
|
||
}
|
||
/// <summary>
|
||
/// 金蝶数据同步时比对数据
|
||
/// 金蝶单据明细行删除后wms明细数量调整为0
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public Result SsynDateComparison(OutStockTask outStockTask, List<ErpDeliveryNoticeOutStockResultDto> erp_list)
|
||
{
|
||
var erpDetails = outStockTask.Details.SelectMany(s => s.ErpDetails).ToList().Clone();
|
||
foreach (var d in outStockTask.Details)
|
||
{
|
||
foreach (var ed in d.ErpDetails)
|
||
{
|
||
//没有找到这条出库任务单里的来源单信息,则跳过这条数据(有些合并的单据,当前没有找到金蝶对应的单据(时间超出范围了等情况))
|
||
var erp_o = erp_list.Where(w => w.SourceBillNo == ed.SourceBillNo).ToList();
|
||
if (erp_o.Count() == 0) continue;
|
||
|
||
var erp_d = erp_list.Where(w => w.MaterialNumber == d.MaterialNumber && ed.Erp_DetailId == w.Erp_DetailId).ToList();
|
||
if (erp_d.Count() == 0)
|
||
{
|
||
//金蝶删除明细数据后 wms对应数据修改为0
|
||
ed.AccruedQty = 0;
|
||
d.AccruedQty = d.ErpDetails.Sum(s => s.AccruedQty);
|
||
}
|
||
}
|
||
}
|
||
return Result.ReSuccess();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 发货通知单同步数据
|
||
/// </summary>
|
||
/// <param name="isTransaction"></param>
|
||
/// <returns></returns>
|
||
public async Task<Result> BillQueryForDeliveryNoticeOutStock(bool isTransaction, List<string> sourceBillNos = null, DateTime? beginTime = null)
|
||
{
|
||
//1.获取金蝶数据:采购订单数据
|
||
var erp_result = await _erpService.BillQueryForDeliveryNoticeOutStock(sourceBillNos, beginTime);
|
||
if (!erp_result.IsSuccess)
|
||
return Result.ReFailure(erp_result.Message, erp_result.Status);
|
||
|
||
return await this.SsynDate(erp_result.Data.ToList(), isTransaction);
|
||
}
|
||
/// <summary>
|
||
/// 直接调拨
|
||
/// </summary>
|
||
/// <param name="isTransaction"></param>
|
||
/// <param name="sourceBillNos"></param>
|
||
/// <returns></returns>
|
||
public async Task<Result> BillQueryForTransferDirectOutStock(bool isTransaction, List<string> sourceBillNos = null, DateTime? beginTime = null)
|
||
{
|
||
var erp_result = await _erpService.BillQueryForTransferDirectOutStock(sourceBillNos, beginTime);
|
||
if (!erp_result.IsSuccess)
|
||
return Result.ReFailure(erp_result.Message, erp_result.Status);
|
||
|
||
return await this.SsynDate(erp_result.Data.ToList(), isTransaction);
|
||
}
|
||
/// <summary>
|
||
/// 分布式调出
|
||
/// </summary>
|
||
/// <param name="isTransaction"></param>
|
||
/// <param name="sourceBillNos"></param>
|
||
/// <returns></returns>
|
||
public async Task<Result> BillQueryForTransferOutOutStock(bool isTransaction, List<string> sourceBillNos = null, DateTime? beginTime = null)
|
||
{
|
||
var erp_result = await _erpService.BillQueryForTransferOutOutStock(sourceBillNos, beginTime);
|
||
if (!erp_result.IsSuccess)
|
||
return Result.ReFailure(erp_result.Message, erp_result.Status);
|
||
|
||
return await this.SsynDate(erp_result.Data.ToList(), isTransaction);
|
||
}
|
||
/// <summary>
|
||
/// 组装拆卸单
|
||
/// </summary>
|
||
/// <param name="isTransaction"></param>
|
||
/// <param name="sourceBillNos"></param>
|
||
/// <returns></returns>
|
||
public async Task<Result> BillQueryForAssembledAppOutStock(bool isTransaction, List<string> sourceBillNos = null, DateTime? beginTime = null)
|
||
{
|
||
List<ErpDeliveryNoticeOutStockResultDto> list = new List<ErpDeliveryNoticeOutStockResultDto>();
|
||
|
||
var erp_result = await _erpService.BillQueryForAssembledAppOutStock_Dassembly(sourceBillNos, beginTime);
|
||
if (!erp_result.IsSuccess)
|
||
return Result.ReFailure(erp_result.Message, erp_result.Status);
|
||
list.AddRange(erp_result.Data);
|
||
|
||
var erp_result_a = await _erpService.BillQueryForAssembledAppOutStock_Assembly(sourceBillNos, beginTime);
|
||
if (!erp_result_a.IsSuccess)
|
||
return Result.ReFailure(erp_result_a.Message, erp_result_a.Status);
|
||
list.AddRange(erp_result_a.Data);
|
||
|
||
return await this.SsynDate(list, isTransaction);
|
||
}
|
||
/// <summary>
|
||
/// 其他入库单
|
||
/// </summary>
|
||
/// <param name="isTransaction"></param>
|
||
/// <param name="sourceBillNos"></param>
|
||
/// <returns></returns>
|
||
public async Task<Result> BillQueryForMisDeliveryOutStock(bool isTransaction, List<string> sourceBillNos = null, DateTime? beginTime = null)
|
||
{
|
||
var erp_result = await _erpService.BillQueryForMisDeliveryOutStock(sourceBillNos, beginTime);
|
||
if (!erp_result.IsSuccess)
|
||
return Result.ReFailure(erp_result.Message, erp_result.Status);
|
||
|
||
return await this.SsynDate(erp_result.Data.ToList(), isTransaction);
|
||
}
|
||
/// <summary>
|
||
/// 销售出库单
|
||
/// </summary>
|
||
/// <param name="isTransaction"></param>
|
||
/// <param name="sourceBillNos"></param>
|
||
/// <param name="beginTime"></param>
|
||
/// <returns></returns>
|
||
public async Task<Result> BillQueryForSalOutStock(bool isTransaction, List<string> sourceBillNos = null, DateTime? beginTime = null)
|
||
{
|
||
var erp_result = await _erpService.BillQueryForSalOutStock(sourceBillNos, beginTime);
|
||
if (!erp_result.IsSuccess)
|
||
return Result.ReFailure(erp_result.Message, erp_result.Status);
|
||
|
||
return await this.SsynDate(erp_result.Data.ToList(), isTransaction);
|
||
}
|
||
/// <summary>
|
||
/// 同步金蝶数据 不传源订单号则更新所有
|
||
/// </summary>
|
||
/// <param name="sourceBillNos"></param>
|
||
/// <returns></returns>
|
||
public async Task<Result> Sync(List<string> billNos = null, DateTime? begin = null)
|
||
{
|
||
//1.事务
|
||
IDbContextTransaction _transaction = _transactionRepositories.GetTransaction();
|
||
bool isRollback = false;
|
||
bool isSuccess = true;
|
||
Result result;
|
||
//定时任务更新
|
||
if (billNos == null)
|
||
{
|
||
//DateTime begin = await _erpOpsSyncDateRepositories.Get(ErpOpsSyncType.OutStock);
|
||
if (begin == null)
|
||
begin = DateTime.Now.AddDays(-3);//默认拉去三天以内的数据
|
||
//更新时间范围内所有
|
||
result = await BillQueryForSalOutStock(false, null, begin);
|
||
if (!result.IsSuccess) isRollback = true;
|
||
result = await BillQueryForTransferDirectOutStock(false, null, begin);
|
||
if (!result.IsSuccess) isRollback = true;
|
||
result = await BillQueryForTransferOutOutStock(false, null, begin);
|
||
if (!result.IsSuccess) isRollback = true;
|
||
result = await BillQueryForAssembledAppOutStock(false, null, begin);
|
||
if (!result.IsSuccess) isRollback = true;
|
||
result = await BillQueryForMisDeliveryOutStock(false, null, begin);
|
||
if (!result.IsSuccess) isRollback = true;
|
||
|
||
//更新时间管理
|
||
//isSuccess = await _erpOpsSyncDateRepositories.Edit(ErpOpsSyncType.OutStock, false);
|
||
//if (!isSuccess) isRollback = true;
|
||
|
||
//4.提交事务
|
||
isSuccess = _transactionRepositories.CommitTransaction(isRollback, _transaction);
|
||
if (!isSuccess)
|
||
return Result.ReFailure(ResultCodes.DateWriteError);
|
||
|
||
return Result.ReSuccess();
|
||
}
|
||
|
||
//根据指定单号更新
|
||
List<string> SalOutStock_Nos = new List<string>();
|
||
List<string> TransferDirect_Nos = new List<string>();
|
||
List<string> TransferOut_Nos = new List<string>();
|
||
List<string> AssembledApp_Nos = new List<string>();
|
||
List<string> MisDeliveryOut_Nos = new List<string>();
|
||
var taskList = await _outStockTaskRepositories.GetListByBillNo(billNos);
|
||
foreach (var entity in taskList)
|
||
{
|
||
if (entity.Type == OutStockType.Sal)
|
||
SalOutStock_Nos.AddRange(entity.Details.SelectMany(s => s.ErpDetails).Select(s => s.SourceBillNo));
|
||
else if (entity.Type == OutStockType.Stkdirecttransfers)
|
||
TransferDirect_Nos.AddRange(entity.Details.SelectMany(s => s.ErpDetails).Select(s => s.SourceBillNo));
|
||
else if (entity.Type == OutStockType.StktransferInst)
|
||
TransferOut_Nos.AddRange(entity.Details.SelectMany(s => s.ErpDetails).Select(s => s.SourceBillNo));
|
||
else if (entity.Type == OutStockType.Assembled)
|
||
AssembledApp_Nos.AddRange(entity.Details.SelectMany(s => s.ErpDetails).Select(s => s.SourceBillNo));
|
||
else if (entity.Type == OutStockType.Miscellaneous)
|
||
MisDeliveryOut_Nos.AddRange(entity.Details.SelectMany(s => s.ErpDetails).Select(s => s.SourceBillNo));
|
||
}
|
||
|
||
if (SalOutStock_Nos.Count() > 0)
|
||
{
|
||
result = await BillQueryForSalOutStock(false, SalOutStock_Nos);
|
||
if (!result.IsSuccess) isRollback = true;
|
||
}
|
||
if (TransferDirect_Nos.Count() > 0)
|
||
{
|
||
result = await BillQueryForTransferDirectOutStock(false, TransferDirect_Nos);
|
||
if (!result.IsSuccess) isRollback = true;
|
||
}
|
||
if (TransferOut_Nos.Count() > 0)
|
||
{
|
||
result = await BillQueryForTransferOutOutStock(false, TransferOut_Nos);
|
||
if (!result.IsSuccess) isRollback = true;
|
||
}
|
||
if (AssembledApp_Nos.Count() > 0)
|
||
{
|
||
result = await BillQueryForAssembledAppOutStock(false, AssembledApp_Nos);
|
||
if (!result.IsSuccess) isRollback = true;
|
||
}
|
||
if (MisDeliveryOut_Nos.Count() > 0)
|
||
{
|
||
result = await BillQueryForMisDeliveryOutStock(false, MisDeliveryOut_Nos);
|
||
if (!result.IsSuccess) isRollback = true;
|
||
}
|
||
|
||
//4.提交事务
|
||
isSuccess = _transactionRepositories.CommitTransaction(isRollback, _transaction);
|
||
if (!isSuccess)
|
||
return Result.ReFailure(ResultCodes.DateWriteError);
|
||
return Result.ReSuccess();
|
||
}
|
||
|
||
}
|
||
}
|