From 3322022068ab818912d6f5d9e5eb31f51cc639c2 Mon Sep 17 00:00:00 2001
From: patrick.xu <patrick.xu@broconcentric.com>
Date: 星期四, 24 二月 2022 09:12:30 +0800
Subject: [PATCH] 添加配置文件的DataShift2

---
 src/Bro.Common.Model/Helper/SettingHelper.cs |  209 ++++++++++++++++++++++++++++++----------------------
 1 files changed, 120 insertions(+), 89 deletions(-)

diff --git a/src/Bro.Common.Model/Helper/SettingHelper.cs b/src/Bro.Common.Model/Helper/SettingHelper.cs
index 59ffa67..f22e684 100644
--- a/src/Bro.Common.Model/Helper/SettingHelper.cs
+++ b/src/Bro.Common.Model/Helper/SettingHelper.cs
@@ -7,135 +7,166 @@
 
 namespace Bro.Common.Helper
 {
-    public class SettingHelper
+    public static class SettingHelper
     {
         const string SETTINGFILE = "Setting.json";
-        const string PROPERTY_PRODUCTIONCODES = "ProductionCodes";
-        const string PROPERTY_CONFIGPATH = "ConfigPath";
-        const string PROPERTY_PROCESSCODES = "ProcessCodes";
-        const string PROPERTY_DESCRIPTION = "Description";
-        const string PROPERTY_ICONPATH = "IconPath";
 
-        static JObject data = null;
-        static JObject Data
+        public static SettingInfo SettingInfo = null;
+        static string SettingPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SETTINGFILE);
+
+        static SettingHelper()
         {
-            get
+            if (File.Exists(SettingPath))
             {
-                string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SETTINGFILE);
-                if (File.Exists(configPath))
+                using (StreamReader reader = new StreamReader(SettingPath, System.Text.Encoding.UTF8))
                 {
-                    using (StreamReader reader = new StreamReader(configPath, System.Text.Encoding.UTF8))
-                    {
-                        string dataStr = reader.ReadToEnd();
-                        data = JsonConvert.DeserializeObject<JObject>(dataStr);
-                    }
+                    string dataStr = reader.ReadToEnd();
+                    SettingInfo = JsonConvert.DeserializeObject<SettingInfo>(dataStr);
                 }
-
-                return data;
             }
         }
 
         public static List<string> GetProductionCodes()
         {
-            List<string> codes = new List<string>();
-
-            if (Data != null && Data.ContainsKey(PROPERTY_PRODUCTIONCODES))
+            if (SettingInfo.ProductionCodes.Count == 0)
             {
-                codes = Data.Value<JArray>(PROPERTY_PRODUCTIONCODES).ToList().ConvertAll(u => u.ToString());
+                SettingInfo.ProductionCodes.Add("Default");
             }
 
-            if (codes.Count == 0)
+            if (!string.IsNullOrWhiteSpace(SettingInfo.DefaultProduction))
             {
-                codes.Add("Default");
+                if (SettingInfo.ProductionCodes.Contains(SettingInfo.DefaultProduction))
+                {
+                    SettingInfo.ProductionCodes.Remove(SettingInfo.DefaultProduction);
+                    SettingInfo.ProductionCodes.Insert(0, SettingInfo.DefaultProduction);
+                }
             }
 
-            return codes;
+            return SettingInfo.ProductionCodes;
         }
 
         public static List<string> GetProcessCodes()
         {
-            List<string> codes = new List<string>();
-
-            if (Data != null && Data.ContainsKey(PROPERTY_PROCESSCODES))
+            if (!string.IsNullOrWhiteSpace(SettingInfo.DefaultProcess))
             {
-                codes = Data.Value<JArray>(PROPERTY_PROCESSCODES).ToList().ConvertAll(u => u.ToString());
+                if (SettingInfo.ProcessCodes.Contains(SettingInfo.DefaultProcess))
+                {
+                    SettingInfo.ProcessCodes.Remove(SettingInfo.DefaultProcess);
+                    SettingInfo.ProcessCodes.Insert(0, SettingInfo.DefaultProcess);
+                }
             }
-            return codes;
+
+            return SettingInfo.ProcessCodes;
         }
 
