using Khd.Core.Domain.Models; using Khd.Core.EntityFramework; using Khd.Core.Plc.S7.Types; using Masuit.Tools.Logging; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Win32; using OfficeOpenXml; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text.RegularExpressions; using System.Threading; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Threading; using Thrift.Protocol; using Thrift.Server; using Thrift.Transport; using ThriftService; using Z.EntityFramework.Plus; using Jc.SnowId; using Masuit.Tools; using System.Windows.Input; namespace Khd.Core.Wpf.Form { /// /// FormBoardT.xaml 的交互逻辑 /// public partial class FormBoardT : Window { 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为损坏 List basePlcpoints = new List(); object timerjilu; public class barcodeinfo { public string barcode { get; set; } public long? materialId { get; set; } public decimal? qty { get; set; } } public List barcodeLsit = new List(); 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 { //LogManager.Info($"--------------------------当前时间 :{DateTime.Now} >>> 客户端启动;"); //设置显示任务栏 if (WindowState == WindowState.Normal) { MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight; WindowState = WindowState.Maximized; } //加载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 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 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.useFlag == 1 && t.containerNo.Contains(palletNo) && t.equipmentNo.Contains(equipCode)).ToList(); 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 { var Currentselected = LoadMaterial0.SelectedItem as WcsTask; using var scope = _host.Services.CreateScope(); using var dbContext = scope.ServiceProvider.GetRequiredService(); Currentselected.useFlag = 0; dbContext.WcsTask.Update(Currentselected); dbContext.SaveChanges(); GetTask(); } } /// /// 完成任务 /// /// /// 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) { using (ExcelPackage package = new ExcelPackage(fileInfo)) { ExcelWorksheet worksheet = package.Workbook.Worksheets.FirstOrDefault(); if (worksheet != null) { // 获取 Excel 表头 var columnHeaders = worksheet.Cells[1, 1, 1, worksheet.Dimension.End.Column] .Select(cell => cell.Text) .ToList(); BaseProductionOrderSplit orderMoedel = new(); // 映射数据库字段,这里需要根据你的实际需求进行调整 var columnMappings = new Dictionary { // 添加映射 {columnHeaders[0],"production_sequence"}, {columnHeaders[1],"line_code"}, {columnHeaders[3],"prod_code"}, {columnHeaders[4],"Quantity"}, {columnHeaders[5],"order_code"} }; int ordernum = 0; // 获取实际的行数和列数 int rowCount = worksheet.Dimension.Rows; int colCount = worksheet.Dimension.Columns; // 遍历 Excel 数据行 for (int row = 2; row <= rowCount; row++) { // 创建 SQL 插入语句 //string insertCommand = $"INSERT INTO YourTableName ({string.Join(", ", columnMappings.Values)}) VALUES ("; List values = new(); // 遍历 Excel 数据列 for (int col = 1; col <= colCount; col++) { string columnName = columnHeaders[col - 1]; string databaseColumnName; // 映射 Excel 列名到数据库列名 if (columnMappings.TryGetValue(columnName, out databaseColumnName)) { // 获取 Excel 单元格的值 var cellValue = worksheet.Cells[row, col].Text; // 添加值到插入语句 values.Add(cellValue); //values.Add($"'{cellValue.Replace("'", "''")}'"); //if (!string.IsNullOrWhiteSpace(cellValue)) //{ // values.Add(cellValue); //} } } // 使用 LINQ 查询空值 bool containsNull = values.Any(item => item == "" || item == null); if (containsNull) { //MessageBox.Show("Excel表格中存在空值,不执行导入操作!", "订单导入操作提示!"); //LogManager.Info($"订单导入按钮事件错误 >>> Excel表格中存在空值!不执行导入操作!"); break; } // 获得插入语句 //string fullInsertCommand = $"{insertCommand} {string.Join(", ", values)} )"; var xianbiename = Char.GetNumericValue(values[1][values[1].Length - 1]).ToString(); int orderSequence = int.Parse(values[0].Trim()); using var scope = _host.Services.CreateScope(); using var dbContext = scope.ServiceProvider.GetRequiredService(); // var orderModel = dbContext.BaseProductionOrderSplit.OrderBy(t => t.productionSequence).ToList(); ///var data = orderModel.Find(t => t.orderCode == values[4].Trim().ToUpper()); // } MessageBox.Show($"{ordernum}条订单导入系统!", "订单导入操作提示!"); //LoadMaterial_GetMessage(""); } } } })); } catch (Exception ex) { 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) { using var scope = _host.Services.CreateScope(); using var dbContext = scope.ServiceProvider.GetRequiredService(); if (int.TryParse(txtNum.Text, out int num)) { //目的地 var endEquip = dbContext.BaseEquip.Where(t => t.equipNo == "FL05").FirstOrDefault(); //所有辅料库位 var wmsBaseLocation = dbContext.WmsBaseLocation.Where(t=> t.warehouseId== 5).ToList(); //五楼辅料库 //有库存的库位 var wmsStock = dbContext.WcsStock.Where(t => t.useFlag == 1).Select(t=>t.locationCode).ToList(); var EmptyBox = wmsBaseLocation.Where(t => !t.locationCode.Contains(wmsStock)).ToList(); for (int i = 0; i < num; i++) { var wcsTask = new WcsTask() { objid = Global.SnowId.NextId(), startPointId = EmptyBox[i].locationId, startPointNo = EmptyBox[i].agvPositionCode, currPointId = EmptyBox[i].locationId, currPointNo = EmptyBox[i].agvPositionCode, nextPointId = endEquip.objid, nextPointNo = endEquip.equipNo, endPointId = endEquip.objid, endPointNo = endEquip.equipNo, taskType = 13, taskStatus = 0, floorNo = 5, containerNo = EmptyBox[i].containerCode, equipmentNo = endEquip.equipNo, createBy = "5楼呼叫料箱任务", createTime = System.DateTime.Now, }; dbContext.Add(wcsTask); dbContext.SaveChanges(); } } else { MessageBox.Show("请输入有效的数字"); } } /// /// 料箱扫描 /// /// /// private void txtBox_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { txtBarCode.Focus(); } } /// /// 条码扫描 /// /// /// private void txtBarCode_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { 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; } barcodeinfo barcodeinfo = new barcodeinfo(); barcodeinfo.barcode=txtBarCode.Text; barcodeinfo.materialId = mesInfo.materialId; barcodeinfo.qty = mesInfo.amount; barcodeLsit.Add(barcodeinfo); txtBarCode.SelectAll(); txtBarCode.Focus(); txtScan.Text = $"已扫描{barcodeLsit.Count}个"; } } /// /// 确认 /// /// /// private void btnConfirmBox_Click(object sender, RoutedEventArgs e) { 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(); if (cbxType.Text == "入库") { //写入wms_raw_instock var wmsRawInstock = new WmsRawInstock() { rawInstockId = Global.SnowId.NextId(), warehouseId = 5, operationType = "3", instockType = "9", materialId = barcodeLsit.FirstOrDefault().materialId, palletInfoCode = txtBox.Text, instockAmount = barcodeLsit.Sum(t=>t.qty), executeStatus="0", applyBy="扫描入库", applyDate=System.DateTime.Now }; dbContext.WmsRawInstock.Add(wmsRawInstock); dbContext.SaveChanges(); var startEquip = dbContext.BaseEquip.Where(t => t.equipNo == "FL05").FirstOrDefault(); var warehouse = dbContext.WmsBaseWarehouse.Where(t => t.warehouseId == 5).FirstOrDefault(); //写入wcs任务 var wcsTask = new WcsTask() { objid = Global.SnowId.NextId(), startPointId = startEquip.objid, startPointNo = startEquip.agvPositionCode, currPointId = startEquip.objid, currPointNo = startEquip.agvPositionCode, nextPointId = warehouse.warehouseId, nextPointNo = warehouse.warehouseCode, endPointId = warehouse.warehouseId, endPointNo = warehouse.warehouseCode, taskType = 4, taskStatus = 0, floorNo = 5, containerNo = txtBox.Text, equipmentNo = startEquip.equipNo, createBy = "5楼入库任务", createTime = System.DateTime.Now, }; dbContext.Add(wcsTask); dbContext.SaveChanges(); } if (cbxType.Text == "出库") { //从料箱里拿出来 } if (cbxType.Text == "回库") { //写入wms_raw_instock var wmsRawInstock = new WmsRawInstock() { rawInstockId = Global.SnowId.NextId(), warehouseId = 5, operationType = "3", instockType = "9", materialId = barcodeLsit.FirstOrDefault().materialId, palletInfoCode = txtBox.Text, instockAmount = barcodeLsit.Sum(t => t.qty), executeStatus = "0", applyBy = "扫描回库", applyDate = System.DateTime.Now }; dbContext.WmsRawInstock.Add(wmsRawInstock); dbContext.SaveChanges(); var startEquip = dbContext.BaseEquip.Where(t => t.equipNo == "FL05").FirstOrDefault(); var warehouse = dbContext.WmsBaseWarehouse.Where(t => t.warehouseId == 5).FirstOrDefault(); //写入wcs任务 var wcsTask = new WcsTask() { objid = Global.SnowId.NextId(), startPointId = startEquip.objid, startPointNo = startEquip.agvPositionCode, currPointId = startEquip.objid, currPointNo = startEquip.agvPositionCode, nextPointId = warehouse.warehouseId, nextPointNo = warehouse.warehouseCode, endPointId = warehouse.warehouseId, endPointNo = warehouse.warehouseCode, taskType = 4, taskStatus = 0, floorNo = 5, containerNo = txtBox.Text, equipmentNo = startEquip.equipNo, createBy = "5楼回库任务", createTime = System.DateTime.Now, }; dbContext.Add(wcsTask); dbContext.SaveChanges(); } } } }