From b5234c5ab1e9e6826b8d8fc1e95fa752aaa40b74 Mon Sep 17 00:00:00 2001
From: chr <chr@mailcom>
Date: 星期六, 02 十一月 2024 10:56:50 +0800
Subject: [PATCH] 优化程序

---
 PdmSwPlugin.PDM/PdmControl.xaml.cs |  364 ++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 272 insertions(+), 92 deletions(-)

diff --git a/PdmSwPlugin.PDM/PdmControl.xaml.cs b/PdmSwPlugin.PDM/PdmControl.xaml.cs
index 6f5a86b..6e6148a 100644
--- a/PdmSwPlugin.PDM/PdmControl.xaml.cs
+++ b/PdmSwPlugin.PDM/PdmControl.xaml.cs
@@ -39,6 +39,7 @@
 using System.Windows.Input;
 using NPOI.POIFS.Properties;
 using NPOI.POIFS.Crypt;
+using System.Threading.Tasks.Schedulers;
 
 namespace PdmSwPlugin.PDM
 {
@@ -144,11 +145,11 @@
                     this.Warning("璇锋墦寮�涓�寮犲浘绾�");
                     return new ObservableCollection<PdmBom>();
                 }
-                if (7 == doc.GetBlockingState())
-                {
-                    this.Warning("璇风瓑寰呮枃妗e姞杞藉畬姣�");
-                    return new ObservableCollection<PdmBom>();
-                }
+                //if (7 == doc.GetBlockingState())
+                //{
+                //    this.Warning("璇风瓑寰呮枃妗e姞杞藉畬姣�");
+                //    return new ObservableCollection<PdmBom>();
+                //}
 
                 DateTime beforeDT = DateTime.Now;
 
@@ -896,7 +897,7 @@
 
         public Dictionary<string, DrawInfo> GetAllProperties(List<string> ErrMsgs)
         {
-            Dictionary<string, DrawInfo> result = new Dictionary<string, DrawInfo>();
+            ConcurrentDictionary<string, DrawInfo> result = new ConcurrentDictionary<string, DrawInfo>();
             PdmBom topBom = model.BomTree[0];
             CountdownEvent counter = new CountdownEvent(1);
             _ = ThreadPool.QueueUserWorkItem(
@@ -906,7 +907,65 @@
                 );
             counter.Wait();
             counter.Dispose();
-            return result;
+            return new Dictionary<string, DrawInfo>(result);
+        }
+
+        /// <summary>
+        /// 鑾峰彇鍏ㄩ儴灞炴��
+        /// </summary>
+        /// <param name="param"></param>
+        public void GetAll(object param)
+        {
+            object[] datas = param as object[];
+            PdmBom bom = (PdmBom)datas[0];
+            ModelDoc2 sldDoc = bom.doc;
+            Component2 component = bom.component;
+            ConcurrentDictionary<string, DrawInfo> result = (ConcurrentDictionary<string, DrawInfo>)datas[1];
+            CountdownEvent counter = (CountdownEvent)datas[2];
+            List<string> ErrMsgs = (List<string>)datas[3];
+            int level = (int)datas[4];
+            try
+            {
+                counter.TryAddCount();
+                string filepath = bom.partNo;
+
+
+                if (result.ContainsKey(filepath))
+                {
+                    return;
+                }
+
+                if (!bom.skipCheck)
+                {
+                    result[filepath] = GetSingleDrawInfo(bom);
+                }
+
+                ObservableCollection<PdmBom> children = bom.modules;
+                if (children != null)
+                {
+                    foreach (PdmBom child in children)
+                    {
+                        _ = ThreadPool.QueueUserWorkItem(
+                              new WaitCallback(GetAll),
+                              new object[] { child, result, counter, ErrMsgs, 1 });
+                    }
+                }
+                MaskAdorner.ShowMessage(content, filepath);
+            }
+            catch (Exception e)
+            {
+                bom.drawInfo.checkRuleResult = 2;
+                bom.drawInfo.errMessages = new List<string> { e.Message };
+                Logger.Error($"V{PdmUser.LoginUser.pluginVersion},[{bom.filePath}] Read MASS Properies Failed.", e);
+            }
+            finally
+            {
+                counter.Signal();
+                if (level == 0)
+                {
+                    counter.Signal();
+                }
+            }
         }
 
         /// <summary>
