ソースを参照

完善生成程序

lichunlei 5 時間 前
コミット
706f9bd7f5
6 ファイル変更216 行追加143 行削除
  1. 3 1
      Program.cs
  2. 46 0
      Task/MakeHelper.cs
  3. 0 139
      Task/MakeSqlHelper.cs
  4. 2 2
      Template/Sql/db.sql
  5. 1 1
      Template/Sql/dbTable.sql
  6. 164 0
      Util/Maker.cs

+ 3 - 1
Program.cs

@@ -12,6 +12,7 @@ using Nacos.AspNetCore.V2;
 using Util;
 using System.Net;
 using System.Net.Sockets;
+using Task;
 
 var builder = WebApplication.CreateBuilder(args);
 
@@ -126,7 +127,8 @@ app.MapControllers();
 
 app.Urls.Add("http://*:8006");
 
-RabbitMQClient.Instance.Conn("MakeSqlQueue");
+
 MakeSqlHelper.Instance.Start("MakeSqlQueue");
+RabbitMQClient.Instance.Conn("MakeSqlCallBackQueue");
 
 app.Run();

+ 46 - 0
Task/MakeHelper.cs

@@ -0,0 +1,46 @@
+using System.Collections;
+using System.Text.RegularExpressions;
+using Common;
+using LitJson;
+using Microsoft.CodeAnalysis.CSharp.Scripting;
+
+namespace Task
+{
+    public class MakeHelper
+    {
+        public readonly static MakeHelper Instance = new MakeHelper();
+        private MakeHelper()
+        { }
+
+        public void Start(string QueueName)
+        {
+            RabbitMQClient.Instance.StartReceive(QueueName, content => DoWorks(content));
+        }
+
+        public void DoWorks(string content)
+        {
+            try
+            {
+                if (!string.IsNullOrEmpty(content))
+                {
+                    DoQueue(content);
+                }
+            }
+            catch (Exception ex)
+            {
+                Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex, "生成异常");
+            }
+        }
+
+        public void DoQueue(string content)
+        {
+            JsonData jsonObj = JsonMapper.ToObject(content);
+            string requestId = jsonObj["requestId"].ToString();
+            string modePath = jsonObj["modePath"].ToString();
+            string templateString = Function.ReadInstance("Template/" + modePath); //读取数据库sql模板
+            string resultString = Util.Maker.StartMake(jsonObj["data"], templateString);
+            RabbitMQClient.Instance.Push("MakeSqlCallBackQueue", requestId + "#cut#" + resultString);
+        }
+
+    }
+}

+ 0 - 139
Task/MakeSqlHelper.cs

