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.

2611 lines
110 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using HandyControl.Tools.Extension;
using Jc.SnowId;
using Khd.Core.Domain.Auth;
using Khd.Core.Domain.Dto.agv;
using Khd.Core.Domain.Dto.webapi;
using Khd.Core.Domain.Models;
using Khd.Core.EntityFramework;
using Khd.Core.Library.Mapper;
using Khd.Core.Plc;
using Khd.Core.Plc.S7;
using Khd.Core.Wpf.dto;
using Khd.Core.Wpf.Scan;
using Khd.Core.Wpf.TaskForm;
using Masuit.Tools;
using Masuit.Tools.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
using Z.EntityFramework.Plus;
namespace Khd.Core.Wpf.Form
{
/// <summary>
/// FormBoardT.xaml 的交互逻辑
/// </summary>
public partial class FormBoardT : Window
{
public static long? UserId = 1;
public static readonly JcSnowId _jcSnowId = new JcSnowId();
private readonly IHost _host;
private Khd.Core.Plc.S7.Plc _plc;
private DispatcherTimer ShowTimer;//刷新时间
private DispatcherTimer PLCMessage;//呈现PLC机柜信息
private DispatcherTimer UPMessageTimer;//刷新上件记录
private DispatcherTimer ShowOrderMessage;//呈现PLC机柜信息
private object order_code;
private int UpState;//对应上件站点的状态,0为良好 1为损坏
private SerialPortModel serialPortModel;
private SerialPortHelper serialPortHelper;
List<BasePlcpoint> basePlcpoints = new List<BasePlcpoint>();
object timerjilu;
Dictionary<long, bool> SelectedItem = new Dictionary<long, bool>();
Dictionary<long, bool> InSelectedItem = new Dictionary<long, bool>();
List<long?> CallSelectedItem = new List<long?>();
public List<MesBaseBarcodeInfo> barcodeLsit = new List<MesBaseBarcodeInfo>();
private int pageCount = 15;
private object updateLock = new object();
private void ScanMessage()
{
while (true)
{
try
{
if (SystemData.isUpdate)
{
if (!string.IsNullOrEmpty(SystemData.message))
{
Dispatcher.Invoke(() =>
{
var focusedElement = FocusManager.GetFocusedElement(this);
if (focusedElement is TextBox textBox)
{
textBox.Text = SystemData.message;
SystemData.isUpdate = false;
}
});
}
}
}
catch (Exception ex)
{
LogManager.Error(ex);
}
Thread.Sleep(500);
}
}
private string selectedId;
//FormShowSelect formSelect;
// public static int WeekNo = CommonHelper.WeekOfYear(DateTime.Now, new System.Globalization.CultureInfo("zh-CN"));
public FormBoardT(IHost host)
{
InitializeComponent();
this._host = host;
}
/// <summary>
/// 页面加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormBoard_Loaded(object sender, RoutedEventArgs e)
{
try
{
foreach (var plcConfig in SystemData.PlcConfigs)
{
if (!SystemData.PlcDic.Any(t => t.Key == plcConfig.Code))
{
Plc.S7.Plc plc;
plc = new Plc.S7.Plc(plcConfig.CpuType, plcConfig.IP, plcConfig.Port, plcConfig.Rack, plcConfig.Slot);
//try
//{
// plc.Open();
//}
//catch (Exception ex)
//{
// LogManager.Error(ex);
//}
SystemData.PlcDic.TryAdd(plcConfig.Code, plc);
}
};
GetMenu();
DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(ShowTime); //委托刷新时间方法
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000);
dispatcherTimer.Start();
Thread AgvStatusThread = new Thread(AgvStatus)
{
IsBackground = true,
};
AgvStatusThread.Start();
Thread ddjThread = new Thread(ddjTimer)
{
IsBackground = true,
};
ddjThread.Start();
Thread RefreshPlcThread = new Thread(RefreshPlc)
{
IsBackground = true,
};
RefreshPlcThread.Start();
//Thread ddjThread = new Thread(ddjTimer);
//ddjThread.Start();
//LogManager.Info($"--------------------------当前时间 :{DateTime.Now} >>> 客户端启动;");
//设置显示任务栏
if (WindowState == WindowState.Normal)
{
MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
WindowState = WindowState.Maximized;
}
Task.Run(() =>
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
SystemData.BasePlcpoints = dbContext.BasePlcpoint.ToList();
SystemData.BaseDictionary = dbContext.BaseDictionary.ToList();
SystemData.BaseWareHouse = dbContext.WmsBaseWarehouse.ToList();
SystemData.MesBaseMaterialInfo = dbContext.MesBaseMaterialInfo.Take(100).ToList();
Dispatcher.Invoke(() =>
{
WarehourseId.ItemsSource = SystemData.BaseWareHouse.Where(t => t.warehouseFloor != 1 && t.warehouseFloor != 4).ToList();
});
});
//MaterialName.ItemsSource = SystemData.MesBaseMaterialInfo;
try
{
serialPortModel = new SerialPortModel();
serialPortModel.PortName = "COM1";
serialPortModel.BaudRate = 9600;
serialPortModel.DataBits = 8;
serialPortModel.Parity = Parity.None;
serialPortModel.StopBits = StopBits.One;
serialPortHelper = new SerialPortHelper();
serialPortHelper.OpenMyConn(serialPortModel);
Thread scanThread = new Thread(ScanMessage);
scanThread.IsBackground = true;
scanThread.Start();
}
catch (Exception ex)
{
MessageBox.Show("条码枪连接失败" + ex.Message);
}
Task.Run(() =>
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
while (true)
{
try
{
dbContext.ChangeTracker.Entries().ToList().ForEach(e => e.Reload());
int waitCtuCount = dbContext.WcsTask.Where(t => t.taskType == 30).Where(t => t.useFlag == 0).Where(t => t.taskStatus == 0).Count();
Dispatcher.Invoke(() =>
{
outTaskCount.Text = waitCtuCount.ToString();
});
}
catch
{
}
Thread.Sleep(3000);
}
});
//加载dategrid信息
//LoadMaterial_GetMessage("");
//连接PLC判断
// var plc = new Khd.Core.Plc.S7.Plc((CpuType)PlcConfig.CpuType, PlcConfig.IP, PlcConfig.Port,
// PlcConfig.Rack, PlcConfig.Slot);
// //plc.Open();
// //if (!plc.IsConnected)
// //{
// // MessageBox.Show("PLC连接失败,重新连接");
// //}
// //else
// //{
// // this._plc = plc;
// //}
// using var scope = _host.Services.CreateScope();
// using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
// basePlcpoints = dbContext.BasePlcpoint.Where(t => t.isDelete == 0).ToList();
// //var baseMaterials = dbContext.BaseMaterial.Where(t => t.IsDelete == 0).ToList();
////
// PLCMessage = new DispatcherTimer();
// PLCMessage.Tick += new EventHandler(PLCmessage); //委托获取点位方法
// PLCMessage.Interval = new TimeSpan(0, 0, 0, 0, 1000);
// PLCMessage.Start();
// UPMessageTimer = new DispatcherTimer();
// UPMessageTimer.Tick += new EventHandler(UpMessage); //委托刷新上件记录
// UPMessageTimer.Interval = new TimeSpan(0, 0, 0, 0, 2000);
// UPMessageTimer.Start();
// Button_Click_1(null, null);
// SetTime();
// List<string> list = new List<string>
// {
// "101上件点",
// "102上件点",
// "103上件点"
// };
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Dictionary<string, string> agvDic = new Dictionary<string, string>
{
{"6011","五楼潜伏" },
{"2034","五楼叉车" },
{"8161","五楼CTU" },
{"2032","三楼叉车" },
{"2033","二楼叉车" },
};
private void AgvStatus()
{
using var scope = _host.Services.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
while (true)
{
try
{
dbContext.ChangeTracker.Entries().ToList().ForEach(e => e.Reload());
var wcsAgvStatuses = dbContext.WcsAgvStatus.ToList();
var list = wcsAgvStatuses.Select(
t => new
{
Battery = t.Battery,
Speed = t.Speed,
Status = t.Status,
Stop = t.Stop == "1" ? "停止" : "运行",
DateNow = t.DateNow?.ToString("yyyy-MM-dd HH:mm:ss"),
ExclType = t.ExclType,
MapCode = t.MapCode == "EE" ? "五楼" : t.MapCode == "BB" ? "三楼" : "二楼",
Online = t.Online.ToUpper() == "TRUE" ? "在线" : "离线",
robotIp = t.robotIp,
Path2 = t.Path2,
RobotCode = agvDic[t.RobotCode],
PosX = t.PosX,
PosY = t.PosY,
PodDir = t.PodDir,
RobotDir = t.RobotDir,
PodCode = t.PodCode,
}
).ToList();
Dispatcher.Invoke(() =>
{
this.AgvDataGrid.ItemsSource = list;
this.AgvDataGrid.Items.Refresh();
});
}
catch
{
}
Thread.Sleep(3000);
}
}
private void ddjTimer()
{
using var scope = _host.Services.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
while (true)
{
try
{
lock (updateLock)
{
dbContext.ChangeTracker.Entries().ToList().ForEach(e => e.Reload());
SystemData.BaseEquip = dbContext.BaseEquip.ToList();
BaseEquip baseEquip = SystemData.BaseEquip.First(t => t.objid == 40);
Dispatcher.Invoke(() =>
{
EquipListSelectDy.ItemsSource = SystemData.BaseEquip.Where(t => t.equipType == 2 || t.equipType == 4 || t.equipType == 5 || t.equipType == 6 || t.equipType == 11);
this.firstRFID.Text = SystemData.BaseEquip.First(t => t.objid == 1).containerNo;
this.DDJEmptyCount.Text = baseEquip.emptyCount.ToString();
if (baseEquip.emptyCount == 0)
{
this.OneIn.IsEnabled = true;
this.SomeIn.IsEnabled = true;
this.OneOut.IsEnabled = false;
this.SomeOut.IsEnabled = false;
}
else if (baseEquip.emptyCount > 0 && baseEquip.emptyCount < 10)
{
this.OneIn.IsEnabled = true;
this.SomeIn.IsEnabled = false;
this.OneOut.IsEnabled = true;
this.SomeOut.IsEnabled = true;
}
else if (baseEquip.emptyCount >= 10)
{
this.OneIn.IsEnabled = false;
this.SomeIn.IsEnabled = false;
this.OneOut.IsEnabled = true;
this.SomeOut.IsEnabled = true;
}
this.EmptyEquip.ItemsSource = SystemData.BaseEquip.Where(t => t.equipType == 20 || t.equipType == 21 || t.equipType == 19).Select(
t => new BaseEquip
{
objid = t.objid,
equipName = t.equipName,
emptyCount = t.emptyCount,
equipType = t.equipType,
ud3 = t.ud3 == "0" ? "空" : t.ud3 == "1" ? "托盘" : t.ud3 == "2" ? "废料" : "未知",
equipNo = t.equipNo,
floorNo = t.floorNo,
containerNo=t.containerNo
}
).OrderBy(t => t.equipNo);
this.EmptyEquip.Items.Refresh();
});
}
}
catch
{
}
Thread.Sleep(3000);
}
}
private void ShowTime(object? sender, EventArgs e)
{
try
{
this.LabDate.Text = DateTime.Now.Date.ToString("yyyy-MM-dd");
this.LabTime.Text = DateTime.Now.ToString("HH:mm:ss");
}
catch
{
}
}
/// <summary>
/// 显示序号事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dgData_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Header = e.Row.GetIndex() + 1;
}
/// <summary>
/// 点击退出按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExit_Click(object sender, RoutedEventArgs e)
{
if (MessageBox.Show("是否确认关闭程序", "提示信息", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
{
System.Environment.Exit(System.Environment.ExitCode);
}
}
private void Close(object sender, EventArgs e)
{
System.Environment.Exit(System.Environment.ExitCode);
}
public int i = 0;
/// <summary>
/// 查询任务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGetTask_Click(object sender, RoutedEventArgs e)
{
GetTask();
}
//private void GetTaskLog()
//{
// try
// {
// using var scope = _host.Services.CreateScope();
// using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
// List<WcsTaskLog> wcsTaskLogs = new List<WcsTaskLog>();
// for (int i = 1; i <= 5; i++)
// {
// BasePlcpoint basePlcpoint = SystemData.BasePlcpoints.First(t => t.plcpointNo == "RFID00" + i);
// string? rifd = SystemData.PlcDic[0].ReadRFID(basePlcpoint.plcpointAddress);
// string floor = i.ToString();
// var wcsTaskLog = dbContext.WcsTaskLog.FirstOrDefault(t => t.ud3 == "1" && t.ud2 == floor && t.containerNo == rifd);
// if (wcsTaskLog != null)
// {
// wcsTaskLogs.Add(wcsTaskLog);
// }
// }
// this.WcsTaskLogDataGrid.ItemsSource = wcsTaskLogs;
// this.WcsTaskLogDataGrid.Items.Refresh();
// if (wcsTaskLogs.Count == 0)
// {
// MessageBox.Show("未查询到数据");
// }
// }
// catch (Exception ex)
// {
// if (ex is PlcException)
// {
// try
// {
// SystemData.PlcDic[0] = new Plc.S7.Plc(SystemData.PlcDic[0].CPU, SystemData.PlcDic[0].IP, SystemData.PlcDic[0].Port, SystemData.PlcDic[0].Rack, SystemData.PlcDic[0].Slot);
// SystemData.PlcDic[0].Open();
// GetTaskLog();
// }
// catch
// {
// MessageBox.Show("PLC连接失败,无法读取任务");
// }
// }
// }
//}
private void GetTask()
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
var equipCode = lba_ThrifTtitle1.Text;
var palletNo = lba_ThrifTtitle2.Text;
long? nextPointId = 0;
if (this.EquipListSelectDy.SelectedItem is BaseEquip baseEquip)
{
nextPointId = baseEquip.objid;
}
var data = dbContext.WcsTask
.WhereIf(nextPointId != 0, t => t.nextPointId == nextPointId)
.Where(t => t.containerNo.Contains(palletNo) && t.equipmentNo.Contains(equipCode)).ToList();
List<taskModel> taskModel = CoreMapper.Map<List<taskModel>>(data);
SelectedItem.Clear();
foreach (var item in taskModel)
{
SelectedItem.Add(item.objid, false);
if (item.nextPointId == 6)
{
item.isShow = Visibility.Visible;
}
else
{
item.isShow = Visibility.Collapsed;
}
}
this.LoadMaterial0.ItemsSource = null;
this.LoadMaterial0.ItemsSource = taskModel;
this.LoadMaterial0.Items.Refresh();
if (data.Count == 0)
{
MessageBox.Show("未查询到数据");
}
}
/// <summary>
/// 删除任务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDelTask_Click(object sender, RoutedEventArgs e)
{
if (LoadMaterial0.SelectedIndex == -1)
{
MessageBox.Show("请选择要删除的任务!");
}
else
{
MessageBoxResult messageBoxResult = MessageBox.Show("确定要删除这些任务吗?", "提示信息", MessageBoxButton.YesNo);
if (messageBoxResult == MessageBoxResult.Yes)
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
List<long?> ids = new List<long?>();
foreach (var item in SelectedItem)
{
if (item.Value == true)
{
ids.Add(item.Key);
}
}
BaseEquip agvEquip = SystemData.BaseEquip.First(t => t.objid == 10);
List<WcsCmd> wcsCmds = dbContext.WcsCmd.Where(t => ids.Contains(t.taskId)).ToList();
List<WcsTask> wcsTasks = dbContext.WcsTask.Where(t => ids.Contains(t.objid)).ToList();
foreach (var item in wcsCmds)
{
var cancelTask = new
{
reqCode = Global.SnowId.NextId().ToString(),
taskCode = item.taskCode
};
string reponse = HttpHelper.SendPostMessage(agvEquip.serverIp, agvEquip.serverPort.Value, "rcms/services/rest/hikRpcService/cancelTask", cancelTask.ToJsonString());
var result = JsonConvert.DeserializeObject<ReponseMessage>(reponse);
if (result != null && result.code == "0")
{
WcsTask? wcsTask = wcsTasks.Where(t => t.objid == item.taskId).FirstOrDefault();
if (wcsTask != null)
{
dbContext.Remove(wcsTask);
}
ids.Remove(item.taskId);
dbContext.Remove(item);
dbContext.WcsTaskLog.Where(t => ids.Contains(t.objid)).Update(t => new WcsTaskLog { IsDelete = 1 });
dbContext.SaveChanges();
}
else
{
MessageBoxResult messageBoxResult1 = MessageBox.Show("取消小车端任务失败,是否继续删除调度任务?", "提示信息", MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (messageBoxResult1 == MessageBoxResult.Yes)
{
ids.Remove(item.taskId);
dbContext.Remove(item);
dbContext.WcsTaskLog.Where(t => ids.Contains(t.objid)).Update(t => new WcsTaskLog { IsDelete = 1 });
dbContext.SaveChanges();
}
}
}
GetTask();
}
else
{
}
}
}
/// <summary>
/// 完成任务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnFinishTask_Click(object sender, RoutedEventArgs e)
{
if (LoadMaterial0.SelectedIndex == -1)
{
MessageBox.Show("请选择要完成的任务!");
}
else
{
var Currentselected = LoadMaterial0.SelectedItem as WcsTask;
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
Currentselected.taskStatus = 5;
dbContext.WcsTask.Update(Currentselected);
dbContext.SaveChanges();
GetTask();
}
}
/// <summary>
/// 查询设备信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGetEquip_Click(object sender, RoutedEventArgs e)
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
var equipCode = lba_ThrifTtitle3.Text;
var data = dbContext.BaseEquip.Where(t => t.useFlag == 1 && t.equipNo.Contains(equipCode)).ToList();
this.LoadMaterial3.ItemsSource = null;
this.LoadMaterial3.ItemsSource = data;
this.LoadMaterial3.Items.Refresh();
if (data.Count == 0)
{
MessageBox.Show("未查询到数据");
}
}
/// <summary>
/// 查询库存信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGetStocK_Click(object sender, RoutedEventArgs e)
{
Pagination1.PageIndex = 1;
this.EquipListSelectDy.SelectedIndex = -1;
GetBaseLocations();
}
/// <summary>
/// 获取库位信息
/// </summary>
private void GetBaseLocations()
{
if (WarehourseId.SelectedItem is WmsBaseWarehouse warehouse)
{
var scope = _host.Services.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
var locationCode = lba_ThrifTtitle6.Text;
long materialId = 0;
try
{
materialId = Convert.ToInt64(lba_ThrifTtitle5.Text);
}
catch
{
}
int sum = dbContext.WmsRawStock
.WhereIf(!string.IsNullOrEmpty(lba_ThrifTtitle6.Text), t => t.locationCode.Contains(locationCode))
.WhereIf(!string.IsNullOrEmpty(lba_ThrifTtitle5.Text), t => t.materialId == materialId)
.Where(t => t.warehouseId == warehouse.warehouseId)
.Count();
Pagination1.MaxPageCount = (sum / pageCount) + (sum % pageCount == 0 ? 0 : 1);
var data = dbContext.WmsRawStock
.WhereIf(!string.IsNullOrEmpty(lba_ThrifTtitle6.Text), t => t.locationCode.Contains(locationCode))
.WhereIf(!string.IsNullOrEmpty(lba_ThrifTtitle5.Text), t => t.materialId == materialId)
.Where(t => t.warehouseId == warehouse.warehouseId)
.Skip((Pagination1.PageIndex - 1) * pageCount)
.Take(pageCount)
.ToList();
this.LoadMaterial1.ItemsSource = null;
this.LoadMaterial1.ItemsSource = data;
this.LoadMaterial1.Items.Refresh();
if (data.Count == 0)
{
MessageBox.Show("未查询到数据");
}
}
else
{
MessageBox.Show("请选择仓库!");
return;
}
}
///// <summary>
///// 查询指令信息
///// </summary>
///// <param name="sender"></param>
///// <param name="e"></param>
//private void btnGetWcsCmd_Click(object sender, RoutedEventArgs e)
//{
// GetWcsCmd();
//}
//private void GetWcsCmd()
//{
// using var scope = _host.Services.CreateScope();
// using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
// var equipCode = txtEquipNo.Text;
// var ContainNo = txtContainNo.Text;
// var data = dbContext.WcsCmd.Where(t => t.useFlag == 1 && t.containerNo.Contains(ContainNo) && t.equipmentNo.Contains(equipCode)).ToList();
// this.LoadMaterial.ItemsSource = null;
// this.LoadMaterial.ItemsSource = data;
// this.LoadMaterial.Items.Refresh();
// if (data.Count == 0)
// {
// MessageBox.Show("未查询到数据");
// }
//}
///// <summary>
///// 删除指令
///// </summary>
///// <param name="sender"></param>
///// <param name="e"></param>
//private void btnDelCmd_Click(object sender, RoutedEventArgs e)
//{
// if (LoadMaterial.SelectedIndex == -1)
// {
// MessageBox.Show("请选择要删除的指令!");
// }
// else
// {
// var Currentselected = LoadMaterial.SelectedItem as WcsCmd;
// using var scope = _host.Services.CreateScope();
// using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
// Currentselected.useFlag = 0;
// dbContext.WcsCmd.Update(Currentselected);
// dbContext.SaveChanges();
// GetWcsCmd();
// }
//}
///// <summary>
///// 重发指令
///// </summary>
///// <param name="sender"></param>
///// <param name="e"></param>
//private void btnReSendCmd_Click(object sender, RoutedEventArgs e)
//{
// if (LoadMaterial.SelectedIndex == -1)
// {
// MessageBox.Show("请选择要重发的指令!");
// }
// else
// {
// var Currentselected = LoadMaterial.SelectedItem as WcsCmd;
// using var scope = _host.Services.CreateScope();
// using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
// Currentselected.sendFlag = 0;
// dbContext.WcsCmd.Update(Currentselected);
// dbContext.SaveChanges();
// GetWcsCmd();
// }
//}
///// <summary>
///// 呼叫料箱
///// </summary>
///// <param name="sender"></param>
///// <param name="e"></param>
//private void btnCallBox_Click(object sender, RoutedEventArgs e)
//{
// try
// {
// if (!string.IsNullOrEmpty(callMaterialId.Text))
// {
// using var scope = _host.Services.CreateScope();
// using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
// if (int.TryParse(txtNum.Text, out int num))
// {
// WmsRawOutstock wmsRawOutstock = new WmsRawOutstock()
// {
// rawOutstockId = _jcSnowId.NextId(),
// taskCode = "task",
// warehouseId = 512,
// materialId = Convert.ToInt32(callMaterialId.Text),
// outstockAmount = Convert.ToInt32(txtNum.Text),
// realOutstockAmount = 0,
// endStationCode = "FL052",
// auditStatus = "0",
// operationType = "1",
// taskType = "1",
// executeStatus = "0"
// };
// dbContext.Add(wmsRawOutstock);
// dbContext.SaveChanges();
// MessageBox.Show("料箱呼叫任务添加成功!");
// }
// else
// {
// MessageBox.Show("请输入有效的数字");
// }
// }
// }
// catch
// {
// }
//}
/// <summary>
/// 料箱扫描
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
//private void txtBox_KeyDown(object sender, KeyEventArgs e)
//{
// if (e.Key == Key.Enter)
// {
// txtBarCode.Focus();
// }
//}
///// <summary>
///// 条码扫描
///// </summary>
///// <param name="sender"></param>
///// <param name="e"></param>
//private void txtBarCode_KeyDown(object sender, RoutedEventArgs e)
//{
// try
// {
// if (!string.IsNullOrEmpty(txtBarCode.Text))
// {
// if (!(barcodeLsit.Where(t => t.barcodeInfo == txtBarCode.Text).Count() > 0))
// {
// using var scope = _host.Services.CreateScope();
// using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
// var mesInfo = dbContext.MesBaseBarcodeInfo.Where(t => t.barcodeInfo == txtBarCode.Text).FirstOrDefault();
// if (mesInfo == null)
// {
// MessageBox.Show("请扫描正确的条码");
// return;
// }
// barcodeLsit.Add(mesInfo);
// txtBarCode.SelectAll();
// txtBarCode.Focus();
// txtScan.Text = $"{(mesInfo.batchFlag == "1" ? 1 : mesInfo.amount)}";
// }
// }
// }
// catch
// {
// }
//}
/// <summary>
/// 确认
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnConfirmBox_Click(object sender, RoutedEventArgs e)
{
//try
//{
// var box = txtBox.Text;
// if (string.IsNullOrEmpty(box))
// {
// MessageBox.Show("容器号不能为空!");
// return;
// }
// if (barcodeLsit.Count == 0)
// {
// MessageBox.Show("未扫描条码!");
// return;
// }
// using var scope = _host.Services.CreateScope();
// using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
// WmsBaseLocation? wmsBaseLocation = dbContext.WmsBaseLocation.Where(t => t.warehouseId == 512 && t.containerCode == box).FirstOrDefault();
// if (wmsBaseLocation == null)
// {
// MessageBox.Show("容器号不存在!,请重新扫描!");
// return;
// }
// if (cbxType.Text == "入库")
// {
// var batchCodes = barcodeLsit.Select(t => t.batchCode);
// List<WmsRawStock> wmsRawStocks = dbContext.WmsRawStock
// .Where(t => batchCodes.Contains(t.instockBatch))
// .Where(t => t.warehouseFloor == 5 && t.warehouseId == 512).ToList();
// if (wmsRawStocks.Count > 0)
// {
// MessageBox.Show("该批次已入库!");
// return;
// }
// foreach (var item in barcodeLsit)
// {
// WmsRawStock? wmsRawStock = dbContext.WmsRawStock
// .Where(t => t.palletInfoCode == box)
// .Where(t => t.instockBatch == item.batchCode).FirstOrDefault();
// if (wmsRawStock == null)
// {
// var newRawStock = new WmsRawStock()
// {
// materialId = item.materialId,
// supplierId = item.manufacturerId,
// instockBatch = item.batchCode,
// locationCode = wmsBaseLocation.locationCode,
// stockType = "1",
// palletInfoCode = box,
// totalAmount = item.batchFlag == "1" ? 1 : item.amount,
// activeFlag = "1",
// occupyAmount = 0,
// completeFlag = "1",
// frozenAmount = 0,
// instockDate = System.DateTime.Now,
// rawStockId = Global.SnowId.NextId(),
// saleOrderId = item.saleOrderId,
// warehouseFloor = 5,
// warehouseId = 512,
// createBy = "扫描入库",
// createDate = System.DateTime.Now
// };
// dbContext.WmsRawStock.Add(newRawStock);
// }
// else
// {
// MessageBox.Show("该批次已入库!");
// return;
// }
// }
// dbContext.SaveChanges();
// MessageBox.Show("入库成功!");
// }
// if (cbxType.Text == "出库")
// {
// long? materialId = barcodeLsit.FirstOrDefault().materialId;
// WmsRawOutstock? wmsRawOutstock = dbContext.WmsRawOutstock
// .Where(t => t.auditStatus == "1")
// .Where(t => t.executeStatus == "1")
// .Where(t => t.warehouseId == 512)
// .Where(t => t.realOutstockAmount < t.outstockAmount)
// .Where(t => t.materialId == materialId)
// .FirstOrDefault();
// if (wmsRawOutstock != null)
// {
// //从料箱里拿出来
// WmsRawStock? wmsRawStock = dbContext.WmsRawStock.Where(t => t.palletInfoCode == box).Where(t => t.materialId == materialId).FirstOrDefault();
// if (wmsRawStock != null)
// {
// dbContext.WmsRawStock.Where(t => t.rawStockId == wmsRawStock.rawStockId).Delete();
// wmsRawOutstock.realOutstockAmount += 1;
// if (wmsRawOutstock.realOutstockAmount >= wmsRawOutstock.outstockAmount)
// {
// wmsRawOutstock.executeStatus = "2";
// wmsRawOutstock.endTime = DateTime.Now;
// }
// dbContext.Update(wmsRawOutstock);
// dbContext.SaveChanges();
// }
// else
// {
// MessageBox.Show("当前容器不存在当前扫描物料");
// }
// }
// }
// if (cbxType.Text == "回库")
// {
// var batchCodes = barcodeLsit.Select(t => t.batchCode);
// List<WmsRawStock> wmsRawStocks = dbContext.WmsRawStock
// .Where(t => batchCodes.Contains(t.instockBatch))
// .Where(t => t.warehouseFloor == 5 && t.warehouseId == 512).ToList();
// if (wmsRawStocks.Count > 0)
// {
// MessageBox.Show("该批次已入库!");
// return;
// }
// foreach (var item in barcodeLsit)
// {
// WmsRawStock? wmsRawStock = dbContext.WmsRawStock
// .Where(t => t.palletInfoCode == box)
// .Where(t => t.instockBatch == item.batchCode).FirstOrDefault();
// if (wmsRawStock == null)
// {
// var newRawStock = new WmsRawStock()
// {
// materialId = item.materialId,
// supplierId = item.manufacturerId,
// instockBatch = item.batchCode,
// locationCode = wmsBaseLocation.locationCode,
// stockType = "1",
// palletInfoCode = box,
// totalAmount = item.batchFlag == "1" ? 1 : item.amount,
// activeFlag = "1",
// occupyAmount = 0,
// completeFlag = "1",
// frozenAmount = 0,
// instockDate = System.DateTime.Now,
// rawStockId = Global.SnowId.NextId(),
// saleOrderId = item.saleOrderId,
// warehouseFloor = 5,
// warehouseId = 512,
// createBy = "扫描回库",
// createDate = System.DateTime.Now
// };
// dbContext.WmsRawStock.Add(newRawStock);
// }
// else
// {
// MessageBox.Show("该批次已入库!");
// return;
// }
// }
// dbContext.SaveChanges();
// MessageBox.Show("回库成功!");
// }
// barcodeLsit.Clear();
// txtScan.Text = string.Empty;
// txtBarCode.Text = string.Empty;
//}
//catch
//{
//}
}
/// <summary>
/// 退出程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void WindowClose_Click(object sender, RoutedEventArgs e)
{
if (MessageBox.Show("是否确认关闭程序", "提示信息", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
{
System.Environment.Exit(System.Environment.ExitCode);
}
}
/// <summary>
/// 获取聚焦打开键盘
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TextBoxGetFocus()
{
try
{
Process process = new Process()
{
StartInfo ={
UseShellExecute=true,
FileName="osk.exe"
}
};
process.Start();
}
catch (Exception ex)
{
MessageBox.Show("打开软键盘时出错: " + ex.Message);
}
}
/// <summary>
/// 失去焦点,关闭软键盘
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TextBoxLostFocus(object sender, RoutedEventArgs e)
{
try
{
//将打开的osk.exe进程关闭
Process[] processes = Process.GetProcessesByName("osk");
foreach (Process process in processes)
{
process.Kill();
}
}
catch (Exception ex)
{
MessageBox.Show("打开软键盘时出错: " + ex.Message);
}
}
/// <summary>
/// 背负式小车确认完成
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnBearConfirmBox_Click(object sender, RoutedEventArgs e)
{
try
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
var wcsCmd = dbContext.WcsCmd
.Where(t => t.cmdStatus == 6)
.Where(t => t.nextPointId == 21)
.FirstOrDefault();
if (wcsCmd == null)
{
MessageBox.Show("未查询到当前有背负式Agv由装配区到捡料区任务");
}
else
{
dbContext.WcsCmd.Where(t => t.objid == wcsCmd.objid).Update(t => new WcsCmd { cmdStatus = 7 });
dbContext.WcsCmdLog.Where(t => t.objid == wcsCmd.objid).Update(t => new WcsCmdLog { cmdStatus = 7 });
dbContext.WcsTask.Where(t => t.objid == wcsCmd.taskId).Update(t => new WcsTask { taskStatus = 7 });
dbContext.WcsTaskLog.Where(t => t.objid == wcsCmd.taskId).Update(t => new WcsTaskLog { taskStatus = 7 });
MessageBox.Show("背负式Agv任务确认完成成功");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 获取到所有控件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
/// <summary>
/// 添加任务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AddTaskButton_Click(object sender, RoutedEventArgs e)
{
AddTask addTask = new AddTask(_host);
addTask.ShowDialog();
}
///// <summary>
///// 清除RFID
///// </summary>
///// <param name="sender"></param>
///// <param name="e"></param>
//private void clearRFID_Click(object sender, RoutedEventArgs e)
//{
// txtBox.Text = string.Empty;
//}
///// <summary>
///// 清除条码
///// </summary>
///// <param name="sender"></param>
///// <param name="e"></param>
//private void clearBarCode_Click(object sender, RoutedEventArgs e)
//{
// txtScan.Text = string.Empty;
// barcodeLsit.Clear();
// txtBarCode.Text = string.Empty;
//}
///// <summary>
///// 人工叫料失去焦点
///// </summary>
///// <param name="sender"></param>
///// <param name="e"></param>
//private void callMaterialId_LostFocus(object sender, RoutedEventArgs e)
//{
// try
// {
// if (!string.IsNullOrEmpty(callMaterialId.Text))
// {
// using var scope = _host.Services.CreateScope();
// using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
// var material = dbContext.WmsRawStock.Where(t => t.instockBatch == callMaterialId.Text).FirstOrDefault();
// if (material == null)
// {
// MessageBox.Show("批次号不存在!");
// callMaterialId.Text = string.Empty;
// }
// else
// {
// }
// }
// }
// catch
// {
// }
//}
/// <summary>
/// 开始任务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnEditTask_Click(object sender, RoutedEventArgs e)
{
try
{
if (LoadMaterial0.SelectedIndex == -1)
{
MessageBox.Show("请选择要开始的任务!");
}
else
{
var Currentselected = LoadMaterial0.SelectedItem as WcsTask;
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
List<WcsTask> wcsTasks = dbContext.WcsTask.ToList();
List<long> ids = new List<long>();
foreach (var item in SelectedItem)
{
if (item.Value == true)
{
WcsTask? wcsTask = wcsTasks.FirstOrDefault();
if (wcsTask != null && wcsTask.taskStatus <= 0)
{
ids.Add(item.Key);
}
}
}
dbContext.WcsTask.Where(t => ids.Contains(t.objid)).Update(t => new WcsTask { useFlag = 1 });
GetTask();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 单选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
if (sender is CheckBox checkbox && LoadMaterial0.SelectedItem is WcsTask Currentselected)
{
SelectedItem[Currentselected.objid] = checkbox.IsChecked ?? false;
}
}
/// <summary>
/// 单选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CheckPersonBox_Click(object sender, RoutedEventArgs e)
{
if (sender is CheckBox checkbox)
{
dynamic Currentselected = PersonCallMaterial.SelectedItem;
if (checkbox.IsChecked != null && checkbox.IsChecked.Value)
{
CallSelectedItem.Add(Currentselected.baseLocationId);
}
else
{
CallSelectedItem.Remove(Currentselected.baseLocationId);
}
}
}
/// <summary>
/// 刷新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RefreshPlc()
{
while (true)
{
try
{
var points = SystemData.BasePlcpoints.OrderBy(t => t.definefield3).ToList();
foreach (var item in points)
{
if (!string.IsNullOrEmpty(item.plcpointAddress))
{
TextBlock? textBlock = null;
Dispatcher.Invoke(() =>
{
textBlock = this.FindName(item.plcpointNo) as TextBlock;
});
if (textBlock != null)
{
var plc = SystemData.PlcDic.Values.Where(t => t.IP == item.definefield3).FirstOrDefault();
if (plc != null)
{
try
{
string? result = "";
if (item.plcpointNo.Contains("RFID"))
{
result = plc.ReadRFID(item.plcpointAddress)?.ToString();
}
else
{
result = plc.Read(item.plcpointAddress)?.ToString();
}
Dispatcher.Invoke(() =>
{
textBlock.Text = result == null ? "" : result;
});
}
catch (Exception ex)
{
try
{
if (ex is PlcException)
{
foreach (var item2 in SystemData.PlcDic)
{
if (item2.Value.IP == ex.Message)
{
SystemData.PlcDic[item2.Key] = new Plc.S7.Plc(item2.Value.CPU, item2.Value.IP, item2.Value.Port, item2.Value.Slot, item2.Value.Rack);
SystemData.PlcDic[item2.Key].Open();
break;
}
}
}
}
catch
{
points.RemoveAll(t => t.definefield3 == ex.Message);
}
}
}
}
}
}
}
catch
{
}
Thread.Sleep(3000);
}
}
/// <summary>
/// 开始任务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCtuInConfirmBox_Click(object sender, RoutedEventArgs e)
{
try
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
WcsTask? wcsTask = dbContext.WcsTask.FirstOrDefault(t => t.nextPointId == 11 && t.taskType == 29);
if (wcsTask == null)
{
MessageBox.Show("未查询到当前有CTU入库任务!");
}
else
{
wcsTask.CTUExecute = "1";
dbContext.WcsTask.Update(wcsTask);
dbContext.SaveChanges();
MessageBox.Show("CTU入库任务确认完成成功!");
}
}
catch
{
}
}
/// <summary>
/// 页码改变时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Pagination_PageUpdated(object sender, HandyControl.Data.FunctionEventArgs<int> e)
{
GetBaseLocations();
}
/// <summary>
/// 物料名称
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MaterialName_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//if(MaterialName.SelectedItem is MesBaseMaterialInfo Currentselected)
//{
// MaterialSpec.ItemsSource = SystemData.MesBaseMaterialInfo.Where(t => t.MaterialName == Currentselected.MaterialName);
//}
//else
//{
// MaterialSpec.ItemsSource = null;
//}
//MaterialSpec.Items.Refresh();
}
/// <summary>
/// 入库扫描
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void InRawBaseLocaltion_Click(object sender, RoutedEventArgs e)
{
try
{
if (!string.IsNullOrEmpty(txtInBox.Text) && !string.IsNullOrEmpty(txtInBarCode.Text))
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
var material = dbContext.MesBaseBarcodeInfo.FirstOrDefault(t => t.barcodeInfo == txtInBarCode.Text);
var container = dbContext.WmsBaseLocation.FirstOrDefault(t => t.containerCode == txtInBox.Text && t.warehouseId == 512);
List<WmsRawStock> wmsRawStocks = dbContext.WmsRawStock
.Where(t => txtInBarCode.Text == t.instockBatch)
.Where(t => t.warehouseFloor == 5 && t.warehouseId == 512).ToList();
if (wmsRawStocks.Count > 0)
{
MessageBox.Show($"该条码已入库!料箱号为{wmsRawStocks.First().palletInfoCode},库位号为{wmsRawStocks.First().locationCode}");
txtInBarCode.Text = string.Empty;
txtInScan.Text = string.Empty;
return;
}
//20240708142603RB014
var newRawStock = new WmsRawStock()
{
materialId = material.materialId,
supplierId = material.manufacturerId,
instockBatch = txtInBarCode.Text,
locationCode = container.locationCode,
stockType = "1",
palletInfoCode = container.containerCode,
totalAmount = material.batchFlag == "1" ? material.amount : 1,
activeFlag = "1",
occupyAmount = 0,
completeFlag = "1",
frozenAmount = 0,
instockDate = System.DateTime.Now,
rawStockId = Global.SnowId.NextId(),
saleOrderId = material.saleOrderId,
warehouseFloor = 5,
warehouseId = 512,
createBy = "扫描入库",
createDate = System.DateTime.Now,
safeFlag = "1",
};
dbContext.WmsRawStock.Add(newRawStock);
dbContext.SaveChanges();
MessageBox.Show("入库成功!");
txtInBarCode.Text = string.Empty;
txtInScan.Text = string.Empty;
txtInBarCode.Focus();
}
else
{
MessageBox.Show("请先扫描容器号和条码!");
}
}
catch (Exception ex)
{
LogManager.Error(ex);
MessageBox.Show("入库失败");
}
}
/// <summary>
/// 入库容器改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtInBox_TextChanged(object sender, RoutedEventArgs e)
{
try
{
string containerCode = txtInBox.Text;
if (!string.IsNullOrEmpty(containerCode))
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
var container = dbContext.WmsBaseLocation.FirstOrDefault(t => t.containerCode == containerCode && t.warehouseId == 512);
if (container == null)
{
MessageBox.Show($"未查询到该容器{txtInBox.Text}!");
txtInBox.Text = string.Empty;
txtInBox.Focus();
}
else
{
txtInBarCode.Focus();
}
}
}
catch
{
}
}
/// <summary>
/// 入库条码改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtInBarCode_TextChanged(object sender, RoutedEventArgs e)
{
try
{
string barCode = txtInBarCode.Text;
if (!string.IsNullOrEmpty(barCode))
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
var material = dbContext.MesBaseBarcodeInfo.FirstOrDefault(t => t.barcodeInfo == barCode);
if (material == null)
{
MessageBox.Show("未查询到该条码内容!");
txtInBarCode.Text = string.Empty;
txtInScan.Text = string.Empty;
txtInBarCode.Focus();
}
else
{
var mesBaseMaterialInfo = dbContext.MesBaseMaterialInfo.FirstOrDefault(t => t.MaterialId == material.materialId);
if (mesBaseMaterialInfo == null)
{
MessageBox.Show("未查询到该条码绑定的物料信息!");
txtInBarCode.Text = string.Empty;
txtInScan.Text = string.Empty;
txtInBarCode.Focus();
}
else
{
txtInScan.Text = (material.batchFlag == "1" ? material.amount : 1).ToString();
if (mesBaseMaterialInfo.BatchFlag == 0)
{
txtInScan.IsReadOnly = true;
}
else
{
txtInScan.IsReadOnly = true;
}
}
}
}
}
catch
{
}
}
/// <summary>
/// 出库容器改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtOutBox_TextChanged(object sender, RoutedEventArgs e)
{
try
{
string containerCode = txtOutBox.Text;
if (!string.IsNullOrEmpty(containerCode))
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
var container = dbContext.WmsBaseLocation.FirstOrDefault(t => t.containerCode == containerCode && t.warehouseId == 512);
if (container == null)
{
MessageBox.Show($"未查询到该容器{txtOutBox.Text}!");
txtOutBox.Text = string.Empty;
txtOutBox.Focus();
}
else
{
txtOutBarCode.Focus();
}
}
}
catch
{
}
}
/// <summary>
/// 出库条码改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtOutBarCode_TextChanged(object sender, RoutedEventArgs e)
{
try
{
string barCode = txtOutBarCode.Text;
string containerCode = txtOutBox.Text;
if (!string.IsNullOrEmpty(containerCode))
{
if (!string.IsNullOrEmpty(barCode))
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
var wmsRawStock = dbContext.WmsRawStock.FirstOrDefault(t => t.instockBatch == barCode && t.palletInfoCode == containerCode);
if (wmsRawStock == null)
{
MessageBox.Show("未在该容器中查询到该条码!");
txtOutBarCode.Text = string.Empty;
txtOutScan.Text = string.Empty;
txtOutBarCode.Focus();
}
else
{
var mesBaseMaterialInfo = dbContext.MesBaseBarcodeInfo.FirstOrDefault(t => t.materialId == wmsRawStock.materialId);
if (mesBaseMaterialInfo == null)
{
MessageBox.Show("未查询到该条码绑定的物料信息!");
txtOutBarCode.Text = string.Empty;
txtOutScan.Text = string.Empty;
txtOutBarCode.Focus();
}
else
{
txtOutScan.Text = (mesBaseMaterialInfo.batchFlag == "1" ? (wmsRawStock.totalAmount - wmsRawStock.frozenAmount) : 1).ToString();
if (mesBaseMaterialInfo.batchFlag == "1")
{
txtOutScan.IsReadOnly = false;
}
else
{
txtOutScan.IsReadOnly = true;
}
}
}
}
}
else
{
if (!string.IsNullOrEmpty(barCode))
{
MessageBox.Show("请先扫描容器号!");
txtOutBarCode.Text = string.Empty;
txtOutScan.Text = string.Empty;
}
}
}
catch
{
}
}
/// <summary>
/// 出库扫描
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OutRawBaseLocaltion_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(txtOutBox.Text) && !string.IsNullOrEmpty(txtOutBarCode.Text))
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
var wmsRawStock = dbContext.WmsRawStock
.FirstOrDefault(t => t.instockBatch == txtOutBarCode.Text && t.palletInfoCode == txtOutBox.Text);
if (wmsRawStock == null)
{
MessageBox.Show("库存已经被清空!");
return;
}
int outScan = Convert.ToInt32(this.txtOutScan.Text);
if (outScan != 1 && outScan > wmsRawStock.totalAmount - wmsRawStock.frozenAmount)
{
MessageBox.Show("出库数量不能大于库存数量!");
return;
}
var wcsTask = dbContext.WcsTask.Where(t => t.containerNo == txtOutBox.Text).FirstOrDefault();
if (wcsTask == null)
{
MessageBox.Show("出库失败非CTU出库任务无法出库!");
return;
}
else
{
WmsRawOutstock? wmsRawOutstock = dbContext.WmsRawOutstock.FirstOrDefault(t => t.rawOutstockId == wcsTask.orderId);
if (wmsRawOutstock != null)
{
wmsRawOutstock.realOutstockAmount += outScan;
if (wmsRawOutstock.outstockAmount == wmsRawOutstock.realOutstockAmount)
{
wmsRawOutstock.executeStatus = "2";
}
wmsRawStock.totalAmount -= outScan;
if (wmsRawStock.totalAmount == 0)
{
dbContext.WmsRawStock.Where(t => t.rawStockId == wmsRawStock.rawStockId).Delete();
dbContext.Update(wmsRawOutstock);
dbContext.SaveChanges();
}
else
{
dbContext.WmsRawStock.Update(wmsRawStock);
dbContext.Update(wmsRawOutstock);
dbContext.SaveChanges();
}
}
else
{
wmsRawStock.totalAmount -= outScan;
if (wmsRawStock.totalAmount == 0)
{
dbContext.WmsRawStock.Where(t => t.rawStockId == wmsRawStock.rawStockId).Delete();
}
else
{
dbContext.WmsRawStock.Update(wmsRawStock);
dbContext.SaveChanges();
}
}
MessageBox.Show("出库成功!");
txtOutScan.Text = string.Empty;
txtOutBarCode.Text = string.Empty;
txtOutBarCode.Focus();
}
}
else
{
MessageBox.Show("请先扫描容器号和条码!");
}
}
/// <summary>
/// 搜索
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SelectButton_Click(object sender, RoutedEventArgs e)
{
CallSelectedItem.Clear();
GetPersonCallMaterialData();
}
/// <summary>
/// 获取人员呼叫的物料数据
/// </summary>
private void GetPersonCallMaterialData()
{
try
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
List<long?> materialIds = new List<long?>();
List<MesBaseMaterialInfo> materialInfos = new List<MesBaseMaterialInfo>();
if (!string.IsNullOrEmpty(MaterialName.Text))
{
materialInfos = dbContext.MesBaseMaterialInfo.Where(t => t.MaterialName.Contains(MaterialName.Text)).ToList();
materialIds = materialInfos.Select(t => t.MaterialId).ToList();
}
var wmsRawStocks = dbContext.WmsRawStock
.Where(t => t.warehouseId == 512)
.WhereIf(materialIds.Count > 0, t => materialIds.Contains(t.materialId))
.ToList();
var groupWmsRawStock = wmsRawStocks.GroupBy(t => new { t.materialId, t.palletInfoCode }).Select(t =>
new
{
materialId = t.Key.materialId,
palletInfoCode = t.Key.palletInfoCode,
totalAmount = t.Sum(x => x.totalAmount),
frozenAmount = t.Sum(x => x.frozenAmount),
});
//.GroupBy(t => t.palletInfoCode)
//.Select(t => new
//{
// palletInfoCode = t.Key,
// totalAmount = string.Join("\n", t.Select(x => x.totalAmount)),
// frozenAmount = string.Join("\n", t.Select(x => x.frozenAmount)),
// materialId = string.Join("\n", t.Select(x => x.materialId)),
//}).ToList();
var palletInfoCodes = wmsRawStocks.Select(t => t.palletInfoCode).Distinct().ToList();
var wmsBaseLocations = dbContext.WmsBaseLocation
.Where(t => t.ContainerStatus == "1")
.Where(t => t.warehouseId == 512)
.Where(t => palletInfoCodes.Contains(t.containerCode)).ToList();
if (string.IsNullOrEmpty(MaterialName.Text))
{
materialInfos = dbContext.MesBaseMaterialInfo.ToList();
}
var bill = from a in wmsBaseLocations
from b in groupWmsRawStock
from c in materialInfos
where a.containerCode == b.palletInfoCode && b.materialId == c.MaterialId
select new
{
baseLocationId = a.locationId,
//wmsRawStocksId = b.rawStockId,
locationCode = a.locationCode,
materialId = b.materialId,
materialName = c.MaterialName,
materialSpec = c.MaterialSpec,
palletInfoCode = b.palletInfoCode,
//instockDate = b.instockDate,
totalAmount = b.totalAmount,
frozenAmount = b.frozenAmount,
IsSelected = CallSelectedItem.Contains(a.locationId)
};
var bills = bill.GroupBy(t => new { t.baseLocationId, t.palletInfoCode, t.locationCode, t.IsSelected })
.Select(t => new
{
baseLocationId = t.Key.baseLocationId,
palletInfoCode = t.Key.palletInfoCode,
locationCode = t.Key.locationCode,
IsSelected = t.Key.IsSelected,
totalAmount = string.Join("\n", t.Select(x => x.totalAmount)),
frozenAmount = string.Join("\n", t.Select(x => x.frozenAmount)),
materialId = string.Join("\n", t.Select(x => x.materialId)),
materialName = string.Join("\n", t.Select(x => x.materialName[..(x.materialName.Length > 20 ? 20 : x.materialName.Length)])),
materialNameSrc = string.Join("\n", t.Select(x => x.materialName)),
materialSpec = string.Join("\n", t.Select(x => x.materialSpec[..(x.materialSpec.Length > 20 ? 20 : x.materialSpec.Length)])),
materialSpecSrc = string.Join("\n", t.Select(x => x.materialSpec))
}).ToList();
bills = bills.OrderBy(t => t.palletInfoCode).ThenBy(t => t.materialId).ToList();
PersonCallMaterial.ItemsSource = bills.Skip((this.Pagination2.PageIndex - 1) * this.pageCount).Take(this.pageCount);
int sum = bills.Count();
Pagination2.MaxPageCount = (sum / pageCount) + (sum % pageCount == 0 ? 0 : 1);
if (bills.Count == 0)
{
MessageBox.Show("未查询到数据");
}
}
catch
{
}
}
/// <summary>
/// 页码改变时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Pagination2_PageUpdated(object sender, HandyControl.Data.FunctionEventArgs<int> e)
{
GetPersonCallMaterialData();
}
/// <summary>
/// 叫空料箱
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CallEmptyBtn_Click(object sender, RoutedEventArgs e)
{
try
{
EmptyCountForm emptyCountForm = new EmptyCountForm(1, 6);
emptyCountForm.ShowDialog();
if (EmptyCountForm.IsSelect)
{
var baseEquip = SystemData.BaseEquip.First(t => t.objid == 11);
BaseEquip endEquip = SystemData.BaseEquip.First(t => t.objid == 20);
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
//int taskCount = dbContext.WcsTask.Where(t => t.taskType == 30).Count();
//if (taskCount >= 6)
//{
// HandyControl.Controls.MessageBox.Show("当前CTU入库任务已满6个,请等待任务完成后再叫空箱!");
// return;
//}
var wmsBaseLocation = dbContext.WmsBaseLocation
.Where(t => t.locationStatus == "1")
.Where(t => t.instockFlag == "0")
.Where(t => t.outstockFlag == "0")
.Where(t => t.warehouseId == 512 && t.ContainerStatus == "1").ToList();
var wmsRawStock = dbContext.WmsRawStock.Where(t => t.warehouseId == 512).ToList();
List<string> palletInfoCodes = wmsRawStock.Select(t => t.palletInfoCode).ToList();
wmsBaseLocation.RemoveAll(t => palletInfoCodes.Contains(t.containerCode));
if (wmsBaseLocation.Count > 0 && EmptyCountForm.EmptyTaskCount <= wmsBaseLocation.Count)
{
List<WcsTaskManual> addList = new List<WcsTaskManual>();
for (int i = 0; i < EmptyCountForm.EmptyTaskCount; i++)
{
WmsBaseLocation startBaseLocation = wmsBaseLocation[i];
startBaseLocation.outstockFlag = "1";
startBaseLocation.locationStatus = "6";
startBaseLocation.ContainerStatus = "2";
addList.Add(new WcsTaskManual()
{
objid = Global.SnowId.NextId(),
taskType = 30,
containerNo = startBaseLocation.containerCode,
startPointId = startBaseLocation.locationId,
startPointNo = startBaseLocation.locationCode,
currPointId = startBaseLocation.locationId,
currPointNo = startBaseLocation.locationCode,
nextPointId = baseEquip.objid,
nextPointNo = baseEquip.equipNo,
endPointId = endEquip.objid,
endPointNo = endEquip.equipNo,
createTime = DateTime.Now,
ud1 = startBaseLocation.locColumn,
taskStatus = 0,
remark = "CTU空库任务",
floorNo = 5,
fromFloorNo = 5,
useFlag = 1
});
dbContext.Update(startBaseLocation);
}
dbContext.AddRange(addList);
dbContext.SaveChanges();
MessageBox.Show("任务创建成功!");
}
else
{
MessageBox.Show($"没有足够的空箱库位库位,剩余空库位数量为{wmsBaseLocation.Count}!");
}
}
}
catch
{
}
}
/// <summary>
/// 叫托盘
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CallTrayBtn_Click(object sender, RoutedEventArgs e)
{
try
{
BaseEquip ctuEquip = SystemData.BaseEquip.First(t => t.objid == 11);
BaseEquip endEquip = SystemData.BaseEquip.First(t => t.objid == 20);
if (CallSelectedItem.Count == 0)
{
MessageBox.Show("请选择料箱!");
return;
}
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
//int taskCount = dbContext.WcsTask.Where(t => t.nextPointId == 11).Count();
//if (taskCount + ids.Count > 6)
//{
// MessageBox.Show($"当前CTU只能有6个任务,当前任务数{taskCount}");
// return;
//}
var wmsBaseLocation = dbContext.WmsBaseLocation.Where(t => t.warehouseId == 512 && t.ContainerStatus == "1").Where(t => CallSelectedItem.Contains(t.locationId)).ToList();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("确认出库:");
foreach (var item in wmsBaseLocation)
{
stringBuilder.AppendLine($"库位号{item.locationCode},料箱号{item.containerCode}.");
}
MessageBoxResult messageBoxResult = MessageBox.Show(stringBuilder.ToString(), "提示", MessageBoxButton.YesNo);
if (messageBoxResult == MessageBoxResult.Yes)
{
List<WcsTaskManual> addList = new List<WcsTaskManual>();
foreach (var item in wmsBaseLocation)
{
addList.Add(new WcsTaskManual()
{
objid = Global.SnowId.NextId(),
taskType = 30,
containerNo = item.containerCode,
startPointId = item.locationId,
startPointNo = item.locationCode,
currPointId = item.locationId,
currPointNo = item.locationCode,
nextPointId = ctuEquip.objid,
nextPointNo = ctuEquip.equipNo,
endPointId = endEquip.objid,
endPointNo = endEquip.equipNo,
createTime = DateTime.Now,
ud1 = item.locColumn,
taskStatus = 0,
remark = "CTU托盘任务",
floorNo = 5,
fromFloorNo = 5,
useFlag = 1
});
item.outstockFlag = "1";
item.ContainerStatus = "2";
item.locationStatus = "6";
dbContext.Update(item);
}
dbContext.AddRange(addList);
dbContext.SaveChanges();
MessageBox.Show("创建出库任务成功!");
}
CallSelectedItem.Clear();
GetPersonCallMaterialData();
}
catch
{
}
}
/// <summary>
/// 执行CTU任务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ExecuteCtuTask_Click(object sender, RoutedEventArgs e)
{
try
{
if (int.TryParse(outTaskCount.Text, out int count))
{
if (count > 0)
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
var tasks = dbContext.WcsTask.Where(t => t.taskType == 30 && t.taskStatus == 0 && t.useFlag == 0).ToList();
foreach (var task in tasks)
{
task.taskStatus = 0;
task.useFlag = 1;
dbContext.Update(task);
dbContext.SaveChanges();
}
MessageBox.Show("任务执行成功!");
}
else
{
MessageBox.Show("没有可执行的任务!");
}
}
}
catch
{
}
}
private void callPlc_Click(object sender, RoutedEventArgs e)
{
try
{
BasePlcpoint basePlcpoint = SystemData.BasePlcpoints.First(t => t.plcpointNo == "canReceive");
SystemData.PlcDic[1].WriteToPoint(basePlcpoint.plcpointAddress, "1", basePlcpoint.plcpointLength.ToString());
MessageBox.Show("一键收料发送成功");
}
catch (Exception ex)
{
try
{
if (ex is PlcException)
{
foreach (var item in SystemData.PlcDic)
{
if (item.Value.IP == ex.Message)
{
SystemData.PlcDic[item.Key] = new Plc.S7.Plc(item.Value.CPU, item.Value.IP, item.Value.Port, item.Value.Slot, item.Value.Rack);
SystemData.PlcDic[item.Key].Open();
BasePlcpoint basePlcpoint = SystemData.BasePlcpoints.First(t => t.plcpointNo == "canReceive");
SystemData.PlcDic[1].WriteToPoint(basePlcpoint.plcpointAddress, "1", basePlcpoint.plcpointLength.ToString());
MessageBox.Show("一键收料发送成功");
break;
}
}
}
}
catch
{
MessageBox.Show("PLC连接失败!");
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
TextBoxGetFocus();
}
#region 清除按钮
private void barClear_Click(object sender, RoutedEventArgs e)
{
txtInBarCode.Text = string.Empty;
}
private void boxOutClear_Click(object sender, RoutedEventArgs e)
{
txtOutBox.Text = string.Empty;
}
private void boxClear_Click(object sender, RoutedEventArgs e)
{
txtInBox.Text = string.Empty;
}
private void barOutClear_Click(object sender, RoutedEventArgs e)
{
txtOutBarCode.Text = string.Empty;
}
#endregion
#region 托盘库进出
private void OneIn_Click(object sender, RoutedEventArgs e)
{
try
{
BasePlcpoint lineSignal = SystemData.BasePlcpoints.First(t => t.plcpointNo == "linesignal01");
var lineSignalValue = SystemData.PlcDic[0].Read(lineSignal.plcpointAddress);
if (lineSignalValue != null && Convert.ToInt32(lineSignalValue) == 1)
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
BaseEquip baseEquip = dbContext.BaseEquip.First(t => t.objid == 40);
BasePlcpoint basePlcpoint = SystemData.BasePlcpoints.First(t => t.plcpointNo == "oneIn");
SystemData.PlcDic[0].WriteToPoint(basePlcpoint.plcpointAddress, "1", basePlcpoint.plcpointLength.ToString());
baseEquip.emptyCount += 1;
dbContext.Update(baseEquip);
dbContext.SaveChanges();
MessageBox.Show("下发成功");
}
else
{
MessageBox.Show("未检测到托盘");
}
}
catch (Exception ex)
{
if (ex is PlcException)
{
foreach (var item in SystemData.PlcDic)
{
if (item.Value.IP == ex.Message)
{
SystemData.PlcDic[item.Key] = new Plc.S7.Plc(item.Value.CPU, item.Value.IP, item.Value.Port, item.Value.Rack, item.Value.Slot);
SystemData.PlcDic[item.Key].Open();
}
}
}
LogManager.Error(ex);
}
}
private void OneOut_Click(object sender, RoutedEventArgs e)
{
try
{
BasePlcpoint lineSignal = SystemData.BasePlcpoints.First(t => t.plcpointNo == "linesignal01");
var lineSignalValue = SystemData.PlcDic[0].Read(lineSignal.plcpointAddress);
if (lineSignalValue != null && Convert.ToInt32(lineSignalValue) == 0)
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
BaseEquip baseEquip = dbContext.BaseEquip.First(t => t.objid == 40);
BasePlcpoint basePlcpoint = SystemData.BasePlcpoints.First(t => t.plcpointNo == "oneOut");
SystemData.PlcDic[0].WriteToPoint(basePlcpoint.plcpointAddress, "1", basePlcpoint.plcpointLength.ToString());
baseEquip.emptyCount -= 1;
dbContext.Update(baseEquip);
dbContext.SaveChanges();
MessageBox.Show("下发成功");
}
else
{
MessageBox.Show("检测到外侧有托盘,无法出堆垛机");
}
}
catch (Exception ex)
{
if (ex is PlcException)
{
foreach (var item in SystemData.PlcDic)
{
if (item.Value.IP == ex.Message)
{
SystemData.PlcDic[item.Key] = new Plc.S7.Plc(item.Value.CPU, item.Value.IP, item.Value.Port, item.Value.Rack, item.Value.Slot);
SystemData.PlcDic[item.Key].Open();
}
}
}
LogManager.Error(ex);
}
}
private void SomeIn_Click(object sender, RoutedEventArgs e)
{
try
{
//BasePlcpoint lineSignal = SystemData.BasePlcpoints.First(t => t.plcpointNo == "linesignal01");
//var lineSignalValue = SystemData.PlcDic[0].Read(lineSignal.plcpointAddress);
//if (lineSignalValue != null && Convert.ToInt32(lineSignalValue) == 1)
{
EmptyCountForm emptyCount = new EmptyCountForm(1, 10);
emptyCount.ShowDialog();
if (EmptyCountForm.IsSelect)
{
int emptyTaskCount = EmptyCountForm.EmptyTaskCount;
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
BaseEquip baseEquip = dbContext.BaseEquip.First(t => t.objid == 40);
BasePlcpoint basePlcpoint = SystemData.BasePlcpoints.First(t => t.plcpointNo == "someIn");
SystemData.PlcDic[0].WriteToPoint(basePlcpoint.plcpointAddress, "1", basePlcpoint.plcpointLength.ToString());
baseEquip.emptyCount = emptyTaskCount;
dbContext.Update(baseEquip);
dbContext.SaveChanges();
MessageBox.Show("下发成功");
}
}
//else
//{
// MessageBox.Show("未检测到托盘");
//}
}
catch (Exception ex)
{
if (ex is PlcException)
{
foreach (var item in SystemData.PlcDic)
{
if (item.Value.IP == ex.Message)
{
SystemData.PlcDic[item.Key] = new Plc.S7.Plc(item.Value.CPU, item.Value.IP, item.Value.Port, item.Value.Rack, item.Value.Slot);
SystemData.PlcDic[item.Key].Open();
}
}
}
LogManager.Error(ex);
}
}
private void SomeOut_Click(object sender, RoutedEventArgs e)
{
try
{
BasePlcpoint lineSignal = SystemData.BasePlcpoints.First(t => t.plcpointNo == "linesignal01");
var lineSignalValue = SystemData.PlcDic[0].Read(lineSignal.plcpointAddress);
if (lineSignalValue != null && Convert.ToInt32(lineSignalValue) == 0)
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
BaseEquip baseEquip = dbContext.BaseEquip.First(t => t.objid == 40);
BasePlcpoint basePlcpoint = SystemData.BasePlcpoints.First(t => t.plcpointNo == "someOut");
SystemData.PlcDic[0].WriteToPoint(basePlcpoint.plcpointAddress, "1", basePlcpoint.plcpointLength.ToString());
baseEquip.emptyCount = 0;
dbContext.Update(baseEquip);
dbContext.SaveChanges();
MessageBox.Show("下发成功");
}
else
{
MessageBox.Show("检测到外侧有托盘,无法出堆垛机");
}
}
catch (Exception ex)
{
if (ex is PlcException)
{
foreach (var item in SystemData.PlcDic)
{
if (item.Value.IP == ex.Message)
{
SystemData.PlcDic[item.Key] = new Plc.S7.Plc(item.Value.CPU, item.Value.IP, item.Value.Port, item.Value.Rack, item.Value.Slot);
SystemData.PlcDic[item.Key].Open();
}
}
}
LogManager.Error(ex);
}
}
#endregion
#region 修改托盘库
private void UpdateEmpty_Click(object sender, RoutedEventArgs e)
{
try
{
lock (updateLock)
{
EmptyCountForm emptyCountForm = new EmptyCountForm(0, 10);
emptyCountForm.ShowDialog();
if (EmptyCountForm.IsSelect)
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
BaseEquip baseEquip = dbContext.BaseEquip.First(t => t.objid == 40);
baseEquip.emptyCount = EmptyCountForm.EmptyTaskCount;
dbContext.Update(baseEquip);
dbContext.SaveChanges();
MessageBox.Show("修改成功");
}
}
}
catch
{
}
}
#endregion
private void WindowHidden_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
private void btnSendTrayToTurnover_Click(object sender, RoutedEventArgs e)
{
try
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
var baseEquip = SystemData.BaseEquip.First(t => t.objid == 38);
var agvEquip = SystemData.BaseEquip.First(t => t.objid == 8);
bool hasTask = dbContext.WcsTask.Where(t => t.endPointId == baseEquip.objid || t.currPointId == baseEquip.objid).Any();
bool hasTaskManual = dbContext.WcsTaskManual.Where(t => t.endPointId == baseEquip.objid || t.currPointId == baseEquip.objid).Any();
if (hasTask || hasTaskManual)
{
MessageBox.Show("有任务正在执行,请等待任务完成后再执行此操作!");
return;
}
else
{
var endEquip = dbContext.BaseEquip.FirstOrDefault(t => t.equipType == 20 && t.equipStatus == 0 && t.emptyCount == 0);
if (endEquip == null)
{
MessageBox.Show("没有空箱库位!");
return;
}
else
{
WcsTaskManual task = new()
{
objid = Global.SnowId.NextId(),
taskType = 56,
containerNo = "00000000",
currPointId = baseEquip.objid,
currPointNo = baseEquip.equipNo,
nextPointId = agvEquip.objid,
nextPointNo = agvEquip.equipNo,
endPointId = endEquip.objid,
endPointNo = endEquip.equipNo,
createTime = DateTime.Now,
taskStatus = 0,
useFlag = 1,
floorNo = 2,
fromFloorNo = 2,
createBy = "WPF",
isEmpty = "0",
};
dbContext.Add(task);
dbContext.SaveChanges();
MessageBox.Show("任务生成成功!");
}
}
}
catch (Exception ex)
{
LogManager.Error(ex);
}
}
private void btnSendEmptyTray_Click(object sender, RoutedEventArgs e)
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
var baseEquip = SystemData.BaseEquip.First(t => t.objid == 38);
var agvEquip = SystemData.BaseEquip.First(t => t.objid == 8);
bool hasTask = dbContext.WcsTask.Where(t => t.endPointId == baseEquip.objid || t.currPointId == baseEquip.objid).Any();
bool hasTaskManual = dbContext.WcsTaskManual.Where(t => t.endPointId == baseEquip.objid || t.currPointId == baseEquip.objid).Any();
if (hasTask || hasTaskManual)
{
MessageBox.Show("有任务正在执行,请等待任务完成后再执行此操作!");
return;
}
else
{
var startEquip = dbContext.BaseEquip.FirstOrDefault(t => t.equipType == 20 && t.equipStatus == 1 && t.emptyCount == 1);
if (startEquip == null)
{
BaseEquip firstEmptyEquip = dbContext.BaseEquip.First(t => t.objid == 40);
if (firstEmptyEquip.emptyCount > 0)
{
MessageBoxResult messageBoxResult = MessageBox.Show("二楼未找到一个托盘的库位,是否从一楼调取?", "提示", MessageBoxButton.OKCancel);
if (messageBoxResult == MessageBoxResult.OK)
{
if (ReadEmptyLocation())
{
BasePlcpoint oneOutPoint = SystemData.BasePlcpoints.First(t => t.plcpointNo == "oneOut");
SystemData.PlcDic[0].WriteToPoint(oneOutPoint.plcpointAddress, "1", oneOutPoint.plcpointLength.ToString());
firstEmptyEquip.emptyCount -= 1;
dbContext.Update(firstEmptyEquip);
dbContext.SaveChanges();
MessageBox.Show("下发成功");
}
else
{
MessageBox.Show("一楼托盘库忙碌中,请稍后再试!");
}
}
}
else
{
MessageBox.Show("未找到一个托盘的库位");
return;
}
}
else
{
WcsTaskManual task = new()
{
objid = Global.SnowId.NextId(),
taskType = 56,
containerNo = "0",
currPointId = startEquip.objid,
currPointNo = startEquip.equipNo,
nextPointId = agvEquip.objid,
nextPointNo = agvEquip.equipNo,
endPointId = baseEquip.objid,
endPointNo = baseEquip.equipNo,
createTime = DateTime.Now,
taskStatus = 0,
useFlag = 1,
floorNo = 2,
fromFloorNo = 2,
createBy = "WPF",
isEmpty = "0",
};
dbContext.Add(task);
dbContext.SaveChanges();
MessageBox.Show("任务生成成功!");
}
}
}
/// <summary>
/// 读取一楼托盘库位是否空闲
/// </summary>
/// <returns></returns>
private bool ReadEmptyLocation()
{
var oneOutPoint = SystemData.BasePlcpoints.First(t => t.plcpointNo == "oneOut");
var someOutPoint = SystemData.BasePlcpoints.First(t => t.plcpointNo == "someOut");
var oneInPoint = SystemData.BasePlcpoints.First(t => t.plcpointNo == "oneIn");
var someInPoint = SystemData.BasePlcpoints.First(t => t.plcpointNo == "someIn");
var oneOutValue = SystemData.PlcDic[0].Read(oneOutPoint.plcpointAddress);
var someOutValue = SystemData.PlcDic[0].Read(someOutPoint.plcpointAddress);
var oneInValue = SystemData.PlcDic[0].Read(oneInPoint.plcpointAddress);
var someInValue = SystemData.PlcDic[0].Read(someInPoint.plcpointAddress);
if (oneOutValue != null && someOutValue != null && oneInValue != null && someInValue != null)
{
if (Convert.ToInt32(oneOutValue) == 0 && Convert.ToInt32(someOutValue) == 0 && Convert.ToInt32(oneInValue) == 0 && Convert.ToInt32(someInValue) == 0)
{
return true;
}
}
return false;
}
private void clearLocation_Click(object sender, RoutedEventArgs e)
{
if (this.EmptyEquip.SelectedItem is BaseEquip equip)
{
MessageBoxResult messageBoxResult = MessageBox.Show($"是否清除{equip.equipName}的托盘信息?", "提示", MessageBoxButton.OKCancel);
if (messageBoxResult == MessageBoxResult.OK)
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
BaseEquip baseEquip = dbContext.BaseEquip.First(t => t.objid == equip.objid);
baseEquip.emptyCount = 0;
baseEquip.equipStatus = 0;
dbContext.Update(baseEquip);
dbContext.SaveChanges();
MessageBox.Show("清除成功");
}
}
}
private void outLocation_Click(object sender, RoutedEventArgs e)
{
if (this.EmptyEquip.SelectedItem is BaseEquip equip)
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
BaseEquip baseEquip = dbContext.BaseEquip.First(t => t.objid == equip.objid);
if (baseEquip.emptyCount > 0)
{
FloorForm floorForm = new FloorForm(new List<int>() { 1, 3 });
floorForm.ShowDialog();
if (FloorForm.SelecetedFloorNo != 0)
{
//WcsTaskManual task = new()
//{
// objid = Global.SnowId.NextId(),
// taskType = 56,
// containerNo = "00000000",
// currPointId = baseEquip.objid,
// currPointNo = baseEquip.equipNo,
//};
}
MessageBox.Show("下发成功");
}
else
{
MessageBox.Show("库位已空!");
}
}
}
/// <summary>
/// 任务完成
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void taskCompeleted_Click(object sender, RoutedEventArgs e)
{
}
private void querenruku_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(this.firstRFID.Text))
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
BaseEquip baseEquip = dbContext.BaseEquip.First(t => t.objid == 1);
baseEquip.containerNo = "0";
dbContext.Update(baseEquip);
dbContext.SaveChanges();
MessageBox.Show("确认入库成功!");
}
else
{
MessageBox.Show("未扫描到RFID");
}
}
/// <summary>
/// 删除用户
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void deleteUser(object sender, RoutedEventArgs e)
{
try
{
if (UserData.SelectedItem is SysUser user)
{
if (user.OBJID == 1)
{
MessageBox.Show("不能删除根用户!");
return;
}
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
dbContext.SysUser.Remove(user);
dbContext.SaveChanges();
MessageBox.Show("删除成功!");
GetMenu();
}
}
catch
{
}
}
/// <summary>
/// 搜索
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SelectUser_Click(object sender, RoutedEventArgs e)
{
GetUser();
}
private void GetUser()
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
UserData.ItemsSource = dbContext.SysUser.WhereIf(!string.IsNullOrEmpty(LoginName.Text), t => t.UserName.Contains(LoginName.Text)).ToList();
}
/// <summary>
/// 添加用户
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AddUser_Click(object sender, RoutedEventArgs e)
{
}
private void GetMenu()
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
var sysMenus = dbContext.SysMenu.Where(t => t.MenuVisble == 1).ToList();
var sysRoles = dbContext.SysRole.Where(t => t.UserId == UserId).ToList();
List<long?> roleIds = sysRoles.Select(t => t.RoleId).ToList();
foreach (var menu in sysMenus)
{
if (FindName(menu.MenuName) is TabItem tabItem)
{
if (roleIds.Contains(menu.OBJID))
{
tabItem.Visibility = Visibility.Visible;
}
else
{
tabItem.Visibility = Visibility.Collapsed;
}
}
}
if (sysRoles.Count() == 0)
{
TabControl.SelectedIndex = 0;
}
}
private void SelectUserRole(object sender, RoutedEventArgs e)
{
try
{
if (UserData.SelectedItem is SysUser user)
{
//if (user.OBJID == 1)
//{
// MessageBox.Show("不能修改根用户!");
// return;
//}
MenuList menuList = new MenuList(_host, user.OBJID);
menuList.ShowDialog();
GetMenu();
}
}
catch
{
}
}
}
}