127 lines
5.3 KiB
C#
127 lines
5.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.IService.Public;
|
||
using WMS.Web.Domain.Services.Public;
|
||
using WMS.Web.Domain.Values;
|
||
|
||
namespace WMS.Web.Domain.Services
|
||
{
|
||
/// <summary>
|
||
/// 物料收发明细:服务
|
||
/// </summary>
|
||
public class InventoryInOutDetailsService: IInventoryInOutDetailsService
|
||
{
|
||
private readonly IMapper _mapper;
|
||
private readonly ILoginRepositories _loginRepositories;
|
||
private readonly IBoxRepositories _boxRepositories;
|
||
private readonly ISerialNumbersRepositories _serialNumbersRepositories;
|
||
private readonly IInventoryDetailsRepositories _inventoryDetailsRepositories;
|
||
private readonly IInventoryInOutDetailsRepositories _inventoryInOutDetailsRepositories;
|
||
public InventoryInOutDetailsService(IMapper mapper, IBoxRepositories boxRepositories,
|
||
ILoginRepositories loginRepositories,
|
||
ISerialNumbersRepositories serialNumbersRepositories,
|
||
IInventoryDetailsRepositories inventoryDetailsRepositories,
|
||
IInventoryInOutDetailsRepositories inventoryInOutDetailsRepositories)
|
||
{
|
||
_mapper = mapper;
|
||
_loginRepositories = loginRepositories;
|
||
_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 orgCodes = dtos.Select(x => x.OrgCode).ToList();
|
||
var materialNumbers = dtos.Select(x => x.MaterialNumber).ToList();
|
||
var stockCodes = dtos.GroupBy(x => x.StockCode).Select(x => x.Key).ToList();
|
||
var inventoryDetails=await _inventoryDetailsRepositories.GetListBy(materialNumbers, stockCodes, orgCodes);
|
||
|
||
//还要汇总出:只对仓库的库存合集,不要仓位
|
||
var inventoryDetailsTotal = inventoryDetails.GroupBy(x => new { x.OrgCode, x.MaterialNumber, x.StockCode })
|
||
.Select(x => new
|
||
{
|
||
OrgCode = x.Key.OrgCode,
|
||
MaterialNumber = x.Key.MaterialNumber,
|
||
StockCode = x.Key.StockCode,
|
||
Qty = x.Sum(t => t.Qty)
|
||
}).ToList();
|
||
|
||
//1.创建要新增的对象
|
||
var add_entitys = new List<InventoryInOutDetails>();
|
||
|
||
//2.遍历:box库存来源数据明细
|
||
foreach (var dto in dtos)
|
||
{
|
||
//2.1当前的即时库存明细
|
||
var currentDetail= inventoryDetailsTotal.Where(x =>
|
||
x.MaterialNumber == dto.MaterialNumber &&
|
||
x.StockCode == dto.StockCode &&
|
||
x.OrgCode==dto.OrgCode).FirstOrDefault();
|
||
|
||
//2.2组装物料收发明细实体
|
||
var entity = _mapper.Map<InventoryInOutDetails>(dto);
|
||
entity.SurplusQty = currentDetail == null ? 0 : currentDetail.Qty;
|
||
entity.Create(_loginRepositories.StaffId);
|
||
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();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 生成dto
|
||
/// </summary>
|
||
/// <param name="boxId"></param>
|
||
/// <param name="materialNumber"></param>
|
||
/// <param name="orgCode"></param>
|
||
/// <param name="stockCode"></param>
|
||
/// <param name="subStockCode"></param>
|
||
/// <param name="orderType"></param>
|
||
/// <param name="billNo"></param>
|
||
/// <param name="qty"></param>
|
||
/// <param name="inventoryInOutType"></param>
|
||
/// <returns></returns>
|
||
public InventoryInOutDetailsGenerateDto GenerateDto(int boxId,string materialNumber, string orgCode,string stockCode, string subStockCode, OrderType orderType,string billNo,decimal qty, InventoryInOutType inventoryInOutType)
|
||
{
|
||
var item = new InventoryInOutDetailsGenerateDto();
|
||
item.BoxId = boxId;
|
||
item.MaterialNumber = materialNumber;
|
||
item.OrgCode = orgCode;
|
||
item.StockCode = stockCode;
|
||
item.SubStockCode = subStockCode;
|
||
item.OrderType = (int)orderType;
|
||
item.OrderBillNo = billNo;
|
||
item.Qty = qty;
|
||
item.InventoryInOutType = (int)inventoryInOutType;
|
||
return item;
|
||
}
|
||
}
|
||
}
|