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.

338 lines
15 KiB
C#

2 years ago
using Khd.Core.Domain.Dto.wcs;
2 years ago
using Khd.Core.Domain.Models;
using Khd.Core.EntityFramework;
using Khd.Core.Wcs.Global;
using Masuit.Tools.Logging;
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 Z.EntityFramework.Plus;
namespace Khd.Core.Wcs.Wcs
{
/// <summary>
/// 根据出入库记录创建出入库任务
/// </summary>
public class CreateTaskByRecord
{
private readonly IHost _host;
private readonly Plc.S7.Plc _plc;
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, Plc.S7.Plc plc, string siteNo)
{
this._host = host;
this._plc = plc;
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 FlowPointThread = new Thread(MonitorInLocatorPoint);
2 years ago
FlowPointThread.Start();
2 years ago
Console.WriteLine(DateTime.Now + ":出库任务监听启动成功");
2 years ago
LogManager.Info("出库任务监听启动成功");
2 years ago
}
public void MonitorInLocatorPoint()
{
while (true)
{
try
{
2 years ago
//原料&辅料出库
CreateRawTask();
//成品&半成品出库
CreateProductTask();
2 years ago
}
catch (Exception ex)
{
LogManager.Error(ex);
}
finally
{
Thread.Sleep(2000);
}
}
}
2 years ago
/// <summary>
/// 成品出库
/// </summary>
public void CreateProductTask()
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
var proStock = dbContext.WmsProductStock.Where(t => t.activeFlag == "0").ToList();
var proOutStock = dbContext.WmsProductOutstock.Where(t => t.executeStatus == "0").ToList();
//获取最早入库时间
foreach (var item in proOutStock)
{
//自动获取id
2 years ago
var objid = StaticData.SnowId.NextId();
//锁定库存
WcsOutstockLock stockLock = new()
{
objid = objid,
materialId = item.productId,
qty = item.applyQty,
warehouseId = item.warehouseId,
wmsOrderId = item.productOutstockId,
wmsOrderDetailId = item.productOutstockId,
createBy = "sys",
createTime = DateTime.Now
};
var dic = StaticData.BaseDictionary.Where(t => t.dicKey == "TaskType" && t.ud1 == "O" && t.dicField == item.productType).FirstOrDefault();
2 years ago
//获取最早入库时间
var firstDate = proStock.Where(t => t.productId == item.productId && t.warehouseId == item.warehouseId).OrderBy(t => t.createDate).FirstOrDefault();
if (firstDate != null && dic != null)
{
//创建出库任务
WcsTask newTask = new()
{
objid = objid,
taskType = Convert.ToInt32(dic.dicValue),
containerNo = "",
taskStatus = 0,
materialId = item.warehouseId,
qty = 1,
startPointId = item.warehouseId,
startPointNo = "",
currPointId = item.warehouseId,
currPointNo = "",
2 years ago
endPointId = item.warehouseId,
endPointNo = "",
};
dbContext.Add(newTask);
dbContext.SaveChanges();
2 years ago
//回写wms记录表
dbContext.WmsProductOutstock.Where(t => t.productOutstockId == item.productOutstockId).Update(t => new WmsProductOutstock()
{
executeStatus = "1",
updateDate = DateTime.Now
});
}
}
}
2 years ago
/// <summary>
/// 原材料出库
/// </summary>
2 years ago
public void CreateRawTask()
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
2 years ago
//原材料库存
2 years ago
var rawStock = dbContext.WmsRawStock.Where(t => t.activeFlag == "1").ToList();
2 years ago
//原材料出库记录
2 years ago
var rawOutStock = dbContext.WmsRawOutstock
.Where(t => t.executeStatus != "2").ToList();
foreach (var item in rawOutStock)
{
//自动获取id
2 years ago
if (item.warehouseId == 512)//CTU出库
{
2 years ago
var wmsRawStocks = rawStock
.Where(t => t.materialId == item.materialId && 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 => wmsRawStocks.Contains(t.containerCode))
.ToList();
decimal needNumber = item.outstockAmount - item.realOutstockAmount;
if (needNumber <= 0)
2 years ago
{
2 years ago
item.executeStatus = "2";
dbContext.Update(item);
dbContext.SaveChanges();
continue;
}
2 years ago
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();
}
//做数量限制
2 years ago
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)
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)//该料箱全部出
{
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;
}
wcsTask = new WcsTask()
{
objid = StaticData.SnowId.NextId(),
orderId = item.rawOutstockId,
taskType = 38,
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,
useFlag = 1,
qty = qty
};
dbContext.Add(wcsTask);
dbContext.Update(stock);
dbContext.SaveChanges();
if (qty == needNumber)
{
break;
}
}
dbContext.Update(item);
dbContext.SaveChanges();
2 years ago
}
2 years ago
else
{
}
//var objid = StaticData.SnowId.NextId();
//long orderID = 0;
////插入锁定库存
//WcsOutstockLock stockLock = new()
//{
// objid = objid,
// materialId = item.productId,
// qty = 1,
// warehouseId = item.warehouseId,
// wmsOrderId = item.rawOutstockId,
// wmsOrderDetailId = item.rawOutstockId,
// createBy = "sys",
// createTime = DateTime.Now
//};
//dbContext.Add(stockLock);
//if (item.warehouseId == 5)//五楼辅料出库
//{
// //辅料出库时,需创建订单表数据
// orderID = StaticData.SnowId.NextId();
// WcsWarehouseOrder order = new()
// {
// objid = orderID,
// wmsOrderId = item.rawOutstockId,
// orderType = Convert.ToInt32(item.taskType),
// qty = 1,
// createBy = "",
// createTime = DateTime.Now
// };
// dbContext.Add(order);
//}
//var dic = StaticData.BaseDictionary.Where(t => t.dicKey == "TaskType" && t.ud1 == "O").FirstOrDefault();
//if (dic != null)
//{
// WcsTask rawTask = new()
// {
// objid = StaticData.SnowId.NextId(),
// orderId = item.rawOutstockId,
// masterId = orderID,
// taskType = Convert.ToInt32(dic.dicValue),
// containerNo = "",
// taskStatus = 0,
// materialId = item.materialId,
// qty = 1,
// startPointId = item.warehouseId,
// startPointNo = "",
// currPointId = item.warehouseId,
// currPointNo = "",
// endPointId = item.warehouseId,
// endPointNo = ""
// };
// dbContext.Add(rawTask);
// //获取最早入库时间
// var firstDate = rawStock.Where(t => t.materialId == item.productId && t.warehouseId == item.warehouseId).OrderBy(t => t.createDate).FirstOrDefault();
// //回写wms出库记录表
// dbContext.WmsRawOutstock.Where(t => t.rawOutstockId == item.rawOutstockId).Update(t => new WmsProductOutstock()
// {
// executeStatus = "1",
// updateDate = DateTime.Now
// });
//}
2 years ago
}
}
}
}