using Autofac.Core;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
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.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, string palletCode)
{
try
{
Dt_Task task = Repository.QueryFirst(x => x.PalletCode == palletCode);
if (task != null)
{
PushTasksToWCS(new List { task });
return WebResponseContent.Instance.OK($"该托盘已生成任务", _mapper.Map(task));
}
if (Repository.QueryFirst(x => x.SourceAddress == stationCode && x.TaskStatus == TaskStatusEnum.New.ObjToInt()) != null)
{
return WebResponseContent.Instance.Error($"该站点已有未执行的任务");
}
Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.QueryFirst(x => x.PalletCode == palletCode);
if (stockInfo == null)
{
return WebResponseContent.Instance.Error($"未找到组盘信息");
}
if (stockInfo.StockStatus != StockStatusEmun.MES余料退库.ObjToInt() && stockInfo.StockStatus != StockStatusEmun.MES空托退库.ObjToInt())
{
return WebResponseContent.Instance.Error($"该托盘状态不正确,不可申请入库");
}
if (!string.IsNullOrEmpty(stockInfo.LocationCode))
{
return WebResponseContent.Instance.Error($"该托盘已绑定货位");
}
Dt_Task newTask = new Dt_Task()
{
CurrentAddress = stationCode,
Grade = 0,
NextAddress = "",
PalletCode = palletCode,
Roadway = "",
SourceAddress = stationCode,
TargetAddress = "",
TaskType = TaskTypeEnum.Inbound.ObjToInt(),
TaskStatus = TaskStatusEnum.New.ObjToInt(),
WarehouseId = stockInfo.WarehouseId,
PalletType = stockInfo.PalletType,
};
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();
WMSTaskDTO wMSTaskDTO = _mapper.Map(newTask);
PushTasksToWCS(new List { newTask });
if (newTask.WarehouseId == 5) PutFinish(stationCode, newTask.PalletCode, newTask.TaskNum.ToString());
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
};
//更新状态
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() && stockInfoOld.IsPick==WhetherEnum.False.ObjToInt() && stationCode.IsNotEmptyOrNull())
{
stockInfoOld.StockStatus = StockStatusEmun.老厂退库.ObjToInt();
stockInfoOld.IsFull = WhetherEnum.True.ObjToInt();
stockInfoOld.StockOutLength = 0;
stockInfoOld.WarehouseId = WarehouseEnum.LLDYL.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())
{
stockInfoOld.StockStatus = StockStatusEmun.老厂退库.ObjToInt();
stockInfoOld.IsFull = WhetherEnum.True.ObjToInt();
stockInfoOld.IsPick = WhetherEnum.False.ObjToInt();
stockInfoOld.StockOutLength = 0;
stockInfoOld.WarehouseId = WarehouseEnum.LLDYL.ObjToInt();
UpdateStock(stockInfoOld, weight, thickness, wide);
return content.OK($"老厂领料退库{stockInfoOld.RfidCode}");
}
else if (stockInfoOld != null)
{
throw new Exception($"{palletCode}RFID信息已存在");
}
if (stationCode.IsNullOrEmpty())
{
#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 void UpdateStock(Dt_StockInfo stockInfo, decimal weight = 0, decimal thickness = 0, decimal wide = 0)
{
stockInfo.CheckWeight = weight;
stockInfo.CheckThickness = thickness;
stockInfo.CheckWide = wide;
_stockRepository.StockInfoRepository.UpdateData(stockInfo);
}
///
/// 原料采购绑定RFID
///
/// 纸卷条码
/// 纸卷RIFD
///
public WebResponseContent PurchaseBoxing(string palletCode)
{
WebResponseContent content=new WebResponseContent();
try
{
if (string.IsNullOrEmpty(palletCode))
{
throw new Exception("条码不能为空");
}
Dt_StockInfo stockInfoOld = _stockRepository.StockInfoRepository.QueryFirst(x => x.PalletCode == palletCode);
if (stockInfoOld != null)
{
throw new Exception("条码信息库存中已存在");
}
//获取采购信息
Dt_PurchaseBSTOrderDetail purchaseBSTOrderDetail = _inboundRepository.PurchaseBSTOrderDetailRepository.QueryFirst(x => x.Barcode == palletCode);
if (purchaseBSTOrderDetail == null)
{
BSTResponse bSTResponse = _invokeERPService.BSTStockAsync(palletCode).DeserializeObject>();
if (bSTResponse.Code == 500)
{
throw new Exception($"未找到条码{palletCode}采购信息并一期ERP库存也不存在");
}
BSTStockInfoDTO bSTStockInfoDTO=bSTResponse.Data ?? throw new Exception($"一期ERP未返回{palletCode}的库存信息");
Dt_MaterielInfo materielInfo = _basicRepository.MaterielInfoRepository.QueryFirst(x => x.MaterialSourceId == bSTStockInfoDTO.MaterialId) ?? throw new Exception($"未找到条码{palletCode}物料信息{bSTStockInfoDTO.MaterialNo}");
//生成库存组盘信息
Dt_StockInfo stockInfo = new Dt_StockInfo()
{
MaterielInvOrgId = materielInfo.MaterielInvOrgId,
PalletCode = palletCode,
RfidCode = "",
LocationCode = "",
PalletType = 1,
WarehouseId = materielInfo.WarehouseId,
StockAttribute = materielInfo.MaterielSourceType,
StockStatus = StockStatusEmun.博思通组盘暂存.ObjToInt(),
MaterielSpec = materielInfo.MaterielSpec,
Unit = materielInfo.MaterielUnit,
MaterielThickness = bSTStockInfoDTO.Thick,
MaterielWide = bSTStockInfoDTO.W,
MaterielWeight = bSTStockInfoDTO.Qty,
MaterielCode = materielInfo.MaterielCode,
MaterielName = materielInfo.MaterielName,
StockLength = bSTStockInfoDTO.StockMeter,
MaterielId = materielInfo.MaterialSourceId
};
if (bSTStockInfoDTO.W > 1200)
{
stockInfo.PalletType = 2;
}
Dt_YLInboundCache? yLInboundCache = _inboundRepository.YLInboundCacheRepository.QueryData(x => x.InvOrg == MaterielInvOrgEnum.老厂.ToString()).FirstOrDefault() ?? throw new Exception("未找到原料条码缓存信息");
int Id = yLInboundCache.Id;
if (yLInboundCache.BarCode == palletCode)
{
throw new Exception($"条码{palletCode}已扫码");
}
else if (yLInboundCache.BarCode != palletCode && yLInboundCache.BindStatus == WhetherEnum.True.ObjToInt())
{
yLInboundCache = _mapper.Map(stockInfo);
yLInboundCache.Id = Id;
_unitOfWorkManage.BeginTran();
//新增组盘信息
_stockRepository.StockInfoRepository.AddData(stockInfo);
_inboundRepository.YLInboundCacheRepository.UpdateData(yLInboundCache);
//启动线体
WebResponseContent webResponse = YLPurchasePush();
if (!webResponse.Status)
{
throw new Exception(webResponse.Message);
}
_unitOfWorkManage.CommitTran();
return content.OK("成功", yLInboundCache);
}
else
{
throw new Exception($"上卷条码{yLInboundCache.BarCode}还未进行绑定");
}
}
else
{
if (purchaseBSTOrderDetail.PurchaseBSTOrderDetailStatus != InOrderStatusEnum.未开始.ObjToInt())
{
throw new Exception($"条码{palletCode}采购信息已入库或入库中");
}
Dt_YLInboundCache? yLInboundCache = _inboundRepository.YLInboundCacheRepository.QueryData(x => x.InvOrg == MaterielInvOrgEnum.老厂.ToString()).FirstOrDefault();
if (yLInboundCache == null)
{
throw new Exception("未找到原料条码缓存信息");
}
int Id = yLInboundCache.Id;
if (yLInboundCache.BarCode == palletCode)
{
throw new Exception($"条码{palletCode}已扫码");
}
else if (yLInboundCache.BarCode != palletCode && yLInboundCache.BindStatus == WhetherEnum.True.ObjToInt())
{
yLInboundCache = _mapper.Map(purchaseBSTOrderDetail);
yLInboundCache.Id = Id;
//获取采购主单
Dt_PurchaseBSTOrder purchaseBSTOrder = _inboundRepository.PurchaseBSTOrderRepository.QueryFirst(x => x.Id == purchaseBSTOrderDetail.PurchaseBSTOrderId);
//获取物料
Dt_MaterielInfo materielInfo = _basicRepository.MaterielInfoRepository.QueryFirst(x => x.MaterialSourceId == purchaseBSTOrderDetail.MaterialId);
//生成库存组盘信息
Dt_StockInfo stockInfo = new Dt_StockInfo()
{
MaterielInvOrgId = materielInfo.MaterielInvOrgId,
PalletCode = purchaseBSTOrderDetail.Barcode,
RfidCode = "",
LocationCode = "",
PalletType = 1,
WarehouseId = materielInfo.WarehouseId,
StockAttribute = materielInfo.MaterielSourceType,
StockStatus = StockStatusEmun.组盘暂存.ObjToInt(),
MaterielSpec = materielInfo.MaterielSpec,
Unit = materielInfo.MaterielUnit,
MaterielThickness = purchaseBSTOrderDetail.MaterialThick,
MaterielWide = purchaseBSTOrderDetail.MaterialWide,
MaterielWeight = purchaseBSTOrderDetail.DeliveryQty,
MaterielCode = materielInfo.MaterielCode,
MaterielName = materielInfo.MaterielName,
StockLength = purchaseBSTOrderDetail.ProcurementLength,
MaterielId = purchaseBSTOrderDetail.MaterialId
};
if (purchaseBSTOrderDetail.MaterialWide > 1200)
{
stockInfo.PalletType = 2;
}
purchaseBSTOrderDetail.PurchaseBSTOrderDetailStatus = InOrderStatusEnum.入库中.ObjToInt();
_unitOfWorkManage.BeginTran();
_inboundRepository.YLInboundCacheRepository.UpdateData(yLInboundCache);
//新增组盘信息
_stockRepository.StockInfoRepository.AddData(stockInfo);
if (purchaseBSTOrder.PurchaseOrderStatus == InOrderStatusEnum.未开始.ObjToInt())
{
purchaseBSTOrder.PurchaseOrderStatus = InOrderStatusEnum.入库中.ObjToInt();
_inboundRepository.PurchaseBSTOrderRepository.UpdateData(purchaseBSTOrder);
}
_inboundRepository.PurchaseBSTOrderDetailRepository.UpdateData(purchaseBSTOrderDetail);
//启动线体
WebResponseContent webResponse = YLPurchasePush();
if (!webResponse.Status)
{
throw new Exception(webResponse.Message);
}
_unitOfWorkManage.CommitTran();
return content.OK("成功", yLInboundCache);
}
else
{
throw new Exception($"上卷条码{yLInboundCache.BarCode}还未进行绑定");
}
}
}
catch (Exception ex)
{
_unitOfWorkManage.RollbackTran();
content.Error(ex.Message);
}
return content;
}
///
/// 推送至WCS原料线体启动
///
///
public WebResponseContent YLPurchasePush()
{
try
{
string url = AppSettings.Get("WCS");
if (string.IsNullOrEmpty(url))
{
throw new Exception($"未找到WCSAApi地址,请检查配置文件");
}
string response = HttpHelper.Post($"{url}/api/Task/YLPurchasePush?code=406");
return JsonConvert.DeserializeObject(response) ?? WebResponseContent.Instance.Error("返回错误");
}
catch (Exception ex)
{
return WebResponseContent.Instance.Error(ex.Message);
}
}
///
/// 原料分配巷道
///
/// 条码号
///
///
public string AssignYLRoadwayNo(string palletCode)
{
try
{
Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.QueryFirst(x => x.PalletCode == palletCode);
if (stockInfo==null)
{
throw new Exception($"组盘库存不存在");
}
Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseId == stockInfo.WarehouseId);
if (warehouse == null)
{
throw new Exception($"未找到巷道对应仓库信息");
}
string roadwayNo = "";
if (stockInfo.MaterielInvOrgId==MaterielInvOrgEnum.新厂.ObjToInt())
{
//限制直径
if (stockInfo.MaterielThickness >= 300 && stockInfo.MaterielThickness <= 1300 && stockInfo.MaterielWide >= 700 && stockInfo.MaterielWide <= 2700)
{
//获取分配
List locationCounts = Db.Queryable().Where(x => x.WarehouseId == warehouse.WarehouseId && x.LocationStatus == LocationStatusEnum.Free.ObjToInt() && (x.EnableStatus == EnableStatusEnum.OnlyIn.ObjToInt() || x.EnableStatus == EnableStatusEnum.Normal.ObjToInt()) && x.RoadwayNo.Contains("YLDual") && (stockInfo.PalletType == LocationTypeEnum.MediumPallet.ObjToInt() ? x.LocationType == LocationTypeEnum.MediumPallet.ObjToInt(): x.LocationType == LocationTypeEnum.SmallPallet.ObjToInt())).GroupBy(x => x.RoadwayNo).Select(x => new LocationCount { RoadwayNo = x.RoadwayNo, Count = SqlFunc.AggregateCount(x) }).ToList();
roadwayNo = HandleRoadway(locationCounts, warehouse);
}
}
else
{
//限制
if (stockInfo.MaterielThickness >= 800 && stockInfo.MaterielThickness <= 1500 && stockInfo.MaterielWide >= 700 && stockInfo.MaterielWide <= 2500)
{
bool LayerLimit = false;
if (stockInfo.MaterielThickness>1300)
{
LayerLimit = true;
}
//获取分配
List locationCounts = Db.Queryable().Where(x => x.WarehouseId == warehouse.WarehouseId && x.LocationStatus == LocationStatusEnum.Free.ObjToInt() && (x.EnableStatus == EnableStatusEnum.OnlyIn.ObjToInt() || x.EnableStatus == EnableStatusEnum.Normal.ObjToInt()) && x.RoadwayNo.Contains("YL") && !x.RoadwayNo.Contains("YLDual") && (LayerLimit ? x.Columns>=56 : x.Columns<=55)).GroupBy(x => x.RoadwayNo).Select(x => new LocationCount { RoadwayNo = x.RoadwayNo, Count = SqlFunc.AggregateCount(x) }).ToList();
if (stockInfo.MaterielWide > 2200)
{
locationCounts = Db.Queryable().Where(x => x.WarehouseId == warehouse.WarehouseId && x.LocationStatus == LocationStatusEnum.Free.ObjToInt() && (x.EnableStatus == EnableStatusEnum.OnlyIn.ObjToInt() || x.EnableStatus == EnableStatusEnum.Normal.ObjToInt()) && x.RoadwayNo == "SC02_YL" && (LayerLimit ? x.Columns >= 56 : x.Columns <= 55)).GroupBy(x => x.RoadwayNo).Select(x => new LocationCount { RoadwayNo = x.RoadwayNo, Count = SqlFunc.AggregateCount(x) }).ToList();
}
roadwayNo = HandleRoadway(locationCounts, warehouse);
}
}
return !string.IsNullOrEmpty(roadwayNo) ? (roadwayNo) : throw new Exception("未找到可分配巷道");
}
catch (Exception ex)
{
_unitOfWorkManage.RollbackTran();
throw new Exception(ex.Message);
}
}
//处理分配巷道 任务数量
public string HandleRoadway(List locationCounts, Dt_Warehouse warehouse)
{
//巷道任务分配数量
List useLocationCounts = Db.Queryable().Where(x => x.WarehouseId == warehouse.WarehouseId
&& locationCounts.Select(j => j.RoadwayNo).Distinct().Contains(x.Roadway)
&& TaskInboundTypes.Contains(x.TaskType)).GroupBy(x => x.Roadway).Select(x => new LocationCount { RoadwayNo = x.Roadway, Count = SqlFunc.AggregateCount(x) }).ToList();
foreach (var item in locationCounts)
{
LocationCount? count = useLocationCounts.FirstOrDefault(x => x.RoadwayNo == item.RoadwayNo);
if (count != null)
{
item.Count -= count.Count;
}
}
return locationCounts.Where(x => x.Count > 0).OrderByDescending(x => x.Count).FirstOrDefault()?.RoadwayNo ?? "";
}
///
/// 入库完成
///
public WebResponseContent InboundTaskCompleted(Dt_Task task)
{
WebResponseContent content = new WebResponseContent();
try
{
Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseId == task.WarehouseId);
if (warehouse.WarehouseCode == WarehouseEnum.LLDCP.ToString() || warehouse.WarehouseCode == WarehouseEnum.LLDFL.ToString()) //成品/辅料完成
{
Dt_LocationInfo locationInfoEnd = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress);
if (locationInfoEnd == null)
{
return content.Error($"未找到对应的终点货位信息");
}
Dt_AGVStationInfo agvstation = _basicRepository.AGVStationInfoRepository.QueryFirst(x => x.AGVStationCode == task.SourceAddress && ( x.StationArea == nameof(AGVStationAreaEnum.一楼月台码头) || x.StationArea == nameof(AGVStationAreaEnum.一楼无纺织布) || x.StationArea == nameof(AGVStationAreaEnum.一楼无纺淋膜) || x.StationArea == nameof(AGVStationAreaEnum.一楼无纺淋膜)));
if (agvstation != null)
{
agvstation.IsOccupied = WhetherEnum.False.ObjToInt();
}
Dt_ProStockInfo proStockInfo = _stockRepository.ProStockInfoRepository.Db.Queryable().Where(x => x.PalletCode == task.PalletCode).Includes(x=>x.proStockInfoDetails).First();
if (proStockInfo != null && proStockInfo.StockStatus == StockStatusEmun.MES空托退库.ObjToInt())
{
task.TaskStatus = TaskStatusEnum.Finish.ObjToInt();
_unitOfWorkManage.BeginTran();
if (agvstation != null)
{
_basicRepository.AGVStationInfoRepository.UpdateData(agvstation);
}
proStockInfo.LocationCode = locationInfoEnd.LocationCode;
proStockInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
_stockRepository.ProStockInfoRepository.UpdateData(proStockInfo);
_basicService.LocationInfoService.UpdateLocationStatus(locationInfoEnd, proStockInfo.PalletType, LocationStatusEnum.InStock, proStockInfo.WarehouseId);
BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId > 0 ? WIDESEA_Core.Enums.OperateTypeEnum.人工完成 : WIDESEA_Core.Enums.OperateTypeEnum.自动完成);
_unitOfWorkManage.CommitTran();
}
else if (proStockInfo != null && proStockInfo.StockStatus == StockStatusEmun.手动组盘入库确认.ObjToInt())
{
task.TaskStatus = TaskStatusEnum.Finish.ObjToInt();
proStockInfo.proStockInfoDetails.ForEach(x =>
{
x.ProStockDetailStatus = StockStatusEmun.入库完成.ObjToInt();
});
_unitOfWorkManage.BeginTran();
if (agvstation != null)
{
_basicRepository.AGVStationInfoRepository.UpdateData(agvstation);
}
proStockInfo.LocationCode = locationInfoEnd.LocationCode;
proStockInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
_stockRepository.ProStockInfoRepository.UpdateData(proStockInfo);
_stockRepository.ProStockInfoDetailRepository.UpdateData(proStockInfo.proStockInfoDetails);
_basicService.LocationInfoService.UpdateLocationStatus(locationInfoEnd, proStockInfo.PalletType, LocationStatusEnum.InStock, proStockInfo.WarehouseId);
BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId > 0 ? WIDESEA_Core.Enums.OperateTypeEnum.人工完成 : WIDESEA_Core.Enums.OperateTypeEnum.自动完成);
_unitOfWorkManage.CommitTran();
}
else
{
return content.Error($"未找到对应库存信息");
}
}
else//原料库完成
{
//获取库存
Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.QueryFirst(x => x.PalletCode == task.PalletCode);
if (stockInfo == null)
{
return content.Error($"未找到对应库存信息");
}
//获取货位信息
Dt_LocationInfo locationInfoEnd = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress);
if (locationInfoEnd == null)
{
return content.Error($"未找到对应的终点货位信息");
}
//更新状态
task.TaskStatus = TaskStatusEnum.Finish.ObjToInt();
_unitOfWorkManage.BeginTran();
if (task.TaskType == TaskTypeEnum.InPick.ObjToInt())
{
Dt_LocationInfo locationInfoStart = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.SourceAddress);
_basicService.LocationInfoService.UpdateLocationStatus(locationInfoStart, stockInfo.PalletType, LocationStatusEnum.Free, stockInfo.WarehouseId);
}
stockInfo.LocationCode = locationInfoEnd.LocationCode;
stockInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
if (task.TaskType!=TaskTypeEnum.Inbound.ObjToInt())
{
stockInfo.IsFull = WhetherEnum.True.ObjToInt();
}
_stockService.StockInfoService.Repository.UpdateData(stockInfo);
_basicService.LocationInfoService.UpdateLocationStatus(locationInfoEnd, stockInfo.PalletType, LocationStatusEnum.InStock, stockInfo.WarehouseId);
BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId > 0 ? WIDESEA_Core.Enums.OperateTypeEnum.人工完成 : WIDESEA_Core.Enums.OperateTypeEnum.自动完成);
//上报老厂ERP
if (stockInfo.MaterielInvOrgId == MaterielInvOrgEnum.老厂.ObjToInt() && task.TaskType==TaskTypeEnum.Inbound.ObjToInt())
{
int Qty = Convert.ToInt32(stockInfo.MaterielWeight);
BSTPurchaseUpModel bSTPurchaseUpModel = new BSTPurchaseUpModel()
{
Barcode = stockInfo.PalletCode,
BarcodeQty = Qty,
Rfid = stockInfo.RfidCode,
RfidUpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
};
BSTResponse