109 lines
4.4 KiB
C#
109 lines
4.4 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 InventoryDetailsService: IInventoryDetailsService
|
|
{
|
|
private readonly IMapper _mapper;
|
|
private readonly IBoxRepositories _boxRepositories;
|
|
private readonly ISerialNumbersRepositories _serialNumbersRepositories;
|
|
private readonly IInventoryDetailsRepositories _inventoryDetailsRepositories;
|
|
public InventoryDetailsService(IMapper mapper, IBoxRepositories boxRepositories,
|
|
ISerialNumbersRepositories serialNumbersRepositories,
|
|
IInventoryDetailsRepositories inventoryDetailsRepositories)
|
|
{
|
|
_mapper = mapper;
|
|
_boxRepositories = boxRepositories;
|
|
_inventoryDetailsRepositories = inventoryDetailsRepositories;
|
|
_serialNumbersRepositories = serialNumbersRepositories;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 生成:即时库存明细
|
|
/// </summary>
|
|
/// <param name="dtos"></param>
|
|
/// <param name="isTransaction"></param>
|
|
/// <returns></returns>
|
|
public async Task<Result> GenerateInventoryDetails(List<InventoryDetailsGenerateDto> dtos,bool isTransaction)
|
|
{
|
|
//找到物料对应的即时库存明细
|
|
var orgCodes = dtos.Select(x => x.OrgCode).ToList();
|
|
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, orgCodes);
|
|
|
|
|
|
var add_entitys = new List<InventoryDetails>();
|
|
var update_entitys = new List<InventoryDetails>();
|
|
|
|
//2.遍历
|
|
foreach (var dto in dtos)
|
|
{
|
|
//2.1当前的即时库存明细
|
|
var currentDetail = inventoryDetails.Where(x =>
|
|
x.MaterialId == dto.MaterialId &&
|
|
x.StockCode == dto.StockCode &&
|
|
x.SubStockId == dto.SubStockId &&
|
|
x.OrgCode==dto.OrgCode).FirstOrDefault();
|
|
if (currentDetail == null)
|
|
{
|
|
//出库的时候,判断是否有库存,没有就返回错误
|
|
if (dto.InventoryInOutType == (int)InventoryInOutType.Out)
|
|
return Result.ReFailure(ResultCodes.InventoryDetailsNoInventoryError);
|
|
|
|
var entity = _mapper.Map<InventoryDetails>(dto);
|
|
add_entitys.Add(entity);
|
|
}
|
|
else
|
|
{
|
|
var entity = currentDetail;
|
|
if (dto.InventoryInOutType == (int)InventoryInOutType.In)
|
|
{
|
|
//加库存
|
|
entity.Qty = entity.Qty + dto.Qty;
|
|
}
|
|
else
|
|
{
|
|
//出库的时候,判断是否有库存,没有就返回错误
|
|
if (entity.Qty<dto.Qty)
|
|
return Result.ReFailure(ResultCodes.InventoryDetailsNoInventoryError);
|
|
//减库存
|
|
entity.Qty = entity.Qty - dto.Qty;
|
|
}
|
|
update_entitys.Add(entity);
|
|
}
|
|
}
|
|
|
|
|
|
//4.数据库更新操作:更新和添加
|
|
var isSuccess = true;
|
|
if (add_entitys.Count != 0)
|
|
{
|
|
isSuccess = await _inventoryDetailsRepositories.AddRange(add_entitys, isTransaction);
|
|
if (!isSuccess) return Result.ReFailure(ResultCodes.InventoryDetailsWriteError);
|
|
}
|
|
if (update_entitys.Count != 0)
|
|
{
|
|
isSuccess = await _inventoryDetailsRepositories.UpdateRange(update_entitys, isTransaction);
|
|
if (!isSuccess) return Result.ReFailure(ResultCodes.InventoryDetailsWriteError);
|
|
}
|
|
return Result.ReSuccess();
|
|
}
|
|
}
|
|
}
|