using Microsoft.AspNetCore.Http;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using WIDESEA_Common.StockEnum;
|
using WIDESEA_Common.WareHouseEnum;
|
using WIDESEA_Core;
|
using WIDESEA_Core.BaseRepository;
|
using WIDESEA_Core.BaseServices;
|
using WIDESEA_Core.Enums;
|
using WIDESEA_Core.Helper;
|
using WIDESEA_DTO.Outbound;
|
using WIDESEA_IRecordService;
|
using WIDESEA_IStockService;
|
using WIDESEA_Model.Models;
|
using WIDESEA_Model.Models.Basic;
|
|
namespace WIDESEA_IOutboundService
|
{
|
public partial class OutStockLockInfoService : ServiceBase<Dt_OutStockLockInfo, IRepository<Dt_OutStockLockInfo>>, IOutStockLockInfoService
|
{
|
public IRepository<Dt_OutStockLockInfo> Repository => BaseDal;
|
public IUnitOfWorkManage _unitOfWorkManage;
|
|
private readonly IStockService _stockService;
|
private readonly IRecordService _recordService;
|
|
public OutStockLockInfoService(IRepository<Dt_OutStockLockInfo> BaseDal, IUnitOfWorkManage unitOfWorkManage, IStockService stockService, IRecordService recordService) : base(BaseDal)
|
{
|
_unitOfWorkManage = unitOfWorkManage;
|
_stockService = stockService;
|
_recordService = recordService;
|
}
|
|
|
/// <summary>
|
/// 创建出库锁定
|
/// </summary>
|
public Dt_OutStockLockInfo GetOutStockLockInfo(
|
Dt_OutboundOrder outboundOrder,
|
Dt_OutboundOrderDetail outboundOrderDetail,
|
Dt_StockInfo outStock,
|
decimal assignQuantity,
|
string barcode = null)
|
{
|
// 获取库存明细信息
|
var stockDetails = outStock.Details
|
.Where(x => x.MaterielCode == outboundOrderDetail.MaterielCode)
|
.ToList();
|
|
if (!stockDetails.Any())
|
{
|
throw new Exception($"未找到物料[{outboundOrderDetail.MaterielCode}]的库存明细");
|
}
|
|
// 确定条码
|
string targetBarcode;
|
if (!string.IsNullOrEmpty(barcode))
|
{
|
// 验证指定的条码是否存在
|
var specifiedBarcodeDetail = stockDetails.FirstOrDefault(x => x.Barcode == barcode);
|
if (specifiedBarcodeDetail == null)
|
{
|
throw new Exception($"指定的条码[{barcode}]在库存中不存在");
|
}
|
targetBarcode = barcode;
|
}
|
else
|
{
|
// 使用第一个可用条码
|
var firstAvailableDetail = stockDetails
|
.Where(x => x.StockQuantity > x.OutboundQuantity)
|
.OrderBy(x => x.CreateDate)
|
.FirstOrDefault();
|
|
if (firstAvailableDetail == null)
|
{
|
throw new Exception($"物料[{outboundOrderDetail.MaterielCode}]没有可用库存");
|
}
|
targetBarcode = firstAvailableDetail.Barcode;
|
}
|
|
return new Dt_OutStockLockInfo()
|
{
|
lineNo= outboundOrderDetail.lineNo,
|
|
PalletCode = outStock.PalletCode,
|
AssignQuantity = assignQuantity,
|
MaterielCode = outboundOrderDetail.MaterielCode,
|
BatchNo = outboundOrderDetail.BatchNo ?? outStock.Details.FirstOrDefault()?.BatchNo,
|
LocationCode = outStock.LocationCode,
|
MaterielName = outboundOrderDetail.MaterielName,
|
OrderDetailId = outboundOrderDetail.Id,
|
OrderNo = outboundOrder.OrderNo,
|
OrderQuantity = outboundOrderDetail.OrderQuantity,
|
OriginalQuantity = outStock.Details
|
.Where(x => x.MaterielCode == outboundOrderDetail.MaterielCode)
|
.Sum(x => x.StockQuantity),
|
Status = (int)OutLockStockStatusEnum.已分配,
|
StockId = outStock.Id,
|
Unit = outboundOrderDetail.Unit,
|
FactoryArea = outboundOrder.FactoryArea,
|
OrderType=outboundOrder.OrderType,
|
SupplyCode = outboundOrderDetail.SupplyCode,
|
WarehouseCode = outboundOrderDetail.WarehouseCode,
|
// 新增字段
|
CurrentBarcode = targetBarcode,
|
OriginalLockQuantity = assignQuantity,
|
IsSplitted = 0
|
};
|
}
|
/// <summary>
|
/// 根据订单明细ID获取出库锁定信息
|
/// </summary>
|
public async Task<List<Dt_OutStockLockInfo>> GetByOrderDetailId(int orderDetailId)
|
{
|
return await Db.Queryable<Dt_OutStockLockInfo>()
|
.Where(x => x.OrderDetailId == orderDetailId)
|
.OrderBy(x => x.Id)
|
.ToListAsync();
|
}
|
|
public async Task<LockInfoDetailDto> GetLockInfoDetail(int lockInfoId)
|
{
|
var lockInfo = await Db.Queryable<Dt_OutStockLockInfo>()
|
.LeftJoin<Dt_OutboundOrderDetail>((lockInfo, detail) => lockInfo.OrderDetailId == detail.Id)
|
.Where((lockInfo, detail) => lockInfo.Id == lockInfoId)
|
.Select((lockInfo, detail) => new LockInfoDetailDto
|
{
|
Id = lockInfo.Id,
|
OrderNo = lockInfo.OrderNo,
|
OrderDetailId = lockInfo.OrderDetailId,
|
BatchNo = lockInfo.BatchNo,
|
MaterielCode = lockInfo.MaterielCode,
|
StockId = lockInfo.StockId,
|
OrderQuantity = lockInfo.OrderQuantity,
|
OriginalQuantity = lockInfo.OriginalQuantity,
|
AssignQuantity = lockInfo.AssignQuantity,
|
PickedQty = lockInfo.PickedQty,
|
LocationCode = lockInfo.LocationCode,
|
PalletCode = lockInfo.PalletCode,
|
Status = lockInfo.Status,
|
IsSplitted = lockInfo.IsSplitted,
|
ParentLockId = lockInfo.ParentLockId,
|
|
MaterielName = detail.MaterielName,
|
Unit = detail.Unit
|
})
|
.FirstAsync();
|
|
return lockInfo;
|
}
|
|
/// <summary>
|
/// 根据托盘编号获取出库锁定信息
|
/// </summary>
|
public async Task<List<Dt_OutStockLockInfo>> GetByPalletCode(string palletCode, int? status = null)
|
{
|
var query = Db.Queryable<Dt_OutStockLockInfo>()
|
.Where(x => x.PalletCode == palletCode);
|
|
if (status.HasValue)
|
{
|
query = query.Where(x => x.Status == status.Value);
|
}
|
|
return await query.OrderBy(x => x.Id).ToListAsync();
|
}
|
|
/// <summary>
|
/// 获取托盘的锁定信息
|
/// </summary>
|
public async Task<List<Dt_OutStockLockInfo>> GetPalletLockInfos(string palletCode)
|
{
|
return await Db.Queryable<Dt_OutStockLockInfo>()
|
.Where(x => x.PalletCode == palletCode && x.Status == (int)OutLockStockStatusEnum.出库中)
|
.ToListAsync();
|
}
|
|
/// <summary>
|
/// 更新出库锁定信息的条码(用于拆包操作)
|
/// </summary>
|
public async Task<WebResponseContent> UpdateLockInfoBarcode(int lockInfoId, string newBarcode)
|
{
|
try
|
{
|
var lockInfo = await Db.Queryable<Dt_OutStockLockInfo>()
|
.Where(x => x.Id == lockInfoId)
|
.FirstAsync();
|
|
if (lockInfo == null)
|
return WebResponseContent.Instance.Error("未找到出库锁定信息");
|
|
// 验证新条码是否存在
|
var stockDetail = await Db.Queryable<Dt_StockInfoDetail>()
|
.Where(x => x.Barcode == newBarcode &&
|
x.StockId == lockInfo.StockId &&
|
x.MaterielCode == lockInfo.MaterielCode)
|
.FirstAsync();
|
|
if (stockDetail == null)
|
return WebResponseContent.Instance.Error("新条码在库存中不存在");
|
|
// 更新条码和拆包状态
|
lockInfo.CurrentBarcode = newBarcode;
|
lockInfo.IsSplitted = 1;
|
|
await Db.Updateable(lockInfo).ExecuteCommandAsync();
|
|
return WebResponseContent.Instance.OK("更新条码成功");
|
}
|
catch (Exception ex)
|
{
|
return WebResponseContent.Instance.Error($"更新条码失败: {ex.Message}");
|
}
|
}
|
|
|
|
}
|
}
|