@@ -926,6 +985,7 @@
             Dictionary<string, string> allProperties = new Dictionary<string, string>();
             allProperties["鐗╂枡鍨嬪彿"] = bom.partModel;
             DrawInfo drawInfo = bom._drawInfo;
+            drawInfo.isHidden = bom.isHidden;
             drawInfo.allProperties = allProperties;
             allProperties["鍘嬬缉"] = bom.suppressed.ToString();
             allProperties["杞婚噺鍖�"] = bom.lightWeight.ToString();
@@ -947,7 +1007,7 @@
             // 宸ョ▼鍥句竴鑷存�ф鏌�
             allProperties["宸ョ▼鍥炬槸鍚﹀瓨鍦�"] = drawExists.ToString();
             allProperties["md5"] = bom.localD3Md5;
-            if (bom.lost || sldDoc == null)
+            if (bom.lost || bom.BomInfo.isHidden || sldDoc == null)
             {
                 allProperties["鍥剧焊鏄惁瀛樺湪"] = "false";
                 return drawInfo;
@@ -1005,17 +1065,28 @@
             return drawInfo;
         }
 
-        /// <summary>
-        /// 鑾峰彇鍏ㄩ儴灞炴��
-        /// </summary>
-        /// <param name="param"></param>
-        public void GetAll(object param)
+        public Dictionary<string, DrawInfo> GetAllPropertiesByTask(List<string> ErrMsgs)
+        {
+            ConcurrentDictionary<string, DrawInfo> result = new ConcurrentDictionary<string, DrawInfo>();
+            PdmBom topBom = model.BomTree[0];
+            CountdownEvent counter = new CountdownEvent(1);
+            Task.Run(() =>
+            {
+                GetAllByTask(new object[] {
+                topBom, result, counter, ErrMsgs, 0 });
+            });
+            counter.Wait();
+            counter.Dispose();
+            return new Dictionary<string, DrawInfo>(result);
+        }
+
+        public void GetAllByTask(object param)
         {
             object[] datas = param as object[];
             PdmBom bom = (PdmBom)datas[0];
             ModelDoc2 sldDoc = bom.doc;
             Component2 component = bom.component;
-            Dictionary<string, DrawInfo> result = (Dictionary<string, DrawInfo>)datas[1];
+            ConcurrentDictionary<string, DrawInfo> result = (ConcurrentDictionary<string, DrawInfo>)datas[1];
             CountdownEvent counter = (CountdownEvent)datas[2];
             List<string> ErrMsgs = (List<string>)datas[3];
             int level = (int)datas[4];
@@ -1023,24 +1094,34 @@
             {
                 counter.TryAddCount();
                 string filepath = bom.partNo;
-                if (result.ContainsKey(filepath))
+                lock (result)
                 {
-                    return;
+                    if (result.ContainsKey(filepath))
+                    {
+                        if (bom.isHidden)
+                        {
+                            result[filepath].isHidden = true;
+                        }
+                        return;
+                    }
+                    if (!bom.skipCheck)
+                    {
+                        result[filepath] = GetSingleDrawInfo(bom);
+                    }
                 }
 
-                if (!bom.skipCheck)
-                {
-                    result[filepath] = GetSingleDrawInfo(bom);
-                }
+
+
 
                 ObservableCollection<PdmBom> children = bom.modules;
                 if (children != null)
                 {
                     foreach (PdmBom child in children)
                     {
-                        _ = ThreadPool.QueueUserWorkItem(
-                              new WaitCallback(GetAll),
-                              new object[] { child, result, counter, ErrMsgs, 1 });
+                        Task.Run(() =>
+                        {
+                            GetAllByTask(new object[] { child, result, counter, ErrMsgs, 1 });
+                        });
                     }
                 }
                 MaskAdorner.ShowMessage(content, filepath);
@@ -1054,7 +1135,10 @@
             finally
             {
                 counter.Signal();
-                if (level == 0) counter.Signal();
+                if (level == 0)
+                {
+                    counter.Signal();
+                }
             }
         }
 
@@ -1096,78 +1180,127 @@
             return allSuccess;
         }
 
