194 lines
7.0 KiB
C#
194 lines
7.0 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;
|
||
public readonly ITransactionRepositories _transactionRepositories;
|
||
private readonly IInStockRepositories _inStockRepositories;
|
||
private readonly IInStockTaskRepositories _inStockTaskRepositories;
|
||
public InStockService(IMapper mapper, ILoginService loginService,
|
||
ITransactionRepositories transactionRepositories,
|
||
IInStockRepositories inStockRepositories, IInStockTaskRepositories inStockTaskRepositories)
|
||
{
|
||
_mapper = mapper;
|
||
_loginService = loginService;
|
||
_transactionRepositories = transactionRepositories;
|
||
_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 = _transactionRepositories.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 = _transactionRepositories.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(SaveInStockRequest instock, LoginInDto loginInfo)
|
||
{
|
||
IDbContextTransaction _transaction = _transactionRepositories.GetTransaction();
|
||
bool isRollback = false;
|
||
bool isTransaction = false;
|
||
|
||
//1.添加入库单:(同步金蝶在save方法里面进行)
|
||
var save_result = await this.Save(instock, loginInfo.UserInfo.StaffId, isTransaction);
|
||
if (!save_result.IsSuccess) isRollback = true;
|
||
{
|
||
//2.修改入库任务单
|
||
var ids = new List<int>();
|
||
ids.Add(instock.TaskId);
|
||
var result = await this.UpdateRange(ids, loginInfo.UserInfo.StaffId, false, isTransaction);
|
||
if (!result.IsSuccess) isRollback = true;
|
||
}
|
||
|
||
//提交事务
|
||
var isSuccess = _transactionRepositories.CommitTransaction(isRollback, _transaction);
|
||
if (!isSuccess)
|
||
return Result.ReFailure(ResultCodes.DateWriteError);
|
||
|
||
return Result.ReSuccess();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 保存-出库单
|
||
/// </summary>
|
||
/// <param name="dto"></param>
|
||
/// <param name="staffId"></param>
|
||
/// <param name="isTransaction"></param>
|
||
/// <returns></returns>
|
||
public async Task<Result> Save(SaveInStockRequest dto, int staffId, bool isTransaction = true)
|
||
{
|
||
var entity = new InStock();
|
||
entity.Type = (InstockType)dto.Type;
|
||
entity.TaskId = dto.TaskId;
|
||
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.Details = _mapper.ToMapList(dto.Details, entity.Details);
|
||
if (isReceive)
|
||
entity.Receive(staffId);
|
||
else
|
||
entity.Shelf(staffId);
|
||
var result = _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);
|
||
}
|
||
}
|
||
}
|