From 3f6a707f2373cfcee7e96c080a01ac1a4964a419 Mon Sep 17 00:00:00 2001
From: wells.liu <wells.liu@broconcentric.com>
Date: 星期四, 02 七月 2020 20:54:55 +0800
Subject: [PATCH] 解决 生成新配置问题

---
 src/Bro.Common.Model/Helper/SettingHelper.cs |   37 ++++++++++++++++++++++++++++---------
 1 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/src/Bro.Common.Model/Helper/SettingHelper.cs b/src/Bro.Common.Model/Helper/SettingHelper.cs
index 8bfefd6..f481646 100644
--- a/src/Bro.Common.Model/Helper/SettingHelper.cs
+++ b/src/Bro.Common.Model/Helper/SettingHelper.cs
@@ -16,24 +16,20 @@
         const string PROPERTY_DESCRIPTION = "Description";
         const string PROPERTY_ICONPATH = "IconPath";
 
+        static string ConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SETTINGFILE);
         static JObject data = null;
         static JObject Data
         {
             get
             {
-                if (data == null)
+                if (File.Exists(ConfigPath))
                 {
-                    string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SETTINGFILE);
-                    if (File.Exists(configPath))
+                    using (StreamReader reader = new StreamReader(ConfigPath, 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();
+                        data = JsonConvert.DeserializeObject<JObject>(dataStr);
                     }
                 }
-
                 return data;
             }
         }
@@ -66,6 +62,29 @@
             return codes;
         }
 
+        public static List<string> AddNewProductionCode(string code)
+        {
+            List<string> codes = GetProductionCodes();
+            if (!codes.Contains(code))
+            {
+                codes.Add(code);
+            }
+            var dataTemp = Data;
+            if (dataTemp != null && dataTemp.ContainsKey(PROPERTY_PRODUCTIONCODES))
+            {
+                dataTemp[PROPERTY_PRODUCTIONCODES] = new JArray(codes);
+
+                string newDataStr = JsonConvert.SerializeObject(dataTemp, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto });
+                using (StreamWriter writer = new StreamWriter(ConfigPath, false, System.Text.Encoding.UTF8))
+                {
+                    writer.Write(newDataStr);
+                    writer.Flush();
+                    writer.Close();
+                }
+            }
+            return GetProductionCodes();
+        }
+
         public static string GetConfigFilePath()
         {
             string path = "";

--
Gitblit v1.8.0