using Khd.Core.Domain.Dto.wcs; using Khd.Core.Domain.Models; using Khd.Core.EntityFramework; using Khd.Core.Library.Mapper; using Khd.Core.Wcs.Global; using Masuit.Tools; using Masuit.Tools.Logging; using Microsoft.CodeAnalysis; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using SixLabors.ImageSharp; using System.Data; using System.Security.Cryptography; using System.Threading.Tasks.Dataflow; using Z.EntityFramework.Plus; namespace Khd.Core.Wcs.Wcs { /// /// 根据出入库记录创建出入库任务 /// public class CreateTaskByRecord { private readonly IHost _host; List ScanPoint { get; set; }//点位信息 private BaseSitenode? sitenode { get; set; }//站台信息 private NodeSetting? NodeSettingCarNo { get; set; } private NodeSetting? NodeSettingCarState { get; set; } private NodeSetting? NodeSettingCarRun { get; set; } private NodeSetting? NodeSettingWcsState { get; set; } private NodeSetting? NodeSettingWcsSend { get; set; } private NodeSetting? NodeSettingWcsSendMaterial { get; set; } private NodeSetting? NodeSettingPLCSendSendMaterialstate { get; set; } public CreateTaskByRecord(IHost host, string siteNo) { this._host = host; this.ScanPoint = StaticData.BasePlcpointList.ToList();//加载当前站点所对应的点位 this.sitenode = StaticData.SiteNodeList.FirstOrDefault(t => t.siteNo == siteNo);//获取当前站台信息 //this.NodeSettingCarNo = this.ScanPoint.FirstOrDefault(t => t.plcpointNo.Contains("carno")); //this.NodeSettingCarState = this.ScanPoint.FirstOrDefault(t => t.plcpointNo.Contains("carstate")); //this.NodeSettingCarRun = this.ScanPoint.FirstOrDefault(t => t.plcpointNo.Contains("carrun")); //this.NodeSettingWcsState = this.ScanPoint.FirstOrDefault(t => t.plcpointNo.Contains("plcsendK")); //this.NodeSettingWcsSend = this.ScanPoint.FirstOrDefault(t => t.plcpointNo.Contains("wcsend")); //this.NodeSettingWcsSendMaterial = this.ScanPoint.FirstOrDefault(t => t.plcpointNo.Contains("wcssendmessage")); //this.NodeSettingPLCSendSendMaterialstate = this.ScanPoint.FirstOrDefault(t => t.plcpointNo.Contains("plcsendmessage")); try { //默认启动,清理plc的上位机写入点位值 //this._plc.Write(NodeSettingCarRun.plcpointAddress, MainCentralControl.QingKongDianWei); //this._plc.Write(NodeSettingWcsSend.plcpointAddress, MainCentralControl.QingKongDianWei); //this._plc.Write(NodeSettingWcsSendMaterial.plcpointAddress, MainCentralControl.QingKongDianWei); } catch (Exception ex) { Console.WriteLine("站点" + siteNo + " 初始化数据异常" + ex.Message); LogManager.Error(ex); } } /// /// 启动上件扫描监听 /// public void StartPoint() { var createBearAgvReturnThread = new Thread(CreateBearAgvReturnLogic); createBearAgvReturnThread.Start(); //var createRawInWareTaskThread = new Thread(CreateRawInWareTaskLogic); //createRawInWareTaskThread.Start(); var createFiveProductInTaskThread = new Thread(CreateFiveProductInTaskLogic); createFiveProductInTaskThread.Start(); var createFiveProductTaskThread = new Thread(CreateFiveProductTaskLogic); createFiveProductTaskThread.Start(); var createRawTaskThread = new Thread(CreateRawTaskLogic); createRawTaskThread.Start(); //var createThirdOutTaskThread = new Thread(CreateThirdOutTaskLogic); //createThirdOutTaskThread.Start(); Console.WriteLine(DateTime.Now + ":出库任务监听启动成功"); LogManager.Info("出库任务监听启动成功"); } /// /// 半成品入库 /// /// private void CreateFiveProductInTaskLogic(object? obj) { using var scope = _host.Services.CreateScope(); using var dbContext = scope.ServiceProvider.GetRequiredService(); BaseEquip agvEquip = StaticData.BaseEquip.First(t => t.objid == 28); BaseEquip endEquip = StaticData.BaseEquip.First(t => t.objid == 30); while (true) { try { dbContext.ChangeTracker.Entries().ToList().ForEach(e => e.Reload()); var wmsProductInstock = dbContext.WmsProductInstock .Where(t => t.productType == "2") .Where(t => t.auditStatus == "1") .Where(t => t.warehouseId == 521) .FirstOrDefault(); if (wmsProductInstock != null && wmsProductInstock.executeStatus == "0") { var wmsBaseLocation = dbContext.WmsBaseLocation .Where(t => t.warehouseId == 521) .Where(t => t.activeFlag == "1") .Where(t => t.instockFlag == "0") .Where(t => t.outstockFlag == "0") .Where(t => t.locationStatus == "1") .Where(t => t.containerCode == null || t.containerCode == "").FirstOrDefault(); if (wmsBaseLocation != null) { var wcsTask = new WcsTask() { objid = StaticData.SnowId.NextId(), serialNo = SystemData.GetSerialNo(dbContext), taskType = 34, startPointId = wmsBaseLocation.locationId, startPointNo = wmsBaseLocation.locationCode, endPointId = endEquip.objid, endPointNo = endEquip.equipNo, currPointId = wmsBaseLocation.locationId, currPointNo = wmsBaseLocation.locationCode, equipmentNo = agvEquip.equipNo, useFlag = 1, qty = 1, containerNo = wmsProductInstock.palletInfoCode, taskStatus = 0, createBy = "WCS", createTime = DateTime.Now, floorNo = 5, fromFloorNo = 5, isEmpty = "0", masterId = wmsProductInstock.productId, orderId = wmsProductInstock.productInstockId, materialId = wmsProductInstock.productId, nextPointId = agvEquip.objid, nextPointNo = agvEquip.equipNo, }; WmsProductInstockDetail wmsProductInstockDetail = new WmsProductInstockDetail() { erpAmount = 0, productId = wmsProductInstock.productId, executeStatus = "1", productInstockDetailId = StaticData.SnowId.NextId(), instockAmount = 1, instockBy = "WCS", instockDate = DateTime.Now, instockWay = "2", locationCode = wmsBaseLocation.locationCode, planAmount = 1, productBarcode = wmsProductInstock.productBatch, productBatch = wmsProductInstock.productBatch, productInstockId = wmsProductInstock.productInstockId, }; WcsTaskLog wcsTaskLog = CoreMapper.Map(wcsTask); wmsProductInstock.executeStatus = "1"; wmsBaseLocation.instockFlag = "1"; wmsBaseLocation.locationStatus = "4"; dbContext.Add(wmsProductInstock); dbContext.Update(wmsProductInstock); dbContext.Update(wmsBaseLocation); dbContext.Add(wcsTask); dbContext.Add(wcsTaskLog); dbContext.SaveChanges(); } } } catch (Exception ex) { LogManager.Error(ex); } Thread.Sleep(1000); } } /// /// 创建去翻转机的任务 /// /// /// private void CreateThirdOutTaskLogic(object? obj) { BaseEquip endEquip = StaticData.BaseEquip.First(t => t.objid == 31); BaseEquip baseEquip = StaticData.BaseEquip.First(t => t.objid == 9); using var scope = _host.Services.CreateScope(); using var dbContext = scope.ServiceProvider.GetRequiredService(); while (true) { try { dbContext.ChangeTracker.Entries().ToList().ForEach(e => e.Reload()); WcsTask? wcsTask = dbContext.WcsTask.FirstOrDefault(t => t.endPointId == endEquip.objid); if (wcsTask == null) { var wmsRawOutstock = dbContext.WmsRawOutstock .Where(t => t.endStationCode == endEquip.equipNo) .Where(t => t.executeStatus == "0") .OrderBy(t => t.auditStatus == "1") .FirstOrDefault(); if (wmsRawOutstock != null) { var wmsRawStocks = dbContext.WmsRawStock .Where(t => t.materialId == wmsRawOutstock.materialId) .Where(t => t.instockBatch == wmsRawOutstock.materialBatch) .Where(t => t.warehouseId == 311) .ToList(); var wmsBaseLocations = dbContext.WmsBaseLocation .Where(t => t.activeFlag == "1") .Where(t => t.delFlag == "0") .Where(t => t.locationScrapType == "1") .Where(t => t.instockFlag == "0") .Where(t => t.outstockFlag == "0") .Where(t => t.ContainerStatus == "1") .Where(t => t.warehouseId == 311).ToList(); var bill = from a in wmsBaseLocations from b in wmsRawStocks where a.locationCode == b.locationCode select new { a, b }; WmsBaseLocation? wmsBaseLocation = null; bill = bill.OrderBy(t => t.b.instockDate); var fistbill = bill.FirstOrDefault(); if (fistbill != null) { int? locRow = 0; if (fistbill.a.locRow % 2 == 1) { locRow = fistbill.a.locRow + 1; } else { locRow = fistbill.a.locRow - 1; } var lastbill = bill.Where(t => t.a.locRow == locRow) .Where(t => t.a.locColumn == fistbill.a.locColumn) .Where(t => t.a.locDeep == 2) .Where(t => t.b.materialId == wmsRawOutstock.materialId) .Where(t => t.b.instockBatch == wmsRawOutstock.materialBatch) .FirstOrDefault(); if (lastbill != null) { if (lastbill.b.instockDate != null && fistbill.b.instockDate != null) { wmsBaseLocation = fistbill.a; if (lastbill.b.instockDate.Value.Date == fistbill.b.instockDate.Value.Date) { wmsBaseLocation = lastbill.a; } else//移库 { var formWmsBaseLocation = lastbill.a; //先找深库位 List list = wmsRawStocks.Select(t => t.locationCode).ToList(); var deepLocation = wmsBaseLocations .Where(t => t.locDeep == 1) .Where(t => !list.Contains(t.locationCode)) .ToList(); if (deepLocation.Count > 0) { var toWmsBaseLocation = deepLocation.First(); var EKWcsTask = new WcsTask() { objid = StaticData.SnowId.NextId(), orderId = wmsRawOutstock.orderId, taskType = 49, containerNo = wmsBaseLocation.containerCode, createBy = "WCS", createTime = DateTime.Now, taskStatus = 0, materialId = wmsRawOutstock.materialId, currPointId = formWmsBaseLocation.locationId, currPointNo = formWmsBaseLocation.locationCode, startPointId = formWmsBaseLocation.locationId, startPointNo = formWmsBaseLocation.locationCode, nextPointId = baseEquip.objid, nextPointNo = baseEquip.equipNo, endPointId = toWmsBaseLocation.locationId, endPointNo = toWmsBaseLocation.locationCode, equipmentNo = baseEquip.equipNo, useFlag = 1, qty = 1 }; formWmsBaseLocation.outstockFlag = "1"; formWmsBaseLocation.locationStatus = "4"; toWmsBaseLocation.instockFlag = "1"; toWmsBaseLocation.locationStatus = "4"; dbContext.Add(EKWcsTask); dbContext.SaveChanges(); Thread.Sleep(2000); } } if (wmsBaseLocation != null) { wcsTask = new WcsTask() { objid = StaticData.SnowId.NextId(), orderId = wmsRawOutstock.orderId, taskType = 41, containerNo = wmsBaseLocation.containerCode, createBy = "WCS", createTime = DateTime.Now, taskStatus = 0, materialId = wmsRawOutstock.materialId, currPointId = wmsBaseLocation.locationId, currPointNo = wmsBaseLocation.locationCode, startPointId = wmsBaseLocation.locationId, startPointNo = wmsBaseLocation.locationCode, nextPointId = baseEquip.objid, nextPointNo = baseEquip.equipNo, endPointId = endEquip.objid, endPointNo = endEquip.equipNo, equipmentNo = endEquip.equipNo, useFlag = 1, qty = 1 }; WcsTaskLog wcsTaskLog = CoreMapper.Map(wcsTask); dbContext.Add(wcsTask); dbContext.Add(wcsTaskLog); dbContext.SaveChanges(); } } } } } } } catch (Exception ex) { LogManager.Error(ex); } } } /// /// 创建返库任务 /// /// private void CreateBearAgvReturnLogic() { using var scope = _host.Services.CreateScope(); using var dbContext = scope.ServiceProvider.GetRequiredService(); BaseEquip endEquip = StaticData.BaseEquip.First(t => t.objid == 21); BaseEquip baseEquip = StaticData.BaseEquip.First(t => t.objid == 10); while (true) { try { dbContext.ChangeTracker.Entries().ToList().ForEach(e => e.Reload()); var wmsRawReturn = dbContext.WmsRawReturn .Where(t => t.auditStatus == "1") .FirstOrDefault(); if (wmsRawReturn != null) { if (wmsRawReturn.executeStatus == "0") { //任务未开始Status=0 BaseEquip startEquip = StaticData.BaseEquip.First(t => t.equipNo == wmsRawReturn.endStationCode); WcsTask wcsTask = new() { objid = StaticData.SnowId.NextId(), orderId = wmsRawReturn.rawReturnId, taskType = 48, containerNo = "", startPointId = startEquip.objid, startPointNo = startEquip.equipNo, currPointId = startEquip.objid, currPointNo = startEquip.equipNo, nextPointId = baseEquip.objid, nextPointNo = baseEquip.equipNo, endPointId = endEquip.objid, endPointNo = endEquip.equipNo, serialNo = SystemData.GetSerialNo(dbContext), taskStatus = 0, createTime = DateTime.Now, createBy = "WCS", materialId = wmsRawReturn.materialId, qty = 1, useFlag = 1, equipmentNo = startEquip.equipNo, remark = "装配区返库", floorNo = 5, masterId = wmsRawReturn.materialId, fromFloorNo = 5, //materialNo = wmsRawReturn.materialBatch, }; wmsRawReturn.executeStatus = "1"; dbContext.Add(wcsTask); WcsTaskLog wcsTaskLog = CoreMapper.Map(wcsTask); dbContext.Add(wcsTaskLog); dbContext.Update(wmsRawReturn); dbContext.SaveChanges(); } } } catch (Exception ex) { LogManager.Error(ex); } Thread.Sleep(1000); } } /// /// 背板安装区到半成品入库 /// private void CreateRawInWareTaskLogic() { using var scope = _host.Services.CreateScope(); using var dbContext = scope.ServiceProvider.GetRequiredService(); while (true) { try { dbContext.ChangeTracker.Entries().ToList().ForEach(e => e.Reload()); } catch (Exception ex) { Console.WriteLine(ex.Message + ex.StackTrace); LogManager.Error(ex); } Thread.Sleep(5000); } } /// /// 成品出库 /// private void CreateFiveProductTaskLogic() { using var scope = _host.Services.CreateScope(); using var dbContext = scope.ServiceProvider.GetRequiredService(); while (true) { try { dbContext.ChangeTracker.Entries().ToList().ForEach(e => e.Reload()); var proStock = dbContext.WmsProductStock .Where(t => t.warehouseId == 531) .Where(t => t.activeFlag == "0").ToList(); var proOutStock = dbContext.WmsProductOutstock .Where(t => t.outstockQty < t.applyQty) .Where(t => t.auditStatus == "1") .Where(t => t.warehouseId == 531) .Where(t => t.productType == "3") .Where(t => t.executeStatus == "0" || t.executeStatus == "1").ToList(); //获取最早入库时间 foreach (var item in proOutStock) { BaseEquip endEquip = dbContext.BaseEquip.First(t => t.floorNo == 5 && t.equipType == 1); var wmsproStocks = proStock .Where(t => t.productId == item.productId && t.warehouseId == item.warehouseId) .Select(t => t.palletInfoCode) .ToList(); var wmsBaseLocations = dbContext.WmsBaseLocation .Where(t => t.activeFlag == "1") .Where(t => t.delFlag == "0") .Where(t => t.locationScrapType == "1") .Where(t => t.instockFlag == "0") .Where(t => t.outstockFlag == "0") .Where(t => t.warehouseId == item.warehouseId) .Where(t => wmsproStocks.Contains(t.containerCode)) .ToList(); decimal needNumber = Convert.ToDecimal(item.applyQty - item.outstockQty); if (needNumber <= 0) { item.executeStatus = "2"; dbContext.Update(item); dbContext.SaveChanges(); continue; } var bill = from a in wmsBaseLocations from b in proStock where a.containerCode == b.palletInfoCode select new { a, b }; //如果第一列满足需求,则按第一列排序,否则按最后一列排序 if (bill.Where(t => t.a.locColumn == 1).Select(t => t.b.totalAmount - t.b.occupyAmount - t.b.frozenAmount).Sum() > needNumber) { bill = bill.OrderBy(t => t.a.locColumn).ToList(); } else { bill = bill.OrderByDescending(t => t.a.locColumn).ToList(); } //做数量限制 BaseEquip ctuEquip = StaticData.BaseEquip.First(t => t.objid == 28); BaseEquip lineEquip = StaticData.BaseEquip.First(t => t.objid == 5); foreach (var b in bill) { item.executeStatus = "1"; WmsBaseLocation location = b.a; WmsProductStock stock = b.b; WcsTask wcsTask; int qty = 0; if (stock.totalAmount - stock.occupyAmount - stock.frozenAmount <= needNumber)//该料箱全部出 { item.outstockQty += stock.totalAmount - stock.occupyAmount - stock.frozenAmount; qty = Convert.ToInt32(stock.totalAmount - stock.frozenAmount); stock.occupyAmount += qty; stock.updateDate = DateTime.Now; } else { item.outstockQty += needNumber; stock.occupyAmount += needNumber; qty = Convert.ToInt32(needNumber); stock.updateDate = DateTime.Now; } MesBasePalletInfo? mesBasePalletInfo = dbContext.MesBasePalletInfo.FirstOrDefault(t => t.palletInfoCode == location.containerCode); if (mesBasePalletInfo != null) { WmsProductOutstockDetail detail = new() { productId = item.productId, productOutstockId = item.productOutstockId, locationCode = location.locationCode, executeStatus = "1", beginTime = DateTime.Now, warehouseId = item.warehouseId, erpAmount = 0, confirmAmount = 0, outstockAmount = 1, planAmount = 1, productBatch = item.productBatch, productOutstockDetailId = StaticData.SnowId.NextId(), productBarcode = mesBasePalletInfo.materialBarcode }; wcsTask = new WcsTask() { objid = StaticData.SnowId.NextId(), orderId = item.productOutstockId, taskType = 38, containerNo = location.containerCode, createBy = "WCS", createTime = DateTime.Now, taskStatus = 0, materialId = item.productId, currPointId = location.locationId, currPointNo = location.locationCode, startPointId = location.locationId, startPointNo = location.locationCode, nextPointId = ctuEquip.objid, nextPointNo = ctuEquip.equipNo, endPointId = lineEquip.objid, endPointNo = lineEquip.equipNo, equipmentNo = ctuEquip.equipNo, useFlag = 1, qty = qty }; location.outstockFlag = "1"; location.locationStatus = "6"; location.updateBy = "WCS"; location.updateTime = DateTime.Now; dbContext.Add(detail); dbContext.Update(location); dbContext.Add(wcsTask); dbContext.Update(stock); dbContext.SaveChanges(); if (qty >= needNumber) { break; } } } if (item.beginTime == null) { item.beginTime = DateTime.Now; } dbContext.Update(item); dbContext.SaveChanges(); } } catch (Exception ex) { Console.WriteLine(ex.Message + ex.StackTrace); LogManager.Error(ex); } } } /// /// 原材料出库 /// private void CreateRawTaskLogic() { using var scope = _host.Services.CreateScope(); using var dbContext = scope.ServiceProvider.GetRequiredService(); while (true) { try { dbContext.ChangeTracker.Entries().ToList().ForEach(e => e.Reload()); //原材料库存 var rawStock = dbContext.WmsRawStock.Where(t => t.activeFlag == "1").ToList(); //原材料出库记录 var rawOutStock = dbContext.WmsRawOutstock .Where(t => t.executeStatus == "0" || t.executeStatus == "1") .Where(t => t.outstockAmount > t.realOutstockAmount) .Where(t => t.auditStatus == "1") .ToList(); List wcsTaskManuals = dbContext.WcsTaskManual.Where(t=>t.nextPointId== 11).OrderBy(t => t.createBy).ToList(); if (wcsTaskManuals.Count > 0) { WcsOutstockLock wcsOutstockLock = dbContext.WcsOutstockLock.Where(t => t.warehouseId == 512).First(); int taskCount = dbContext.WcsTask.Where(t => t.nextPointId == 11).Count(); if (taskCount == 0 && wcsOutstockLock.qty == 0 && wcsOutstockLock.boxStatus == 0) { foreach (var wcs in wcsTaskManuals.Take(6)) { WcsTask wcsTask = CoreMapper.Map(wcs); WcsTaskLog wcsTaskLog = CoreMapper.Map(wcsTask); wcsOutstockLock.qty += 1; wcsOutstockLock.boxStatus = 1; dbContext.Update(wcsOutstockLock); dbContext.Add(wcsTask); dbContext.Add(wcsTaskLog); dbContext.WcsTaskManual.Where(t => t.objid == wcs.objid).Delete(); } dbContext.SaveChanges(); } } foreach (var item in rawOutStock) { decimal? RealOutNumber = item.realOutstockAmount; var endEquip = StaticData.BaseEquip.FirstOrDefault(t => t.equipNo == item.endStationCode); if (endEquip != null && endEquip.floorNo == 5) { lock (SystemData.outStockLock) { if (endEquip.equipType == 8)//CTU出库 { var wcsOutstockLock = dbContext.WcsOutstockLock.Where(t => t.warehouseId == 512).First(); if (wcsOutstockLock.qty == 0 && wcsOutstockLock.boxStatus == 0) { var wmsRawStocks = rawStock .Where(t => t.materialId == item.materialId) .Where(t => t.warehouseId == item.warehouseId) .Select(t => t.locationCode) .ToList(); var wmsBaseLocations = dbContext.WmsBaseLocation .Where(t => t.activeFlag == "1") .Where(t => t.delFlag == "0") .Where(t => t.locationScrapType == "1") .Where(t => t.instockFlag == "0") .Where(t => t.outstockFlag == "0") .Where(t => t.ContainerStatus == "1") .Where(t => t.warehouseId == item.warehouseId) .Where(t => wmsRawStocks.Contains(t.locationCode)) .ToList(); decimal? needNumber = item.outstockAmount - RealOutNumber; var bill = from a in wmsBaseLocations from b in rawStock where a.locationCode == b.locationCode select new { a, b }; //如果第一列满足需求,则按第一列排序,否则按最后一列排序 if (bill.Where(t => t.a.locColumn == 1).Select(t => t.b.totalAmount - t.b.occupyAmount - t.b.frozenAmount).Sum() > needNumber) { bill = bill.OrderBy(t => t.a.locColumn).ToList(); } else { bill = bill.OrderByDescending(t => t.a.locColumn).ToList(); } //做数量限制 //bill = bill.Take(5 - dbContext.WcsCmd.Where(t => t.cmdStatus < 3 && t.nextPointId == 20).Count()); bill = bill.Take(6); BaseEquip ctuEquip = StaticData.BaseEquip.First(t => t.objid == 11); BaseEquip lineEquip = StaticData.BaseEquip.First(t => t.objid == 20); foreach (var b in bill) { needNumber = item.outstockAmount - RealOutNumber; item.executeStatus = "1"; WmsBaseLocation location = b.a; WmsRawStock stock = b.b; WcsTask wcsTask; int qty = 0; int outNumber = 0; if (stock.totalAmount - stock.occupyAmount - stock.frozenAmount <= needNumber)//该料箱全部出 { RealOutNumber += stock.totalAmount - stock.occupyAmount - stock.frozenAmount; outNumber = Convert.ToInt32(stock.totalAmount - stock.occupyAmount - stock.frozenAmount); qty = Convert.ToInt32(stock.totalAmount - stock.frozenAmount); stock.updateDate = DateTime.Now; } else { RealOutNumber += needNumber; outNumber = Convert.ToInt32(needNumber); qty = Convert.ToInt32(needNumber); stock.updateDate = DateTime.Now; } wcsTask = new WcsTask() { objid = StaticData.SnowId.NextId(), orderId = item.rawOutstockId, taskType = 38, containerNo = location.containerCode, createBy = "WCS", createTime = DateTime.Now, taskStatus = -1, materialId = item.materialId, currPointId = location.locationId, currPointNo = location.locationCode, startPointId = location.locationId, startPointNo = location.locationCode, nextPointId = ctuEquip.objid, nextPointNo = ctuEquip.equipNo, endPointId = lineEquip.objid, endPointNo = lineEquip.equipNo, equipmentNo = ctuEquip.equipNo, ud1 = location.locColumn, useFlag = 1, qty = outNumber }; MesBaseBarcodeInfo? mesBaseBarcodeInfo = dbContext.MesBaseBarcodeInfo.Where(t => t.barcodeInfo == stock.instockBatch).FirstOrDefault(); if (mesBaseBarcodeInfo != null) { WmsRawOutstockDetail wmsProductOutstockDetail = new WmsRawOutstockDetail() { rawOutstockDetailId = StaticData.SnowId.NextId(), rawOutstockId = item.rawOutstockId, materialId = item.materialId, createDate = DateTime.Now, createBy = "WCS", taskCode = wcsTask.objid.ToString(), executeStatus = "1", locationCode = location.locationCode, outstockAmount = qty, planAmount = item.outstockAmount, warehouseId = item.warehouseId, materialBarcode = mesBaseBarcodeInfo.barcodeInfo, instockBatch = mesBaseBarcodeInfo.batchCode, stackAmount = qty, outstockPerson = "WCS", outstockTime = DateTime.Now, outstockWay = "2", materialProductionDate = mesBaseBarcodeInfo.productionDate }; dbContext.Add(wmsProductOutstockDetail); location.outstockFlag = "1"; location.locationStatus = "6"; location.updateBy = "WCS"; location.updateTime = DateTime.Now; wcsOutstockLock.boxStatus = 1; dbContext.Update(location); dbContext.Add(wcsTask); dbContext.Update(stock); dbContext.SaveChanges(); if (qty >= needNumber) { break; } } } if (item.beginTime == null) { item.beginTime = DateTime.Now; } dbContext.Update(wcsOutstockLock); dbContext.Update(item); dbContext.SaveChanges(); } }//CTU出库 if (endEquip.equipType == 10)//CTU出库到装配区 { bool CreateSuccess = false; var wcsOutstockLock = dbContext.WcsOutstockLock .Where(t => t.warehouseId == 512) .First(); if (wcsOutstockLock.qty == 0 && wcsOutstockLock.boxStatus == 0) { var orderList = rawOutStock.Where(t => t.endStationCode == item.endStationCode).ToList(); foreach (var order in orderList) { decimal? RealOutNummber = order.realOutstockAmount; WcsTask? task = dbContext.WcsTask.FirstOrDefault(t => t.taskType == 32); if (task == null) { var wmsRawStocks = rawStock .Where(t => t.materialId == order.materialId && t.warehouseId == order.warehouseId) .Select(t => t.palletInfoCode) .ToList(); var wmsBaseLocations = dbContext.WmsBaseLocation .Where(t => t.activeFlag == "1") .Where(t => t.delFlag == "0") .Where(t => t.locationScrapType == "1") .Where(t => t.instockFlag == "0") .Where(t => t.outstockFlag == "0") .Where(t => t.warehouseId == order.warehouseId) .Where(t => wmsRawStocks.Contains(t.containerCode)) .ToList(); decimal? needNumber = order.outstockAmount - RealOutNummber; var bill = from a in wmsBaseLocations from b in rawStock where a.containerCode == b.palletInfoCode select new { a, b }; //如果第一列满足需求,则按第一列排序,否则按最后一列排序 if (bill.Where(t => t.a.locColumn == 1).Select(t => t.b.totalAmount - t.b.occupyAmount - t.b.frozenAmount).Sum() > needNumber) { bill = bill.OrderBy(t => t.a.locColumn).ToList(); } else { bill = bill.OrderByDescending(t => t.a.locColumn).ToList(); } //做数量限制 bill = bill.Take(5 - dbContext.WcsCmd.Where(t => t.cmdStatus < 3 && t.nextPointId == 20).Count()); BaseEquip ctuEquip = StaticData.BaseEquip.First(t => t.objid == 11); BaseEquip lineEquip = StaticData.BaseEquip.First(t => t.objid == 20); foreach (var b in bill) { item.executeStatus = "1"; WmsBaseLocation location = b.a; WmsRawStock stock = b.b; WcsTask wcsTask; int qty = 0; if (stock.totalAmount - stock.occupyAmount - stock.frozenAmount <= needNumber)//该料箱全部出 { RealOutNummber += stock.totalAmount - stock.occupyAmount - stock.frozenAmount; qty = Convert.ToInt32(stock.totalAmount - stock.frozenAmount); stock.occupyAmount += qty; stock.updateDate = DateTime.Now; } else { RealOutNummber += needNumber; stock.occupyAmount += needNumber; qty = Convert.ToInt32(needNumber); stock.updateDate = DateTime.Now; } wcsTask = new WcsTask() { objid = StaticData.SnowId.NextId(), orderId = item.rawOutstockId, taskType = 30, containerNo = location.containerCode, createBy = "WCS", createTime = DateTime.Now, taskStatus = 0, materialId = item.materialId, currPointId = location.locationId, currPointNo = location.locationCode, startPointId = location.locationId, startPointNo = location.locationCode, nextPointId = ctuEquip.objid, nextPointNo = ctuEquip.equipNo, endPointId = lineEquip.objid, endPointNo = lineEquip.equipNo, equipmentNo = ctuEquip.equipNo, ud1 = location.locColumn, useFlag = 0, qty = qty }; MesBaseBarcodeInfo? mesBaseBarcodeInfo = dbContext.MesBaseBarcodeInfo.Where(t => t.barcodeInfo == stock.instockBatch).FirstOrDefault(); if (mesBaseBarcodeInfo != null) { WmsRawOutstockDetail wmsProductOutstockDetail = new WmsRawOutstockDetail() { rawOutstockDetailId = StaticData.SnowId.NextId(), rawOutstockId = item.rawOutstockId, materialId = item.materialId, createDate = DateTime.Now, createBy = "WCS", taskCode = wcsTask.objid.ToString(), executeStatus = "1", locationCode = location.locationCode, outstockAmount = qty, planAmount = item.outstockAmount, warehouseId = item.warehouseId, materialBarcode = mesBaseBarcodeInfo.barcodeInfo, instockBatch = mesBaseBarcodeInfo.batchCode, stackAmount = qty, outstockPerson = "WCS", outstockTime = DateTime.Now, outstockWay = "2", materialProductionDate = mesBaseBarcodeInfo.productionDate }; dbContext.Add(wmsProductOutstockDetail); location.outstockFlag = "1"; location.locationStatus = "6"; location.updateBy = "WCS"; location.updateTime = DateTime.Now; wcsOutstockLock.qty += 1; wcsOutstockLock.boxStatus = 1; dbContext.Update(wcsOutstockLock); dbContext.Update(location); dbContext.Add(wcsTask); dbContext.Update(stock); dbContext.SaveChanges(); CreateSuccess = true; if (qty >= needNumber) { break; } } } } if (CreateSuccess) { BaseEquip startStationEquip = StaticData.BaseEquip.First(t => t.objid == 21); BaseEquip bearAgvEquip = StaticData.BaseEquip.First(t => t.objid == 10); BaseEquip endStationEquip = StaticData.BaseEquip.First(t => t.equipNo == item.endStationCode); WcsTask bearAgvTask = new() { objid = StaticData.SnowId.NextId(), orderId = item.orderId, taskType = 32, containerNo = "", startPointId = startStationEquip.objid, startPointNo = startStationEquip.equipNo, currPointId = startStationEquip.objid, currPointNo = startStationEquip.equipNo, nextPointId = bearAgvEquip.objid, nextPointNo = bearAgvEquip.equipNo, endPointId = endStationEquip.objid, endPointNo = endStationEquip.equipNo, serialNo = SystemData.GetSerialNo(dbContext), taskStatus = 0, createTime = DateTime.Now, createBy = "WCS", materialId = item.materialId, qty = 1, useFlag = 1, equipmentNo = startStationEquip.equipNo, remark = "组装出库", floorNo = 5, masterId = item.materialId, fromFloorNo = 5, materialNo = item.materialBatch, }; if (item.beginTime == null) { item.beginTime = DateTime.Now; } dbContext.Add(bearAgvTask); dbContext.Update(item); dbContext.SaveChanges(); } } } } } if (endEquip.equipType == 12 || endEquip.equipType == 13)//原材料到柜体验收区 { if (endEquip.emptyCount == 0) { var wmsRawStocks = rawStock .Where(t => t.materialId == item.materialId && t.warehouseId == item.warehouseId) .WhereIf(endEquip.equipType == 13, t => t.completeFlag == "0") .WhereIf(endEquip.equipType == 12, t => t.completeFlag == "1") .Where(t => t.totalAmount > t.occupyAmount + t.frozenAmount) .Select(t => t.palletInfoCode) .ToList(); var wmsBaseLocations = dbContext.WmsBaseLocation .Where(t => t.activeFlag == "1") .Where(t => t.delFlag == "0") .Where(t => t.locationScrapType == "1") .Where(t => t.instockFlag == "0") .Where(t => t.outstockFlag == "0") .Where(t => t.warehouseId == item.warehouseId) .Where(t => wmsRawStocks.Contains(t.containerCode)) .ToList(); decimal? needNumber = item.outstockAmount - item.realOutstockAmount; if (needNumber <= 0) { item.executeStatus = "2"; dbContext.Update(item); dbContext.SaveChanges(); continue; } var bill = from a in wmsBaseLocations from b in rawStock where a.containerCode == b.palletInfoCode select new { a, b }; //如果第一列满足需求,则按第一列排序,否则按最后一列排序 if (bill.Where(t => t.a.locColumn == 1).Select(t => t.b.totalAmount - t.b.occupyAmount - t.b.frozenAmount).Sum() > needNumber) { bill = bill.OrderBy(t => t.a.locColumn).Take(1).ToList(); } else { bill = bill.OrderByDescending(t => t.a.locColumn).Take(1).ToList(); } BaseEquip bearAgvEquip = StaticData.BaseEquip.First(t => t.objid == 28); BaseEquip lineEquip = StaticData.BaseEquip.First(t => t.equipNo == item.endStationCode); foreach (var b in bill) { item.executeStatus = "1"; WmsBaseLocation location = b.a; WmsRawStock stock = b.b; WcsTask wcsTask; int qty = 0; if (stock.totalAmount - stock.occupyAmount - stock.frozenAmount <= needNumber)//该料箱全部出 { item.realOutstockAmount += stock.totalAmount - stock.occupyAmount - stock.frozenAmount; qty = Convert.ToInt32(stock.totalAmount - stock.frozenAmount); stock.occupyAmount += qty; stock.updateDate = DateTime.Now; } else { item.realOutstockAmount += needNumber; stock.occupyAmount += needNumber; qty = Convert.ToInt32(needNumber); stock.updateDate = DateTime.Now; } MesBaseBarcodeInfo? mesBaseBarcodeInfo = dbContext.MesBaseBarcodeInfo.Where(t => t.barcodeInfo == stock.instockBatch).FirstOrDefault(); if (mesBaseBarcodeInfo != null) { wcsTask = new WcsTask() { objid = StaticData.SnowId.NextId(), orderId = item.rawOutstockId, taskType = endEquip.equipType == 12 ? 46 : 33,//如果是12那么就是原材料到柜体验收区,如果是13那么就是原材料到背板安装区 containerNo = location.containerCode, createBy = "WCS", createTime = DateTime.Now, taskStatus = 0, materialId = item.materialId, currPointId = location.locationId, currPointNo = location.locationCode, startPointId = location.locationId, startPointNo = location.locationCode, nextPointId = bearAgvEquip.objid, nextPointNo = bearAgvEquip.equipNo, endPointId = lineEquip.objid, endPointNo = lineEquip.equipNo, equipmentNo = bearAgvEquip.equipNo, useFlag = 1, qty = qty }; WmsRawOutstockDetail wmsProductOutstockDetail = new WmsRawOutstockDetail() { rawOutstockDetailId = StaticData.SnowId.NextId(), rawOutstockId = item.rawOutstockId, materialId = item.materialId, createDate = DateTime.Now, createBy = "WCS", taskCode = wcsTask.objid.ToString(), executeStatus = "1", locationCode = location.locationCode, outstockAmount = qty, planAmount = item.outstockAmount, warehouseId = item.warehouseId, materialBarcode = mesBaseBarcodeInfo.barcodeInfo, instockBatch = mesBaseBarcodeInfo.batchCode, stackAmount = qty, outstockPerson = "WCS", outstockTime = DateTime.Now, outstockWay = "2", materialProductionDate = mesBaseBarcodeInfo.productionDate }; dbContext.Add(wmsProductOutstockDetail); location.outstockFlag = "1"; location.locationStatus = "6"; location.updateBy = "WCS"; location.updateTime = DateTime.Now; endEquip.emptyCount = 1; dbContext.Update(endEquip); dbContext.Update(location); dbContext.Add(wcsTask); dbContext.Update(stock); dbContext.SaveChanges(); if (qty >= needNumber) { break; } } if (item.beginTime == null) { item.beginTime = DateTime.Now; } dbContext.Update(item); dbContext.SaveChanges(); } } } } } } catch { } Thread.Sleep(1000); } } } }