@@ -1,139 +0,0 @@
-using System.Collections;
-using System.Text.RegularExpressions;
-using Common;
-using LitJson;
-using Microsoft.CodeAnalysis.CSharp.Scripting;
-
-//营业额日汇总统计
-public class MakeSqlHelper
-{
-    public readonly static MakeSqlHelper Instance = new MakeSqlHelper();
-    private MakeSqlHelper()
-    { }
-
-    public void Start(string QueueName)
-    {
-        RabbitMQClient.Instance.StartReceive(QueueName, content => DoWorks(content));
-    }
-
-    public void DoWorks(string content)
-    {
-        try
-        {
-            if(!string.IsNullOrEmpty(content))
-            {
-                DoQueue(content);
-            }
-        }
-        catch (Exception ex)
-        {
-            Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex, "生成SQL异常");
-        }
-    }
-
-    public void DoQueue(string content)
-    {
-        JsonData jsonObj = JsonMapper.ToObject(content);
-        string templateString = Function.ReadInstance("Template/Sql/db.sql");
-        string resultString = StartMake(jsonObj, templateString);
-    }
-
-    public string StartMake(JsonData jsonObj, string templateString)
-    {
-        foreach(string key in jsonObj.Keys)
-        {
-            JsonData obj = jsonObj[key];
-            if(obj.IsArray)
-            {
-                MatchCollection mc = Regex.Matches(templateString, "<<ym-loop:" + key + ".*?>>[\\s\\S]*?<</ym-loop:" + key + ">>");
-                foreach(Match m in mc)
-                {
-                    string listString = "";
-                    string matchValue = m.Value;
-                    Match head = Regex.Match(matchValue, "<<ym-loop:" + key + ".*>>");
-                    if(head.Success)
-                    {
-                        string headValue = head.Value;
-                        string itemTemplate = matchValue.Replace(headValue, "").Replace("<</ym-loop:" + key + ">>", "");
-                        foreach(JsonData item in obj)
-                        {
-                            string itemString = itemTemplate;
-                            IDictionary<string, JsonData> itemData = item as IDictionary<string, JsonData>;
-                            foreach(string itemKey in itemData.Keys)
-                            {
-                                JsonData itemObj = itemData[itemKey];
-                                if(itemObj.IsArray || itemObj.IsObject)
-                                {
-                                    itemString = StartMake(itemObj, itemTemplate);
-                                }
-                                else
-                                {
-                                    itemString = itemString.Replace("<<" + itemKey + ">>", itemObj[itemKey].ToString());
-                                }
-                            }
-                            listString += itemString;
-                        }
-                    }
-                    templateString.Replace(matchValue, listString);
-                }
-            }
-            else if(obj.IsObject)
-            {
-                MatchCollection mc = Regex.Matches(templateString, "<<ym-item:" + key + ".*?>>[\\s\\S]*?<</ym-item:" + key + ">>");
-                foreach(Match m in mc)
-                {
-                    string listString = "";
-                    string matchValue = m.Value;
-                    Match head = Regex.Match(matchValue, "<<ym-item:" + key + ".*>>");
-                    if(head.Success)
-                    {
-                        string headValue = head.Value;
-                        string itemTemplate = matchValue.Replace(headValue, "").Replace("<</ym-item:" + key + ">>", "");
-                        string itemString = itemTemplate;
-                        IDictionary<string, JsonData> itemData = obj as IDictionary<string, JsonData>;
-                        foreach(string itemKey in itemData.Keys)
-                        {
-                            JsonData itemObj = itemData[itemKey];
-                            if(itemObj.IsArray || itemObj.IsObject)
-                            {
-                                itemString = StartMake(itemObj, itemTemplate);
-                            }
-                            else
-                            {
-                                itemString = itemString.Replace("<<" + itemKey + ">>", itemObj[itemKey].ToString());
-                            }
-                        }
-                        listString += itemString;
-                    }
-                    templateString.Replace(matchValue, listString);
-                }
-            }
-            else
-            {
-                templateString = templateString.Replace("<<ym:" + key + ">>", jsonObj[key].ToString());
-            }
-        }
-        return templateString;
-    }
-
-    public async Task<string> PublicMakeAsync(string content)
-    {
-        MatchCollection mc = Regex.Matches(content, "<<ym-if:.*?>>.*?<</ym-if>>");
-        foreach(Match m in mc)
-        {
-            string matchValue = m.Value;
-            Match head = Regex.Match(matchValue, "<<ym-if:.*>>");
-            if(head.Success)
-            {
-                string headValue = head.Value;
-                string resultValue = matchValue.Replace(headValue, "").Replace("<</ym-if>>", "");
-                string condition = headValue.Substring(8, headValue.Length - 10);
-                condition = condition.Replace("isEmpty", "string.IsNullOrEmpty");
-                bool result = await CSharpScript.EvaluateAsync<bool>(condition);
-                content = content.Replace(matchValue, result ? resultValue : "");
-            }
-        }
-        return content;
-    }
-
-}

+ 2 - 2
Template/Sql/db.sql

