using Khd.Core.Domain.Models; using Khd.Core.EntityFramework; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Threading; using Z.EntityFramework.Plus; using Masuit.Tools; using System.Windows.Input; using Khd.Core.Wpf.Scan; using System.IO.Ports; using System.Threading; using System.Diagnostics; using Jc.SnowId; using System.IO; using System.Runtime.InteropServices; using System.Windows.Media; using HandyControl.Tools.Extension; using Khd.Core.Wpf.TaskForm; using Microsoft.IdentityModel.Tokens; using Khd.Core.Plc; using Khd.Core.Plc.S7; using System.Threading.Tasks; using System.Windows.Media.Media3D; using Masuit.Tools.Logging; using Khd.Core.Library.Mapper; using System.Windows.Markup; using System.Security.Cryptography.Xml; using Newtonsoft.Json; using Khd.Core.Domain.Dto.webapi; using System.Text; namespace Khd.Core.Wpf.Form { /// /// FormBoardT.xaml 的交互逻辑 /// public partial class FormBoardT : Window { 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 basePlcpoints = new List(); object timerjilu; Dictionary SelectedItem = new Dictionary(); Dictionary InSelectedItem = new Dictionary(); List CallSelectedItem = new List(); public List barcodeLsit = new List(); private int pageCount = 15; 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 { } 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; } /// /// 页面加载事件 /// /// /// private void FormBoard_Loaded(object sender, RoutedEventArgs e) { try { DispatcherTimer dispatcherTimer = new DispatcherTimer(); dispatcherTimer.Tick += new EventHandler(ShowTime); //委托刷新时间方法 dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000); dispatcherTimer.Start(); //LogManager.Info($"--------------------------当前时间 :{DateTime.Now} >>> 客户端启动;"); //设置显示任务栏 if (WindowState == WindowState.Normal) { MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight; WindowState = WindowState.Maximized; } using var scope = _host.Services.CreateScope(); using var dbContext = scope.ServiceProvider.GetRequiredService(); SystemData.BasePlcpoints = dbContext.BasePlcpoint.ToList(); SystemData.BaseDictionary = dbContext.BaseDictionary.ToList(); SystemData.BaseEquip = dbContext.BaseEquip.ToList(); SystemData.BaseWareHouse = dbContext.WmsBaseWarehouse.ToList(); SystemData.MesBaseMaterialInfo = dbContext.MesBaseMaterialInfo.Take(100).ToList(); //MaterialName.ItemsSource = SystemData.MesBaseMaterialInfo; WarehourseId.ItemsSource = SystemData.BaseWareHouse.Where(t => t.warehouseFloor != 1 && t.warehouseFloor != 4).ToList(); Task.Run(() => { while (true) { try { serialPortModel = new SerialPortModel(); serialPortModel.PortName = "COM7"; 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(); break; } catch { } Thread.Sleep(10000); } }); foreach (var plcConfig in SystemData.PlcConfigs) { Task.Run(() => { 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); } }); }; Task.Run(() => { using var scope = _host.Services.CreateScope(); using var dbContext = scope.ServiceProvider.GetRequiredService(); 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(); // 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 list = new List // { // "101上件点", // "102上件点", // "103上件点" // }; } catch (Exception ex) { MessageBox.Show(ex.Message); } } 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 { } } /// /// 显示序号事件 /// /// /// private void dgData_LoadingRow(object sender, DataGridRowEventArgs e) { e.Row.Header = e.Row.GetIndex() + 1; } /// /// 点击退出按钮 /// /// /// 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; /// /// 查询任务 /// /// /// 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(); // List wcsTaskLogs = new List(); // 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(); var equipCode = lba_ThrifTtitle1.Text; var palletNo = lba_ThrifTtitle2.Text; var data = dbContext.WcsTask.Where(t => t.containerNo.Contains(palletNo) && t.equipmentNo.Contains(equipCode)).ToList(); SelectedItem.Clear(); foreach (var item in data) { SelectedItem.Add(item.objid, false); } this.LoadMaterial0.ItemsSource = null; this.LoadMaterial0.ItemsSource = data; this.LoadMaterial0.Items.Refresh(); if (data.Count == 0) { MessageBox.Show("未查询到数据"); } } /// /// 删除任务 /// /// /// 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(); List ids = new List(); foreach (var item in SelectedItem) { if (item.Value == true) { ids.Add(item.Key); } } BaseEquip agvEquip = SystemData.BaseEquip.First(t => t.objid == 10); List wcsCmds = dbContext.WcsCmd.Where(t => ids.Contains(t.taskId)).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, "", cancelTask.ToJsonString()); var result = JsonConvert.DeserializeObject(reponse); if (result != null && result.code == "0") { ids.Remove(item.taskId); dbContext.WcsCmd.Where(t => t.objid == item.objid).Delete(); dbContext.WcsTask.Where(t => t.objid == item.taskId).Delete(); dbContext.WcsTaskLog.Where(t => t.objid == item.taskId).Update(t => new WcsTaskLog { IsDelete = 1 }); } } dbContext.WcsTask.Where(t => ids.Contains(t.objid)).Delete(); dbContext.WcsTaskLog.Where(t => ids.Contains(t.objid)).Update(t => new WcsTaskLog { IsDelete = 1 }); dbContext.SaveChanges(); GetTask(); } else { } } } /// /// 完成任务 /// /// /// 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(); Currentselected.taskStatus = 5; dbContext.WcsTask.Update(Currentselected); dbContext.SaveChanges(); GetTask(); } } /// /// 查询设备信息 /// /// /// private void btnGetEquip_Click(object sender, RoutedEventArgs e) { using var scope = _host.Services.CreateScope(); using var dbContext = scope.ServiceProvider.GetRequiredService(); 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("未查询到数据"); } } /// /// 查询库存信息 /// /// /// private void btnGetStocK_Click(object sender, RoutedEventArgs e) { Pagination1.PageIndex = 1; GetBaseLocations(); } private void GetBaseLocations() { if (WarehourseId.SelectedItem is WmsBaseWarehouse warehouse) { var scope = _host.Services.CreateScope(); var dbContext = scope.ServiceProvider.GetRequiredService(); 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; } } ///// ///// 查询指令信息 ///// ///// ///// //private void btnGetWcsCmd_Click(object sender, RoutedEventArgs e) //{ // GetWcsCmd(); //} //private void GetWcsCmd() //{ // using var scope = _host.Services.CreateScope(); // using var dbContext = scope.ServiceProvider.GetRequiredService(); // 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("未查询到数据"); // } //} ///// ///// 删除指令 ///// ///// ///// //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(); // Currentselected.useFlag = 0; // dbContext.WcsCmd.Update(Currentselected); // dbContext.SaveChanges(); // GetWcsCmd(); // } //} ///// ///// 重发指令 ///// ///// ///// //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(); // Currentselected.sendFlag = 0; // dbContext.WcsCmd.Update(Currentselected); // dbContext.SaveChanges(); // GetWcsCmd(); // } //} ///// ///// 呼叫料箱 ///// ///// ///// //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(); // 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 // { // } //} /// /// 料箱扫描 /// /// /// //private void txtBox_KeyDown(object sender, KeyEventArgs e) //{ // if (e.Key == Key.Enter) // { // txtBarCode.Focus(); // } //} ///// ///// 条码扫描 ///// ///// ///// //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(); // 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 // { // } //} /// /// 确认 /// /// /// 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(); // 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 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 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 //{ //} } /// /// 退出程序 /// /// /// private void WindowClose_Click(object sender, RoutedEventArgs e) { if (MessageBox.Show("是否确认关闭程序", "提示信息", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes) { System.Environment.Exit(System.Environment.ExitCode); } } /// /// 获取聚焦打开键盘 /// /// /// private void TextBoxGetFocus() { try { Process process = new Process() { StartInfo ={ UseShellExecute=true, FileName="osk.exe" } }; process.Start(); } catch (Exception ex) { MessageBox.Show("打开软键盘时出错: " + ex.Message); } } /// /// 失去焦点,关闭软键盘 /// /// /// 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); } } /// /// 背负式小车确认完成 /// /// /// private void btnBearConfirmBox_Click(object sender, RoutedEventArgs e) { try { using var scope = _host.Services.CreateScope(); using var dbContext = scope.ServiceProvider.GetRequiredService(); 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); } } /// /// 获取到所有控件 /// /// /// private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e) { } /// /// 添加任务 /// /// /// private void AddTaskButton_Click(object sender, RoutedEventArgs e) { AddTask addTask = new AddTask(_host); addTask.ShowDialog(); } ///// ///// 清除RFID ///// ///// ///// //private void clearRFID_Click(object sender, RoutedEventArgs e) //{ // txtBox.Text = string.Empty; //} ///// ///// 清除条码 ///// ///// ///// //private void clearBarCode_Click(object sender, RoutedEventArgs e) //{ // txtScan.Text = string.Empty; // barcodeLsit.Clear(); // txtBarCode.Text = string.Empty; //} ///// ///// 人工叫料失去焦点 ///// ///// ///// //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(); // var material = dbContext.WmsRawStock.Where(t => t.instockBatch == callMaterialId.Text).FirstOrDefault(); // if (material == null) // { // MessageBox.Show("批次号不存在!"); // callMaterialId.Text = string.Empty; // } // else // { // } // } // } // catch // { // } //} /// /// 开始任务 /// /// /// 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(); List wcsTasks = dbContext.WcsTask.ToList(); List ids = new List(); 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, taskStatus = 0 }); GetTask(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } /// /// 单选 /// /// /// private void CheckBox_Click(object sender, RoutedEventArgs e) { if (sender is CheckBox checkbox && LoadMaterial0.SelectedItem is WcsTask Currentselected) { SelectedItem[Currentselected.objid] = checkbox.IsChecked ?? false; } } /// /// 单选 /// /// /// 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); } } } /// /// 刷新 /// /// /// private void btnRefreshPlc_Click(object sender, RoutedEventArgs e) { try { for (int i = 1; i <= 5; i++) { BasePlcpoint rfid = SystemData.BasePlcpoints.First(t => t.plcpointNo == $"RFID00{i}"); BasePlcpoint linesignal = SystemData.BasePlcpoints.First(t => t.plcpointNo == $"linesignal0{i}"); BasePlcpoint wcsrun = SystemData.BasePlcpoints.First(t => t.plcpointNo == $"wcsrun0{i}"); var textBlock = FindName(rfid.plcpointNo) as TextBlock; if (textBlock != null) { textBlock.Text = SystemData.PlcDic[0].ReadRFID(rfid.plcpointAddress); } textBlock = FindName(linesignal.plcpointNo) as TextBlock; if (textBlock != null) { textBlock.Text = SystemData.PlcDic[0].Read(linesignal.plcpointAddress)?.ToString(); } textBlock = FindName(wcsrun.plcpointNo) as TextBlock; if (textBlock != null) { textBlock.Text = SystemData.PlcDic[0].Read(wcsrun.plcpointAddress)?.ToString(); } } } 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(); break; } } } } catch { MessageBox.Show("提升机PLC连接失败!"); } } try { BasePlcpoint RFID5001 = SystemData.BasePlcpoints.First(t => t.plcpointNo == "RFID5001"); BasePlcpoint isarrive = SystemData.BasePlcpoints.First(t => t.plcpointNo == "isarrive"); BasePlcpoint isput = SystemData.BasePlcpoints.First(t => t.plcpointNo == "isput"); var CtuTextBlock = FindName(RFID5001.plcpointNo) as TextBlock; if (CtuTextBlock != null) { CtuTextBlock.Text = SystemData.PlcDic[1].ReadRFID(RFID5001.plcpointAddress); } CtuTextBlock = FindName(isarrive.plcpointNo) as TextBlock; if (CtuTextBlock != null) { CtuTextBlock.Text = SystemData.PlcDic[1].Read(isarrive.plcpointAddress)?.ToString(); } CtuTextBlock = FindName(isput.plcpointNo) as TextBlock; if (CtuTextBlock != null) { CtuTextBlock.Text = SystemData.PlcDic[1].Read(isput.plcpointAddress)?.ToString(); } } 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(); break; } } } } catch { MessageBox.Show("U型线PLC连接失败!"); } } } /// /// 开始任务 /// /// /// private void btnCtuInConfirmBox_Click(object sender, RoutedEventArgs e) { try { using var scope = _host.Services.CreateScope(); using var dbContext = scope.ServiceProvider.GetRequiredService(); 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 { } } /// /// 页码改变时 /// /// /// private void Pagination_PageUpdated(object sender, HandyControl.Data.FunctionEventArgs e) { GetBaseLocations(); } /// /// 物料名称 /// /// /// 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(); } /// /// 入库扫描 /// /// /// 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(); var material = dbContext.MesBaseBarcodeInfo.FirstOrDefault(t => t.barcodeInfo == txtInBarCode.Text); var container = dbContext.WmsBaseLocation.FirstOrDefault(t => t.containerCode == txtInBox.Text && t.warehouseId == 512); List 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; } var newRawStock = new WmsRawStock() { materialId = material.materialId, supplierId = material.manufacturerId, instockBatch = material.batchCode, locationCode = container.locationCode, stockType = "1", palletInfoCode = container.containerCode, totalAmount = material.batchFlag == "1" ? 1 : material.amount, 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 { } } /// /// 入库容器改变 /// /// /// 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(); 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 { } } /// /// 入库条码改变 /// /// /// 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(); 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 = (mesBaseMaterialInfo.BatchFlag == 1 ? mesBaseMaterialInfo.BatchAmount : 1).ToString(); if (mesBaseMaterialInfo.BatchFlag == 0) { txtInScan.IsReadOnly = true; } else { txtInScan.IsReadOnly = false; } } } } } catch { } } /// /// 出库容器改变 /// /// /// 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(); 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 { } } /// /// 出库条码改变 /// /// /// 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(); 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.MesBaseMaterialInfo.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 == 0) { txtOutScan.IsReadOnly = true; } else { txtOutScan.IsReadOnly = false; } } } } } else { if (!string.IsNullOrEmpty(barCode)) { MessageBox.Show("请先扫描容器号!"); txtOutBarCode.Text = string.Empty; txtOutScan.Text = string.Empty; } } } catch { } } /// /// 出库扫描 /// /// /// 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(); 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) { wmsRawStock.occupyAmount -= outScan; wmsRawStock.totalAmount -= outScan; if (wmsRawStock.totalAmount == 0) { dbContext.WmsRawStock.Where(t => t.rawStockId == wmsRawStock.rawStockId).Delete(); } else { dbContext.WmsRawStock.Update(wmsRawStock); dbContext.SaveChanges(); } txtOutScan.Text = string.Empty; txtOutBarCode.Text = string.Empty; txtOutBarCode.Focus(); MessageBox.Show("出库成功!"); } 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.occupyAmount -= outScan; 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.occupyAmount -= outScan; 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("请先扫描容器号和条码!"); } } /// /// 搜索 /// /// /// private void SelectButton_Click(object sender, RoutedEventArgs e) { CallSelectedItem.Clear(); GetPersonCallMaterialData(); } /// /// 获取人员呼叫的物料数据 /// private void GetPersonCallMaterialData() { try { using var scope = _host.Services.CreateScope(); using var dbContext = scope.ServiceProvider.GetRequiredService(); List materialIds = new List(); List materialInfos = new List(); 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 { Key = t.Key, Value = t.Sum(x => x.totalAmount) }); var palletInfoCodes = wmsRawStocks.Select(t => t.palletInfoCode).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 wmsRawStocks 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) }; PersonCallMaterial.ItemsSource = bill.ToList().Skip((this.Pagination2.PageIndex - 1) * this.pageCount).Take(this.pageCount); int sum = bill.Count(); Pagination2.MaxPageCount = (sum / pageCount) + (sum % pageCount == 0 ? 0 : 1); if (bill.Count() == 0) { MessageBox.Show("未查询到数据"); } } catch { } } /// /// 页码改变时 /// /// /// private void Pagination2_PageUpdated(object sender, HandyControl.Data.FunctionEventArgs e) { GetPersonCallMaterialData(); } /// /// 叫空料箱 /// /// /// private void CallEmptyBtn_Click(object sender, RoutedEventArgs e) { try { EmptyCountForm emptyCountForm = new EmptyCountForm(); 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(); //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 palletInfoCodes = wmsRawStock.Select(t => t.palletInfoCode).ToList(); wmsBaseLocation.RemoveAll(t => palletInfoCodes.Contains(t.containerCode)); if (wmsBaseLocation.Count > 0 && EmptyCountForm.EmptyTaskCount <= wmsBaseLocation.Count) { for (int i = 0; i < EmptyCountForm.EmptyTaskCount; i++) { WmsBaseLocation startBaseLocation = wmsBaseLocation[i]; startBaseLocation.outstockFlag = "1"; startBaseLocation.locationStatus = "6"; WcsTaskManual wcsTask = 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.Add(wcsTask); dbContext.Update(startBaseLocation); dbContext.SaveChanges(); } MessageBox.Show("任务创建成功!"); } else { MessageBox.Show($"没有足够的空箱库位库位,剩余空库位数量为{wmsBaseLocation.Count}!"); } } } catch { } } /// /// 叫托盘 /// /// /// 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; } if (CallSelectedItem.Count > 6) { MessageBox.Show("一次只能叫托盘6个!"); return; } using var scope = _host.Services.CreateScope(); using var dbContext = scope.ServiceProvider.GetRequiredService(); //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) { foreach (var item in wmsBaseLocation) { var wcsTask = 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 }; dbContext.Add(wcsTask); item.outstockFlag = "1"; item.ContainerStatus = "2"; item.locationStatus = "6"; dbContext.Update(item); dbContext.SaveChanges(); } MessageBox.Show("创建出库任务成功!"); } CallSelectedItem.Clear(); GetPersonCallMaterialData(); } catch { } } /// /// 执行CTU任务 /// /// /// 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(); 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()); } 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()); break; } } } } catch { MessageBox.Show("PLC连接失败!"); } } } private void Button_Click(object sender, RoutedEventArgs e) { TextBoxGetFocus(); } } }