2 Commits ccae9bc25a ... 66ff2ff294

Author SHA1 Message Date
  zhangliwen 66ff2ff294 更新扫码逻辑 1 year ago
  zhangliwen f09de7c5aa 更新扫码逻辑v2 1 year ago

+ 82 - 288
SCADA_DAQ/Customer/Machines/ScannerMachine.cs

@@ -1,9 +1,15 @@
-using LiveCharts.Helpers;
+/*
+ * V2
+ */
+
+using LiveCharts.Helpers;
 using Microsoft.DwayneNeed.Numerics;
 using Mysqlx.Crud;
 using NPOI.SS.Formula.Functions;
 using SCADA.BarcodeReader;
 using SCADA.Comm;
+using SCADA.CommonCtrl.WpfWindow;
+using SCADA.CommonLib;
 using SCADA.CommonLib.Data;
 using SCADA.CommonLib.Data.DIL;
 using SCADA.CommonLib.Helper;
@@ -35,7 +41,7 @@ namespace SCADA_DAQ.Customer.Machines
     /// 二维扫码枪
     /// </summary>
     [DisplayName("二维扫码枪")]
-    public class ScannerMachine : BaseBarcodeReaderMachine
+    public class ScannerMachine_v1 : BaseBarcodeReaderMachine
     {
         // 数据库对象
         private static SqlSchema.DIL.DILDB DAL = Env.SgIDAL;
@@ -48,27 +54,27 @@ namespace SCADA_DAQ.Customer.Machines
         private static int orderIndex = 0;
         // 当前生产的工单号
         private static string currentOrderId = "";
-        // 当前生的车型名称
+        // 当前生的车型名称
         private static string currentMotorcycleTypeName = "";
+        // 当前生产的id
+        private static int currentId = -1;
 
         // 上次扫码号
         private static string lastScanCode = "";
-        // 上次生产的工单号
-        private static string lastOrderId = "";
-        // 上次生产的工单号
-        private static int lastId = -1;
+        //// 上次生产的工单号
+        //private static string lastOrderId = "";
+        //// 上次生产的工单号
+        //private static int lastId = -1;
 
         // 扫码队列
         Queue scanQueue = new Queue();
 
-        //// 任务模型
-        //private TaskManagementModel taskManagementModel = new TaskManagementModel();
 
         /// <summary>
         /// 构造函数
         /// </summary>
         /// <param name="id"></param>
-        public ScannerMachine(string id) : base(id)
+        public ScannerMachine_v1(string id) : base(id)
         {
             Env.Schedual.DateTimeChanged += Schedual_DateTimeChanged;   // 注册定时器事件
 
@@ -80,9 +86,7 @@ namespace SCADA_DAQ.Customer.Machines
             //    $"AND WorkShopId_Str='{scanItem.WorkShopId}' AND ProductionLineId_Str='{scanItem.ProductionLineId}'");
 
             Init();
-            //if (lastOrderId == "")
-            if (lastId == -1)
-                Log.Warn("上次生产的工单号数据已丢失");
+            if (currentId == -1) Log.Warn("上次生产的工单号数据已丢失");
         }
 
         /// <summary>
@@ -95,25 +99,12 @@ namespace SCADA_DAQ.Customer.Machines
             // e.ChangeType == SCADA.CommonLib.DateTimeChangeType.SecondChanged   秒变化
             // e.TimeFlag.Second   取值
 
-            //if (e.TimeFlag.Second % 60 == 0)
-            //{
-            //    // 清空缓存
-            //    CustomerEnv.TaskInfoCache.Clear();
-            //}
 
             if (e.TimeFlag.Second % 1 == 0)
             {
-                if (scanQueue.Count > 0)   // 队列不为空
+                if (BaseDevice.DeviceID == "上料扫码枪")
                 {
-                    //ScanCounter();
-                    if (BaseDevice.DeviceID == "上料扫码枪")
-                    {
-                        ImportSyncQueue();
-                    }
-                    else if (BaseDevice.DeviceID == "下料扫码枪")
-                    {
-                        ExportSyncQueue();
-                    }
+                    ImportSyncQueue();
                 }
             }
 
@@ -127,10 +118,6 @@ namespace SCADA_DAQ.Customer.Machines
                     {
                         LineImport(dr);
                     }
-                    else if (BaseDevice.DeviceID == "下料扫码枪")
-                    {
-                        LineExport(dr);
-                    }
                 }
             }
 
@@ -147,10 +134,6 @@ namespace SCADA_DAQ.Customer.Machines
             {
                 LineImport(e);
             }
-            else if (BaseDevice.DeviceID == "下料扫码枪")
-            {
-                LineExport(e);
-            }
             else
             {
                 Log.Warn($"扫码枪ID无效 —— 当前扫码枪ID为{BaseDevice.DeviceID}");
@@ -188,22 +171,13 @@ namespace SCADA_DAQ.Customer.Machines
 
             if (BaseDevice.DeviceID == "上料扫码枪")
             {
+                currentId = taskInfoItem.ID;
                 currentOrderId = taskInfoItem.OrderId;
                 currentMotorcycleTypeName = taskInfoItem.MotorcycleTypeName;
                 CustomerEnv.CurrentTaskInfo.OrderSequence = taskInfoItem.OrderSequence;
-                CustomerEnv.CurrentTaskInfo.OrderId = currentOrderId;
                 CustomerEnv.CurrentTaskInfo.MotorcycleTypeName = currentMotorcycleTypeName;
                 CustomerEnv.CurrentTaskInfo.CurrentProduction = taskInfoItem.CurrentProduction;
             }
-            else if (BaseDevice.DeviceID == "下料扫码枪")
-            {
-                //lastOrderId = taskInfoItem.OrderId;
-                lastId = taskInfoItem.ID;
-                CustomerEnv.CurrentTaskInfo.OrderSequence = taskInfoItem.OrderSequence;
-                CustomerEnv.CurrentTaskInfo.OrderId = taskInfoItem.OrderId;
-                CustomerEnv.CurrentTaskInfo.MotorcycleTypeName = taskInfoItem.MotorcycleTypeName;
-                CustomerEnv.CurrentTaskInfo.CurrentProduction = taskInfoItem.CurrentProduction;
-            }
             
         }
 
@@ -238,12 +212,18 @@ namespace SCADA_DAQ.Customer.Machines
                 // 条形码 
                 if (QRInfo[0] == "")     // 空信息
                 {
+                    System.Windows.Application.Current.Dispatcher.Invoke(() => {
+                        FrmShowMessage.ShowMessage("空信息!", "空信息!", SCADA.CommonCtrl.MessageLevel.Alarm, 2000);
+                    });
                     Log.Debug("空信息");
                     return;
                 }
                 if (lastScanCode == scanMsg)  // 重复扫码
                 {
-                    Log.Debug("重复扫码");
+                    System.Windows.Application.Current.Dispatcher.Invoke(() => {
+                        FrmShowMessage.ShowMessage("重复扫码!", "重复扫码!", SCADA.CommonCtrl.MessageLevel.Alarm, 2000);
+                    });
+                    Log.Warn("重复扫码");
                     return;
                 }
                 lastScanCode = scanMsg;
@@ -256,6 +236,7 @@ namespace SCADA_DAQ.Customer.Machines
                     WorkShopId = CustomerEnv.ProductionLineConfigValue.WorkShopId,
                     ProductionLineId = CustomerEnv.ProductionLineConfigValue.ProductionLineId,
                     ProductId = QRInfo[0],
+                    ID = currentId,
                     OrderId = currentOrderId,
                     MotorcycleTypeName = currentMotorcycleTypeName
                 });
