132 lines
6.0 KiB
C#
132 lines
6.0 KiB
C#
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)
|
||
{
|
||
if (type == BackRecordType.InstockOff)
|
||
_logger.LogInformation($"回退下架:{JsonConvert.SerializeObject(dto)} 操作人:{loginInfo.UserInfo.StaffId + loginInfo.UserInfo.Nickname}");
|
||
else
|
||
_logger.LogInformation($"回退上架:{JsonConvert.SerializeObject(dto)} 操作人:{loginInfo.UserInfo.StaffId + loginInfo.UserInfo.Nickname}");
|
||
//回退上下架:不能扫同样的箱,按箱入库时候
|
||
if (dto.Details != null && dto.Details.Count != 0 && dto.Method== (int)InventoryInOutMethod.Box)
|
||
{
|
||
if (dto.Details.Select(x => x.BoxId).Distinct().Count() != dto.Details.Select(x => x.BoxId).Count())
|
||
return Result<InStock>.ReFailure(ResultCodes.BoxOutStockTaskBoxError);
|
||
}
|
||
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.SubStockCode = dto.SubStockCode;
|
||
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;
|
||
|
||
//保存成功后:变更库存
|
||
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();
|
||
|
||
}
|
||
}
|
||
}
|