using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MailKit.Search;
using WIDESEA_Common;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Core;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_Model.Models;
namespace WIDESEA_TaskInfoService
{
public partial class TaskService
{
///
/// 成品出库
///
///
///
public WebResponseContent WMSGenerateNewOutboundTask(int[] keys)
{
try
{
List tasks = new List();
List locationInfos = new List();
List outStockLockInfos = new List();
List outboundOrderDetails = _outboundService.NewOutboundOrderDetailService.Repository.QueryData(x => keys.Contains(x.Id));
if (outboundOrderDetails == null || outboundOrderDetails.Count == 0)
{
throw new Exception("未找到出库单明细信息");
}
if (outboundOrderDetails.FirstOrDefault(x => x.OrderDetailStatus > OrderDetailStatusEnum.New.ObjToInt() && x.OrderDetailStatus != OrderDetailStatusEnum.AssignOverPartial.ObjToInt()) != null)
{
throw new Exception("所选出库单明细存在出库中或已完成");
}
var lpngroup = outboundOrderDetails.GroupBy(x => x.LPNNo);
List newOutboundOrderDetails = new List();
List stockInfos = new List();
foreach (var lpngroups in lpngroup)
{
string lpn = lpngroups.Key;
List detailsForThisLPN = _outboundService.NewOutboundOrderDetailService.Repository.QueryData(x => x.LPNNo == lpn && x.OrderDetailStatus == OrderDetailStatusEnum.New.ObjToInt());
if (detailsForThisLPN.Count == 0) continue;
newOutboundOrderDetails.AddRange(detailsForThisLPN);
Dt_StockInfo? stockInfo = _stockService.StockInfoService.Repository.QueryFirst(x => x.PalletCode == lpn);
if (stockInfo == null)
{
throw new Exception($"未找到库存信息,托盘号{lpn}");
}
stockInfos.Add(stockInfo);
}
if (stockInfos == null || stockInfos.Count == 0)
{
throw new Exception("未找到库存信息");
}
List orderId = newOutboundOrderDetails.Select(x => x.OrderId).Distinct().ToList();
List outboundOrders = _outboundService.NewOutboundOrderService.Repository.QueryData(x => orderId.Contains(x.Id));
_unitOfWorkManage.BeginTran();
var Newtasks = GetTasks(stockInfos, TaskTypeEnum.OutProduct);
List stockInfoDetails = _stockService.StockInfoDetailService.Repository.QueryData(x => stockInfos.Select(x => x.Id).Contains(x.StockId));
List palletcodelpn = new List();
foreach (var item in newOutboundOrderDetails)
{
Dt_StockInfo? stockInfo = stockInfos.FirstOrDefault(x => x.PalletCode == item.LPNNo);
if (stockInfo == null) continue;
Dt_StockInfoDetail? stockInfoDetail = stockInfoDetails.FirstOrDefault(x => x.StockId == stockInfo.Id && x.BatchNo == item.BatchNo);
if (stockInfoDetail == null) continue;
stockInfoDetail.OutboundQuantity += item.OrderQuantity;
item.LockQuantity += item.OrderQuantity;
item.OrderDetailStatus = OrderDetailStatusEnum.Outbound.ObjToInt();
var outboundOrder = outboundOrders.FirstOrDefault(x => x.Id == item.OrderId);
Dt_OutStockLockInfo outStockLockInfo = _outboundService.OutboundStockLockInfoService.GetOutStockLockInfo(outboundOrder, item, stockInfo, stockInfoDetail, item.OrderQuantity);
outStockLockInfo.Status = OutStockStatus.出库中.ObjToInt();
item.OrderDetailStatus = OrderDetailStatusEnum.Outbound.ObjToInt();
outboundOrder.OrderStatus = OutboundStatusEnum.出库中.ObjToInt();
stockInfo.StockStatus = StockStatusEmun.出库锁定.ObjToInt();
var task = Newtasks.FirstOrDefault(x => x.PalletCode == item.LPNNo);
if (task != null && !palletcodelpn.Contains(item.LPNNo))
{
if (outboundOrder.OrderType == 1)
{
task.TaskType = TaskTypeEnum.OutProduct.ObjToInt();
}
else if (outboundOrder.OrderType == 2)
{
task.TaskType = TaskTypeEnum.OutAllocate.ObjToInt();
}
task.OrderNo = outboundOrder.OrderNo;
tasks.Add(task);
palletcodelpn.Add(task.PalletCode);
}
outStockLockInfo.TaskNum = task.TaskNum;
outStockLockInfos.Add(outStockLockInfo);
}
locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(stockInfos.Select(x => x.LocationCode).ToList()));
BaseDal.AddData(tasks);
_stockService.StockInfoService.Repository.UpdateData(stockInfos);
_stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetails);
_outboundService.NewOutboundOrderDetailService.Repository.UpdateData(newOutboundOrderDetails);
_outboundService.NewOutboundOrderService.Repository.UpdateData(outboundOrders);
_outboundService.OutboundStockLockInfoService.Repository.AddData(outStockLockInfos);
_recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfos, LocationStatusEnum.Lock.ObjToInt(), StockChangeType.Outbound.ObjToInt(), "", tasks?.Select(x => x.TaskNum).ToList());
_basicService.LocationInfoService.Repository.UpdateLocationStatus(locationInfos, LocationStatusEnum.Lock);
_unitOfWorkManage.CommitTran();
//将任务推送到WCS
return PushTasksWCS(tasks);
}
catch (Exception ex)
{
_unitOfWorkManage.RollbackTran();
return WebResponseContent.Instance.Error(ex.Message);
}
}
///
/// 合托出库
///
///
///
public WebResponseContent WMSGenerateSTOutboundTask(int[] keys)
{
try
{
List tasks = new List();
List locationInfos = new List();
List outStockLockInfos = new List();
List stockInfos = _stockService.StockInfoService.Repository.QueryData(x => keys.Contains(x.Id) && x.StockStatus == StockStatusEmun.入库完成.ObjToInt());
if (stockInfos == null || stockInfos.Count == 0)
{
throw new Exception("未找到库存信息,或库存状态不为入库完成");
}
_unitOfWorkManage.BeginTran();
var Newtasks = GetTasks(stockInfos, TaskTypeEnum.OutSyncretism);
List stockInfoDetails = _stockService.StockInfoDetailService.Repository.QueryData(x => stockInfos.Select(x => x.Id).Contains(x.StockId));
foreach (var item in stockInfos)
{
Dt_StockInfo? stockInfo = stockInfos.FirstOrDefault(x => x.PalletCode == item.PalletCode);
if (stockInfo == null) continue;
stockInfo.StockStatus = StockStatusEmun.出库锁定.ObjToInt();
var task = Newtasks.FirstOrDefault(x => x.PalletCode == item.PalletCode);
tasks.Add(task);
}
locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(stockInfos.Select(x => x.LocationCode).ToList()));
BaseDal.AddData(tasks);
_stockService.StockInfoService.Repository.UpdateData(stockInfos);
_stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetails);
_recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfos, LocationStatusEnum.Lock.ObjToInt(), StockChangeType.Outbound.ObjToInt(), "", tasks?.Select(x => x.TaskNum).ToList());
_basicService.LocationInfoService.Repository.UpdateLocationStatus(locationInfos, LocationStatusEnum.Lock);
_unitOfWorkManage.CommitTran();
//将任务推送到WCS
return PushTasksWCS(tasks);
}
catch (Exception ex)
{
_unitOfWorkManage.RollbackTran();
return WebResponseContent.Instance.Error(ex.Message);
}
}
///
/// SMOM盘点出库
///
///
///
public WebResponseContent SMOMGenerateSTOutboundTask(int[] keys)
{
try
{
List tasks = new List();
List locationInfos = new List();
List outStockLockInfos = new List();
List takeStockOrderCPs = _takeStockOrderCPRepository.QueryData(x => keys.Contains(x.Id));
if (takeStockOrderCPs == null || takeStockOrderCPs.Count == 0)
{
throw new Exception("未找到库存信息,或库存状态不为入库完成");
}
if (takeStockOrderCPs.FirstOrDefault(x => x.TakeStockStatus > TakeStockDetailStatusEnum.未盘点.ObjToInt()) != null)
{
throw new Exception("所选出库单明细存在出库中或已完成");
}
List stockInfos = new List();
_unitOfWorkManage.BeginTran();
foreach (var takeStock in takeStockOrderCPs)
{
takeStock.TakeStockStatus = TakeStockStatusEnum.盘点中.ObjToInt();
List takeStockOrderDetailCPs = _takeStockOrderDetailCPRepository.QueryData(x => x.TakeStockId == takeStock.Id);
foreach (var item in takeStockOrderDetailCPs)
{
item.TakeDetalStatus = TakeStockDetailStatusEnum.盘点出库中.ObjToInt();
_takeStockOrderDetailCPRepository.UpdateData(item);
}
var lpngroup = takeStockOrderDetailCPs.GroupBy(x => x.TakePalletCode);
foreach (var lpngroups in lpngroup)
{
string lpn = lpngroups.Key;
Dt_StockInfo? stockInfo = _stockService.StockInfoService.Repository.QueryFirst(x => x.PalletCode == lpn);
if (stockInfo == null)
{
throw new Exception($"未找到库存信息,托盘号{lpn}");
}
stockInfos.Add(stockInfo);
}
if (stockInfos == null || stockInfos.Count == 0)
{
throw new Exception("未找到库存信息");
}
}
var Newtasks = GetTasks(stockInfos, TaskTypeEnum.OutInventory);
List stockInfoDetails = _stockService.StockInfoDetailService.Repository.QueryData(x => stockInfos.Select(x => x.Id).Contains(x.StockId));
foreach (var item in stockInfos)
{
Dt_StockInfo? stockInfo = stockInfos.FirstOrDefault(x => x.PalletCode == item.PalletCode);
if (stockInfo == null) continue;
stockInfo.StockStatus = StockStatusEmun.出库锁定.ObjToInt();
var task = Newtasks.FirstOrDefault(x => x.PalletCode == item.PalletCode);
tasks.Add(task);
}
locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(stockInfos.Select(x => x.LocationCode).ToList()));
BaseDal.AddData(tasks);
_stockService.StockInfoService.Repository.UpdateData(stockInfos);
_stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetails);
_takeStockOrderCPRepository.UpdateData(takeStockOrderCPs);
_recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfos, LocationStatusEnum.Lock.ObjToInt(), StockChangeType.Outbound.ObjToInt(), "", tasks?.Select(x => x.TaskNum).ToList());
_basicService.LocationInfoService.Repository.UpdateLocationStatus(locationInfos, LocationStatusEnum.Lock);
_unitOfWorkManage.CommitTran();
//将任务推送到WCS
return PushTasksWCS(tasks);
}
catch (Exception ex)
{
_unitOfWorkManage.RollbackTran();
return WebResponseContent.Instance.Error(ex.Message);
}
}
///
/// SMOM盘点后库存调整
///
///
///
public WebResponseContent SMOMAdjustment(HouseAdjustment houseAdjustment)
{
WebResponseContent responseContent = new WebResponseContent();
try
{
if (houseAdjustment.DetailList.Count == 0)
{
return WebResponseContent.Instance.Error("无明细信息");
}
List stockdetail = new List();
foreach (var item in houseAdjustment.DetailList)
{
var dt_StockInfo = BaseDal.Db.Queryable().LeftJoin((stockdetail, stock) => stockdetail.StockId == stock.Id)
.Where((stockdetail, stock) => stockdetail.MaterielCode == item.MaterielCode && stockdetail.BatchNo == item.BatchNo && stock.PalletCode == item.LPNNo).Select((stockdetail, stock) => stockdetail).First();
if (dt_StockInfo != null)
{
dt_StockInfo.StockQuantity = item.OrderQuantity;
stockdetail.Add(dt_StockInfo);
}
}
_unitOfWorkManage.BeginTran();
_stockInfoDetailRepository.UpdateData(stockdetail);
_unitOfWorkManage.CommitTran();
}
catch (Exception ex)
{
_unitOfWorkManage.RollbackTran();
return WebResponseContent.Instance.Error($"Error: {ex.Message}");
}
return WebResponseContent.Instance.OK();
}
}
}