136 lines
6.0 KiB
C#
136 lines
6.0 KiB
C#
using AutoMapper;
|
||
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.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;
|
||
private readonly ILogger<InventoryDetailsService> _logger;
|
||
public InventoryDetailsService(IMapper mapper, IBoxRepositories boxRepositories,
|
||
ISerialNumbersRepositories serialNumbersRepositories, ILogger<InventoryDetailsService> logger,
|
||
IInventoryDetailsRepositories inventoryDetailsRepositories)
|
||
{
|
||
_mapper = mapper;
|
||
_logger = logger;
|
||
_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>();
|
||
_logger.LogInformation(" 生成:即时库存明细:" + JsonConvert.SerializeObject(dtos));
|
||
//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)
|
||
{
|
||
_logger.LogInformation(" 生成:即时库存明细-->明细NULL:" + JsonConvert.SerializeObject(dto));
|
||
//出库的时候,判断是否有库存,没有就返回错误
|
||
if (dto.InventoryInOutType == (int)InventoryInOutType.Out)
|
||
return Result.ReFailure(ResultCodes.InventoryDetailsNoInventoryError);
|
||
|
||
var entity = _mapper.Map<InventoryDetails>(dto);
|
||
add_entitys.Add(entity);
|
||
}
|
||
else
|
||
{
|
||
_logger.LogInformation(" 生成:即时库存明细-->明细HAVE:" + JsonConvert.SerializeObject(currentDetail));
|
||
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();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取即时库存明细汇总
|
||
/// </summary>
|
||
/// <param name="mids"></param>
|
||
/// <param name="stockCodes"></param>
|
||
/// <param name="orgCodes"></param>
|
||
/// <returns></returns>
|
||
public async Task<List<InventoryDetailsSummaryResponse>> GetInventoryDetailsSummary(List<int> mids, List<string> stockCodes, List<string> orgCodes)
|
||
{
|
||
var inventoryDetails = await _inventoryDetailsRepositories.GetListBy(mids, stockCodes, orgCodes);
|
||
var resultList = inventoryDetails.GroupBy(x => new { MaterialId = x.MaterialId, StockCode = x.StockCode, OrgCode = x.OrgCode })
|
||
.Select(x => new InventoryDetailsSummaryResponse()
|
||
{
|
||
MaterialId=x.Key.MaterialId,
|
||
StockCode=x.Key.StockCode,
|
||
OrgCode=x.Key.OrgCode,
|
||
Qty=x.Sum(t=>t.Qty)
|
||
}).ToList();
|
||
return resultList;
|
||
}
|
||
}
|
||
}
|