256 lines
10 KiB
C#
256 lines
10 KiB
C#
using AutoMapper;
|
||
using Microsoft.EntityFrameworkCore.Storage;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using WMS.Web.Core.Dto;
|
||
using WMS.Web.Core.Dto.InStock;
|
||
using WMS.Web.Core.Dto.InStockTask;
|
||
using WMS.Web.Core.Dto.Login;
|
||
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.Mappers;
|
||
using WMS.Web.Domain.Values;
|
||
|
||
namespace WMS.Web.Domain.Services
|
||
{
|
||
/// <summary>
|
||
/// 入库单服务
|
||
/// </summary>
|
||
public class InStockService : IInStockService
|
||
{
|
||
private readonly IMapper _mapper;
|
||
private readonly ILoginService _loginService;
|
||
private readonly IBoxRepositories _boxRepositories;
|
||
private readonly IBasicsRepositories _basicsRepositories;
|
||
private readonly IInStockRepositories _inStockRepositories;
|
||
private readonly IInStockTaskRepositories _inStockTaskRepositories;
|
||
public InStockService(IMapper mapper, ILoginService loginService, IBoxRepositories boxRepositories,
|
||
IBasicsRepositories basicsRepositories,
|
||
IInStockRepositories inStockRepositories, IInStockTaskRepositories inStockTaskRepositories)
|
||
{
|
||
_mapper = mapper;
|
||
_loginService = loginService;
|
||
_boxRepositories = boxRepositories;
|
||
_basicsRepositories = basicsRepositories;
|
||
_inStockRepositories = inStockRepositories;
|
||
_inStockTaskRepositories = inStockTaskRepositories;
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 同步金蝶
|
||
/// </summary>
|
||
/// <param name="dto"></param>
|
||
/// <returns></returns>
|
||
public Task<Result> Sync(OperateRequest dto)
|
||
{
|
||
//调用同步服务方法
|
||
|
||
return Task.FromResult(Result.ReSuccess());
|
||
}
|
||
|
||
/// <summary>
|
||
/// 收货
|
||
/// </summary>
|
||
/// <param name="dto"></param>
|
||
/// <param name="loginInfo"></param>
|
||
/// <returns></returns>
|
||
public async Task<Result> Receive(UpdateInStockTaskRequest dto, LoginInDto loginInfo)
|
||
{
|
||
IDbContextTransaction _transaction = _basicsRepositories.GetTransaction();
|
||
bool isRollback = false;
|
||
bool isTransaction = false;
|
||
var result = await this.Update(dto, loginInfo.UserInfo.StaffId,true, isTransaction);
|
||
if (!result.IsSuccess) isRollback = true;
|
||
|
||
//提交事务
|
||
var isSuccess = _basicsRepositories.CommitTransaction(isRollback, _transaction);
|
||
if (!isSuccess)
|
||
return Result.ReFailure(ResultCodes.DateWriteError);
|
||
|
||
return Result.ReSuccess();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 上架-采购订单
|
||
/// </summary>
|
||
/// <param name="instock"></param>
|
||
/// <param name="loginInfo"></param>
|
||
/// <returns></returns>
|
||
public async Task<Result> Shelf(PurchaseShelfRequest instock, LoginInDto loginInfo)
|
||
{
|
||
IDbContextTransaction _transaction = _basicsRepositories.GetTransaction();
|
||
bool isRollback = false;
|
||
bool isTransaction = false;
|
||
//1.添加入库单:(同步金蝶在save方法里面进行)
|
||
var save_result = await this.Save(instock, InstockType.Purchase, loginInfo.UserInfo.StaffId, isTransaction);
|
||
if (!save_result.IsSuccess) isRollback = true;
|
||
{
|
||
//2.修改入库任务单
|
||
var ids = instock.Details.GroupBy(x => x.TaskId).Select(x => x.Key).ToList();
|
||
var result = await this.UpdateRange(ids, loginInfo.UserInfo.StaffId, false, isTransaction);
|
||
if (!result.IsSuccess) isRollback = true;
|
||
}
|
||
|
||
//提交事务
|
||
var isSuccess = _basicsRepositories.CommitTransaction(isRollback, _transaction);
|
||
if (!isSuccess)
|
||
return Result.ReFailure(ResultCodes.DateWriteError);
|
||
|
||
return Result.ReSuccess();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取任务单:根据箱号信息
|
||
/// </summary>
|
||
/// <param name="boxBillNo"></param>
|
||
/// <returns></returns>
|
||
public async Task<Result<InStockTaskInfoDto>> GetInfoByBox(string boxBillNo)
|
||
{
|
||
//1.先判断:箱号已经绑定了入库任务单中;备注:业务说法就是箱是否收货了
|
||
var tast= await _inStockTaskRepositories.GetBy(boxBillNo);
|
||
if (tast==null)
|
||
return Result<InStockTaskInfoDto>.ReFailure(ResultCodes.Box_NoBind_Task_Data);
|
||
|
||
//2.找到箱对应的物料信息
|
||
var box= await _boxRepositories.GetByNo(boxBillNo);
|
||
if(box==null)
|
||
return Result<InStockTaskInfoDto>.ReFailure(ResultCodes.BoxNoData);
|
||
|
||
//3.组装返回数据
|
||
var result = _mapper.Map<InStockTaskInfoDto>(tast);
|
||
foreach (var item in tast.Details)
|
||
{
|
||
//3.1判断当前物料是否相同
|
||
var isHave = box.Details.Where(x => x.MaterialId == item.MaterialId).Any();
|
||
if (!isHave)
|
||
continue;
|
||
//3.2映射返回明细对象
|
||
var task_detail = _mapper.Map<InStockTaskDetailsInfoDto>(item);
|
||
result.Details.Add(task_detail);
|
||
}
|
||
return Result<InStockTaskInfoDto>.ReSuccess(result);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 采购上架-保存
|
||
/// </summary>
|
||
/// <param name="dto"></param>
|
||
/// <param name="staffId"></param>
|
||
/// <param name="isTransaction"></param>
|
||
/// <returns></returns>
|
||
private async Task<Result> Save(PurchaseShelfRequest dto, InstockType type, int staffId, bool isTransaction = true)
|
||
{
|
||
var entity = new InStock();
|
||
entity.Type = type;
|
||
entity.Details = _mapper.Map<List<InStockDetails>>(dto.Details);
|
||
entity.Create(staffId);
|
||
|
||
//需要填写序列号
|
||
//需要修改库存
|
||
//需要同步金蝶
|
||
|
||
entity = await _inStockRepositories.Add(entity, isTransaction);
|
||
|
||
if (entity != null)
|
||
return Result.ReSuccess();
|
||
else
|
||
return Result.ReFailure(ResultCodes.DateWriteError);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 修改-入库任务信息
|
||
/// </summary>
|
||
/// <param name="dto"></param>
|
||
/// <param name="staffId"></param>
|
||
/// <param name="isReceive"></param>
|
||
/// <param name="isTransaction"></param>
|
||
/// <returns></returns>
|
||
public async Task<Result> Update(UpdateInStockTaskRequest dto, int staffId,bool isReceive, bool isTransaction = true)
|
||
{
|
||
var entity = await _inStockTaskRepositories.Get(dto.Id);
|
||
if (entity == null)
|
||
return Result.ReFailure(ResultCodes.DateWriteError);
|
||
|
||
entity = _mapper.Map(dto, entity);
|
||
//子集单独映射
|
||
entity.Boxs = _mapper.ToMapList(dto.Boxs, entity.Boxs);
|
||
//子集单独映射
|
||
entity.Details = _mapper.ToMapList(dto.Details, entity.Details);
|
||
if (isReceive)
|
||
entity.Receive(staffId);
|
||
else
|
||
entity.Shelf(staffId);
|
||
var result =await _inStockTaskRepositories.Update(entity, isTransaction);
|
||
if (result != null)
|
||
return Result.ReSuccess();
|
||
else
|
||
return Result.ReFailure(ResultCodes.DateWriteError);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 批量修改-入库任务信息
|
||
/// </summary>
|
||
/// <param name="ids"></param>
|
||
/// <param name="staffId"></param>
|
||
/// <param name="isReceive"></param>
|
||
/// <param name="isTransaction"></param>
|
||
/// <returns></returns>
|
||
public async Task<Result> UpdateRange(List<int> ids, int staffId, bool isReceive, bool isTransaction = true)
|
||
{
|
||
var entitys = await _inStockTaskRepositories.GetList(ids);
|
||
if (entitys == null || entitys.Count==0)
|
||
return Result.ReFailure(ResultCodes.DateWriteError);
|
||
|
||
foreach (var item in entitys)
|
||
{
|
||
if (isReceive)
|
||
item.Receive(staffId);
|
||
else
|
||
item.Shelf(staffId);
|
||
}
|
||
var isSuccess = await _inStockTaskRepositories.UpdateRange(entitys, isTransaction);
|
||
if (isSuccess)
|
||
return Result.ReSuccess();
|
||
else
|
||
return Result.ReFailure(ResultCodes.DateWriteError);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 采购订单物料明细和箱物料明细-对比
|
||
/// </summary>
|
||
/// <param name="dto"></param>
|
||
/// <returns></returns>
|
||
public async Task<Result<ContrastMaterialsResponse>> Contrast(ContrastMaterialsRequest dto)
|
||
{
|
||
//1.找到任务单的明细信息
|
||
var task = await _inStockTaskRepositories.Get(dto.TaskId);
|
||
if (task == null)
|
||
return Result<ContrastMaterialsResponse>.ReFailure(ResultCodes.OrderNoData);
|
||
|
||
//2.找到箱对应的物料信息
|
||
var box = await _boxRepositories.GetByNo(dto.BoxBillNo);
|
||
if (box == null)
|
||
return Result<ContrastMaterialsResponse>.ReFailure(ResultCodes.BoxNoData);
|
||
|
||
//3.比对
|
||
bool isRight = box.Details.All(x => task.Details.Any(t => t.MaterialId == x.MaterialId && t.AccruedQty == x.Qty)) && box.Details.Count == task.Details.Count;
|
||
|
||
//4.返回对比结果:true为比对成功,false为比对失败;并把箱ID和箱号返回
|
||
var response = new ContrastMaterialsResponse();
|
||
response.BoxBillNo = box.BoxBillNo;
|
||
response.BoxId = box.Id;
|
||
response.TotalCount = box.Details.Sum(x => x.Qty);
|
||
response.IsRight = isRight;
|
||
return Result<ContrastMaterialsResponse>.ReSuccess(response);
|
||
}
|
||
}
|
||
}
|