813 lines
41 KiB
C#
813 lines
41 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;
|
||
using WMS.Web.Core.Dto;
|
||
using WMS.Web.Core.Dto.Erp;
|
||
using WMS.Web.Core.Dto.Erp.Customer;
|
||
using WMS.Web.Core.Dto.Erp.Org;
|
||
using WMS.Web.Core.Dto.Erp.OutStock;
|
||
using WMS.Web.Core.Dto.Login;
|
||
using WMS.Web.Core.Dto.OutStockTask;
|
||
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.Services.Public;
|
||
using WMS.Web.Domain.Values;
|
||
using WMS.Web.Domain.Values.Single;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using Microsoft.Extensions.Logging;
|
||
using Newtonsoft.Json;
|
||
|
||
namespace WMS.Web.Domain.Services
|
||
{
|
||
/// <summary>
|
||
/// 出库任务
|
||
/// </summary>
|
||
public class OutStockTaskService : IOutStockTaskService
|
||
{
|
||
private readonly IMapper _mapper;
|
||
private IErpService _erpService;
|
||
private readonly ILoginService _loginService;
|
||
private readonly IBasicsRepositories _transactionRepositories;
|
||
private IOutStockRepositories _outStockRepositories;
|
||
private IOutStockTaskRepositories _outStockTaskRepositories;
|
||
private readonly IErpOpsSyncDateRepositories _erpOpsSyncDateRepositories;
|
||
private readonly RedisClientService _redisClientService;
|
||
private readonly IErpBasicDataExtendService _erpBasicDataExtendService;
|
||
private readonly ISingleDataService _singleDataService;
|
||
private IBoxRepositories _boxRepositories;
|
||
private readonly IExportExcelService _exportExcelService;
|
||
private readonly IServiceScopeFactory _serviceScopeFactory;
|
||
private readonly ILogger<OutStockTaskService> _logger;
|
||
|
||
public OutStockTaskService(IMapper mapper, IErpService erpService, ILoginService loginService,
|
||
IBasicsRepositories transactionRepositories,
|
||
IOutStockRepositories outStockRepositories, IOutStockTaskRepositories outStockTaskRepositories, IErpOpsSyncDateRepositories erpOpsSyncDateRepositories,
|
||
RedisClientService redisClientService, IErpBasicDataExtendService erpBasicDataExtendService,
|
||
ISingleDataService singleDataService, IBoxRepositories boxRepositories, IExportExcelService exportExcelService, IServiceScopeFactory serviceScopeFactory,
|
||
ILogger<OutStockTaskService> logger)
|
||
{
|
||
_mapper = mapper;
|
||
_erpService = erpService;
|
||
_loginService = loginService;
|
||
_transactionRepositories = transactionRepositories;
|
||
_outStockRepositories = outStockRepositories;
|
||
_outStockTaskRepositories = outStockTaskRepositories;
|
||
_erpOpsSyncDateRepositories = erpOpsSyncDateRepositories;
|
||
_redisClientService = redisClientService;
|
||
_erpBasicDataExtendService = erpBasicDataExtendService;
|
||
_singleDataService = singleDataService;
|
||
_boxRepositories = boxRepositories;
|
||
_exportExcelService = exportExcelService;
|
||
_serviceScopeFactory = serviceScopeFactory;
|
||
_logger = logger;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 出库任务作废
|
||
/// </summary>
|
||
/// <param name="dto"></param>
|
||
/// <returns></returns>
|
||
public async Task<Result> Repeal(OperateRequest dto, LoginInDto loginInfo)
|
||
{
|
||
var list = await _outStockTaskRepositories.GetEntityListByDetailIds(dto.Ids);
|
||
foreach (var entity in list)
|
||
{
|
||
List<int> e_ids = entity.Details.Select(s => s.Id).ToList();
|
||
var j_ids = e_ids.Intersect(dto.Ids).ToList();
|
||
//作废
|
||
entity.Repeal(loginInfo.UserInfo.StaffId, j_ids);
|
||
}
|
||
var isSuccess = await _outStockTaskRepositories.EditEntityList(list, true);
|
||
if (!isSuccess) return Result.ReFailure(ResultCodes.DateWriteError);
|
||
|
||
return Result.ReSuccess();
|
||
}
|
||
/// <summary>
|
||
/// 反作废
|
||
/// </summary>
|
||
/// <param name="dto"></param>
|
||
/// <param name="loginInfo"></param>
|
||
/// <returns></returns>
|
||
/// <exception cref="NotImplementedException"></exception>
|
||
public async Task<Result> NoRepeal(OperateRequest dto, LoginInDto loginInfo)
|
||
{
|
||
var list = await _outStockTaskRepositories.GetEntityListByDetailIds(dto.Ids);
|
||
foreach (var entity in list)
|
||
{
|
||
List<int> e_ids = entity.Details.Select(s => s.Id).ToList();
|
||
var j_ids = e_ids.Intersect(dto.Ids).ToList();
|
||
//反作废
|
||
entity.NoRepeal(loginInfo.UserInfo.StaffId, j_ids);
|
||
}
|
||
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)
|
||
{
|
||
if (erp_list.Count() <= 0) return Result.ReSuccess();
|
||
_logger.LogInformation($"获取到金蝶出库任务单数据:{JsonConvert.SerializeObject(erp_list)}");
|
||
|
||
//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);
|
||
List<int> update_ids = new List<int>();
|
||
if (data_list.Count != 0)
|
||
{
|
||
//2.1提取出wms任务单明细信息
|
||
foreach (var outStockTask in data_list)
|
||
{
|
||
bool isUpdate = false;
|
||
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();
|
||
//比对数据
|
||
isUpdate = SsynDateComparison(outStockTask, erps);
|
||
foreach (var erp in erps)
|
||
{
|
||
//仓库不同 拆分成不同的 //如果该明细作废 则不修改了
|
||
var details = outStockTask.Details.Where(w => w.MaterialNumber == erp.MaterialNumber).ToList();
|
||
bool isAdd = false;
|
||
if (details.Count() == 0) isAdd = true;
|
||
OutStockTaskDetails detail = null;
|
||
if (details.Where(w => w.IsRepeal != true).Count() > 0)
|
||
{
|
||
detail = details.Where(w => w.IsRepeal != true).FirstOrDefault();
|
||
isAdd = false;
|
||
}
|
||
if (details.Count() != 0 && details.Count() == details.Where(w => w.IsRepeal == true).Count())
|
||
{
|
||
//因为出库任务单有合并的情况 所以需要过滤一下
|
||
//如果这个明细已经作废,同时金蝶的明细id对不上 则新增一条明细
|
||
if (details.SelectMany(s => s.ErpDetails).Where(w => w.Erp_DetailId == erp.Erp_DetailId).Count() == 0)
|
||
isAdd = true;
|
||
else
|
||
{
|
||
//操作完后剔除
|
||
erp_removeList.Add(erp);
|
||
continue;
|
||
}
|
||
}
|
||
if (isAdd == true)
|
||
{
|
||
//添加一条物料明细
|
||
detail = _mapper.Map<OutStockTaskDetails>(erp);
|
||
var erpDetail = _mapper.Map<OutStockTaskErpDetails>(erp);
|
||
detail.ErpDetails.Add(erpDetail);
|
||
outStockTask.Details.Add(detail);
|
||
isUpdate = true;
|
||
}
|
||
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);
|
||
isUpdate = true;
|
||
}
|
||
else
|
||
{
|
||
if (erpDetail.AccruedQty != erp.AccruedQty)
|
||
{
|
||
isUpdate = true;
|
||
erpDetail.AccruedQty = erp.AccruedQty;
|
||
}
|
||
if (erpDetail.InStockCode != erp.InStockCode)
|
||
{
|
||
isUpdate = true;
|
||
erpDetail.InStockCode = erp.InStockCode;
|
||
}
|
||
}
|
||
|
||
detail.AccruedQty = detail.ErpDetails.Sum(s => s.AccruedQty);
|
||
}
|
||
//操作完后剔除
|
||
erp_removeList.Add(erp);
|
||
}
|
||
if (isUpdate)
|
||
update_ids.Add(outStockTask.Id);
|
||
//执行完后重新计算一下状态
|
||
outStockTask.GenerateStatus();
|
||
}
|
||
}
|
||
//
|
||
//var update_date_list = data_list.Where(w => update_ids.Contains(w.Id) && w.Status != OutStockStatus.Repeal).ToList();
|
||
var update_date_list = data_list.Where(w => update_ids.Contains(w.Id)).ToList();
|
||
//更新修改时间
|
||
update_date_list.ForEach(f => f.WmsUpdateTime = DateTime.Now);
|
||
//2.2.提交修改
|
||
var isSuccess = await _outStockTaskRepositories.EditEntityList(update_date_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 bool SsynDateComparison(OutStockTask outStockTask, List<ErpDeliveryNoticeOutStockResultDto> erp_list)
|
||
{
|
||
bool isUpdate = false;
|
||
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);
|
||
if (d.AccruedQty == 0)
|
||
d.IsRepeal = true;
|
||
isUpdate = true;
|
||
}
|
||
}
|
||
}
|
||
return isUpdate;
|
||
}
|
||
|
||
/// <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)
|
||
{
|
||
var info = _redisClientService.GetStringKey<bool>($"wms_outstock_sync");
|
||
if (info == true) return Result.ReFailure(ResultCodes.ErpSyns);
|
||
_redisClientService.SetStringKey($"wms_outstock_sync", true, TimeSpan.FromMinutes(5));
|
||
//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.AddHours(-8);//默认拉去8小时以内的数据
|
||
//更新时间范围内所有
|
||
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);
|
||
_redisClientService.SetStringKey($"wms_outstock_sync", false, TimeSpan.FromMinutes(5));
|
||
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);
|
||
_redisClientService.SetStringKey($"wms_outstock_sync", false, TimeSpan.FromMinutes(5));
|
||
if (!isSuccess)
|
||
return Result.ReFailure(ResultCodes.DateWriteError);
|
||
|
||
return Result.ReSuccess();
|
||
}
|
||
/// <summary>
|
||
/// 获取出库任务单详情
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
public async Task<Result<OutStockTaskInfoResponse>> GetInfo(int id, LoginInDto loginInfo, IServiceScope scope = null)
|
||
{
|
||
if (scope != null)
|
||
{
|
||
_outStockTaskRepositories = scope.ServiceProvider.GetRequiredService<IOutStockTaskRepositories>();
|
||
_outStockRepositories = scope.ServiceProvider.GetRequiredService<IOutStockRepositories>();
|
||
_erpService = scope.ServiceProvider.GetRequiredService<IErpService>();
|
||
_boxRepositories = scope.ServiceProvider.GetRequiredService<IBoxRepositories>();
|
||
}
|
||
|
||
var entity = await _outStockTaskRepositories.Get(id);
|
||
if (entity == null)
|
||
return Result<OutStockTaskInfoResponse>.ReFailure(ResultCodes.OutStockTaskNoData);
|
||
//获取任务单对应出库信息
|
||
var outStockList = await _outStockRepositories.GetByTaskId(id);
|
||
|
||
//取组织
|
||
var org_result = await _erpService.BillQueryForOrg();
|
||
List<ErpOrgDto> orgs = new List<ErpOrgDto>();
|
||
if (org_result.IsSuccess)
|
||
orgs = org_result.Data.ToList();
|
||
|
||
//取客户
|
||
var customer_result = await _erpService.BillQueryForCustomer();
|
||
List<ErpCustomerDto> customers = new List<ErpCustomerDto>();
|
||
if (customer_result.IsSuccess)
|
||
customers = customer_result.Data.ToList();
|
||
|
||
var materials_result = await _erpService.BillQueryForMaterial();
|
||
List<ErpMaterialDto> materials = new List<ErpMaterialDto>();
|
||
if (materials_result.IsSuccess)
|
||
materials = materials_result.Data.ToList();
|
||
|
||
OutStockTaskInfoResponse response = new OutStockTaskInfoResponse()
|
||
{
|
||
Id = entity.Id,
|
||
BillNo = entity.BillNo,
|
||
SourceBillNo = string.Join(",", entity.Details.SelectMany(s => s.ErpDetails).Select(s => s.SourceBillNo).Distinct()),
|
||
SaleBillNo = string.Join(",", entity.Details.SelectMany(s => s.ErpDetails).Select(s => s.SaleBillNo).Distinct()),
|
||
CreateTime = entity.CreateTime.DateToStringSeconds(),
|
||
Status = entity.Status.GetRemark(),
|
||
Type = entity.Type.GetRemark(),
|
||
DeliveryOrg = _erpBasicDataExtendService.GetOrgName(orgs, entity.DeliveryOrgId),
|
||
ReceiptCustomer = entity.Type == OutStockType.Sal
|
||
? _erpBasicDataExtendService.GetCustomerName(customers, entity.ReceiptCustomerId)
|
||
: _erpBasicDataExtendService.GetOrgName(orgs, entity.DeliveryOrgId),
|
||
};
|
||
var details = outStockList.SelectMany(s => s.Details).ToList();
|
||
var boxDetails = outStockList.SelectMany(s => s.Details).SelectMany(s => s.BoxsDetails).ToList();
|
||
var boxList = await _boxRepositories.GetEntityList(boxDetails.Select(s => s.BoxId).ToList());
|
||
foreach (var b in boxDetails)
|
||
{
|
||
var detail = details.FirstOrDefault(f => f.Id == b.DetailId);
|
||
var outStock = outStockList.FirstOrDefault(f => f.Id == detail.Fid);
|
||
var m = materials.FirstOrDefault(f => f.MaterialNumber.Equals(detail.MaterialNumber));
|
||
OutStockTaskInfoDetailsResponse infoDetail = new OutStockTaskInfoDetailsResponse()
|
||
{
|
||
BoxBillNo = boxList.FirstOrDefault(f => f.Id == b.BoxId)?.BoxBillNo ?? "",
|
||
OutStockId = outStock.Id,
|
||
OutStockBoxDetailsId = b.Id,
|
||
BoxLength = b.BoxLength,
|
||
BoxWide = b.BoxWide,
|
||
BoxHigh = b.BoxHigh,
|
||
BoxWeight = b.BoxWeight,
|
||
Qty = b.Qty,
|
||
SerialNumbers = string.Join(",", b.SerialNumbers),
|
||
SerialNumberList = b.SerialNumbers,
|
||
Method = outStock.Method.GetRemark(),
|
||
Creator = _singleDataService.GetSingleData(SingleAction.Staffs, loginInfo.UserInfo.CompanyId, outStock.CreatorId),
|
||
CreateTime = outStock.CreateTime.DateToStringSeconds(),
|
||
MaterialNumber = detail.MaterialNumber,
|
||
Specifications = m?.Specifications ?? "",
|
||
BarCode = m?.BarCode ?? "",
|
||
MaterialName = m?.MaterialName ?? "",
|
||
//AccruedQty = entity.Details.FirstOrDefault(f => f.MaterialNumber == detail.MaterialNumber && f.IsRepeal != true)?.AccruedQty ?? 0
|
||
AccruedQty = entity.Details.FirstOrDefault(f => f.ErpDetails.Where(w => detail.ErpDetails.Select(s => s.Erp_DetailId).Contains(w.Erp_DetailId)).Any())?.AccruedQty ?? 0
|
||
};
|
||
response.Details.Add(infoDetail);
|
||
}
|
||
//排序和加序号值
|
||
//response.Details = response.Details.OrderByDescending(s => s.Specifications)
|
||
// .ThenByDescending(s => s.Method).ThenByDescending(s => s.Qty).ToList();
|
||
// response.Details.ForEach(f => f.IndexNumber = response.Details.IndexOf(f) + 1);
|
||
response.Details = response.Details.OrderByDescending(s => s.Method)
|
||
.ThenByDescending(s => s.BoxBillNo).ThenByDescending(s => s.BoxLength).ToList();
|
||
|
||
for (int i = 0; i < response.Details.Count(); i++)
|
||
{
|
||
if (i == 0)
|
||
response.Details[0].IndexNumber = 1;
|
||
else
|
||
{
|
||
//和上一个箱子比 如果是相同的箱子序号一样
|
||
if (response.Details[i].BoxBillNo == response.Details[i - 1].BoxBillNo)
|
||
response.Details[i].IndexNumber = response.Details[i - 1].IndexNumber;
|
||
else
|
||
response.Details[i].IndexNumber = response.Details[i - 1].IndexNumber + 1;
|
||
}
|
||
}
|
||
|
||
return Result<OutStockTaskInfoResponse>.ReSuccess(response);
|
||
}
|
||
/// <summary>
|
||
/// 导出详情
|
||
/// </summary>
|
||
/// <param name="dto"></param>
|
||
/// <returns></returns>
|
||
public async Task ExportInfo(OperateRequest dto, string fileName, LoginInDto loginInfo)
|
||
{
|
||
try
|
||
{
|
||
var ids = dto.Ids.Distinct().ToList();
|
||
using (var scope = _serviceScopeFactory.CreateScope())
|
||
{
|
||
List<ExportInfoResponse> list = new List<ExportInfoResponse>();
|
||
foreach (var id in ids)
|
||
{
|
||
var info = await this.GetInfo(id, loginInfo, scope);
|
||
if (!info.IsSuccess) continue;
|
||
var response = info.Data;
|
||
foreach (var d in response.Details)
|
||
{
|
||
if (d.SerialNumberList == null || d.SerialNumberList.Count() <= 0)
|
||
{
|
||
list.Add(new ExportInfoResponse()
|
||
{
|
||
SourceBillNo = response.SourceBillNo,
|
||
SaleBillNo = response.SaleBillNo,
|
||
ReceiptCustomer = response.ReceiptCustomer,
|
||
CreateTime = d.CreateTime,
|
||
BoxBillNo = d.BoxBillNo,
|
||
Specifications = d.Specifications,
|
||
MaterialName = d.MaterialName,
|
||
MaterialNumber = d.MaterialNumber,
|
||
BarCode = d.BarCode,
|
||
IndexNumber = d.IndexNumber,
|
||
BoxLength = d.BoxLength,
|
||
BoxWide = d.BoxWide,
|
||
BoxHigh = d.BoxHigh,
|
||
BoxWeight = d.BoxWeight,
|
||
SerialNumbers = ""
|
||
});
|
||
}
|
||
else
|
||
{
|
||
foreach (var s in d.SerialNumberList)
|
||
{
|
||
list.Add(new ExportInfoResponse()
|
||
{
|
||
SourceBillNo = response.SourceBillNo,
|
||
SaleBillNo = response.SaleBillNo,
|
||
ReceiptCustomer = response.ReceiptCustomer,
|
||
CreateTime = d.CreateTime,
|
||
BoxBillNo = d.BoxBillNo,
|
||
Specifications = d.Specifications,
|
||
MaterialName = d.MaterialName,
|
||
MaterialNumber = d.MaterialNumber,
|
||
BarCode = d.BarCode,
|
||
IndexNumber = d.IndexNumber,
|
||
BoxLength = d.BoxLength,
|
||
BoxWide = d.BoxWide,
|
||
BoxHigh = d.BoxHigh,
|
||
BoxWeight = d.BoxWeight,
|
||
SerialNumbers = s
|
||
});
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
var _exportExcelService = scope.ServiceProvider.GetRequiredService<IExportExcelService>();
|
||
await _exportExcelService.Export(list, fileName, loginInfo.UserInfo.StaffId, loginInfo.UserInfo.CompanyId, FileDownLoadOrderType.OutStockTaskInfo);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 金蝶删单后wms单据作废
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
/// <exception cref="NotImplementedException"></exception>
|
||
public async Task<Result> ErpDel()
|
||
{
|
||
var list = await _outStockTaskRepositories.GetEntityListByStatus();
|
||
var result = await GetData(list);
|
||
if (!result.IsSuccess) return Result.ReFailure(result.Message, result.Status);
|
||
var erp_list = result.Data;
|
||
List<OutStockTask> update_list = new List<OutStockTask>();
|
||
//处理数据
|
||
foreach (var entity in list)
|
||
{
|
||
//合并单有多个来源订单
|
||
var nos = entity.Details.SelectMany(s => s.ErpDetails).Select(s => s.SourceBillNo).ToList();
|
||
foreach (var no in nos)
|
||
{
|
||
//判断是否在erp数据池里
|
||
int count = erp_list.Where(w => w.SourceBillNo.Equals(no)).Count();
|
||
if (count > 0) continue;
|
||
//如果不在数据池里 被删除了 那么需要把订单作废
|
||
entity.ErpDel(no);
|
||
_logger.LogInformation($"出库任务单 金蝶未找到单据:{no} wms订单号:{entity.BillNo} 作废来源单号:{no}");
|
||
if (update_list.FirstOrDefault(f => f.Id == entity.Id) == null)
|
||
update_list.Add(entity);
|
||
}
|
||
}
|
||
if (update_list.Count() <= 0) return Result.ReSuccess();
|
||
|
||
var isSuccess = await _outStockTaskRepositories.EditEntityList(update_list, true);
|
||
if (!isSuccess)
|
||
return Result.ReFailure(ResultCodes.DateWriteError);
|
||
|
||
return Result.ReSuccess();
|
||
}
|
||
/// <summary>
|
||
/// 获取金蝶数据
|
||
/// </summary>
|
||
/// <param name="list"></param>
|
||
/// <returns></returns>
|
||
public async Task<Result<List<ErpDeliveryNoticeOutStockResultDto>>> GetData(List<OutStockTask> list)
|
||
{
|
||
List<ErpDeliveryNoticeOutStockResultDto> erp_list = new List<ErpDeliveryNoticeOutStockResultDto>();
|
||
//根据指定单号更新
|
||
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>();
|
||
foreach (var entity in list)
|
||
{
|
||
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)
|
||
{
|
||
var erp_result = await _erpService.BillQueryForSalOutStock(SalOutStock_Nos, null);
|
||
if (!erp_result.IsSuccess)
|
||
return Result<List<ErpDeliveryNoticeOutStockResultDto>>.ReFailure(erp_result.Message, erp_result.Status);
|
||
erp_list.AddRange(erp_result.Data);
|
||
}
|
||
if (TransferDirect_Nos.Count() > 0)
|
||
{
|
||
var erp_result = await _erpService.BillQueryForTransferDirectOutStock(TransferDirect_Nos, null);
|
||
if (!erp_result.IsSuccess)
|
||
return Result<List<ErpDeliveryNoticeOutStockResultDto>>.ReFailure(erp_result.Message, erp_result.Status);
|
||
erp_list.AddRange(erp_result.Data);
|
||
}
|
||
if (TransferOut_Nos.Count() > 0)
|
||
{
|
||
var erp_result = await _erpService.BillQueryForTransferOutOutStock(TransferOut_Nos, null);
|
||
if (!erp_result.IsSuccess)
|
||
return Result<List<ErpDeliveryNoticeOutStockResultDto>>.ReFailure(erp_result.Message, erp_result.Status);
|
||
erp_list.AddRange(erp_result.Data);
|
||
}
|
||
if (AssembledApp_Nos.Count() > 0)
|
||
{
|
||
var erp_result = await _erpService.BillQueryForAssembledAppOutStock_Dassembly(AssembledApp_Nos, null);
|
||
if (!erp_result.IsSuccess)
|
||
return Result<List<ErpDeliveryNoticeOutStockResultDto>>.ReFailure(erp_result.Message, erp_result.Status);
|
||
erp_list.AddRange(erp_result.Data);
|
||
|
||
var erp_result_a = await _erpService.BillQueryForAssembledAppOutStock_Assembly(AssembledApp_Nos, null);
|
||
if (!erp_result_a.IsSuccess)
|
||
return Result<List<ErpDeliveryNoticeOutStockResultDto>>.ReFailure(erp_result_a.Message, erp_result_a.Status);
|
||
erp_list.AddRange(erp_result_a.Data);
|
||
}
|
||
if (MisDeliveryOut_Nos.Count() > 0)
|
||
{
|
||
var erp_result = await _erpService.BillQueryForMisDeliveryOutStock(MisDeliveryOut_Nos, null);
|
||
if (!erp_result.IsSuccess)
|
||
return Result<List<ErpDeliveryNoticeOutStockResultDto>>.ReFailure(erp_result.Message, erp_result.Status);
|
||
erp_list.AddRange(erp_result.Data);
|
||
}
|
||
|
||
return Result<List<ErpDeliveryNoticeOutStockResultDto>>.ReSuccess(erp_list);
|
||
}
|
||
}
|
||
}
|