Files
WMS-Api/src/WMS.Web.Domain/Services/OutStockTaskService.cs
tongfei 7b4f6f82a1 test
2023-11-06 16:58:05 +08:00

236 lines
10 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;
public OutStockTaskService(IMapper mapper, IErpService erpService, ILoginService loginService,
IBasicsRepositories transactionRepositories,
IOutStockRepositories outStockRepositories, IOutStockTaskRepositories outStockTaskRepositories)
{
_mapper = mapper;
_erpService = erpService;
_loginService = loginService;
_transactionRepositories = transactionRepositories;
_outStockRepositories = outStockRepositories;
_outStockTaskRepositories = outStockTaskRepositories;
}
/// <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> SsynDeliveryNoticeOutStock(bool isTransaction, List<string> sourceBillNos = null)
{
//1.获取金蝶数据:采购订单数据
var erp_result = await _erpService.BillQueryForDeliveryNoticeOutStock(sourceBillNos);
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)
{
var erp_result = await _erpService.BillQueryForTransferDirectOutStock(sourceBillNos);
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)
{
var erp_result = await _erpService.BillQueryForTransferOutOutStock(sourceBillNos);
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)
{
List<ErpDeliveryNoticeOutStockResultDto> list = new List<ErpDeliveryNoticeOutStockResultDto>();
var erp_result = await _erpService.BillQueryForAssembledAppOutStock_Dassembly(sourceBillNos);
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);
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)
{
var erp_result = await _erpService.BillQueryForMisDeliveryOutStock(sourceBillNos);
if (!erp_result.IsSuccess)
return Result.ReFailure(erp_result.Message, erp_result.Status);
return await this.SsynDate(erp_result.Data.ToList(), isTransaction);
}
}
}