@@ -267,20 +248,19 @@ namespace SCADA_DAQ.Customer.Machines
             }
             else if (QRInfo.Count() == 3 && QRInfo[0] == "change")
             {
+                currentOrderId = "";
+                currentMotorcycleTypeName = "";
                 CustomerEnv.CurrentTaskInfo.OrderSequence = -1;
-                CustomerEnv.CurrentTaskInfo.OrderId = "";
-                CustomerEnv.CurrentTaskInfo.MotorcycleTypeName = "";
+                CustomerEnv.CurrentTaskInfo.OrderId = currentOrderId;
+                CustomerEnv.CurrentTaskInfo.MotorcycleTypeName = currentMotorcycleTypeName;
 
                 // 换型码
                 currentMotorcycleTypeName = QRInfo[1];
-
-                //// 查询车型
-                //currentMotorcycleTypeName = DAL.APP_TaiLing_MotorcycleTypeManagement.GetData<MotorcycleTypeModel>()?
-                //    .Where(t => t.MotorcycleTypeId == QRInfo[2])
-                //    .OrderBy(t => t.MotorcycleTypeId).Select(t => t.MotorcycleTypeName)
-                //    .LastOrDefault();
-
+                System.Windows.Application.Current.Dispatcher.Invoke(() => {
+                    FrmShowMessage.ShowMessage("正在换型!", "正在换型!", SCADA.CommonCtrl.MessageLevel.Alarm, 2000);
+                });
                 Log.Debug($"正在换型:{currentMotorcycleTypeName}");
+
                 // 查询工单号
                 var taskList = DAL.APP_TaiLing_TaskManagement.GetData<TaskManagementModel>()?
                     .Where(t => t.MotorcycleTypeName == currentMotorcycleTypeName
@@ -289,66 +269,51 @@ namespace SCADA_DAQ.Customer.Machines
                         && t.ProductionLineId == CustomerEnv.ProductionLineConfigValue.ProductionLineId)
                     .ToList();
                 
-                if (taskList == null)
-                {
-                    CustomerEnv.CurrentTaskInfo.OrderSequence = -1;
-                    CustomerEnv.CurrentTaskInfo.OrderId = "";
-                    CustomerEnv.CurrentTaskInfo.MotorcycleTypeName = "查无工单任务\r\n请先添加工单";
-                    Log.Warn("查无工单任务,请先添加工单");
-                    return;
-                }
-                if (taskList.Count <= 0)
+                if (taskList == null || taskList.Count <= 0)
                 {
-                    CustomerEnv.CurrentTaskInfo.OrderSequence = -1;
-                    CustomerEnv.CurrentTaskInfo.OrderId = "";
-                    CustomerEnv.CurrentTaskInfo.MotorcycleTypeName = "查无工单任务\r\n请先添加工单";
-                    Log.Warn("查无工单任务,请先添加工单");
+                    currentId = -1;
+                    FrmShowMessage.ShowMessage("暂无可生产的订单!", "暂无可生产的订单!", SCADA.CommonCtrl.MessageLevel.Alarm, 2000);
+                    CustomerEnv.CurrentTaskInfo.MotorcycleTypeName = "暂无可生产的订单";
+                    Log.Warn("暂无可生产的订单");
                     return;
                 }
 
-                var OrderSequence = -1;
                 // 工单切换
                 if (lastScanCode != scanMsg)  // 换码
                 {
                     orderIndex = 0;
                     lastScanCode = scanMsg;
                 }
-                if (currentOrderId != taskList[orderIndex].OrderId)
+                if (currentId == taskList[orderIndex].ID)
                 {
-                    currentOrderId = taskList[orderIndex].OrderId;
-                    OrderSequence = taskList[orderIndex].OrderSequence;
+                    orderIndex = orderIndex < taskList.Count - 1 ? orderIndex + 1 : 0; // 循环切换
                 }
-                else
-                {
-                    if (orderIndex < taskList.Count - 1)
-                    {
-                        orderIndex++;
-                        currentOrderId = taskList[orderIndex].OrderId;
-                        OrderSequence = taskList[orderIndex].OrderSequence;
-                    }
-                    else
-                    {
-                        // 循环切换
-                        orderIndex = 0;
-                        currentOrderId = taskList[orderIndex].OrderId;
-                        OrderSequence = taskList[orderIndex].OrderSequence;
-                    }
-                }
-
-                CustomerEnv.CurrentTaskInfo.OrderSequence = OrderSequence;
-                CustomerEnv.CurrentTaskInfo.OrderId = currentOrderId;
+                currentId = taskList[orderIndex].ID;
+                currentOrderId = taskList[orderIndex].OrderId;
+                CustomerEnv.CurrentTaskInfo.OrderSequence = taskList[orderIndex].OrderSequence;
+                //CustomerEnv.CurrentTaskInfo.OrderId = currentOrderId;
                 CustomerEnv.CurrentTaskInfo.MotorcycleTypeName = currentMotorcycleTypeName;
 
+                // 切换订单
+                DAL.ExecuteNonQuery($"UPDATE APP_TaiLing_TaskManagement SET OrderState_Str=CASE " +
+                    $"WHEN ID={currentId} THEN '生产中' ELSE '待生产' END " +
+                    $"WHERE WorkShopId_Str = '{CustomerEnv.ProductionLineConfigValue.WorkShopId}' " +
+                    $"AND ProductionLineId_Str = '{CustomerEnv.ProductionLineConfigValue.ProductionLineId}' " +
+                    $"AND OrderState_Str != '已生产'");
+                Log.Debug($"订单已切换为:{currentId}");
             }
             else
             {
+                System.Windows.Application.Current.Dispatcher.Invoke(() => {
+                    FrmShowMessage.ShowMessage("二维码信息不符合规则!", "二维码信息不符合规则!", SCADA.CommonCtrl.MessageLevel.Warn, 3000);
+                });                
                 Log.Warn("二维码信息不符合规则");
             }
 
-            if (currentOrderId == "")
+            if (currentId == -1)
                 return;
             var taskInfoItem = DAL.APP_TaiLing_TaskManagement.GetData<TaskManagementModel>()?