-        public void sb(dynamic[] datas) {
-            /*Dictionary<string, DrawInfo> properties = datas[0];
-            string key = datas[1];
-            KeyValuePair<string, DrawInfo> property = datas[2];
-            List<string> temp = datas[3];
-            CountdownEvent counter = datas[4];
-            try
-            {
-               
-                int iii, ref bool allSuccess
-
-                Logger.Info($"{key} checking rule...");
-                iii++;
-                Result<DrawInfo> result = Client.PostSyncAction<DrawInfo>(property.Value, "wpf/bom/openApi/checkRuleSingle");
-                DrawInfo checkResult = result.HandleResult();
-                properties[key].checkRuleResult = checkResult.checkRuleResult;
-                properties[key].errMessages = checkResult.errMessages;
-                var msgs = checkResult.errMessages;
-                if (!properties[key].HistoryData && !checkResult.checkRuleOk)
-                {
-                    allSuccess = false;
-                }
-
-                if (!properties[key].HistoryData && msgs != null && msgs.Count > 0)
-                {
-                    lock (temp)
-                    {
-                        foreach (string msg in msgs)
-                        {
-                            temp.Add($"鐗╂枡銆恵key}銆戞湭閫氳繃瑙勫垯妫�鏌ワ紒{msg}");
-                        }
-                    }
-                }
-                MaskAdorner.ShowMessage(content, $"鐗╂枡銆恵key}銆戞鏌ュ畬鎴�");
-            }
-            catch (Exception ex)
-            {
-                properties[key].checkRuleResult = 2;
-                properties[key].errMessages = new List<string> { "鍙戣捣妫�鏌ュけ璐ワ紝璇烽噸璇�" };
-                lock (temp)
-                {
-                    temp.Add($"鐗╂枡銆恵key}銆戝彂璧锋鏌ュけ璐ワ紝璇烽噸璇�");
-                }
-                allSuccess = false;
-                MaskAdorner.ShowMessage(content, $"鐗╂枡銆恵key}銆戞鏌ュ畬鎴�");
-            }
-            finally
-            {
-                counter.Signal();
-            }*/
-        }
-
         public bool DoCheckRuleAsync(out List<string> messages)
         {
             var temp = new List<string>();
             bool allSuccess = true;
-            Dictionary<string, DrawInfo> properties = GetAllProperties(temp);
+            Dictionary<string, DrawInfo> properties = GetAllPropertiesByTask(temp);
             MaskAdorner.ShowMessage(content, "姝e湪璇锋眰鏁版嵁...");
-            var factory = LimitedConcurrencyLevelTaskScheduler.Factory;
+           // var factory = LimitedConcurrencyLevelTaskScheduler.Factory;
             CountdownEvent counter = new CountdownEvent(properties.Count);
-            int iii = 1;
+            int iii = 0;
             foreach (KeyValuePair<string, DrawInfo> property in properties)
             {
-                string key = property.Key;
-                _ = ThreadPool.QueueUserWorkItem(
-                (WaitCallback)delegate
+                /// 杩欎釜鏂规硶鏈�蹇紝浣嗕笉濂界鎺э紝鍥犱负涓嶇煡閬撳睘鎬у瓧鍏告湁澶氬ぇ
+                Task.Run(() =>
                 {
-
+                    string key = property.Key;
+                    iii++;
                     try
                     {
                         Logger.Error($"{key} checking rule...");
-                        iii++;
+                        Result<DrawInfo> result = Client.PostSyncAction<DrawInfo>(property.Value, "wpf/bom/openApi/checkRuleSingle");
+                        DrawInfo checkResult = result.HandleResult();
+                        properties[key].checkRuleResult = checkResult.checkRuleResult;
+                        properties[key].errMessages = checkResult.errMessages;
+                        var msgs = checkResult.errMessages;
+                        if (!properties[key].HistoryData && (!checkResult.checkRuleOk||property.Value.isHidden))
+                        {
+                            allSuccess = false;
+                        }
+
+                        if (!properties[key].HistoryData)
+                        {
+                            lock (temp)
+                            {
+                                if (property.Value.isHidden)
+                                {
+                                    temp.Add($"鐗╂枡銆恵key}銆戣璁剧疆涓洪殣钘�!");
+                                }
+                                if (msgs != null && msgs.Count > 0)
+                                {
+                                    foreach (string msg in msgs)
+                                    {
+                                        temp.Add($"鐗╂枡銆恵key}銆戞湭閫氳繃瑙勫垯妫�鏌ワ紒{msg}");
+                                    }
+                                }
+                            }
+                        }
+                        MaskAdorner.ShowMessage(content, $"鐗╂枡銆恵key}銆戞鏌ュ畬鎴�");
+                    }
+                    catch (Exception ex)
+                    {
+                        properties[key].checkRuleResult = 2;
+                        properties[key].errMessages = new List<string> { "鍙戣捣妫�鏌ュけ璐ワ紝璇烽噸璇�" };
+                        lock (temp)
+                        {
+                            temp.Add($"鐗╂枡銆恵key}銆戝彂璧锋鏌ュけ璐ワ紝璇烽噸璇�");
+                        }
+                        allSuccess = false;
+                        MaskAdorner.ShowMessage(content, $"鐗╂枡銆恵key}銆戞鏌ュ畬鎴�");
+                    }
+                    finally
+                    {
+                        counter.Signal();
+                    }
+                });
+
+                //LimitedConcurrencyLevelTaskScheduler scheduler = new LimitedConcurrencyLevelTaskScheduler(int.MaxValue);
+                //Task.Factory.StartNew(() => {
+                //    string key = property.Key;
+                //    iii++;
+                //    try
+                //    {
+                //        Logger.Error($"{key} checking rule...");
+                //        Result<DrawInfo> result = Client.PostSyncAction<DrawInfo>(property.Value, "wpf/bom/openApi/checkRuleSingle");
+                //        DrawInfo checkResult = result.HandleResult();
+                //        properties[key].checkRuleResult = checkResult.checkRuleResult;
+                //        properties[key].errMessages = checkResult.errMessages;
+                //        var msgs = checkResult.errMessages;
+                //        if (!properties[key].HistoryData && !checkResult.checkRuleOk)
+                //        {
+                //            allSuccess = false;
+                //        }
+
+                //        if (!properties[key].HistoryData && msgs != null && msgs.Count > 0)
+                //        {
+                //            lock (temp)
+                //            {
+                //                foreach (string msg in msgs)
+                //                {
+                //                    temp.Add($"鐗╂枡銆恵key}銆戞湭閫氳繃瑙勫垯妫�鏌ワ紒{msg}");
+                //                }
+                //            }
+                //        }
+                //        MaskAdorner.ShowMessage(content, $"鐗╂枡銆恵key}銆戞鏌ュ畬鎴�");
+                //    }
+                //    catch (Exception ex)
+                //    {
+                //        properties[key].checkRuleResult = 2;
+                //        properties[key].errMessages = new List<string> { "鍙戣捣妫�鏌ュけ璐ワ紝璇烽噸璇�" };
+                //        lock (temp)
+                //        {
+                //            temp.Add($"鐗╂枡銆恵key}銆戝彂璧锋鏌ュけ璐ワ紝璇烽噸璇�");
+                //        }
+                //        allSuccess = false;
+                //        MaskAdorner.ShowMessage(content, $"鐗╂枡銆恵key}銆戞鏌ュ畬鎴�");
+                //    }
+                //    finally
+                //    {
+                //        counter.Signal();
+                //    }
+                //}, CancellationToken.None, TaskCreationOptions.None, scheduler);
+
+
+                /// 涓嬮潰涓や釜閮藉湪绠℃帶Task鏁伴噺锛屼絾杩樻槸寰堟參
+                /*string key = property.Key;
+                _ = ThreadPool.QueueUserWorkItem(
+                (WaitCallback)delegate
+                {
+                    iii++;
+                    try
+                    {
+                        Logger.Error($"{key} checking rule...");
                         Result<DrawInfo> result = Client.PostSyncAction<DrawInfo>(property.Value, "wpf/bom/openApi/checkRuleSingle");
                         DrawInfo checkResult = result.HandleResult();
                         properties[key].checkRuleResult = checkResult.checkRuleResult;
@@ -1205,12 +1338,53 @@
                     {
                         counter.Signal();
                     }
-                });
+                });*/
 
-                /* factory.StartNew(() =>
-                 {
+                //factory.StartNew(() =>
+                //{
+                //    string key = property.Key;
+                //    iii++;
+                //    try
+                //    {
+                //        Logger.Error($"{key} checking rule...");
+                //        Result<DrawInfo> result = Client.PostSyncAction<DrawInfo>(property.Value, "wpf/bom/openApi/checkRuleSingle");
+                //        DrawInfo checkResult = result.HandleResult();
+                //        properties[key].checkRuleResult = checkResult.checkRuleResult;
+                //        properties[key].errMessages = checkResult.errMessages;
+                //        var msgs = checkResult.errMessages;
+                //        if (!properties[key].HistoryData && !checkResult.checkRuleOk)
+                //        {
+                //            allSuccess = false;
+                //        }
 
-                 });*/
+                //        if (!properties[key].HistoryData && msgs != null && msgs.Count > 0)
+                //        {
+                //            lock (temp)
+                //            {
+                //                foreach (string msg in msgs)
+                //                {
+                //                    temp.Add($"鐗╂枡銆恵key}銆戞湭閫氳繃瑙勫垯妫�鏌ワ紒{msg}");
+                //                }
+                //            }
+                //        }
+                //        MaskAdorner.ShowMessage(content, $"鐗╂枡銆恵key}銆戞鏌ュ畬鎴�");
+                //    }
+                //    catch (Exception ex)
+                //    {
+                //        properties[key].checkRuleResult = 2;
+                //        properties[key].errMessages = new List<string> { "鍙戣捣妫�鏌ュけ璐ワ紝璇烽噸璇�" };
+                //        lock (temp)
+                //        {
+                //            temp.Add($"鐗╂枡銆恵key}銆戝彂璧锋鏌ュけ璐ワ紝璇烽噸璇�");
+                //        }
+                //        allSuccess = false;
+                //        MaskAdorner.ShowMessage(content, $"鐗╂枡銆恵key}銆戞鏌ュ畬鎴�");
+                //    }
+                //    finally
+                //    {
+                //        counter.Signal();
+                //    }
+                //});
             }
             counter.Wait();
             counter.Dispose();
@@ -1245,7 +1419,7 @@
                 try
                 {
                     RefreshBomList(SwApp.IActiveDoc2);
-                    if (!DoCheckRule(out List<string> msgs))
+                    if (!DoCheckRuleAsync(out List<string> msgs))
                     {
                         Dispatcher.Invoke(() =>
                         {
@@ -1615,13 +1789,19 @@
                     return;
                 }
 
-                if (bom.checkRuleOk)
+                if (bom.checkRuleOk && !bom.isHidden)
                 {
                     this.Show("瑙勫垯妫�鏌ラ�氳繃");
                 }
                 else
                 {
-                    MultiExWindow exWin = new MultiExWindow(this, "妫�鏌ョ粨鏋�", bom.drawInfo.errMessages);
+                    List<string> errs = new List<string>();
+                    if (bom.isHidden)
+                    {
+                        errs.Add("鏂囨。琚缃负闅愯棌");
+                    }
+                    errs.AddRange(bom.drawInfo.errMessages);
+                    MultiExWindow exWin = new MultiExWindow(this, "妫�鏌ョ粨鏋�", errs);
                     exWin.ShowDialog();
                 }
             }
@@ -1817,7 +1997,7 @@
                         {
                             throw new CantCheckInException(new List<string> { "璇锋墦寮�涓�寮犲浘绾�" });
                         }
-                        DoCheckRule(out _);
+                        DoCheckRuleAsync(out _);
                         // 绛涢�夋墍鏈夊湪鐗╂枡鏄庣粏琛ㄤ腑锛屼笉璺宠繃妫�鏌ュ苟涓旀鏌ユ湭閫氳繃鐨勶紝绫诲瀷鏄姞宸ヤ欢鐨勬湭閫氳繃瀹℃牳鐨勶紝鍔犲伐浠跺鏍搁�氳繃鐨勪絾鍥剧焊鍙樻洿鐨�
                         List<string> errs = model.bomTreeReader.CacheList.Select(b => b.NeedStopCheckIn())
                         .Where(s => s != null).ToList();

--
Gitblit v1.9.1