@@ -8,11 +8,11 @@ GRANT create,alter,drop,select,insert,update,delete ON <<username>>.* TO <<usern
 <<ym-loop:databaseTable>>
 DROP TABLE IF EXISTS <<tableName>>;
 CREATE table <<tableName>>(
-<<ym-loop:databaseField tableId=<<parent:id>>>>
+<<ym-loop:databaseField>>
 <<fieldName>> <<fieldType>><<ym-if:!isEmpty(<<fieldLength>>)>>(<<fieldLength>>)<</ym-if>><<ym-if:<<notNull>>=1>> not null<</ym-if>><<ym-if:<<autoIncrement>>=1>> AUTO_INCREMENT<</ym-if>><<ym-if:!isEmpty(<<fieldDefaultValue>>)>> default <<fieldDefaultValue>><</ym-if>> COMMENT <<fieldTitle>><<fieldDetail>>,
 <</ym-loop:databaseField>>
 PRIMARY KEY(
-<<ym-loop:databaseField tableId=<<parent:id>> removeEnd=",">>
+<<ym-loop:databaseField removeEnd=",">>
 <<fieldName>>,
 <</ym-loop:databaseField>>
 )

+ 1 - 1
Template/Sql/dbTable.sql

@@ -1,7 +1,7 @@
 <<ym-item:databaseTable>>
 DROP TABLE IF EXISTS <<tableName>>;
 CREATE table <<tableName>>(
-<<ym-loop:databaseField tableId=<<parent:id>>>>
+<<ym-loop:databaseField>>
 <<fieldName>> <<fieldType>><<ym-if:!isEmpty(<<fieldLength>>)>>(<<fieldLength>>)<</ym-if>><<ym-if:<<notNull>>="1">> not null<</ym-if>><<ym-if:<<autoIncrement>>="1">> AUTO_INCREMENT<</ym-if>><<ym-if:!isEmpty(<<fieldDefaultValue>>)>> default <<fieldDefaultValue>><</ym-if>> COMMENT <<fieldTitle>><<fieldDetail>>,
 <</ym-loop:databaseField>>
 PRIMARY KEY(

+ 164 - 0
Util/Maker.cs

@@ -0,0 +1,164 @@
+using System.Collections;
+using System.Text.RegularExpressions;
+using Common;
+using LitJson;
+using Microsoft.CodeAnalysis.CSharp.Scripting;
+
+namespace Util
+{
+    public class Maker
+    {
+        
+        #region 读取模版数据,替换对应的标签内容
+        public static string StartMake(JsonData jsonObj, string templateString)
+        {
+            foreach (string key in jsonObj.Keys)
+            {
+                JsonData obj = jsonObj[key];
+                if (obj.IsArray) //如果是数组
+                {
+                    //匹配循环带loop循环标记的内容
+                    MatchCollection mc = Regex.Matches(templateString, "<<ym-loop:" + key + ".*?>>[\\s\\S]*?<</ym-loop:" + key + ">>");
+                    foreach (Match m in mc)
+                    {
+                        string listString = "";
+                        string matchValue = m.Value;
+                        Match head = Regex.Match(matchValue, "<<ym-loop:" + key + ".*>>");
+                        if (head.Success)
+                        {
+                            string headValue = head.Value;
+                            char[] removeString = new char[] { }; //两头要过滤的符号
+                            char[] removeStartString = new char[] { }; //头部要过滤的符号
+                            char[] removeEndString = new char[] { }; //尾部要过滤的符号
+                                                                     //匹配两头要过滤的符号
+                            Match remove = Regex.Match(headValue, "remove=\".*?\"");
+                            if (remove.Success)
+                            {
+                                removeString = remove.Value.Replace("remove=\"", "").Replace("\"", "").ToCharArray();
+                            }
+                            //匹配头部要过滤的符号
+                            Match removeStart = Regex.Match(headValue, "removeStart=\".*?\"");
+                            if (removeStart.Success)
+                            {
+                                removeStartString = removeStart.Value.Replace("removeStart=\"", "").Replace("\"", "").ToCharArray();
+                            }
+                            //匹配尾头要过滤的符号
+                            Match removeEnd = Regex.Match(headValue, "removeEnd=\".*?\"");
+                            if (removeEnd.Success)
+                            {
+                                removeEndString = removeEnd.Value.Replace("removeEnd=\"", "").Replace("\"", "").ToCharArray();
+                            }
+                            //匹配要循环的模板内容
+                            string itemTemplate = matchValue.Replace(headValue, "").Replace("<</ym-loop:" + key + ">>", "");
+                            //循环读取数组数据
+                            foreach (JsonData item in obj)
+                            {
+                                string itemString = itemTemplate;
+                                IDictionary<string, JsonData> itemData = item as IDictionary<string, JsonData>;
+                                //循环读取每个字段的数据
+                                foreach (string itemKey in itemData.Keys)
+                                {
+                                    JsonData itemObj = itemData[itemKey];
+                                    if (itemObj.IsArray || itemObj.IsObject)
+                                    {
+                                        itemString = StartMake(itemObj, itemTemplate);
+                                    }
+                                    else
+                                    {
+                                        itemString = itemString.Replace("<<" + itemKey + ">>", itemObj[itemKey].ToString());
+                                    }
+                                }
+                                listString += itemString;
+                            }
+                            //过滤两头的符号
+                            if (removeString.Length > 0)
+                            {
+                                listString.TrimStart(removeString);
+                            }
+                            //过滤头部的符号
+                            if (removeStartString.Length > 0)
+                            {
+                                listString.TrimStart(removeStartString);
+                            }
+                            //过滤尾部的符号
+                            if (removeEndString.Length > 0)
+                            {
+                                listString.TrimEnd(removeEndString);
+                            }
+                        }
+                        templateString.Replace(matchValue, listString);
+                    }
+                }
+                else if (obj.IsObject) //如果是单个对象
+                {
+                    //匹配循环带item单个对象标记的内容
+                    MatchCollection mc = Regex.Matches(templateString, "<<ym-item:" + key + ".*?>>[\\s\\S]*?<</ym-item:" + key + ">>");
+                    foreach (Match m in mc)
+                    {
+                        string listString = "";
+                        string matchValue = m.Value;
+                        Match head = Regex.Match(matchValue, "<<ym-item:" + key + ".*>>");
+                        if (head.Success)
+                        {
+                            string headValue = head.Value;
+                            string itemTemplate = matchValue.Replace(headValue, "").Replace("<</ym-item:" + key + ">>", "");
+                            string itemString = itemTemplate;
+                            IDictionary<string, JsonData> itemData = obj as IDictionary<string, JsonData>;
+                            //循环读取每个字段的数据
+                            foreach (string itemKey in itemData.Keys)
+                            {
+                                JsonData itemObj = itemData[itemKey];
+                                if (itemObj.IsArray || itemObj.IsObject)
+                                {
+                                    itemString = StartMake(itemObj, itemTemplate);
+                                }
+                                else
+                                {
+                                    itemString = itemString.Replace("<<" + itemKey + ">>", itemObj[itemKey].ToString());
+                                }
+                            }
+                            listString += itemString;
+                        }
+                        templateString.Replace(matchValue, listString);
+                    }
+                }
+                else //否则当数值处理
+                {
+                    templateString = templateString.Replace("<<ym:" + key + ">>", jsonObj[key].ToString());
+                }
+            }
+            return templateString;
+        }
+        #endregion
+
+        #region 公共标签处理
+        public static async Task<string> PublicMakeAsync(string content)
+        {
+            MatchCollection mc = Regex.Matches(content, "<<ym-if:.*?>>.*?<</ym-if>>");
+            foreach (Match m in mc)
+            {
+                string matchValue = m.Value;
+                Match head = Regex.Match(matchValue, "<<ym-if:.*>>");
+                if (head.Success)
+                {
+                    string headValue = head.Value;
+                    string resultValue = matchValue.Replace(headValue, "").Replace("<</ym-if>>", "");
+                    string elseValue = "";
+                    if (resultValue.Contains("<<ym-else>>"))
+                    {
+                        string[] valueData = resultValue.Split(new string[] { "<<ym-else>>" }, StringSplitOptions.None);
+                        resultValue = valueData[0];
+                        elseValue = valueData[1];
+                    }
+                    string condition = headValue.Substring(8, headValue.Length - 10);
+                    condition = condition.Replace("isEmpty", "string.IsNullOrEmpty");
+                    bool result = await CSharpScript.EvaluateAsync<bool>(condition);
+                    content = content.Replace(matchValue, result ? resultValue : elseValue);
+                }
+            }
+            return content;
+        }
+        #endregion
+    
+    }
+}