79 lines
3.3 KiB
C#
79 lines
3.3 KiB
C#
using AutoMapper;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using WMS.Web.Core.Dto.Inventory;
|
||
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.Values;
|
||
|
||
namespace WMS.Web.Domain.Services
|
||
{
|
||
/// <summary>
|
||
/// 物料收发明细:服务
|
||
/// </summary>
|
||
public class InventoryInOutDetailsService: IInventoryInOutDetailsService
|
||
{
|
||
private readonly IMapper _mapper;
|
||
private readonly IBoxRepositories _boxRepositories;
|
||
private readonly ISerialNumbersRepositories _serialNumbersRepositories;
|
||
private readonly IInventoryDetailsRepositories _inventoryDetailsRepositories;
|
||
private readonly IInventoryInOutDetailsRepositories _inventoryInOutDetailsRepositories;
|
||
public InventoryInOutDetailsService(IMapper mapper, IBoxRepositories boxRepositories,
|
||
ISerialNumbersRepositories serialNumbersRepositories,
|
||
IInventoryDetailsRepositories inventoryDetailsRepositories,
|
||
IInventoryInOutDetailsRepositories inventoryInOutDetailsRepositories)
|
||
{
|
||
_mapper = mapper;
|
||
_boxRepositories = boxRepositories;
|
||
_inventoryDetailsRepositories = inventoryDetailsRepositories;
|
||
_serialNumbersRepositories = serialNumbersRepositories;
|
||
_inventoryInOutDetailsRepositories = inventoryInOutDetailsRepositories;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 生成:物料收发明细
|
||
/// </summary>
|
||
/// <param name="dtos"></param>
|
||
/// <param name="isTransaction"></param>
|
||
/// <returns></returns>
|
||
public async Task<Result> GenerateInOrOutDetails(List<InventoryInOutDetailsGenerateDto> dtos,bool isTransaction)
|
||
{
|
||
//找到物料对应的即时库存明细
|
||
var materialIds = dtos.Select(x => x.MaterialId).ToList();
|
||
var stockCodes = dtos.GroupBy(x => x.StockCode).Select(x => x.Key).ToList();
|
||
var subStockIds = dtos.GroupBy(x => x.SubStockId).Select(x => x.Key).ToList();
|
||
var inventoryDetails=await _inventoryDetailsRepositories.GetListBy(materialIds, stockCodes, subStockIds);
|
||
|
||
//1.创建要新增的对象
|
||
var add_entitys = new List<InventoryInOutDetails>();
|
||
|
||
//2.遍历:box库存来源数据明细
|
||
foreach (var dto in dtos)
|
||
{
|
||
//2.1当前的即时库存明细
|
||
var currentDetail= inventoryDetails.Where(x => x.MaterialId == dto.MaterialId && x.StockCode == dto.StockCode && x.SubStockId == dto.SubStockId).FirstOrDefault();
|
||
|
||
//2.2组装物料收发明细实体
|
||
var entity = _mapper.Map<InventoryInOutDetails>(dto);
|
||
entity.SurplusQty = currentDetail == null ? 0 : currentDetail.Qty;
|
||
add_entitys.Add(entity);
|
||
}
|
||
|
||
var isSuccess = true;
|
||
if (add_entitys.Count != 0)
|
||
{
|
||
isSuccess = await _inventoryInOutDetailsRepositories.AddRange(add_entitys, isTransaction);
|
||
if(!isSuccess)
|
||
return Result.ReFailure(ResultCodes.InventoryInOutDetailsWriteError);
|
||
}
|
||
|
||
return Result.ReSuccess();
|
||
}
|
||
}
|
||
}
|