Files
WMS-Api/src/WMS.Web.Domain/Services/BackRecordService.cs
2023-11-16 17:37:19 +08:00

107 lines
4.4 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.Dto.BackRecord;
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;
public readonly IBasicsRepositories _basicsRepositories;
private readonly ISerialNumberService _serialNumberService;
private readonly IBackRecordRepositories _backRecordRepositories;
private readonly IBoxInventoryService _boxInventoryService;
public BackRecordService(IMapper mapper, ILoginService loginService,
IBasicsRepositories basicsRepositories,
IBoxInventoryService boxInventoryService,
ISerialNumberService serialNumberService,
IBackRecordRepositories backRecordRepositories)
{
_mapper = mapper;
_loginService = loginService;
_boxInventoryService = boxInventoryService;
_basicsRepositories = basicsRepositories;
_backRecordRepositories = backRecordRepositories;
}
/// <summary>
/// 回退上下架
/// </summary>
/// <param name="dto"></param>
/// <param name="type"></param>
/// <param name="loginInfo"></param>
/// <returns></returns>
public async Task<Result> BackShelf(SaveBackRecordRequest dto, BackRecordType type, LoginInDto loginInfo)
{
IDbContextTransaction _transaction = _basicsRepositories.GetTransaction();
bool isRollback = false;
bool isTransaction = false;
var entity = new BackRecord();
entity.Type = type;
entity.Details = _mapper.Map<List<BackRecordDetails>>(dto.Details);
entity.Create(loginInfo.UserInfo.StaffId);
entity = await _backRecordRepositories.Add(entity, isTransaction);
//提交事务
var isSuccess = _basicsRepositories.CommitTransaction(isRollback, _transaction);
if (!isSuccess)
return Result.ReFailure(ResultCodes.DateWriteError);
if (entity != null)
{
//保存成功后:序列号跟踪流程添加
var serialNumber_result = await _serialNumberService.BackRecord(entity, loginInfo, isTransaction);
if (!serialNumber_result.IsSuccess)
return serialNumber_result;
//保存成功后:变更库存
var boxInventoryBackGenerateDto = dto.Details.GroupBy(x =>new { x.BoxId,x.StockCode,x.SubStockId}).Select(x => new BoxInventoryBackGenerateDto()
{
BoxId=x.Key.BoxId,
StockCode=x.Key.StockCode,
SubStockId=x.Key.SubStockId,
InventoryInOutType= type== BackRecordType.OutstockOn? (int)InventoryInOutType.In:(int)InventoryInOutType.Out,
}).ToList();
foreach (var item in dto.Details)
{
var current= boxInventoryBackGenerateDto.Where(x => x.BoxId == item.BoxId).FirstOrDefault();
if (current != null)
{
var detail = new BoxInventoryBackDetailsGenerateDto();
detail.MaterialId = item.MaterialId;
detail.Qty = item.Qty;
detail.SerialNumbers = item.SerialNumbers;
current.Details.Add(detail);
}
}
var boxInventoryResult= await _boxInventoryService.GenerateBackBox(boxInventoryBackGenerateDto, isTransaction);
if (!boxInventoryResult.IsSuccess)
return boxInventoryResult;
return Result.ReSuccess();
}
else
return Result.ReFailure(ResultCodes.DateWriteError);
}
}
}