Parcourir la source

生成算法,基于模版

lichunlei il y a 3 jours
Parent
commit
a1ff1b5d91
4 fichiers modifiés avec 144 ajouts et 10 suppressions
  1. 105 10
      Task/MakeSqlHelper.cs
  2. 20 0
      Template/Sql/db.sql
  3. 6 0
      Template/Sql/dbField.sql
  4. 13 0
      Template/Sql/dbTable.sql

+ 105 - 10
Task/MakeSqlHelper.cs

@@ -1,13 +1,8 @@
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Text;
-using System.Threading;
+using System.Collections;
+using System.Text.RegularExpressions;
 using Common;
-using Infrastructure;
-using Services;
-using Model;
 using LitJson;
+using Microsoft.CodeAnalysis.CSharp.Scripting;
 
 //营业额日汇总统计
 public class MakeSqlHelper
@@ -37,8 +32,108 @@ public class MakeSqlHelper
     }
 
     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;
     }
 
 }

+ 20 - 0
Template/Sql/db.sql

@@ -0,0 +1,20 @@
+<<ym-item:databaseInfo>>
+drop database IF EXISTS <<dbName>>;
+CREATE DATABASE <<dbName>> CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
+use <<dbName>>;
+CREATE USER <<username>> IDENTIFIED BY '<<pwd>>';
+GRANT create,alter,drop,select,insert,update,delete ON <<username>>.* TO <<username>>;
+<</ym-item:databaseInfo>>
+<<ym-loop:databaseTable>>
+DROP TABLE IF EXISTS <<tableName>>;
+CREATE table <<tableName>>(
+<<ym-loop:databaseField tableId=<<parent:id>>>>
+<<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=",">>
+<<fieldName>>,
+<</ym-loop:databaseField>>
+)
+) COMMENT '<<tableTitle>>' ENGINE=InnoDB DEFAULT charset=utf8mb4 COLLATE utf8mb4_general_ci;
+<</ym-loop:databaseTable>>

+ 6 - 0
Template/Sql/dbField.sql

@@ -0,0 +1,6 @@
+<<ym-item:databaseTable>>
+Alter table <<tableName>> <<ym:opType>> 
+<</ym-item:databaseTable>>
+<<ym-item: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-item:databaseField>>

+ 13 - 0
Template/Sql/dbTable.sql

@@ -0,0 +1,13 @@
+<<ym-item:databaseTable>>
+DROP TABLE IF EXISTS <<tableName>>;
+CREATE table <<tableName>>(
+<<ym-loop:databaseField tableId=<<parent:id>>>>
+<<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 removeEnd=",">>
+<<fieldName>>,
+<</ym-loop:databaseField>>
+)
+) COMMENT '<<tableTitle>>' ENGINE=InnoDB DEFAULT charset=utf8mb4 COLLATE utf8mb4_general_ci;
+<</ym-item:databaseTable>>