-                   .Where(t => t.OrderId == currentOrderId && t.WorkShopId == CustomerEnv.ProductionLineConfigValue.WorkShopId
+                   .Where(t => t.ID == currentId && t.WorkShopId == CustomerEnv.ProductionLineConfigValue.WorkShopId
                        && t.ProductionLineId == CustomerEnv.ProductionLineConfigValue.ProductionLineId)
                    .OrderBy(t => t.ID).LastOrDefault();
             if (taskInfoItem == null)
@@ -361,6 +326,9 @@ namespace SCADA_DAQ.Customer.Machines
         /// </summary>
         public void ImportSyncQueue()
         {
+            if (scanQueue.Count <= 0)   // 队列不为空
+                return;
+
             // 出队列
             ScanRecordModel scanItem = scanQueue.Dequeue() as ScanRecordModel;
             //CustomerEnv.ScanList.RemoveAt(0);
@@ -379,237 +347,63 @@ namespace SCADA_DAQ.Customer.Machines
             // 产品朔源记录
             DAL.APP_TaiLing_ProductionRecord.Insert(
                 new UpdateItem(T_Col_Name.APP_TaiLing_ProductionRecord.StartTime_Dt, DateTime.Now.Format()),
+                new UpdateItem(T_Col_Name.APP_TaiLing_ProductionRecord.EndTime_Dt, DateTime.Now.Format()),
                 new UpdateItem(T_Col_Name.APP_TaiLing_ProductionRecord.WorkShopId_Str, scanItem.WorkShopId),
                 new UpdateItem(T_Col_Name.APP_TaiLing_ProductionRecord.ProductionLineId_Str, scanItem.ProductionLineId),
                 new UpdateItem(T_Col_Name.APP_TaiLing_ProductionRecord.ProductId_Str, scanItem.ProductId)
             );
 
-            // 生产状态为“待生产” 
             var taskInfo = DAL.APP_TaiLing_TaskManagement.GetData<TaskManagementModel>()?
-                .Where(t => t.OrderId == scanItem.OrderId && t.WorkShopId == scanItem.WorkShopId
-                    && t.ProductionLineId == scanItem.ProductionLineId && t.OrderState == "待生产").ToList();
+                .Where(t => t.ID == scanItem.ID && t.WorkShopId == scanItem.WorkShopId
+                     && t.ProductionLineId == scanItem.ProductionLineId)
+                .OrderBy(t => t.ID).LastOrDefault();
             if (taskInfo == null)
-                return;
-            if (taskInfo.Count <= 0)
-                return;
-
-
-            // 开始时间是否为默认值 new DateTime()
-            var firstWorkingTask = taskInfo.Where(t => t.WorkStartTime == new DateTime()).ToList();
-            if (firstWorkingTask.Count > 0)
-            {
-                // 开始时间为默认值
-                // 第一次生产更新开始生产时间和生产状态
-                DAL.APP_TaiLing_TaskManagement.Update($"OrderId_Str='{scanItem.OrderId}' AND WorkShopId_Str='{scanItem.WorkShopId}' AND ProductionLineId_Str='{scanItem.ProductionLineId}'",
-                    new UpdateItem(SqlSchema.DBNames.T_Col_Name.APP_TaiLing_TaskManagement.OrderState_Str, "生产中"),
-                    new UpdateItem(SqlSchema.DBNames.T_Col_Name.APP_TaiLing_TaskManagement.WorkStartTime_Dt, DateTime.Now.Format())
-                );
-            }
-            else
             {
-                // 非第一次生产更新生产状态
-                DAL.APP_TaiLing_TaskManagement.Update($"OrderId_Str='{scanItem.OrderId}' AND WorkShopId_Str='{scanItem.WorkShopId}' AND ProductionLineId_Str='{scanItem.ProductionLineId}'",
-                    new UpdateItem(SqlSchema.DBNames.T_Col_Name.APP_TaiLing_TaskManagement.OrderState_Str, "生产中")
-                );
-            }
-        }
-
-        #endregion
-
-
-        #region 下料
-
-        /// <summary>
-        /// 下料
-        /// </summary>
-        /// <param name="e">扫码信息</param>
-        public void LineExport(DataReceivedEventArg e)
-        {
-            string scanMsg = e.ReceiveMsg;
-
-            if (IsDev)
-            {
-                // 测试
-                scanMsg = "33333333333";
-            }
-
-            // 条形码 
-            if (scanMsg == "")     // 空信息
-            {
-                Log.Debug("空信息");
-                return;
-            }
-            if (lastScanCode == scanMsg && !IsDev)  // 重复扫码,调试允许重复
-            {
-                Log.Debug("重复扫码");
+                System.Windows.Application.Current.Dispatcher.Invoke(() => {
+                    FrmShowMessage.ShowMessage("发生错误,请确认订单信息!", "发生错误,请确认订单信息!", SCADA.CommonCtrl.MessageLevel.Warn, 2500);
+                });                
+                Log.Warn("请确认订单信息!");
                 return;
             }
-            lastScanCode = scanMsg;
-
-            // 升序取最新的数据
-            ScanRecordModel scanRecord = DAL.APP_TaiLing_ScanRecord.GetData<ScanRecordModel>()?
-                .Where(t => t.ProductId == scanMsg && t.ScannerId == "上料扫码枪")
-                .OrderBy(t => t.ID).LastOrDefault(); 
-            if (scanRecord == null)
+            if (taskInfo.OrderState == "已生产")
             {
-                //CustomerEnv.CurrentOrderId = $"请先将“{scanMsg}”绑定车型";
-                //CustomerEnv.CurrentMotorcycleTypeName = "";
-                CustomerEnv.CurrentTaskInfo.OrderSequence = -1;
-                CustomerEnv.CurrentTaskInfo.OrderId = "";
-                CustomerEnv.CurrentTaskInfo.MotorcycleTypeName = $"请先将\r\n“{scanMsg}”\r\n绑定车型";
-                Log.Warn($"请先将“{scanMsg}”绑定车型");
+                System.Windows.Application.Current.Dispatcher.Invoke(() => {
+                    FrmShowMessage.ShowMessage("订单已完成!", "订单已完成!", SCADA.CommonCtrl.MessageLevel.Alarm, 2000);
+                });                
+                Log.Warn("订单已完成!");
                 return;
             }
 
-            currentOrderId = scanRecord.OrderId;
-            currentMotorcycleTypeName = scanRecord.MotorcycleTypeName;
-            //CustomerEnv.CurrentOrderId = currentOrderId;
-            //CustomerEnv.CurrentMotorcycleTypeName = currentMotorcycleTypeName;
-            CustomerEnv.CurrentTaskInfo.OrderId = currentOrderId;
-            CustomerEnv.CurrentTaskInfo.MotorcycleTypeName = currentMotorcycleTypeName;
-
-            var taskInfo = DAL.APP_TaiLing_TaskManagement.GetData<TaskManagementModel>()?
-                .Where(t => t.OrderId == currentOrderId
-                    && t.WorkShopId == CustomerEnv.ProductionLineConfigValue.WorkShopId
-                    && t.ProductionLineId == CustomerEnv.ProductionLineConfigValue.ProductionLineId)
-                .OrderBy(t => t.ID).Last();
-            CustomerEnv.CurrentTaskInfo.OrderSequence = taskInfo.OrderSequence;
-
-            // 进队列
-            scanQueue.Enqueue(new ScanRecordModel
-            {
-                ScannerId = "下料扫码枪",
-                ScanData = scanMsg,
-                WorkShopId = CustomerEnv.ProductionLineConfigValue.WorkShopId,
-                ProductionLineId = CustomerEnv.ProductionLineConfigValue.ProductionLineId,
-                ProductId = scanMsg,
-                OrderId = currentOrderId,
-                MotorcycleTypeName = currentMotorcycleTypeName
-            });
-            CustomerEnv.ScanList.Add(scanMsg);  // 显示用
-            if (CustomerEnv.ScanList.Count > 10)
-            {
-                CustomerEnv.ScanList.RemoveAt(0);
-            }
-        }
-
-        /// <summary>
-        /// 下料同步队列
-        /// </summary>
-        public void ExportSyncQueue()
-        {
-            // 出队列
-            ScanRecordModel scanItem = scanQueue.Dequeue() as ScanRecordModel;
-            //CustomerEnv.ScanList.RemoveAt(0);
-
-            // 保存下料记录
-            DAL.APP_TaiLing_ScanRecord.Insert(
-                new UpdateItem(T_Col_Name.APP_TaiLing_ScanRecord.ScannerId_Str, scanItem.ScannerId),
-                new UpdateItem(T_Col_Name.APP_TaiLing_ScanRecord.ScanData_Str, scanItem.ScanData),
-                new UpdateItem(T_Col_Name.APP_TaiLing_ScanRecord.WorkShopId_Str, scanItem.WorkShopId),
-                new UpdateItem(T_Col_Name.APP_TaiLing_ScanRecord.ProductionLineId_Str, scanItem.ProductionLineId),
-                new UpdateItem(T_Col_Name.APP_TaiLing_ScanRecord.ProductId_Str, scanItem.ProductId),
-                new UpdateItem(T_Col_Name.APP_TaiLing_ScanRecord.OrderId_Str, scanItem.OrderId),
-                new UpdateItem(T_Col_Name.APP_TaiLing_ScanRecord.MotorcycleTypeName_Str, scanItem.MotorcycleTypeName)
+            // 开始时间是否为默认值 new DateTime()            
+            if (taskInfo.WorkStartTime == new DateTime())
+                taskInfo.WorkStartTime = DateTime.Now;
+            DAL.APP_TaiLing_TaskManagement.Update($"ID={scanItem.ID}",
+                //new UpdateItem(SqlSchema.DBNames.T_Col_Name.APP_TaiLing_TaskManagement.OrderState_Str, "生产中"),
+                new UpdateItem(SqlSchema.DBNames.T_Col_Name.APP_TaiLing_TaskManagement.WorkStartTime_Dt, taskInfo.WorkStartTime.Format()),
+                new UpdateItem(T_Col_Name.APP_TaiLing_TaskManagement.CurrentProduction_Int, taskInfo.CurrentProduction + 1)
             );
-
-            var taskInfo = DAL.APP_TaiLing_TaskManagement.GetData<TaskManagementModel>()?
-                .Where(t => t.OrderId == scanItem.OrderId && t.WorkShopId == scanItem.WorkShopId
-                     && t.OrderState != "已生产" && t.ProductionLineId == scanItem.ProductionLineId)
-                .OrderBy(t => t.ID).LastOrDefault();
-            if (taskInfo == null)
-            {
-                Log.Warn($"“{scanItem.OrderId}”-查无生产的订单,请确认订单信息及状态!");
-                return;
-            }
-
-
-            // 更新工单
-            // 计数
-            //DAL.APP_TaiLing_TaskManagement.Update($"OrderId_Str='{scanItem.OrderId}' AND WorkShopId_Str='{scanItem.WorkShopId}' AND ProductionLineId_Str='{scanItem.ProductionLineId}'",
-            //    new UpdateItem(T_Col_Name.APP_TaiLing_TaskManagement.CurrentProduction_Int, taskInfo.CurrentProduction + 1));
-            DAL.APP_TaiLing_TaskManagement.Update($"ID={taskInfo.ID}",
-                new UpdateItem(T_Col_Name.APP_TaiLing_TaskManagement.CurrentProduction_Int, taskInfo.CurrentProduction + 1));
             CustomerEnv.CurrentTaskInfo.CurrentProduction = taskInfo.CurrentProduction + 1;
 
-
             if (taskInfo.CurrentProduction + 1 >= taskInfo.ScheduledProduction)
             {
                 // 结束生产 
                 var dateDiff = (DateTime.Now - taskInfo.WorkStartTime).TotalSeconds.ToInt();  // 时间差/s
-                //DAL.APP_TaiLing_TaskManagement.Update($"OrderId_Str='{scanItem.OrderId}' AND WorkShopId_Str='{scanItem.WorkShopId}' AND ProductionLineId_Str='{scanItem.ProductionLineId}'",
-                //    new UpdateItem(T_Col_Name.APP_TaiLing_TaskManagement.WorkEndTime_Dt, DateTime.Now.Format()),
-                //    new UpdateItem(T_Col_Name.APP_TaiLing_TaskManagement.DurationSec_Int, dateDiff),
-                //    new UpdateItem(T_Col_Name.APP_TaiLing_TaskManagement.OrderState_Str, "已生产")
-                //);
                 DAL.APP_TaiLing_TaskManagement.Update($"ID={taskInfo.ID}",
                     new UpdateItem(T_Col_Name.APP_TaiLing_TaskManagement.WorkEndTime_Dt, DateTime.Now.Format()),
                     new UpdateItem(T_Col_Name.APP_TaiLing_TaskManagement.DurationSec_Int, dateDiff),
                     new UpdateItem(T_Col_Name.APP_TaiLing_TaskManagement.OrderState_Str, "已生产")
                 );
-                //// 更新API缓存
-                //if (tableCacheItem != null)
-                //{
-                //    tableCacheItem.WorkEndTime = DateTime.Now;
-                //    tableCacheItem.DurationSec = dateDiff;
-                //    tableCacheItem.OrderState = "已生产";
-                //}
             }
 
-
             // 更新产品记录
             DAL.ExecuteNonQuery("UPDATE APP_TaiLing_ProductionRecord SET EndTime_Dt=GETDATE(), " +
                 "DurationSec_Int=DATEDIFF(s, StartTime_Dt, GETDATE()), ModifyTime_Dt=GETDATE() " +
                 $"WHERE ProductId_Str='{scanItem.ProductId}' AND WorkShopId_Str='{scanItem.WorkShopId}' " +
                 $"AND ProductionLineId_Str='{scanItem.ProductionLineId}'");
-
-
-            // 切换生产工单
-            //if (currentOrderId != lastOrderId)
-            if (taskInfo.ID != lastId)
-            {
-                // 表示该订单已被切换了                
-                //var oldTaskInfo = DAL.APP_TaiLing_TaskManagement.GetData<TaskManagementModel>()?
-                //    .Where(t => t.OrderId == lastOrderId).OrderBy(t => t.ID)
-                //    .Select(t => t.OrderState).LastOrDefault();
-                var oldTaskInfo = DAL.APP_TaiLing_TaskManagement.GetData<TaskManagementModel>()?
-                    .Where(t => t.ID == lastId).Select(t => t.OrderState).LastOrDefault();
-                //if (oldTaskInfo == null)
-                //    return;
-                if (oldTaskInfo == "生产中")
-                {
-                    //DAL.APP_TaiLing_TaskManagement.Update($"OrderId_Str='{lastOrderId}' AND WorkShopId_Str='{scanItem.WorkShopId}' AND ProductionLineId_Str='{scanItem.ProductionLineId}'",
-                    //    new UpdateItem(T_Col_Name.APP_TaiLing_TaskManagement.OrderState_Str, "待生产"));
-                    DAL.APP_TaiLing_TaskManagement.Update($"ID={lastId}",
-                        new UpdateItem(T_Col_Name.APP_TaiLing_TaskManagement.OrderState_Str, "待生产"));
-
-                }
-                //lastOrderId = currentOrderId;
-                lastId = taskInfo.ID;
-                Log.Debug($"工单已切换为:{lastOrderId}");
-            }
         }
 
-
         #endregion
 
-
-        ///// <summary>
-        ///// 取出缓存表的数据
-        ///// </summary>
-        ///// <param name="filter"></param>
-        ///// <returns></returns>
-        //private TaskManagementModel GetTableCacheItem(string filter)
-        //{
-        //    if (CustomerEnv.TaskInfoCache.Count <= 0)
-        //        return null;
-        //    var result = CustomerEnv.TaskInfoCache.Where(t => t.OrderId == filter)
-        //        .OrderBy(t => t.ID).LastOrDefault();
-        //    return result;
-        //}
-
-
-
         /// <summary>
         /// 释放非托管资源
         /// </summary>

+ 626 - 0
SCADA_DAQ/Customer/Machines/ScannerMachine_v1.cs

@@ -0,0 +1,626 @@
+/* 
+ * V1
+*/
+
+//using LiveCharts.Helpers;
+//using Microsoft.DwayneNeed.Numerics;
+//using Mysqlx.Crud;
+//using NPOI.SS.Formula.Functions;
+//using SCADA.BarcodeReader;
+//using SCADA.Comm;
+//using SCADA.CommonLib.Data;
+//using SCADA.CommonLib.Data.DIL;
+//using SCADA.CommonLib.Helper;
+//using SCADA.SECS;
+//using SCADA_DAQ.Customer.Models.DeviceManagement;
+//using SCADA_DAQ.Customer.Models.MotorcycleTypeManagement;
+//using SCADA_DAQ.Customer.Models.RecordManagement;
+//using SCADA_DAQ.Customer.Models.TaskManagement;
+//using SCADA_DAQ.Plugin.Machine;
+//using SCADA_DAQ.Plugin.Machine.Device;
+//using SqlSchema.DBNames;
+//using System;
+//using System.Collections;
+//using System.Collections.Generic;
+//using System.ComponentModel;
+//using System.Diagnostics;
+//using System.Linq;
+//using System.Runtime.ConstrainedExecution;
+//using System.Security.Cryptography;
+//using System.Text;
+//using System.Text.RegularExpressions;
+//using System.Threading.Tasks;
+//using System.Windows.Forms;
+//using Xceed.Wpf.Toolkit;
+
+//namespace SCADA_DAQ.Customer.Machines
+//{
+//    /// <summary>
+//    /// 二维扫码枪
+//    /// </summary>
+//    [DisplayName("二维扫码枪")]
+//    public class ScannerMachine : BaseBarcodeReaderMachine
+//    {
+//        // 数据库对象
+//        private static SqlSchema.DIL.DILDB DAL = Env.SgIDAL;
+//        private static DIL.DILDB sqliteDAL = Env.DAL;
+
+//        // 测试环境
+//        private static bool IsDev = false;
+
+//        // 工单切换索引
+//        private static int orderIndex = 0;
+//        // 当前生产的工单号
+//        private static string currentOrderId = "";
+//        // 当前生成的车型名称
+//        private static string currentMotorcycleTypeName = "";
+
+//        // 上次扫码号
+//        private static string lastScanCode = "";
+//        // 上次生产的工单号
+//        private static string lastOrderId = "";
+//        // 上次生产的工单号
+//        private static int lastId = -1;
+
+//        // 扫码队列
+//        Queue scanQueue = new Queue();
+
+//        //// 任务模型
+//        //private TaskManagementModel taskManagementModel = new TaskManagementModel();
+
+//        /// <summary>
+//        /// 构造函数
+//        /// </summary>
+//        /// <param name="id"></param>
+//        public ScannerMachine(string id) : base(id)
+//        {
+//            Env.Schedual.DateTimeChanged += Schedual_DateTimeChanged;   // 注册定时器事件
+
+//            //A = MachineServer.GetInstance().GetMachine<ScannerMachine>("BarcodeReader1");
+//            //A.BarCodeReceived += A_BarCodeReceived;
+
+//            //DAL.ReadDataTable();
+//            //DAL.ExecuteNonQuery("update APP_TaiLing_TaskManagement set CurrentProduction_Int=CurrentProduction_Int+1 where OrderState_Str='生产中' " +
+//            //    $"AND WorkShopId_Str='{scanItem.WorkShopId}' AND ProductionLineId_Str='{scanItem.ProductionLineId}'");
+
+//            Init();
+//            //if (lastOrderId == "")
+//            if (lastId == -1)
+//                Log.Warn("上次生产的工单号数据已丢失");
+//        }
+
+//        /// <summary>
+//        /// 定时器
+//        /// </summary>
+//        /// <param name="sender"></param>
+//        /// <param name="e"></param>
+//        private void Schedual_DateTimeChanged(object sender, SCADA.CommonLib.DateTimeChangedArgs e)
+//        {
+//            // e.ChangeType == SCADA.CommonLib.DateTimeChangeType.SecondChanged   秒变化
+//            // e.TimeFlag.Second   取值
+
+//            //if (e.TimeFlag.Second % 60 == 0)
+//            //{
+//            //    // 清空缓存
+//            //    CustomerEnv.TaskInfoCache.Clear();
+//            //}
+
+//            if (e.TimeFlag.Second % 1 == 0)
+//            {
+//                if (scanQueue.Count > 0)   // 队列不为空
+//                {
+//                    //ScanCounter();
+//                    if (BaseDevice.DeviceID == "上料扫码枪")
+//                    {
+//                        ImportSyncQueue();
+//                    }
+//                    else if (BaseDevice.DeviceID == "下料扫码枪")
+//                    {
+//                        ExportSyncQueue();
+//                    }
+//                }
+//            }
+
+//            if (IsDev)
+//            {
+//                // 测试
+//                if (e.TimeFlag.Second % 5 == 0)
+//                {
+//                    DataReceivedEventArg dr = new DataReceivedEventArg();
+//                    if (BaseDevice.DeviceID == "上料扫码枪")
+//                    {
+//                        LineImport(dr);
+//                    }
+//                    else if (BaseDevice.DeviceID == "下料扫码枪")
+//                    {
+//                        LineExport(dr);
+//                    }
+//                }
+//            }
+
+//        }
+
+//        /// <summary>
+//        /// 扫码事件
+//        /// </summary>
+//        /// <param name="e"></param>
+//        protected override void OnBarCodeReceived(DataReceivedEventArg e)
+//        {
+//            base.OnBarCodeReceived(e);
+//            if (BaseDevice.DeviceID == "上料扫码枪")
+//            {
+//                LineImport(e);
+//            }
+//            else if (BaseDevice.DeviceID == "下料扫码枪")
+//            {
+//                LineExport(e);
+//            }
+//            else
+//            {
+//                Log.Warn($"扫码枪ID无效 —— 当前扫码枪ID为{BaseDevice.DeviceID}");
+//            }
+//            // 持久化数据
+//            sqliteDAL.APP_TaiLing_ScanRecord.Insert(
+//                new UpdateItem(DBNames.T_Col_Name.APP_TaiLing_ScanRecord.ScannerId_Str, BaseDevice.DeviceID),
+//                new UpdateItem(DBNames.T_Col_Name.APP_TaiLing_ScanRecord.ScanData_Str, e.ReceiveMsg),
+//                new UpdateItem(DBNames.T_Col_Name.APP_TaiLing_ScanRecord.WorkShopId_Str, CustomerEnv.ProductionLineConfigValue.WorkShopId),
+//                new UpdateItem(DBNames.T_Col_Name.APP_TaiLing_ScanRecord.ProductionLineId_Str, CustomerEnv.ProductionLineConfigValue.ProductionLineId)
+//            );
+
+            
+//        }
+
+//        /// <summary>
+//        /// 初始化
+//        /// </summary>
+//        private void Init()
+//        {
+//            // 取上次的扫码记录
+//            var scanRecordItem = DAL.APP_TaiLing_ScanRecord.GetData<ScanRecordModel>()?
+//                    .Where(t => t.ScannerId == BaseDevice.DeviceID && t.WorkShopId == CustomerEnv.ProductionLineConfigValue.WorkShopId
+//                        && t.ProductionLineId == CustomerEnv.ProductionLineConfigValue.ProductionLineId)
+//                    .OrderBy(t => t.ID).LastOrDefault();
+//            if (scanRecordItem == null)
+//                return;
+
+//            var taskInfoItem = DAL.APP_TaiLing_TaskManagement.GetData<TaskManagementModel>()?
+//               .Where(t => t.OrderId == scanRecordItem.OrderId && t.WorkShopId == CustomerEnv.ProductionLineConfigValue.WorkShopId
+//                   && t.ProductionLineId == CustomerEnv.ProductionLineConfigValue.ProductionLineId)
+//               .OrderBy(t => t.ID).LastOrDefault();
+//            if (taskInfoItem == null)
+//                return;
+
+//            if (BaseDevice.DeviceID == "上料扫码枪")
+//            {
+//                currentOrderId = taskInfoItem.OrderId;
+//                currentMotorcycleTypeName = taskInfoItem.MotorcycleTypeName;
+//                CustomerEnv.CurrentTaskInfo.OrderSequence = taskInfoItem.OrderSequence;
+//                CustomerEnv.CurrentTaskInfo.OrderId = currentOrderId;
+//                CustomerEnv.CurrentTaskInfo.MotorcycleTypeName = currentMotorcycleTypeName;
+//                CustomerEnv.CurrentTaskInfo.CurrentProduction = taskInfoItem.CurrentProduction;
+//            }
+//            else if (BaseDevice.DeviceID == "下料扫码枪")
+//            {
+//                //lastOrderId = taskInfoItem.OrderId;
+//                lastId = taskInfoItem.ID;
+//                CustomerEnv.CurrentTaskInfo.OrderSequence = taskInfoItem.OrderSequence;
+//                CustomerEnv.CurrentTaskInfo.OrderId = taskInfoItem.OrderId;
+//                CustomerEnv.CurrentTaskInfo.MotorcycleTypeName = taskInfoItem.MotorcycleTypeName;
+//                CustomerEnv.CurrentTaskInfo.CurrentProduction = taskInfoItem.CurrentProduction;
+//            }
+            
+//        }
+
+
+//        #region 上料
+//        /// <summary>
+//        /// 上料
+//        /// </summary>
+//        /// <param name="e">扫码信息</param>
+//        public void LineImport(DataReceivedEventArg e)
+//        {
+//            string scanMsg = e.ReceiveMsg;
+
+//            if (IsDev)
+//            {
+//                // 测试
+//                scanMsg = "change&&豪鲨2.0优享E版HD&&H3014010";
+//                //scanMsg = "change&&夺冠超智版KS&&H3014010";
+//                //Random random = new Random();
+//                //scanMsg = $"{random.Next(100, 1000)}";
+//                //currentOrderId = "Order0001";
+//                //currentMotorcycleTypeName = "豪鲨2.0优享E版HD";
+//                //CustomerEnv.CurrentOrderId = currentOrderId;
+//                //CustomerEnv.CurrentMotorcycleTypeName = currentMotorcycleTypeName;
+//            }
+
+//            string[] QRInfo = Regex.Split(scanMsg, "&&", RegexOptions.None);
+
+
+//            if (QRInfo.Count() == 1)
+//            {
+//                // 条形码 
+//                if (QRInfo[0] == "")     // 空信息
+//                {
+//                    Log.Debug("空信息");
+//                    return;
+//                }
+//                if (lastScanCode == scanMsg)  // 重复扫码
+//                {
+//                    Log.Debug("重复扫码");
+//                    return;
+//                }
+//                lastScanCode = scanMsg;
+
+//                // 进队列
+//                scanQueue.Enqueue(new ScanRecordModel
+//                {
+//                    ScannerId = "上料扫码枪",
+//                    ScanData = scanMsg,
+//                    WorkShopId = CustomerEnv.ProductionLineConfigValue.WorkShopId,
+//                    ProductionLineId = CustomerEnv.ProductionLineConfigValue.ProductionLineId,
+//                    ProductId = QRInfo[0],
+//                    OrderId = currentOrderId,
+//                    MotorcycleTypeName = currentMotorcycleTypeName
+//                });
+//                CustomerEnv.ScanList.Add(scanMsg);  // 显示用
+//                if (CustomerEnv.ScanList.Count > 10)
+//                {
+//                    CustomerEnv.ScanList.RemoveAt(0);
+//                }
+//            }
+//            else if (QRInfo.Count() == 3 && QRInfo[0] == "change")
+//            {
+//                CustomerEnv.CurrentTaskInfo.OrderSequence = -1;
+//                CustomerEnv.CurrentTaskInfo.OrderId = "";
+//                CustomerEnv.CurrentTaskInfo.MotorcycleTypeName = "";
+
+//                // 换型码
+//                currentMotorcycleTypeName = QRInfo[1];
+
+//                //// 查询车型
+//                //currentMotorcycleTypeName = DAL.APP_TaiLing_MotorcycleTypeManagement.GetData<MotorcycleTypeModel>()?
+//                //    .Where(t => t.MotorcycleTypeId == QRInfo[2])
+//                //    .OrderBy(t => t.MotorcycleTypeId).Select(t => t.MotorcycleTypeName)
+//                //    .LastOrDefault();
+
+//                Log.Debug($"正在换型:{currentMotorcycleTypeName}");
+//                // 查询工单号
+//                var taskList = DAL.APP_TaiLing_TaskManagement.GetData<TaskManagementModel>()?
+//                    .Where(t => t.MotorcycleTypeName == currentMotorcycleTypeName
+//                        && t.OrderState != "已生产"
+//                        && t.WorkShopId == CustomerEnv.ProductionLineConfigValue.WorkShopId
+//                        && t.ProductionLineId == CustomerEnv.ProductionLineConfigValue.ProductionLineId)
+//                    .ToList();
+                
+//                if (taskList == null)
+//                {
+//                    CustomerEnv.CurrentTaskInfo.OrderSequence = -1;
+//                    CustomerEnv.CurrentTaskInfo.OrderId = "";
+//                    CustomerEnv.CurrentTaskInfo.MotorcycleTypeName = "查无工单任务\r\n请先添加工单";
+//                    Log.Warn("查无工单任务,请先添加工单");
+//                    return;
+//                }
+//                if (taskList.Count <= 0)
+//                {
+//                    CustomerEnv.CurrentTaskInfo.OrderSequence = -1;
+//                    CustomerEnv.CurrentTaskInfo.OrderId = "";
+//                    CustomerEnv.CurrentTaskInfo.MotorcycleTypeName = "查无工单任务\r\n请先添加工单";
+//                    Log.Warn("查无工单任务,请先添加工单");
+//                    return;
+//                }
+
+//                var OrderSequence = -1;
+//                // 工单切换
+//                if (lastScanCode != scanMsg)  // 换码
+//                {
+//                    orderIndex = 0;
+//                    lastScanCode = scanMsg;
+//                }
+//                if (currentOrderId != taskList[orderIndex].OrderId)
+//                {
+//                    currentOrderId = taskList[orderIndex].OrderId;
+//                    OrderSequence = taskList[orderIndex].OrderSequence;
+//                }
+//                else
+//                {
+//                    if (orderIndex < taskList.Count - 1)
+//                    {
+//                        orderIndex++;
+//                        currentOrderId = taskList[orderIndex].OrderId;
+//                        OrderSequence = taskList[orderIndex].OrderSequence;
+//                    }
+//                    else
+//                    {
+//                        // 循环切换
+//                        orderIndex = 0;
+//                        currentOrderId = taskList[orderIndex].OrderId;
+//                        OrderSequence = taskList[orderIndex].OrderSequence;
+//                    }
+//                }
+
+//                CustomerEnv.CurrentTaskInfo.OrderSequence = OrderSequence;
+//                CustomerEnv.CurrentTaskInfo.OrderId = currentOrderId;
+//                CustomerEnv.CurrentTaskInfo.MotorcycleTypeName = currentMotorcycleTypeName;
+
+//            }
+//            else
+//            {
+//                Log.Warn("二维码信息不符合规则");
+//            }
+
+//            if (currentOrderId == "")
+//                return;
+//            var taskInfoItem = DAL.APP_TaiLing_TaskManagement.GetData<TaskManagementModel>()?
+//                   .Where(t => t.OrderId == currentOrderId && t.WorkShopId == CustomerEnv.ProductionLineConfigValue.WorkShopId
+//                       && t.ProductionLineId == CustomerEnv.ProductionLineConfigValue.ProductionLineId)
+//                   .OrderBy(t => t.ID).LastOrDefault();
+//            if (taskInfoItem == null)
+//                return;
+//            CustomerEnv.CurrentTaskInfo.CurrentProduction = taskInfoItem.CurrentProduction;
+//        }
+
+//        /// <summary>
+//        /// 上料同步队列
+//        /// </summary>
+//        public void ImportSyncQueue()
+//        {
+//            // 出队列
+//            ScanRecordModel scanItem = scanQueue.Dequeue() as ScanRecordModel;
+//            //CustomerEnv.ScanList.RemoveAt(0);
+
+//            // 保存上料记录
+//            DAL.APP_TaiLing_ScanRecord.Insert(
+//                new UpdateItem(T_Col_Name.APP_TaiLing_ScanRecord.ScannerId_Str, scanItem.ScannerId),
+//                new UpdateItem(T_Col_Name.APP_TaiLing_ScanRecord.ScanData_Str, scanItem.ScanData),
+//                new UpdateItem(T_Col_Name.APP_TaiLing_ScanRecord.WorkShopId_Str, scanItem.WorkShopId),
+//                new UpdateItem(T_Col_Name.APP_TaiLing_ScanRecord.ProductionLineId_Str, scanItem.ProductionLineId),
+//                new UpdateItem(T_Col_Name.APP_TaiLing_ScanRecord.ProductId_Str, scanItem.ProductId),
+//                new UpdateItem(T_Col_Name.APP_TaiLing_ScanRecord.OrderId_Str, scanItem.OrderId),
+//                new UpdateItem(T_Col_Name.APP_TaiLing_ScanRecord.MotorcycleTypeName_Str, scanItem.MotorcycleTypeName)
+//            );
+
+//            // 产品朔源记录
+//            DAL.APP_TaiLing_ProductionRecord.Insert(
+//                new UpdateItem(T_Col_Name.APP_TaiLing_ProductionRecord.StartTime_Dt, DateTime.Now.Format()),
+//                new UpdateItem(T_Col_Name.APP_TaiLing_ProductionRecord.WorkShopId_Str, scanItem.WorkShopId),
+//                new UpdateItem(T_Col_Name.APP_TaiLing_ProductionRecord.ProductionLineId_Str, scanItem.ProductionLineId),
+//                new UpdateItem(T_Col_Name.APP_TaiLing_ProductionRecord.ProductId_Str, scanItem.ProductId)
+//            );
+
+//            // 生产状态为“待生产” 
+//            var taskInfo = DAL.APP_TaiLing_TaskManagement.GetData<TaskManagementModel>()?
+//                .Where(t => t.OrderId == scanItem.OrderId && t.WorkShopId == scanItem.WorkShopId
+//                    && t.ProductionLineId == scanItem.ProductionLineId && t.OrderState == "待生产").ToList();
+//            if (taskInfo == null)
+//                return;
+//            if (taskInfo.Count <= 0)
+//                return;
+
+
+//            // 开始时间是否为默认值 new DateTime()
+//            var firstWorkingTask = taskInfo.Where(t => t.WorkStartTime == new DateTime()).ToList();
+//            if (firstWorkingTask.Count > 0)
+//            {
+//                // 开始时间为默认值
+//                // 第一次生产更新开始生产时间和生产状态
+//                DAL.APP_TaiLing_TaskManagement.Update($"OrderId_Str='{scanItem.OrderId}' AND WorkShopId_Str='{scanItem.WorkShopId}' AND ProductionLineId_Str='{scanItem.ProductionLineId}'",
+//                    new UpdateItem(SqlSchema.DBNames.T_Col_Name.APP_TaiLing_TaskManagement.OrderState_Str, "生产中"),
+//                    new UpdateItem(SqlSchema.DBNames.T_Col_Name.APP_TaiLing_TaskManagement.WorkStartTime_Dt, DateTime.Now.Format())
+//                );
+//            }
+//            else
+//            {
+//                // 非第一次生产更新生产状态
+//                DAL.APP_TaiLing_TaskManagement.Update($"OrderId_Str='{scanItem.OrderId}' AND WorkShopId_Str='{scanItem.WorkShopId}' AND ProductionLineId_Str='{scanItem.ProductionLineId}'",
+//                    new UpdateItem(SqlSchema.DBNames.T_Col_Name.APP_TaiLing_TaskManagement.OrderState_Str, "生产中")
+//                );
+//            }
+//        }
+
+//        #endregion
+
+
+//        #region 下料
+
+//        /// <summary>
+//        /// 下料
+//        /// </summary>
+//        /// <param name="e">扫码信息</param>
+//        public void LineExport(DataReceivedEventArg e)
+//        {
+//            string scanMsg = e.ReceiveMsg;
+
+//            if (IsDev)
+//            {
+//                // 测试
+//                scanMsg = "33333333333";
+//            }
+
+//            // 条形码 
+//            if (scanMsg == "")     // 空信息
+//            {
+//                Log.Debug("空信息");
+//                return;
+//            }
+//            if (lastScanCode == scanMsg && !IsDev)  // 重复扫码,调试允许重复
+//            {
+//                Log.Debug("重复扫码");
+//                return;
+//            }
+//            lastScanCode = scanMsg;
+
+//            // 升序取最新的数据
+//            ScanRecordModel scanRecord = DAL.APP_TaiLing_ScanRecord.GetData<ScanRecordModel>()?
+//                .Where(t => t.ProductId == scanMsg && t.ScannerId == "上料扫码枪")
+//                .OrderBy(t => t.ID).LastOrDefault(); 
+//            if (scanRecord == null)
+//            {
+//                //CustomerEnv.CurrentOrderId = $"请先将“{scanMsg}”绑定车型";
+//                //CustomerEnv.CurrentMotorcycleTypeName = "";
+//                CustomerEnv.CurrentTaskInfo.OrderSequence = -1;
+//                CustomerEnv.CurrentTaskInfo.OrderId = "";
+//                CustomerEnv.CurrentTaskInfo.MotorcycleTypeName = $"请先将\r\n“{scanMsg}”\r\n绑定车型";
+//                Log.Warn($"请先将“{scanMsg}”绑定车型");
+//                return;
+//            }
+
+//            currentOrderId = scanRecord.OrderId;
+//            currentMotorcycleTypeName = scanRecord.MotorcycleTypeName;
+//            //CustomerEnv.CurrentOrderId = currentOrderId;
+//            //CustomerEnv.CurrentMotorcycleTypeName = currentMotorcycleTypeName;
+//            CustomerEnv.CurrentTaskInfo.OrderId = currentOrderId;
+//            CustomerEnv.CurrentTaskInfo.MotorcycleTypeName = currentMotorcycleTypeName;
+
+//            var taskInfo = DAL.APP_TaiLing_TaskManagement.GetData<TaskManagementModel>()?
+//                .Where(t => t.OrderId == currentOrderId
+//                    && t.WorkShopId == CustomerEnv.ProductionLineConfigValue.WorkShopId
+//                    && t.ProductionLineId == CustomerEnv.ProductionLineConfigValue.ProductionLineId)
+//                .OrderBy(t => t.ID).Last();
+//            CustomerEnv.CurrentTaskInfo.OrderSequence = taskInfo.OrderSequence;
+
+//            // 进队列
+//            scanQueue.Enqueue(new ScanRecordModel
+//            {
+//                ScannerId = "下料扫码枪",
+//                ScanData = scanMsg,
+//                WorkShopId = CustomerEnv.ProductionLineConfigValue.WorkShopId,
+//                ProductionLineId = CustomerEnv.ProductionLineConfigValue.ProductionLineId,
+//                ProductId = scanMsg,
+//                OrderId = currentOrderId,
+//                MotorcycleTypeName = currentMotorcycleTypeName
+//            });
+//            CustomerEnv.ScanList.Add(scanMsg);  // 显示用
+//            if (CustomerEnv.ScanList.Count > 10)
+//            {
+//                CustomerEnv.ScanList.RemoveAt(0);
+//            }
+//        }
+
+//        /// <summary>
+//        /// 下料同步队列
+//        /// </summary>
+//        public void ExportSyncQueue()
+//        {
+//            // 出队列
+//            ScanRecordModel scanItem = scanQueue.Dequeue() as ScanRecordModel;
+//            //CustomerEnv.ScanList.RemoveAt(0);
+
+//            // 保存下料记录
+//            DAL.APP_TaiLing_ScanRecord.Insert(
+//                new UpdateItem(T_Col_Name.APP_TaiLing_ScanRecord.ScannerId_Str, scanItem.ScannerId),
+//                new UpdateItem(T_Col_Name.APP_TaiLing_ScanRecord.ScanData_Str, scanItem.ScanData),
+//                new UpdateItem(T_Col_Name.APP_TaiLing_ScanRecord.WorkShopId_Str, scanItem.WorkShopId),
+//                new UpdateItem(T_Col_Name.APP_TaiLing_ScanRecord.ProductionLineId_Str, scanItem.ProductionLineId),
+//                new UpdateItem(T_Col_Name.APP_TaiLing_ScanRecord.ProductId_Str, scanItem.ProductId),
+//                new UpdateItem(T_Col_Name.APP_TaiLing_ScanRecord.OrderId_Str, scanItem.OrderId),
+//                new UpdateItem(T_Col_Name.APP_TaiLing_ScanRecord.MotorcycleTypeName_Str, scanItem.MotorcycleTypeName)
+//            );
+
+//            var taskInfo = DAL.APP_TaiLing_TaskManagement.GetData<TaskManagementModel>()?
+//                .Where(t => t.OrderId == scanItem.OrderId && t.WorkShopId == scanItem.WorkShopId
+//                     && t.OrderState != "已生产" && t.ProductionLineId == scanItem.ProductionLineId)
+//                .OrderBy(t => t.ID).LastOrDefault();
+//            if (taskInfo == null)
+//            {
+//                Log.Warn($"“{scanItem.OrderId}”-查无生产的订单,请确认订单信息及状态!");
+//                return;
+//            }
+
+
+//            // 更新工单
+//            // 计数
+//            //DAL.APP_TaiLing_TaskManagement.Update($"OrderId_Str='{scanItem.OrderId}' AND WorkShopId_Str='{scanItem.WorkShopId}' AND ProductionLineId_Str='{scanItem.ProductionLineId}'",
+//            //    new UpdateItem(T_Col_Name.APP_TaiLing_TaskManagement.CurrentProduction_Int, taskInfo.CurrentProduction + 1));
+//            DAL.APP_TaiLing_TaskManagement.Update($"ID={taskInfo.ID}",
+//                new UpdateItem(T_Col_Name.APP_TaiLing_TaskManagement.CurrentProduction_Int, taskInfo.CurrentProduction + 1));
+//            CustomerEnv.CurrentTaskInfo.CurrentProduction = taskInfo.CurrentProduction + 1;
+
+
+//            if (taskInfo.CurrentProduction + 1 >= taskInfo.ScheduledProduction)
+//            {
+//                // 结束生产 
+//                var dateDiff = (DateTime.Now - taskInfo.WorkStartTime).TotalSeconds.ToInt();  // 时间差/s
+//                //DAL.APP_TaiLing_TaskManagement.Update($"OrderId_Str='{scanItem.OrderId}' AND WorkShopId_Str='{scanItem.WorkShopId}' AND ProductionLineId_Str='{scanItem.ProductionLineId}'",
+//                //    new UpdateItem(T_Col_Name.APP_TaiLing_TaskManagement.WorkEndTime_Dt, DateTime.Now.Format()),
+//                //    new UpdateItem(T_Col_Name.APP_TaiLing_TaskManagement.DurationSec_Int, dateDiff),
+//                //    new UpdateItem(T_Col_Name.APP_TaiLing_TaskManagement.OrderState_Str, "已生产")
+//                //);
+//                DAL.APP_TaiLing_TaskManagement.Update($"ID={taskInfo.ID}",
+//                    new UpdateItem(T_Col_Name.APP_TaiLing_TaskManagement.WorkEndTime_Dt, DateTime.Now.Format()),
+//                    new UpdateItem(T_Col_Name.APP_TaiLing_TaskManagement.DurationSec_Int, dateDiff),
+//                    new UpdateItem(T_Col_Name.APP_TaiLing_TaskManagement.OrderState_Str, "已生产")
+//                );
+//                //// 更新API缓存
+//                //if (tableCacheItem != null)
+//                //{
+//                //    tableCacheItem.WorkEndTime = DateTime.Now;
+//                //    tableCacheItem.DurationSec = dateDiff;
+//                //    tableCacheItem.OrderState = "已生产";
+//                //}
+//            }
+
+
+//            // 更新产品记录
+//            DAL.ExecuteNonQuery("UPDATE APP_TaiLing_ProductionRecord SET EndTime_Dt=GETDATE(), " +
+//                "DurationSec_Int=DATEDIFF(s, StartTime_Dt, GETDATE()), ModifyTime_Dt=GETDATE() " +
+//                $"WHERE ProductId_Str='{scanItem.ProductId}' AND WorkShopId_Str='{scanItem.WorkShopId}' " +
+//                $"AND ProductionLineId_Str='{scanItem.ProductionLineId}'");
+
+
+//            // 切换生产工单
+//            //if (currentOrderId != lastOrderId)
+//            if (taskInfo.ID != lastId)
+//            {
+//                // 表示该订单已被切换了                
+//                //var oldTaskInfo = DAL.APP_TaiLing_TaskManagement.GetData<TaskManagementModel>()?
+//                //    .Where(t => t.OrderId == lastOrderId).OrderBy(t => t.ID)
+//                //    .Select(t => t.OrderState).LastOrDefault();
+//                var oldTaskInfo = DAL.APP_TaiLing_TaskManagement.GetData<TaskManagementModel>()?
+//                    .Where(t => t.ID == lastId).Select(t => t.OrderState).LastOrDefault();
+//                //if (oldTaskInfo == null)
+//                //    return;
+//                if (oldTaskInfo == "生产中")
+//                {
+//                    //DAL.APP_TaiLing_TaskManagement.Update($"OrderId_Str='{lastOrderId}' AND WorkShopId_Str='{scanItem.WorkShopId}' AND ProductionLineId_Str='{scanItem.ProductionLineId}'",
+//                    //    new UpdateItem(T_Col_Name.APP_TaiLing_TaskManagement.OrderState_Str, "待生产"));
+//                    DAL.APP_TaiLing_TaskManagement.Update($"ID={lastId}",
+//                        new UpdateItem(T_Col_Name.APP_TaiLing_TaskManagement.OrderState_Str, "待生产"));
+
+//                }
+//                //lastOrderId = currentOrderId;
+//                lastId = taskInfo.ID;
+//                Log.Debug($"工单已切换为:{lastOrderId}");
+//            }
+//        }
+
+
+//        #endregion
+
+
+//        ///// <summary>
+//        ///// 取出缓存表的数据
+//        ///// </summary>
+//        ///// <param name="filter"></param>
+//        ///// <returns></returns>
+//        //private TaskManagementModel GetTableCacheItem(string filter)
+//        //{
+//        //    if (CustomerEnv.TaskInfoCache.Count <= 0)
+//        //        return null;
+//        //    var result = CustomerEnv.TaskInfoCache.Where(t => t.OrderId == filter)
+//        //        .OrderBy(t => t.ID).LastOrDefault();
+//        //    return result;
+//        //}
+
+
+
+//        /// <summary>
+//        /// 释放非托管资源
+//        /// </summary>
+//        public override void Dispose()
+//        {
+//            Env.Schedual.DateTimeChanged -= Schedual_DateTimeChanged;  // 销毁定时器
+//            base.Dispose();
+//        }
+//    }
+//}

+ 5 - 5
SCADA_DAQ/Customer/UctFrmCodeScanningGun.xaml.cs

@@ -35,8 +35,8 @@ namespace SCADA_DAQ.Customer
     [UctMenu("扫码界面", Icon = "看板")]
     public partial class UctFrmCodeScanningGun : BaseUctFrm
     {
-        private ScannerMachine FirstMachinelCodeScanningSun = MachineServer.GetInstance().GetMachine<ScannerMachine>("上料扫码枪");
-        private ScannerMachine SencndMachinelCodeScanningSun = MachineServer.GetInstance().GetMachine<ScannerMachine>("下料扫码枪");
+        private ScannerMachine_v1 FirstMachinelCodeScanningSun = MachineServer.GetInstance().GetMachine<ScannerMachine_v1>("上料扫码枪");
+        private ScannerMachine_v1 SencndMachinelCodeScanningSun = MachineServer.GetInstance().GetMachine<ScannerMachine_v1>("下料扫码枪");
 
         private List<SolidColorBrush> brushesList = new List<SolidColorBrush>()
         {
@@ -68,9 +68,9 @@ namespace SCADA_DAQ.Customer
             //{
             //    System.Threading.Thread.Sleep(3000);
             //    // 应用程序当前线程
-            //    //Application.Current.Dispatcher.Invoke(()=>{ coding });
+            //    Application.Current.Dispatcher.Invoke(() => { coding });
             //});
-            
+
         }
 
         private void Machinel_BarCodeReceived(object sender, BarcodeReceivedEventArgs e)
@@ -86,7 +86,7 @@ namespace SCADA_DAQ.Customer
                     }
                     TextContentTime.Text = e.TimeStamp.ToLongTimeString();
                     // 全屏提示、语音播报
-                    FrmShowMessage.ShowMessage(e.Barcode, "扫码成功", SCADA.CommonCtrl.MessageLevel.Info, 3000);
+                    FrmShowMessage.ShowMessage(e.Barcode, "扫码成功", SCADA.CommonCtrl.MessageLevel.Info, 2500);
                 });
         }
 

+ 2 - 2
SCADA_DAQ/Env.cs

@@ -30,8 +30,8 @@ namespace SCADA_DAQ
 
 
         // SQL Server
-        //public static SqlSchema.DIL.DILDB SgIDAL = new SqlSchema.DIL.DILDB("127.0.0.1", "TL_DB", "sa", "M+123456");
-        public static SqlSchema.DIL.DILDB SgIDAL = new SqlSchema.DIL.DILDB(@"10.255.254.250", "TL_DB", "sa", "M+123456");
+        public static SqlSchema.DIL.DILDB SgIDAL = new SqlSchema.DIL.DILDB("127.0.0.1", "TL_DB", "sa", "M+123456");
+        //public static SqlSchema.DIL.DILDB SgIDAL = new SqlSchema.DIL.DILDB(@"10.255.254.250", "TL_DB", "sa", "M+123456");
 
 
         public static bool EnvInitFinish = false;