Files
WMS-Api/src/WMS.Web.Domain/Services/OutStockTaskService.cs
2023-11-10 15:15:28 +08:00

335 lines
15 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.Erp.Purchase;
using WMS.Web.Core.Dto.Login;
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 e in erp_list)
{
//代表单据已经存在 那么就对单据进行修改
var data = data_list.FirstOrDefault(f => f.SourceBillNo == e.SourceBillNo);
var detail = data.Details.FirstOrDefault(w => w.MaterialId == e.MaterialId);
//存在就修改,没有就添加
if (detail != null)
detail.AccruedQty = e.AccruedQty;
else
data.Details.Add(_mapper.Map<OutStockTaskDetails>(e));
erp_removeList.Add(e);
}
//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).Select(x => x.Key).ToList();
foreach (var item in billNos)
{
var e = erp_list.FirstOrDefault(f => f.SourceBillNo == item);
var dto = new OutStockTask();
dto.SourceBillNo = e.SourceBillNo;
dto.Create((OutStockType)e.Type, e.SourceBillNo, e.DeliveryOrgId, e.ReceiptCustomerId, (DateTime)e.CreateTime);
//找到当前对应来源单据编号的集合数据
var current_erp_details = erp_list.Where(x => x.SourceBillNo == item).ToList();
//给到dto的实体明细中
dto.Details = _mapper.Map<List<OutStockTaskDetails>>(current_erp_details);
add_entitys.Add(dto);
}
//3.1提交新增
isSuccess = await _outStockTaskRepositories.AddRange(add_entitys, isTransaction);
if (!isSuccess)
return Result.ReFailure(ResultCodes.DateWriteError);
}
}
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="sourceBillNos"></param>
/// <returns></returns>
public async Task<Result> Sync(List<string> sourceBillNos = null)
{
//1.事务
IDbContextTransaction _transaction = _transactionRepositories.GetTransaction();
bool isRollback = false;
bool isSuccess = true;
Result result;
//定时任务更新
if (sourceBillNos == null)
{
DateTime begin = await _erpOpsSyncDateRepositories.Get(ErpOpsSyncType.OutStock);
//更新时间范围内所有
result = await BillQueryForDeliveryNoticeOutStock(false, null);
if (!result.IsSuccess) isRollback = true;
result = await BillQueryForTransferDirectOutStock(false, null);
if (!result.IsSuccess) isRollback = true;
result = await BillQueryForTransferOutOutStock(false, null);
if (!result.IsSuccess) isRollback = true;
result = await BillQueryForAssembledAppOutStock(false, null);
if (!result.IsSuccess) isRollback = true;
result = await BillQueryForMisDeliveryOutStock(false, null);
if (!result.IsSuccess) isRollback = true;
//同步成功后 更新定时开始时间
if (!isRollback)
{
//更新时间管理
isSuccess = await _erpOpsSyncDateRepositories.Edit(ErpOpsSyncType.Ops, false);
if (!isSuccess) isRollback = true;
}
//4.提交事务
isSuccess = _transactionRepositories.CommitTransaction(isRollback, _transaction);
if (!isSuccess)
return Result.ReFailure(ResultCodes.DateWriteError);
return Result.ReSuccess();
}
//根据指定单号更新
List<string> DeliveryNotice_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.GetListBySourceBillNo(sourceBillNos);
foreach (var entity in taskList)
{
if (entity.Type == OutStockType.Sal)
DeliveryNotice_Nos.Add(entity.SourceBillNo);
else if (entity.Type == OutStockType.Stkdirecttransfers)
TransferDirect_Nos.Add(entity.SourceBillNo);
else if (entity.Type == OutStockType.StktransferInst)
TransferOut_Nos.Add(entity.SourceBillNo);
else if (entity.Type == OutStockType.Assembled)
AssembledApp_Nos.Add(entity.SourceBillNo);
else if (entity.Type == OutStockType.Miscellaneous)
MisDeliveryOut_Nos.Add(entity.SourceBillNo);
}
if (DeliveryNotice_Nos.Count() > 0)
{
result= await BillQueryForDeliveryNoticeOutStock(false, DeliveryNotice_Nos);
if (!result.IsSuccess) isRollback = true;
}
if (TransferDirect_Nos.Count() > 0)
{
result = await BillQueryForDeliveryNoticeOutStock(false, TransferDirect_Nos);
if (!result.IsSuccess) isRollback = true;
}
if (TransferOut_Nos.Count() > 0)
{
result = await BillQueryForDeliveryNoticeOutStock(false, TransferOut_Nos);
if (!result.IsSuccess) isRollback = true;
}
if (AssembledApp_Nos.Count() > 0)
{
result = await BillQueryForDeliveryNoticeOutStock(false, AssembledApp_Nos);
if (!result.IsSuccess) isRollback = true;
}
if (MisDeliveryOut_Nos.Count() > 0)
{
result = await BillQueryForDeliveryNoticeOutStock(false, MisDeliveryOut_Nos);
if (!result.IsSuccess) isRollback = true;
}
//4.提交事务
isSuccess = _transactionRepositories.CommitTransaction(isRollback, _transaction);
if (!isSuccess)
return Result.ReFailure(ResultCodes.DateWriteError);
return Result.ReSuccess();
}
}
}