箱库存方法-优化1
This commit is contained in:
@@ -1,22 +1,191 @@
|
||||
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 InventoryService: IInventoryService
|
||||
public class InventoryService : IInventoryService
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
public InventoryService(IMapper mapper)
|
||||
private readonly IBoxRepositories _boxRepositories;
|
||||
private readonly ISerialNumbersRepositories _serialNumbersRepositories;
|
||||
private readonly IBoxInventoryRepositories _boxInventoryRepositories;
|
||||
public InventoryService(IMapper mapper, IBoxRepositories boxRepositories,
|
||||
ISerialNumbersRepositories serialNumbersRepositories,
|
||||
IBoxInventoryRepositories boxInventoryRepositories)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_boxRepositories = boxRepositories;
|
||||
_serialNumbersRepositories = serialNumbersRepositories;
|
||||
_boxInventoryRepositories = boxInventoryRepositories;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 改箱-箱库存的变更
|
||||
/// </summary>
|
||||
/// <param name="dtoDatas"></param>
|
||||
/// <param name="isTransaction"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Result> GenerateChangeBox(List<BoxInventoryChangeGenerateDto> dtoDatas, bool isTransaction)
|
||||
{
|
||||
//1.判断来源数据是否存在
|
||||
if (dtoDatas != null || dtoDatas.Count == 0)
|
||||
return Result.ReFailure(ResultCodes.InventoryNoSourceError);
|
||||
|
||||
var update_entitys = new List<BoxInventory>();
|
||||
var add_entitys = new List<BoxInventory>();
|
||||
|
||||
var boxIds = dtoDatas.GroupBy(x => x.BoxId).Select(x => x.Key).ToList();
|
||||
var targetBoxIds = dtoDatas.GroupBy(x => x.TargetBoxId).Select(x => x.Key).ToList();
|
||||
|
||||
//2.原来箱库存集合
|
||||
var sourceBoxInventorys = await _boxInventoryRepositories.GetList(boxIds);
|
||||
|
||||
//2.1目标箱库存集合
|
||||
var targetBoxInventorys = await _boxInventoryRepositories.GetList(targetBoxIds);
|
||||
|
||||
|
||||
foreach (var dto in dtoDatas)
|
||||
{
|
||||
#region 原箱库存-库存处理
|
||||
//2.1找到要改变的源箱数据:备注(原箱号库存一定是存在的)
|
||||
var sour = sourceBoxInventorys.Where(x => x.BoxId == dto.BoxId).FirstOrDefault();
|
||||
if (sour == null)
|
||||
return Result.ReFailure(ResultCodes.BoxInventoryNoDataError);
|
||||
|
||||
var sour_update_entity = new BoxInventory();
|
||||
sour_update_entity.Details = new List<BoxInventoryDetails>();
|
||||
foreach (var dtoItem in dto.Details)
|
||||
{
|
||||
//找到原来的箱库存对应要改变的物料:这个一定是存在的,不存在就有问题
|
||||
var sour_ChangeDetail = sour.Details.Where(x => x.MaterialId == dtoItem.MaterialId).FirstOrDefault();
|
||||
if(sour_ChangeDetail==null)
|
||||
return Result.ReFailure(ResultCodes.BoxInventoryNoDataError);
|
||||
//箱物料的库存数量不足;小于要拿出来的数量
|
||||
if(sour_ChangeDetail.Qty<dtoItem.Qty)
|
||||
return Result.ReFailure(ResultCodes.BoxInventoryNoInventoryError);
|
||||
|
||||
var s_update_entity_det = new BoxInventoryDetails();
|
||||
s_update_entity_det.SerialNumbers = new List<string>();
|
||||
s_update_entity_det.Qty = sour_ChangeDetail.Qty - dtoItem.Qty;
|
||||
|
||||
//先把现有的序列号复制过来
|
||||
s_update_entity_det.SerialNumbers = sour_ChangeDetail.SerialNumbers;
|
||||
//再移除要改箱的序列号;最后就是原箱库存的新序列号
|
||||
s_update_entity_det.SerialNumbers.RemoveAll(x => dtoItem.SerialNumbers.Contains(x));
|
||||
|
||||
sour_update_entity.Details.Add(s_update_entity_det);
|
||||
|
||||
}
|
||||
update_entitys.Add(sour_update_entity);
|
||||
#endregion
|
||||
|
||||
#region 目标箱库存-库存处理
|
||||
//2.2找到要改变的目标箱数据:备注(目标箱库存可能不存在库存,就要新增)
|
||||
var tagBox = targetBoxInventorys.Where(x => x.BoxId == dto.TargetBoxId).FirstOrDefault();
|
||||
if (tagBox == null)
|
||||
{
|
||||
var newTagBox = new BoxInventory();
|
||||
newTagBox.BoxId = dto.TargetBoxId;
|
||||
newTagBox.Details =_mapper.Map<List<BoxInventoryDetails>>(dto.Details);
|
||||
add_entitys.Add(newTagBox);
|
||||
}
|
||||
else
|
||||
{
|
||||
var tag_update_entity = new BoxInventory();
|
||||
tag_update_entity.Details = new List<BoxInventoryDetails>();
|
||||
//2.2.1遍历改变的明细
|
||||
foreach (var item in dto.Details)
|
||||
{
|
||||
//2.2.2找到目标箱对应要改变的物料
|
||||
var tagChangeDetail = tagBox.Details.Where(x => x.MaterialId == item.MaterialId).FirstOrDefault();
|
||||
if (tagChangeDetail != null)
|
||||
{
|
||||
var update_entity_det = new BoxInventoryDetails();
|
||||
update_entity_det.SerialNumbers = new List<string>();
|
||||
update_entity_det.Qty = tagChangeDetail.Qty + item.Qty;
|
||||
update_entity_det.SerialNumbers.AddRange(tagChangeDetail.SerialNumbers);
|
||||
update_entity_det.SerialNumbers.AddRange(item.SerialNumbers);
|
||||
tag_update_entity.Details.Add(update_entity_det);
|
||||
}//2.3.没有对应的箱物料,就新增明细
|
||||
else
|
||||
{
|
||||
var newTagChangeDetail = new BoxInventoryDetails();
|
||||
newTagChangeDetail.MaterialId = item.MaterialId;
|
||||
newTagChangeDetail.Qty = item.Qty;
|
||||
newTagChangeDetail.SerialNumbers = item.SerialNumbers;
|
||||
tag_update_entity.Details.Add(newTagChangeDetail);
|
||||
}
|
||||
}
|
||||
update_entitys.Add(tag_update_entity);
|
||||
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
//4.数据库更新操作:更新和添加
|
||||
var isSuccess= await _boxInventoryRepositories.AddRange(add_entitys, isTransaction);
|
||||
if(!isSuccess) return Result.ReFailure(ResultCodes.DateWriteError);
|
||||
|
||||
isSuccess = await _boxInventoryRepositories.UpdateRange(update_entitys, isTransaction);
|
||||
if (!isSuccess) return Result.ReFailure(ResultCodes.DateWriteError);
|
||||
|
||||
return Result.ReSuccess();
|
||||
}
|
||||
|
||||
public async Task<Result> GenerateMoveBox(List<BoxInventoryMoveGenerateDto> dtoDatas, bool isTransaction)
|
||||
{
|
||||
//1.判断来源数据是否存在
|
||||
if (dtoDatas != null || dtoDatas.Count == 0)
|
||||
return Result.ReFailure(ResultCodes.InventoryNoSourceError);
|
||||
|
||||
var update_entitys = new List<BoxInventory>();
|
||||
var add_entitys = new List<BoxInventory>();
|
||||
|
||||
//2.获取“箱库存集合”和“箱基本信息集合”
|
||||
var boxIds = dtoDatas.GroupBy(x => x.BoxId).Select(x => x.Key).ToList();
|
||||
var boxInventorys = await _boxInventoryRepositories.GetList(boxIds);
|
||||
var boxs= await _boxRepositories.GetEntityList(boxIds);
|
||||
|
||||
//3.遍历:dto
|
||||
foreach (var dto in dtoDatas)
|
||||
{
|
||||
//上架
|
||||
if (dto.IsOn)
|
||||
{
|
||||
//3.1上架的时候:箱一定是不存在于箱库存当中的,
|
||||
var ishave = boxInventorys.Where(x => x.BoxId == dto.BoxId).Any();
|
||||
if (ishave)
|
||||
return Result.ReFailure(ResultCodes.BoxInventoryHaveInventoryError);
|
||||
var box = boxs.Where(x => x.Id == dto.BoxId).FirstOrDefault();
|
||||
//var serialNumbers= _serialNumbersRepositories.ge
|
||||
if (box==null)
|
||||
return Result.ReFailure(ResultCodes.BoxNoData);
|
||||
|
||||
var addEntity = _mapper.Map<BoxInventory>(dto);
|
||||
addEntity.Details = new List<BoxInventoryDetails>();
|
||||
|
||||
|
||||
}//下架
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return Result.ReSuccess();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user