using Autofac.Core;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using OfficeOpenXml.FormulaParsing.Excel.Operators;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using WIDESEA_Common.CommonEnum;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Common.MaterielEnum;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Common.WareHouseEnum;
using WIDESEA_Core;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_DTO;
using WIDESEA_DTO.Basic;
using WIDESEA_DTO.ERP;
using WIDESEA_DTO.MES;
using WIDESEA_DTO.Task;
using WIDESEA_External.Model;
using WIDESEA_Model.Models;
using WIDESEA_TaskInfoRepository;
namespace WIDESEA_TaskInfoService
{
public partial class TaskService
{
///
/// 仅申请任务,让WCS根据路由确定下一地址
///
///
///
///
public WebResponseContent DeviceRequestInboundTaskSimple(string stationCode)
{
try
{
Dt_Task task = Repository.QueryFirst(x => x.PalletCode == stationCode);
if (task != null)
{
PushTasksToWCS(new List { task });
return WebResponseContent.Instance.OK($"该托盘已生成任务", _mapper.Map(task));
}
Dt_ProStockInfo stockInfo = BaseDal.Db.Queryable().Where(x => x.PalletCode == stationCode).Includes(x => x.proStockInfoDetails).First(); ;
if (stockInfo == null)
{
return WebResponseContent.Instance.Error($"未找到组盘信息");
}
if (stockInfo.StockStatus != StockStatusEmun.组盘暂存.ObjToInt())
{
return WebResponseContent.Instance.Error($"该托盘状态不正确,不可申请入库");
}
Dt_Task newTask = new Dt_Task()
{
CurrentAddress = stationCode,
Grade = 0,
NextAddress = "",
PalletCode = stationCode,
Roadway = "",
SourceAddress = stationCode,
TargetAddress = "",
TaskType = TaskTypeEnum.InProduct.ObjToInt(),
TaskStatus = TaskStatusEnum.New.ObjToInt(),
WarehouseId = stockInfo.WarehouseId,
PalletType = stockInfo.PalletType,
};
stockInfo.StockStatus = StockStatusEmun.入库确认.ObjToInt();
stockInfo.proStockInfoDetails.ForEach(x =>
{
x.ProStockDetailStatus = StockStatusEmun.入库确认.ObjToInt();
});
_unitOfWorkManage.BeginTran();
int taskId = BaseDal.AddData(newTask);
newTask.TaskId = taskId;
_stockRepository.ProStockInfoRepository.UpdateData(stockInfo);
_stockRepository.ProStockInfoDetailRepository.UpdateData(stockInfo.proStockInfoDetails);
_unitOfWorkManage.CommitTran();
WMSTaskDTO wMSTaskDTO = _mapper.Map(newTask);
return WebResponseContent.Instance.OK(data: wMSTaskDTO);
}
catch (Exception ex)
{
_unitOfWorkManage.RollbackTran();
return WebResponseContent.Instance.Error(ex.Message);
}
}
///
/// 原料申请入库
///
/// 起始站点
/// 托盘
///
public WebResponseContent RequestYLWMSTaskSimple(string stationCode, string palletCode)
{
WebResponseContent content = new WebResponseContent();
try
{
Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.QueryFirst(x => x.RfidCode == palletCode);
if (stockInfo == null)
{
return content.Error($"未找到组盘信息");
}
Dt_Task task = Repository.QueryFirst(x => x.PalletCode == stockInfo.PalletCode);
if (task != null)
{
PushTasksToWCS(new List { task });
return content.OK($"该托盘已生成任务", _mapper.Map(task));
}
if (Repository.QueryFirst(x => x.SourceAddress == stationCode && x.TaskStatus == TaskStatusEnum.New.ObjToInt()) != null)
{
return content.Error($"该站点已有未执行的任务");
}
if (stockInfo.StockStatus != StockStatusEmun.手动组盘暂存.ObjToInt() && stockInfo.StockStatus != StockStatusEmun.组盘暂存.ObjToInt() && stockInfo.StockStatus != StockStatusEmun.老厂退库.ObjToInt() && stockInfo.StockStatus != StockStatusEmun.博思通组盘暂存.ObjToInt())
{
return content.Error($"该托盘状态不正确,不可申请入库");
}
//分配巷道
string rowWay = AssignYLRoadwayNo(stockInfo.PalletCode);
if (string.IsNullOrEmpty(rowWay))
{
return content.Error($"未找到可分配巷道");
}
//生成任务
Dt_Task newTask = new Dt_Task()
{
CurrentAddress = stationCode,
Grade = 0,
NextAddress = "",
PalletCode = stockInfo.PalletCode,
RfidCode=stockInfo.RfidCode,
Roadway = rowWay,
SourceAddress = stationCode,
TargetAddress = "",
TaskType = stationCode=="307" ? TaskTypeEnum.PaperOldYLBackInbound.ObjToInt() : TaskTypeEnum.Inbound.ObjToInt(),
TaskStatus = TaskStatusEnum.New.ObjToInt(),
WarehouseId = stockInfo.WarehouseId,
PalletType = stockInfo.PalletType,
TaskLength = (int)stockInfo.MaterielWide,
MaterielCode=stockInfo.MaterielCode,
Quantity=stockInfo.StockLength
};
//更新状态
if (stockInfo.StockStatus == StockStatusEmun.手动组盘暂存.ObjToInt())
{
stockInfo.StockStatus = StockStatusEmun.手动组盘入库确认.ObjToInt();
}
else
{
stockInfo.StockStatus = StockStatusEmun.入库确认.ObjToInt();
}
//数据更新
_unitOfWorkManage.BeginTran();
int taskId = BaseDal.AddData(newTask);
newTask.TaskId = taskId;
_stockRepository.StockInfoRepository.UpdateData(stockInfo);
_unitOfWorkManage.CommitTran();
PushTasksToWCS(new List { newTask });
WMSTaskDTO wMSTaskDTO = _mapper.Map(newTask);
return content.OK(data: wMSTaskDTO);
}
catch (Exception ex)
{
_unitOfWorkManage.RollbackTran();
content.Error(ex.Message);
}
return content;
}
private readonly static object lockerYLBoxing = new object();
///
/// 原料请求
///
/// RFID信息
///
public WebResponseContent YLPurchaseBoxing(string palletCode, decimal weight = 0, decimal thickness = 0, decimal wide = 0, string stationCode = "")
{
WebResponseContent content = new WebResponseContent();
try
{
lock (lockerYLBoxing)
{
Dt_StockInfo stockInfoOld = _stockRepository.StockInfoRepository.QueryFirst(x => x.RfidCode == palletCode);
if (stockInfoOld != null && stockInfoOld.StockStatus == StockStatusEmun.手动组盘暂存.ObjToInt())
{
UpdateStock(stockInfoOld, weight, thickness, wide);
return content.OK($"临时入库{stockInfoOld.RfidCode}");
}
else if (stockInfoOld != null && stockInfoOld.MaterielInvOrgId == MaterielInvOrgEnum.老厂.ObjToInt() && stockInfoOld.StockStatus == StockStatusEmun.出库完成.ObjToInt() && stationCode.IsNotEmptyOrNull())
{
decimal oldQty = stockInfoOld.StockLength;
//调用更新退料库存及上报重量
WebResponseContent returnContent = ReturnStockUp(stockInfoOld, thickness, weight);
if (!returnContent.Status)
{
throw new Exception(returnContent.Message);
}
stockInfoOld = returnContent.Data as Dt_StockInfo;
//更新数据
UpdateStock(stockInfoOld, weight, thickness, wide);
//记录库存变动
_recordService.StockQuantityChangeRecordService.AddStockChangeRecord(stockInfoOld, oldQty, oldQty < stockInfoOld.StockLength ? stockInfoOld.StockLength - oldQty : oldQty - stockInfoOld.StockLength, StockChangeTypeEnum.MaterielGroup);
return content.OK($"老厂未领料退库RFID{stockInfoOld.RfidCode}条码{stockInfoOld.PalletCode}");
}
else if (stockInfoOld != null && stockInfoOld.MaterielInvOrgId == MaterielInvOrgEnum.老厂.ObjToInt() && stockInfoOld.StockStatus == StockStatusEmun.老厂退料暂存.ObjToInt() && stationCode.IsNotEmptyOrNull())
{
decimal oldQty = stockInfoOld.StockLength;
//调用更新退料库存及上报重量
WebResponseContent returnContent = ReturnStockUp(stockInfoOld, thickness, weight);
if (!returnContent.Status)
{
throw new Exception(returnContent.Message);
}
stockInfoOld = returnContent.Data as Dt_StockInfo;
//更新数据
UpdateStock(stockInfoOld, weight, thickness, wide);
//记录库存变动
_recordService.StockQuantityChangeRecordService.AddStockChangeRecord(stockInfoOld, oldQty, oldQty < stockInfoOld.StockLength ? stockInfoOld.StockLength- oldQty: oldQty- stockInfoOld.StockLength, StockChangeTypeEnum.MaterielGroup);
return content.OK($"老厂领料退库RFID{stockInfoOld.RfidCode}条码{stockInfoOld.PalletCode}");
}
else if (stockInfoOld != null && stockInfoOld.MaterielInvOrgId == MaterielInvOrgEnum.老厂.ObjToInt() && stockInfoOld.StockStatus == StockStatusEmun.出库完成.ObjToInt() && stockInfoOld.StockLength<=0 && stationCode.IsNullOrEmpty())
{
_stockRepository.StockInfoRepository.DeleteAndMoveIntoHty(stockInfoOld, App.User.UserId > 0 ? OperateTypeEnum.人工完成 : OperateTypeEnum.自动完成);
throw new Exception($"{palletCode}RFID信息老厂领料后无库存退料");
}
else if (stockInfoOld != null)
{
throw new Exception($"{palletCode}RFID信息已存在");
}
if (stationCode.IsNullOrEmpty())
{
//新厂RFID绑定逻辑
if (palletCode.StartsWith("A"))
{
#region 处理采购绑定RFID逻辑
Dt_YLInboundCache? yLInboundCache = _inboundRepository.YLInboundCacheRepository.QueryData(x => x.InvOrg == MaterielInvOrgEnum.新厂.ToString()).FirstOrDefault();
if (yLInboundCache == null)
{
return content.Error("未找到原料缓存条码信息");
}
if (yLInboundCache.BindStatus == WhetherEnum.True.ObjToInt())
{
return content.Error($"原料缓存条码{yLInboundCache.BarCode}已绑定RFID{yLInboundCache.RfidCode}");
}
else
{
Dt_StockInfo ExistStockInfo = _stockRepository.StockInfoRepository.QueryFirst(x => x.PalletCode == yLInboundCache.BarCode);
if (ExistStockInfo != null && ExistStockInfo.MaterielInvOrgId == MaterielInvOrgEnum.新厂.ObjToInt() && ExistStockInfo.StockStatus == StockStatusEmun.组盘暂存.ObjToInt())
{
//获取采购信息
yLInboundCache.RfidCode = palletCode;
yLInboundCache.BindStatus = WhetherEnum.True.ObjToInt();
ExistStockInfo.RfidCode = palletCode;
_unitOfWorkManage.BeginTran();
UpdateStock(ExistStockInfo, weight, thickness, wide);
_inboundRepository.YLInboundCacheRepository.UpdateData(yLInboundCache);
_unitOfWorkManage.CommitTran();
return content.OK($"采购入库RFID{ExistStockInfo.RfidCode}绑定{ExistStockInfo.PalletCode}");
}
else
{
throw new Exception("未知错误");
}
}
#endregion
}
else //老厂RFID绑定逻辑
{
#region 处理采购绑定RFID逻辑
Dt_YLInboundCache? yLInboundCache = _inboundRepository.YLInboundCacheRepository.QueryData(x => x.InvOrg == MaterielInvOrgEnum.老厂.ToString()).FirstOrDefault();
if (yLInboundCache == null)
{
return content.Error("未找到原料缓存条码信息");
}
if (yLInboundCache.BindStatus == WhetherEnum.True.ObjToInt())
{
return content.Error($"原料缓存条码{yLInboundCache.BarCode}已绑定RFID{yLInboundCache.RfidCode}");
}
else
{
Dt_StockInfo ExistStockInfo = _stockRepository.StockInfoRepository.QueryFirst(x => x.PalletCode == yLInboundCache.BarCode);
if (ExistStockInfo != null && ExistStockInfo.MaterielInvOrgId == MaterielInvOrgEnum.老厂.ObjToInt() && ExistStockInfo.StockStatus == StockStatusEmun.博思通组盘暂存.ObjToInt())
{
yLInboundCache.RfidCode = palletCode;
yLInboundCache.BindStatus = WhetherEnum.True.ObjToInt();
ExistStockInfo.RfidCode = palletCode;
_unitOfWorkManage.BeginTran();
//更新组盘信息
UpdateStock(ExistStockInfo, weight, thickness, wide);
_inboundRepository.YLInboundCacheRepository.UpdateData(yLInboundCache);
_unitOfWorkManage.CommitTran();
return content.OK($"博思通库存转存{ExistStockInfo.RfidCode}");
}
else if (ExistStockInfo != null && ExistStockInfo.MaterielInvOrgId == MaterielInvOrgEnum.老厂.ObjToInt() && ExistStockInfo.StockStatus == StockStatusEmun.组盘暂存.ObjToInt())
{
//获取采购信息
Dt_PurchaseBSTOrderDetail purchaseBSTOrderDetail = _inboundRepository.PurchaseBSTOrderDetailRepository.QueryFirst(x => x.Barcode == yLInboundCache.BarCode);
yLInboundCache.RfidCode = palletCode;
yLInboundCache.BindStatus = WhetherEnum.True.ObjToInt();
ExistStockInfo.RfidCode = palletCode;
purchaseBSTOrderDetail.RfidCode = palletCode;
_unitOfWorkManage.BeginTran();
UpdateStock(ExistStockInfo, weight, thickness, wide);
_inboundRepository.YLInboundCacheRepository.UpdateData(yLInboundCache);
_inboundRepository.PurchaseBSTOrderDetailRepository.UpdateData(purchaseBSTOrderDetail);
_unitOfWorkManage.CommitTran();
return content.OK($"采购入库{ExistStockInfo.RfidCode}");
}
else
{
throw new Exception("未知错误");
}
}
#endregion
}
}
else if (!stationCode.IsNullOrEmpty() && stockInfoOld == null)
{
throw new Exception($"{palletCode}RFID信息不存在");
}
else
{
throw new Exception("未知错误");
}
}
}
catch (Exception ex)
{
_unitOfWorkManage.RollbackTran();
content.Error(ex.Message);
}
return content;
}
///
/// 更新退料库存
///
///
public WebResponseContent ReturnStockUp(Dt_StockInfo stockInfoOld,decimal thickness,decimal weight)
{
WebResponseContent content = new WebResponseContent();
try
{
if (thickness < 400)
{
throw new Exception($"RFID{stockInfoOld.RfidCode}条码{stockInfoOld.PalletCode}直径小于400mm");
}
BSTResponse bSTResponse = _invokeERPService.BSTStockAsync(stockInfoOld.PalletCode).DeserializeObject>();
if (bSTResponse.Code == 500)
{
throw new Exception($"未找到条码{stockInfoOld.PalletCode}一期ERP库存不存在");
}
BSTStockInfoDTO bSTStockInfoDTO = bSTResponse.Data ?? throw new Exception($"一期ERP未返回{stockInfoOld.PalletCode}的库存信息");
stockInfoOld.IsPick = WhetherEnum.False.ObjToInt();
decimal stockLength = bSTStockInfoDTO.StockMeter;
decimal errWeight = Math.Abs(weight - bSTStockInfoDTO.Qty);
if (weight != bSTStockInfoDTO.Qty && weight < stockInfoOld.InitialWeight && errWeight <= AppSettings.Get("ErrWeight").ObjToInt())
{
Dt_MaterielInfo materielInfo = _basicRepository.MaterielInfoRepository.QueryFirst(x => x.MaterialSourceId == stockInfoOld.MaterielId);
int gramWeight = (int)(materielInfo.MaterielWeight * 1000);
BSTWeightUpDTO bSTWeightUpDTO = new BSTWeightUpDTO()
{
Paper_code = stockInfoOld.PalletCode,
Estimate_weight = bSTStockInfoDTO.Qty,
Actual_weight = weight,
Error_weight = errWeight,
Weigh_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
Operator = "LiKu",
Width = bSTStockInfoDTO.W,
Gram_weight = gramWeight
};
BSTResponse