-        public static List<string> AddNewProductionCode(string code)
+        public static void AddNewProductionCode(string code)
         {
-            List<string> codes = GetProcessCodes();
-            if (!codes.Contains(code))
+            if (!SettingInfo.ProductionCodes.Contains(code))
             {
-                codes.Add(code);
+                SettingInfo.ProductionCodes.Add(code);
             }
-            if (Data != null && Data.ContainsKey(PROPERTY_PRODUCTIONCODES))
+
+            SaveSettingInfo();
+        }
+
+        public static string GetConfigFilePath()
+        {
+            return SettingInfo.ConfigPath;
+        }
+
+        public static string GetProgramDescription()
+        {
+            if (string.IsNullOrWhiteSpace(SettingInfo.Description))
             {
-                Data[PROPERTY_PRODUCTIONCODES] = new JArray(codes);
-                string newDataStr = JsonConvert.SerializeObject(Data, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto });
-                using (StreamWriter writer = new StreamWriter(GetSettingFilePath(), false, System.Text.Encoding.UTF8))
+                SettingInfo.Description = "浼偗妫嚜鍔ㄥ寲鎶�鏈湁闄愬叕鍙�";
+            }
+
+            return SettingInfo.Description;
+        }
+
+        public static string GetProgramIcon()
+        {
+            string iconPath = SettingInfo.IconPath;
+            if (string.IsNullOrWhiteSpace(iconPath) || !File.Exists(iconPath))
+            {
+                iconPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logo.ico");
+            }
+
+            if (!File.Exists(iconPath))
+            {
+                iconPath = "";
+            }
+            return iconPath;
+        }
+
+        #region 淇濆瓨
+        public static void SetDefaultProcess(string processCode)
+        {
+            if (SettingInfo.DefaultProcess != processCode)
+            {
+                SettingInfo.DefaultProcess = processCode;
+                SaveSettingInfo();
+            }
+        }
+
+        public static void SetDefaultProduction(string productionCode)
+        {
+            if (SettingInfo.DefaultProduction != productionCode)
+            {
+                SettingInfo.DefaultProduction = productionCode;
+                SaveSettingInfo();
+            }
+        }
+
+        public static void SetDefault(string processCode,string productionCode)
+        {
+            bool isChanged = false;
+            if (SettingInfo.DefaultProcess != processCode)
+            {
+                isChanged = true;
+                SettingInfo.DefaultProcess = processCode;
+            }
+
+            if (SettingInfo.DefaultProduction != productionCode)
+            {
+                isChanged = true;
+                SettingInfo.DefaultProduction = productionCode;
+            }
+
+            if (isChanged)
+            {
+                SaveSettingInfo();
+            }
+        }
+
+        static object saveLock = new object();
+        private static void SaveSettingInfo()
+        {
+            string newDataStr = JsonConvert.SerializeObject(SettingInfo);
+            lock (saveLock)
+            {
+                using (StreamWriter writer = new StreamWriter(SettingPath, false, System.Text.Encoding.UTF8))
                 {
                     writer.Write(newDataStr);
                     writer.Flush();
                     writer.Close();
                 }
             }
-
-            return GetProcessCodes();
         }
+        #endregion
+    }
 
-        public static string GetConfigFilePath()
-        {
-            string path = "";
+    public class SettingInfo
+    {
+        public List<string> ProcessCodes { get; set; } = new List<string>();
+        public List<string> ProductionCodes { get; set; } = new List<string>();
 
-            if (Data != null && Data.ContainsKey(PROPERTY_CONFIGPATH))
-            {
-                path = Data.Value<string>(PROPERTY_CONFIGPATH);
-            }
+        public string DefaultProcess { get; set; }
 
-            return path;
-        }
+        public string DefaultProduction { get; set; }
 
-        public static string GetSettingFilePath()
-        {
-            string path = "";
-
-            if (Data != null && Data.ContainsKey(SETTINGFILE))
-            {
-                path = Data.Value<string>(SETTINGFILE);
-            }
-
-            return path;
-        }
-
-        public static string GetProgramDescription()
-        {
-            string desc = "浼偗妫嚜鍔ㄥ寲鎶�鏈湁闄愬叕鍙�";
-
-            if (Data != null && Data.ContainsKey(PROPERTY_DESCRIPTION))
-            {
-                desc = Data.Value<string>(PROPERTY_DESCRIPTION);
-            }
-
-            return desc;
-        }
-
-        public static string GetProgramIcon()
-        {
-            string iconPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logo.ico");
-
-            if (!File.Exists(iconPath))
-            {
-                iconPath = "";
-            }
-
-            if (Data != null && Data.ContainsKey(PROPERTY_ICONPATH))
-            {
-                iconPath = Data.Value<string>(PROPERTY_ICONPATH);
-            }
-
-            return iconPath;
-        }
+        public string ConfigPath { get; set; }
+        public string Description { get; set; }
+        public string IconPath { get; set; }
     }
 }

--
Gitblit v1.8.0