You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1118 lines
68 KiB
C#

2 years ago
using Khd.Core.Domain.Dto.wcs;
2 years ago
using Khd.Core.Domain.Models;
using Khd.Core.EntityFramework;
2 years ago
using Khd.Core.Library.Mapper;
2 years ago
using Khd.Core.Wcs.Global;
2 years ago
using Masuit.Tools;
2 years ago
using Masuit.Tools.Logging;
2 years ago
using Microsoft.CodeAnalysis;
2 years ago
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2 years ago
using SixLabors.ImageSharp;
2 years ago
using System.Data;
2 years ago
using System.Security.Cryptography;
2 years ago
using System.Threading.Tasks.Dataflow;
2 years ago
using Z.EntityFramework.Plus;
namespace Khd.Core.Wcs.Wcs
{
/// <summary>
/// 根据出入库记录创建出入库任务
/// </summary>
public class CreateTaskByRecord
{
private readonly IHost _host;
List<BasePlcpoint> ScanPoint { get; set; }//点位信息
2 years ago
private BaseSitenode? sitenode { get; set; }//站台信息
2 years ago
2 years ago
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; }
2 years ago
public CreateTaskByRecord(IHost host, string siteNo)
2 years ago
{
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);
}
}
/// <summary>
/// 启动上件扫描监听
/// </summary>
public void StartPoint()
{
2 years ago
var createBearAgvReturnThread = new Thread(CreateBearAgvReturnLogic);
createBearAgvReturnThread.Start();
2 years ago
2 years ago
//var createRawInWareTaskThread = new Thread(CreateRawInWareTaskLogic);
//createRawInWareTaskThread.Start();
2 years ago
2 years ago
var createFiveProductInTaskThread = new Thread(CreateFiveProductInTaskLogic);
createFiveProductInTaskThread.Start();
var createFiveProductTaskThread = new Thread(CreateFiveProductTaskLogic);
createFiveProductTaskThread.Start();
2 years ago
2 years ago
var createRawTaskThread = new Thread(CreateRawTaskLogic);
2 years ago
createRawTaskThread.Start();
2 years ago
2 years ago
//var createThirdOutTaskThread = new Thread(CreateThirdOutTaskLogic);
//createThirdOutTaskThread.Start();
2 years ago
2 years ago
Console.WriteLine(DateTime.Now + ":出库任务监听启动成功");
2 years ago
LogManager.Info("出库任务监听启动成功");
2 years ago
}
2 years ago
1 year ago
/// <summary>
/// 半成品入库
/// </summary>
/// <param name="obj"></param>
2 years ago
private void CreateFiveProductInTaskLogic(object? obj)
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
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<WcsTaskLog>(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);
}
}
2 years ago
/// <summary>
/// 创建去翻转机的任务
/// </summary>
/// <param name="obj"></param>
/// <exception cref="NotImplementedException"></exception>
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<DefaultDbContext>();
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();
2 years ago
if (fistbill != null)
2 years ago
{
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)
{
2 years ago
if (lastbill.b.instockDate != null && fistbill.b.instockDate != null)
2 years ago
{
wmsBaseLocation = fistbill.a;
2 years ago
if (lastbill.b.instockDate.Value.Date == fistbill.b.instockDate.Value.Date)
2 years ago
{
wmsBaseLocation = lastbill.a;
}
else//移库
{
var formWmsBaseLocation = lastbill.a;
//先找深库位
List<string> list = wmsRawStocks.Select(t => t.locationCode).ToList();
var deepLocation = wmsBaseLocations
.Where(t => t.locDeep == 1)
2 years ago
.Where(t => !list.Contains(t.locationCode))
2 years ago
.ToList();
2 years ago
if (deepLocation.Count > 0)
2 years ago
{
2 years ago
var toWmsBaseLocation = deepLocation.First();
2 years ago
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<WcsTaskLog>(wcsTask);
dbContext.Add(wcsTask);
dbContext.Add(wcsTaskLog);
dbContext.SaveChanges();
}
}
}
}
2 years ago
2 years ago
}
}
}
catch (Exception ex)
{
LogManager.Error(ex);
}
}
}
/// <summary>
/// 创建返库任务
/// </summary>
/// <param name="obj"></param>
private void CreateBearAgvReturnLogic()
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
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());
2 years ago
var wmsRawReturn = dbContext.WmsRawReturn
2 years ago
.Where(t => t.auditStatus == "1")
2 years ago
.FirstOrDefault();
if (wmsRawReturn != null)
2 years ago
{
if (wmsRawReturn.executeStatus == "0")
{
//任务未开始Status=0
1 year ago
BaseEquip startEquip = StaticData.BaseEquip.First(t => t.equipNo == wmsRawReturn.endStationCode);
2 years ago
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,
2 years ago
//materialNo = wmsRawReturn.materialBatch,
2 years ago
};
wmsRawReturn.executeStatus = "1";
dbContext.Add(wcsTask);
2 years ago
WcsTaskLog wcsTaskLog = CoreMapper.Map<WcsTaskLog>(wcsTask);
dbContext.Add(wcsTaskLog);
2 years ago
dbContext.Update(wmsRawReturn);
dbContext.SaveChanges();
}
}
}
catch (Exception ex)
{
LogManager.Error(ex);
}
Thread.Sleep(1000);
}
}
2 years ago
/// <summary>
/// 背板安装区到半成品入库
/// </summary>
private void CreateRawInWareTaskLogic()
2 years ago
{
2 years ago
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
2 years ago
while (true)
{
try
{
2 years ago
dbContext.ChangeTracker.Entries().ToList().ForEach(e => e.Reload());
2 years ago
}
catch (Exception ex)
{
2 years ago
Console.WriteLine(ex.Message + ex.StackTrace);
2 years ago
LogManager.Error(ex);
}
2 years ago
Thread.Sleep(5000);
2 years ago
}
2 years ago
2 years ago
}
2 years ago
/// <summary>
/// 成品出库
/// </summary>
2 years ago
private void CreateFiveProductTaskLogic()
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
2 years ago
while (true)
{
2 years ago
try
{
2 years ago
dbContext.ChangeTracker.Entries().ToList().ForEach(e => e.Reload());
2 years ago
var proStock = dbContext.WmsProductStock
.Where(t => t.warehouseId == 531)
.Where(t => t.activeFlag == "0").ToList();
2 years ago
var proOutStock = dbContext.WmsProductOutstock
.Where(t => t.outstockQty < t.applyQty)
.Where(t => t.auditStatus == "1")
2 years ago
.Where(t => t.warehouseId == 531)
.Where(t => t.productType == "3")
2 years ago
.Where(t => t.executeStatus == "0" || t.executeStatus == "1").ToList();
2 years ago
//获取最早入库时间
foreach (var item in proOutStock)
2 years ago
{
2 years ago
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;
}
2 years ago
MesBasePalletInfo? mesBasePalletInfo = dbContext.MesBasePalletInfo.FirstOrDefault(t => t.palletInfoCode == location.containerCode);
if (mesBasePalletInfo != null)
2 years ago
{
2 years ago
WmsProductOutstockDetail detail = new()
2 years ago
{
productId = item.productId,
2 years ago
productOutstockId = item.productOutstockId,
2 years ago
locationCode = location.locationCode,
2 years ago
executeStatus = "1",
beginTime = DateTime.Now,
2 years ago
warehouseId = item.warehouseId,
2 years ago
erpAmount = 0,
confirmAmount = 0,
outstockAmount = 1,
planAmount = 1,
productBatch = item.productBatch,
productOutstockDetailId = StaticData.SnowId.NextId(),
productBarcode = mesBasePalletInfo.materialBarcode
2 years ago
};
2 years ago
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);
2 years ago
dbContext.Update(location);
dbContext.Add(wcsTask);
dbContext.Update(stock);
dbContext.SaveChanges();
if (qty >= needNumber)
{
break;
}
2 years ago
}
2 years ago
}
2 years ago
if (item.beginTime == null)
{
item.beginTime = DateTime.Now;
}
2 years ago
dbContext.Update(item);
dbContext.SaveChanges();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + ex.StackTrace);
LogManager.Error(ex);
}
}
}
2 years ago
/// <summary>
/// 原材料出库
/// </summary>
2 years ago
private void CreateRawTaskLogic()
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
2 years ago
while (true)
{
2 years ago
try
{
2 years ago
dbContext.ChangeTracker.Entries().ToList().ForEach(e => e.Reload());
//原材料库存
var rawStock = dbContext.WmsRawStock.Where(t => t.activeFlag == "1").ToList();
//原材料出库记录
var rawOutStock = dbContext.WmsRawOutstock
2 years ago
.Where(t => t.executeStatus == "0" || t.executeStatus == "1")
.Where(t => t.outstockAmount > t.realOutstockAmount)
2 years ago
.Where(t => t.auditStatus == "1")
2 years ago
.ToList();
1 year ago
List<WcsTaskManual> wcsTaskManuals = dbContext.WcsTaskManual.Where(t=>t.nextPointId== 11).OrderBy(t => t.createBy).ToList();
2 years ago
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<WcsTask>(wcs);
WcsTaskLog wcsTaskLog = CoreMapper.Map<WcsTaskLog>(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();
}
}
2 years ago
foreach (var item in rawOutStock)
2 years ago
{
2 years ago
decimal? RealOutNumber = item.realOutstockAmount;
2 years ago
var endEquip = StaticData.BaseEquip.FirstOrDefault(t => t.equipNo == item.endStationCode);
if (endEquip != null && endEquip.floorNo == 5)
{
2 years ago
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,
2 years ago
instockBatch = mesBaseBarcodeInfo.batchCode,
stackAmount = qty,
outstockPerson = "WCS",
outstockTime = DateTime.Now,
outstockWay = "2",
materialProductionDate = mesBaseBarcodeInfo.productionDate
2 years ago
};
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
};
2 years ago
MesBaseBarcodeInfo? mesBaseBarcodeInfo = dbContext.MesBaseBarcodeInfo.Where(t => t.barcodeInfo == stock.instockBatch).FirstOrDefault();
if (mesBaseBarcodeInfo != null)
2 years ago
{
2 years ago
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;
}
2 years ago
}
}
}
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)//原材料到柜体验收区
2 years ago
{
2 years ago
if (endEquip.emptyCount == 0)
2 years ago
{
2 years ago
var wmsRawStocks = rawStock
2 years ago
.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)
2 years ago
.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)
2 years ago
.Where(t => wmsRawStocks.Contains(t.containerCode))
2 years ago
.ToList();
2 years ago
decimal? needNumber = item.outstockAmount - item.realOutstockAmount;
if (needNumber <= 0)
{
item.executeStatus = "2";
dbContext.Update(item);
dbContext.SaveChanges();
continue;
}
2 years ago
var bill = from a in wmsBaseLocations
from b in rawStock
2 years ago
where a.containerCode == b.palletInfoCode
2 years ago
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)
2 years ago
{
2 years ago
bill = bill.OrderBy(t => t.a.locColumn).Take(1).ToList();
2 years ago
}
else
{
2 years ago
bill = bill.OrderByDescending(t => t.a.locColumn).Take(1).ToList();
2 years ago
}
2 years ago
2 years ago
BaseEquip bearAgvEquip = StaticData.BaseEquip.First(t => t.objid == 28);
BaseEquip lineEquip = StaticData.BaseEquip.First(t => t.equipNo == item.endStationCode);
2 years ago
foreach (var b in bill)
2 years ago
{
2 years ago
item.executeStatus = "1";
WmsBaseLocation location = b.a;
WmsRawStock stock = b.b;
WcsTask wcsTask;
int qty = 0;
if (stock.totalAmount - stock.occupyAmount - stock.frozenAmount <= needNumber)//该料箱全部出
{
2 years ago
item.realOutstockAmount += stock.totalAmount - stock.occupyAmount - stock.frozenAmount;
2 years ago
qty = Convert.ToInt32(stock.totalAmount - stock.frozenAmount);
2 years ago
stock.occupyAmount += qty;
2 years ago
stock.updateDate = DateTime.Now;
}
else
{
2 years ago
item.realOutstockAmount += needNumber;
stock.occupyAmount += needNumber;
2 years ago
qty = Convert.ToInt32(needNumber);
stock.updateDate = DateTime.Now;
}
2 years ago
MesBaseBarcodeInfo? mesBaseBarcodeInfo = dbContext.MesBaseBarcodeInfo.Where(t => t.barcodeInfo == stock.instockBatch).FirstOrDefault();
if (mesBaseBarcodeInfo != null)
2 years ago
{
2 years ago
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)
2 years ago
{
2 years ago
item.beginTime = DateTime.Now;
2 years ago
}
2 years ago
dbContext.Update(item);
dbContext.SaveChanges();
2 years ago
}
2 years ago
}
}
2 years ago
}
}
2 years ago
}
2 years ago
catch
2 years ago
{
}
1 year ago
Thread.Sleep(1000);
2 years ago
}
2 years ago
2 years ago
}
2 years ago
2 years ago
}
}