using Magicodes.IE.Core;
using MailKit.Search;
using System;
using System.Threading.Tasks;
using WIDESEA_Core;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.Inbound;
using WIDESEA_DTO.WCSInfo;
using WIDESEA_Model.Models;
using WIDESEAWCS_DTO.WCSInfo;
namespace WIDESEA_TaskInfoService
{
public partial class TaskService
{
///
/// 产线申请
///
///
///
public WebResponseContent ProductionlineRequest(ProductionlineDTO productionlineDTO)
{
WebResponseContent content = new WebResponseContent();
try
{
#region MyRegion
Dt_InventoryInfo inventoryInfo = _inventoryInfoRepository.QueryFirst(x => x.PalletCode == productionlineDTO.Barcode);
if (inventoryInfo != null) throw new Exception($"托盘【{productionlineDTO.Barcode}】已存在库存信息");
Dt_InboundOrderDetail inboundOrderDetail = _inboundService.InboundOrderDetailService.Repository.QueryFirst(x => x.BatchNo == productionlineDTO.batchNo);
if (inboundOrderDetail == null) throw new Exception($"未找到批号【{productionlineDTO.batchNo}】的入库单");
if (inboundOrderDetail.OrderQuantity - inboundOrderDetail.ReceiptQuantity < 1) throw new Exception($"批号【{productionlineDTO.batchNo}】的可组盘数量不足");
Dt_InboundOrder inboundOrder = _inboundService.InbounOrderService.Repository.QueryFirst(x => x.Id == inboundOrderDetail.OrderId);
if (inboundOrder == null) throw new Exception($"未找到批号为【{productionlineDTO.batchNo}】的入库单");
var materielInfo = _basicRepository.MaterielInfoRepository.QueryFirst(x => x.MaterielCode == inboundOrderDetail.MaterielCode);
if (materielInfo == null) throw new Exception($"未找到物料信息,物料编码【{inboundOrderDetail.MaterielCode}】");
Dt_Task dt_Task = new Dt_Task()
{
CurrentAddress = productionlineDTO.stationCode,
NextAddress = "SC01",
SourceAddress = productionlineDTO.stationCode,
TargetAddress = "SC01",
Creater = "System",
PalletCode = productionlineDTO.Barcode,
Roadway = "SC01",
OrderNo = productionlineDTO.batchNo,// inboundOrder.OrderNo,
TaskNum = BaseDal.GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
TaskState = InTaskStatusEnum.InNew.ObjToInt(),
TaskType = TaskTypeEnum.Inbound.ObjToInt(),
CreateDate = DateTime.Now,
Dispatchertime = DateTime.Now,
};
var productionDate = DateTime.Now.ToString("yyyy-MM-dd");
var validityPeriod = DateTime.Now.AddDays(materielInfo.Validity).ToString("yyyy-MM-dd");//加天
//var validityPeriod = DateTime.Now.AddMonths(materielInfo.Validity).ToString("yyyy-MM-dd");//加月
//var validityPeriod = DateTime.Now.AddYears(materielInfo.Validity).ToString("yyyy-MM-dd");//加年
#region 库存
inventoryInfo = new Dt_InventoryInfo()
{
BatchNo = productionlineDTO.batchNo,
MaterielCode = inboundOrderDetail.MaterielCode,
MaterielName = inboundOrderDetail.MaterielName,
PalletCode = productionlineDTO.Barcode,
ProductionDate = productionDate,
StockStatus = StockStatusEmun.组盘暂存.ObjToInt(),
ProductStatus = ProductStatusEmun.待检1.ObjToInt(),
ShelfLife = materielInfo.Validity,
ValidityPeriod = validityPeriod,
Creater = "System"
};
#endregion
#region 入库单
inboundOrder.OrderStatus = InboundStatusEnum.入库中.ObjToInt();
var Item1 = _inboundService.InboundOrderDetailService.GetBoxStockQuantity(inboundOrderDetail);
inventoryInfo.StockQuantity = Item1.StockQuantity;
inventoryInfo.BoxQuantity = Item1.BoxQuantity;
inboundOrderDetail.ReceiptQuantity = inboundOrderDetail.ReceiptQuantity + Item1.StockQuantity;
inboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.GroupAndInbound.ObjToInt();
#endregion
Db.Ado.BeginTran();
_inboundService.InbounOrderService.UpdateData(inboundOrder);
_inboundService.InboundOrderDetailService.UpdateData(inboundOrderDetail);
_inventoryInfoRepository.AddData(inventoryInfo);
AddData(dt_Task);
content.OK(data: dt_Task);
Db.Ado.CommitTran();
#endregion
}
catch (Exception ex)
{
Db.Ado.RollbackTran();
content.Error(ex.Message);
}
return content;
}
///
/// 产线申请
///
///
///
public WebResponseContent ProductionlineRequest(ConveyorLineDTO lineDTO)
{
WebResponseContent content = new WebResponseContent();
try
{
var task = BaseDal.QueryFirst(x => x.PalletCode == lineDTO.Barcode);
if (task != null && task.TaskType == TaskTypeEnum.Inbound.ObjToInt())
return content.OK(data: task);
if (task != null) throw new Exception($"托盘号[{lineDTO.Barcode}]已存在任务");
Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(lineDTO.Barcode);
(bool, string) result = CheckRequestInbound(lineDTO.stationCode, lineDTO.Barcode, true, stockInfo);
if (!result.Item1) return content = WebResponseContent.Instance.Error(result.Item2);
var StockInfoDetails = stockInfo.Details.Where(x => x.StockId == stockInfo.Id).ToList();
#region 生成入库任务
Dt_Task dt_Task = new Dt_Task()
{
CurrentAddress = lineDTO.stationCode,
NextAddress = "SC01",
SourceAddress = lineDTO.stationCode,
TargetAddress = "SC01",
CreateDate = DateTime.Now,
Creater = "System",
PalletCode = lineDTO.Barcode,
OrderNo = StockInfoDetails.Count() == 1 ? StockInfoDetails.First().OrderNo : null,
Roadway = "SC01",
TaskNum = BaseDal.GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
TaskState = InTaskStatusEnum.InNew.ObjToInt(),
TaskType = TaskTypeEnum.Inbound.ObjToInt(),
};
dt_Task.Dispatchertime = dt_Task.CreateDate;
Db.Ado.BeginTran();
BaseDal.AddData(dt_Task);
task = BaseDal.QueryFirst(x => x.PalletCode == lineDTO.Barcode && x.TaskType == TaskTypeEnum.Inbound.ObjToInt());
//content.Data = dt_Task;
Db.Ado.CommitTran();
if (task != null) return content.OK(data: task);
#endregion
}
catch (Exception ex)
{
Db.Ado.RollbackTran();
content.Error(ex.Message);
}
return content;
}
}
}