林豪 左 2 months ago
parent
commit
28fe8e7b8d

+ 85 - 81
YWGC/FJK/WCS.WorkEngineering/Systems/分拣主线/满轮主线预写入目标地址.cs

@@ -1,9 +1,9 @@
-using Newtonsoft.Json;
-using PlcSiemens.Core.Extension;
+using PlcSiemens.Core.Extension;
 using ServiceCenter.Extensions;
 using ServiceCenter.Logs;
 using ServiceCenter.SqlSugars;
 using SqlSugar;
+using System.Collections.Concurrent;
 using System.ComponentModel;
 using WCS.Core;
 using WCS.Entity;
@@ -25,74 +25,76 @@ namespace WCS.WorkEngineering.Systems
     {
         protected override bool ParallelDo => true;
 
+        private static Dictionary<string, string> CodeMap { get; set; } = new Dictionary<string, string>();
+        private static ConcurrentDictionary<string, fjSysConfig> FlowConfig { get; set; } = new ConcurrentDictionary<string, fjSysConfig>();
+
+        public 满轮主线预写入目标地址()
+        {
+            // 映射 Code 到 devCode 的映射关系
+            CodeMap = new Dictionary<string, string>
+            {
+                ["18"] = "22",
+                ["38"] = "41",
+                ["58"] = "61",
+                ["118"] = "122",
+                ["138"] = "141",
+                ["158"] = "161"
+            };
+        }
+
         public override void Do(Device<IStation523, IBCR83, IStation525> obj)
         {
             try
             {
-                var devCode = obj.Entity.Code switch
+                if (!CodeMap.TryGetValue(obj.Entity.Code, out var devCode))
                 {
-                    "18" => "22",
-                    "38" => "41",
-                    "58" => "61",
-                    "118" => "122",
-                    "138" => "141",
-                    "158" => "161",
-                    _ => throw new ArgumentOutOfRangeException()
-                };
+                    World.Log($"设备号[{obj.Entity.Code}]未初始化对应关系", LogLevelEnum.High);
+                    return;
+                }
+
                 var dev = new Device<IStation520>(Device.All.First(x => x.Code == devCode), World);
                 dev.Data.Mode = StationMode.Automatic;
                 //从DB83中获取待处理条码组
-                var pendingBcrList = obj.Data2.GetBcrCodeList().Where(x => !x.IsNullOrEmpty());
+                var pendingBcrList = obj.Data2.GetBcrCodeList().Where(x => !x.IsNullOrEmpty()).ToList();
                 //从DB525获取已处理条码组
-                var cacheBcrList = obj.Data3.GetBcrCodeList();
+                var cacheBcrList = obj.Data3.GetBcrCodeList().ToList();
                 //筛选出未处理条码组
-                var codes = pendingBcrList.Where(x => !cacheBcrList.Contains(x));
-                World.Log($"扫码器:{JsonConvert.SerializeObject(pendingBcrList)}");
-                World.Log($"缓存信息:{JsonConvert.SerializeObject(cacheBcrList)}");
+                var codes = pendingBcrList.Where(x => !cacheBcrList.Contains(x)).ToList();
+                fjSysConfig configFlowError = null;
+
                 //循环处理所有缓存条码组中没有的条码
                 foreach (var bcrCode in codes)
                 {
                     WCS_TaskInfo taskInfo = null;
-                    fjSysConfig config = new fjSysConfig();
-                    fjSysConfig configFlowError = new fjSysConfig();
+                    fjSysConfig config = null;
+
                     SqlSugarHelper.Do(_db =>
                     {
                         var db = _db.Default;
-                        var task = db.Queryable<WCS_TaskInfo>().NoLock().First(x => x.BarCode == bcrCode && x.Status == TaskStatus.NewBuild);
-                        if (task == null)
+                        taskInfo = db.Queryable<WCS_TaskInfo>().NoLock().First(x => x.BarCode == bcrCode && x.Status == TaskStatus.NewBuild);
+                        if (taskInfo == null)
                         {
-                            World.Log($"{bcrCode}:找不到匹配的任务", LogLevelEnum.High);
-                            var errorInfo = new BaseErrorInfo()
-                            {
-                                BusName = "主线分流",
-                                BarCode = bcrCode,
-                                Message = "找不到匹配的任务",
-                                Count = 1,
-                                AddTime = DateTime.Now,
-                                Memo = obj.Entity.Code
-                            };
-                            errorInfo.AddBaseErrorInfo(db);
-                            dev.Data.Mode = StationMode.Automatic;
+                            LogAndRecordError(bcrCode, "找不到匹配的任务", obj.Entity.Code);
                             return;
                         }
-                        var config1 = db.Queryable<fjSysConfig>().NoLock().First(x => x.Code == $"{task.WarehouseCode}-Flow");
-                        var config2 = db.Queryable<fjSysConfig>().NoLock().First(x => x.Code == "FlowErrorAllocation");
-                        
-                        if (config1 == null)
+
+                        var key = taskInfo.WarehouseCode + "-Flow";
+                        if (!FlowConfig.TryGetValue(key, out config))
                         {
-                            config.Code = "4";
+                            config = db.Queryable<fjSysConfig>().NoLock().First(x => x.Code == key) ?? new fjSysConfig { SContent = "4" };
+                            FlowConfig.AddOrUpdate(key, config, (k, v) => config);
                         }
-                        if (config2 == null)
+                        if (!db.Queryable<fjSysConfig>().NoLock().Any(x => x.Code == key && x.SContent == config.SContent))
                         {
-                            configFlowError.SContent = "3";
+                            config = db.Queryable<fjSysConfig>().NoLock().First(x => x.Code == key) ?? new fjSysConfig { SContent = "4" };
+                            FlowConfig.AddOrUpdate(key, config, (k, v) => config);
                         }
-                        else
+
+                        //获取排异错误
+                        if (configFlowError == null)
                         {
-                            configFlowError = config2;
+                            configFlowError = db.Queryable<fjSysConfig>().NoLock().First(x => x.Code == "FlowErrorAllocation") ?? new fjSysConfig { SContent = "3" };
                         }
-                        config = config1;                        
-                        
-                        taskInfo = task;
                     });
                     if (taskInfo == null)
                     {
@@ -113,7 +115,7 @@ namespace WCS.WorkEngineering.Systems
                     else if (obj.Entity.Code == "118" || obj.Entity.Code == "138" || obj.Entity.Code == "158")
                     {
                         if (taskInfo.WarehouseCode.Contains("N"))
-                        { 
+                        {
                             flowError = true;
                         }
                     }
@@ -121,17 +123,7 @@ namespace WCS.WorkEngineering.Systems
                     if ((path == null || path is { Points.Count: < 2 }) && !flowError)
                     {
                         var msg = $"{bcrCode}:路径错误,当前位置{obj.Entity.Code},目标位置:{srmCode}";
-                        World.Log(msg, LogLevelEnum.High);
-                        var errorInfo = new BaseErrorInfo()
-                        {
-                            BusName = "主线分流",
-                            BarCode = bcrCode,
-                            Message = msg,
-                            Count = 1,
-                            AddTime = DateTime.Now,
-                            Memo = obj.Entity.Code
-                        };
-                        errorInfo.AddBaseErrorInfo();
+                        LogAndRecordError(bcrCode, msg, obj.Entity.Code);
                         dev.Data.Mode = StationMode.Automatic;
                         continue;
                     }
@@ -162,7 +154,6 @@ namespace WCS.WorkEngineering.Systems
                             _ => next
                         };
                     }
-
                     if (flowError)
                     {
                         if (taskInfo.WarehouseCode.Contains("N"))
@@ -177,7 +168,7 @@ namespace WCS.WorkEngineering.Systems
                         }
                         else if (taskInfo.WarehouseCode.Contains("S"))
                         {
-                            next = config.SContent switch
+                            next = configFlowError.SContent switch
                             {
                                 "1" => "418",
                                 "2" => "818",
@@ -189,30 +180,12 @@ namespace WCS.WorkEngineering.Systems
 
                     #endregion 计算应该去哪个分拣库
 
-                    if (taskInfo == null)
-                    {
-                        dev.Data.Mode = StationMode.Automatic;
-                        continue;
-                    }
-
                     //开始赋值
-                    obj.Data3.GetType().GetProperty($"TaskNumber{obj.Data3.NextIndex}")
-                        .SetValue(obj.Data3, taskInfo.ID);
-                    obj.Data3.GetType().GetProperty($"TaskNumber{obj.Data3.NextIndex}")
-                        .SetValue(obj.Data3, taskInfo.ID);
-                    obj.Data3.GetType().GetProperty($"GoodsEnd{obj.Data3.NextIndex}")
-                        .SetValue(obj.Data3, next.ToShort());
-                    obj.Data3.GetType().GetProperty($"GoodsEnd{obj.Data3.NextIndex}")
-                        .SetValue(obj.Data3, next.ToShort());
-                    obj.Data3.GetType().GetProperty($"BcrCode{obj.Data3.NextIndex}").SetValue(obj.Data3, bcrCode);
-                    if (obj.Data3.NextIndex >= 49)
-                    {
-                        obj.Data3.NextIndex = 0;
-                    }
-                    else
-                    {
-                        obj.Data3.NextIndex++;
-                    }
+                    SetStation525Value(obj.Data3, "TaskNumber", taskInfo.ID);
+                    SetStation525Value(obj.Data3, "GoodsEnd", next.ToShort());
+                    SetStation525Value(obj.Data3, "BcrCode", bcrCode);
+                    obj.Data3.NextIndex = (short)(obj.Data3.NextIndex >= 49 ? 0 : obj.Data3.NextIndex + 1);
+
                     dev.Data.Mode = StationMode.Automatic;
                 }
                 dev.Data.Mode = StationMode.Automatic;
@@ -232,6 +205,37 @@ namespace WCS.WorkEngineering.Systems
 
             return codes.Contains(dev.Code);
         }
+
+        private void LogAndRecordError(string barcode, string message, string memo)
+        {
+            World.Log($"{barcode}:{message}", LogLevelEnum.High);
+            var errorInfo = new BaseErrorInfo()
+            {
+                BusName = "主线分流",
+                BarCode = barcode,
+                Message = message,
+                Count = 1,
+                AddTime = DateTime.Now,
+                Memo = memo
+            };
+            errorInfo.AddBaseErrorInfo();
+        }
+
+        /// <summary>
+        ///  设置站525的值
+        /// </summary>
+        /// <param name="data3"></param>
+        /// <param name="propertyName"></param>
+        /// <param name="value"></param>
+        private void SetStation525Value(IStation525 data3, string propertyName, object value)
+        {
+            var property = data3.GetType().GetProperty($"{propertyName}{data3.NextIndex}");
+            if (property != null && property.CanWrite)
+            {
+                property.SetValue(data3, value);
+                property.SetValue(data3, value);
+            }
+        }
     }
 
     /// <summary>