Files
WMS-Api/src/WMS.Web.Domain/Services/BackRecordService.cs
tongfei 98335346ba 日志
2024-01-03 16:44:14 +08:00

173 lines
7.7 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 Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WMS.Web.Core.Dto.BackRecord;
using WMS.Web.Core.Dto.ChangeBoxRecord;
using WMS.Web.Core.Dto.Inventory;
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 BackRecordService : IBackRecordService
{
private readonly IMapper _mapper;
private readonly ILoginService _loginService;
private readonly ILogger<BackRecordService> _logger;
public readonly IBasicsRepositories _basicsRepositories;
private readonly ISerialNumberService _serialNumberService;
private readonly IBackRecordRepositories _backRecordRepositories;
private readonly IBoxInventoryService _boxInventoryService;
private readonly IChangeMoveBoxService _changeMoveBoxService;
private readonly ISerialNumbersRepositories _serialNumbersRepositories;
private readonly IInStockTaskBoxService _inStockTaskBoxService;
public BackRecordService(IMapper mapper, ILoginService loginService,
ILogger<BackRecordService> logger,
IBasicsRepositories basicsRepositories,
IBoxInventoryService boxInventoryService,
ISerialNumberService serialNumberService,
IChangeMoveBoxService changeMoveBoxService,
ISerialNumbersRepositories serialNumbersRepositories,
IBackRecordRepositories backRecordRepositories,
IInStockTaskBoxService inStockTaskBoxService)
{
_mapper = mapper;
_logger = logger;
_loginService = loginService;
_inStockTaskBoxService = inStockTaskBoxService;
_changeMoveBoxService = changeMoveBoxService;
_serialNumberService = serialNumberService;
_boxInventoryService = boxInventoryService;
_basicsRepositories = basicsRepositories;
_serialNumbersRepositories = serialNumbersRepositories;
_backRecordRepositories = backRecordRepositories;
}
/// <summary>
/// 回退上下架
/// </summary>
/// <param name="dto"></param>
/// <param name="type"></param>
/// <param name="loginInfo"></param>
/// <returns></returns>
public async Task<Result> BackShelf(BackRecordOnOffRequest dto, BackRecordType type, LoginInDto loginInfo)
{
IDbContextTransaction _transaction = _basicsRepositories.GetTransaction();
bool isRollback = false;
bool isTransaction = false;
var stocks = await _basicsRepositories.GetUcStockAsync(ManagementSystemCode.GLXT0004.ToString(), "", loginInfo.UserInfo.CompanyId);
if (stocks.Count == 0)
return Result.ReFailure(ResultCodes.ErpStockNoData);
var entity = new BackRecord();
entity.Method = (InventoryInOutMethod)dto.Method;
entity.OrgCode = dto.OrgCode;
entity.StockCode = dto.StockCode;
entity.SubStockId = dto.SubStockId;
entity.Type = type;
entity.Details = _mapper.Map<List<BackRecordDetails>>(dto.Details);
entity.Create(loginInfo.UserInfo.StaffId);
entity = await _backRecordRepositories.Add(entity, isTransaction);
if (entity == null)
return Result.ReFailure(ResultCodes.DateWriteError);
//保存成功后:序列号跟踪流程添加
var serialNumber_result = await _serialNumberService.BackRecord(entity, loginInfo, isTransaction);
if (!serialNumber_result.IsSuccess)
return serialNumber_result;
//#region 需要改箱操作
////获取目标箱和原箱信息
//var current_box_about = dto.Details.GroupBy(x => new { x.BoxId }).Select(x => new { x.Key.BoxId }).ToList();
//var current_sernub_box = dto.Details.GroupBy(x => x.SerialNumberBoxId).Select(x => x.Key).ToList();
////要改箱的数据集合
//var ganenrateChangeBoxs = new List<SaveChangeBoxRecordRequest>();
////遍历组装改箱dto的头部集合
//foreach (var boxAbout in current_box_about)
//{
// foreach (var serBoxId in current_sernub_box)
// {
// if (boxAbout.BoxId != serBoxId)
// {
// var changeBox = new SaveChangeBoxRecordRequest();
// changeBox.SrcBoxId = serBoxId;
// changeBox.DestBoxId = boxAbout.BoxId;
// changeBox.SubStockId = entity.SubStockId;
// ganenrateChangeBoxs.Add(changeBox);
// }
// }
//}
////遍历组装明细改箱dto的明细集合
//foreach (var detItem in dto.Details)
//{
// ganenrateChangeBoxs.ForEach(x =>
// {
// if (x.SrcBoxId == detItem.SerialNumberBoxId && x.DestBoxId == detItem.BoxId)
// {
// var changeBoxRD = new SaveChangeBoxRecordDetailsRequest();
// changeBoxRD.MaterialId = detItem.MaterialId;
// changeBoxRD.Qty = detItem.Qty;
// changeBoxRD.SerialNumbers = detItem.SerialNumbers;
// x.Details.Add(changeBoxRD);
// }
// });
//}
////改箱保存操作:这里不需要在改箱的操作-进行库存的变更(因为会有问题),下面会去更新库存的
//var changeBoxSave_Result = await _changeMoveBoxService.ChangeBoxSave(ganenrateChangeBoxs, loginInfo, isTransaction,false);
//if (!changeBoxSave_Result.IsSuccess)
// return changeBoxSave_Result;
//#endregion
//改箱保存操作
var changeBoxSave_Result = await _changeMoveBoxService.ChangeBox_BackRecord(entity, loginInfo, isTransaction);
if (!changeBoxSave_Result.IsSuccess)
return changeBoxSave_Result;
//保存成功后:变更库存
var boxInventoryResult = await _boxInventoryService.GenerateBackBox(entity, isTransaction);
if (!boxInventoryResult.IsSuccess)
return boxInventoryResult;
//下架:需要解绑收货过的箱子
if (entity.Type == BackRecordType.InstockOff)
{
_logger.LogInformation("入库回退下架:" + entity.BillNo);
var boxIds= entity.Details.GroupBy(x => x.BoxId).Select(x => x.Key).ToList();
var unBindResult= await _inStockTaskBoxService.UnBind(boxIds, isTransaction);
_logger.LogInformation("入库回退下架-BOXID" + JsonConvert.SerializeObject(boxIds));
if (!unBindResult.IsSuccess)
return unBindResult;
_logger.LogInformation("入库回退下架-结果:" + unBindResult.IsSuccess);
}
//提交事务
var isSuccess = _basicsRepositories.CommitTransaction(isRollback, _transaction);
if (!isSuccess)
return Result.ReFailure(ResultCodes.DateWriteError);
return Result.ReSuccess();
}
}
}