lichunlei 4 месяцев назад
Родитель
Сommit
b806a24d50
2 измененных файлов с 84 добавлено и 61 удалено
  1. 55 53
      Task/MakeHelper.cs
  2. 29 8
      Util/Maker.cs

+ 55 - 53
Task/MakeHelper.cs

@@ -23,69 +23,71 @@ namespace Tasks
 
         public void DoWorks(string content)
         {
-            try
+            if (!string.IsNullOrEmpty(content))
             {
-                if (!string.IsNullOrEmpty(content))
-                {
-                    DoQueue(content);
-                }
-            }
-            catch (Exception ex)
-            {
-                Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex, "生成异常");
+                DoQueue(content);
             }
         }
 
         public async void DoQueue(string content)
         {
-            JsonData jsonObj = JsonMapper.ToObject(content);
-            //流水号
-            string requestId = jsonObj["requestId"].ToString();
-            //模板文件路径
-            string modePath = jsonObj["modePath"].ToString();
-            //附加数据,原样返回
-            string attach = jsonObj["attach"].ToString();
-            //读取模板文件内容
-            // string templateString = Function.ReadInstance("Template/" + modePath + ".json");
-            var templateService = App.GetRequiredService<IMakeTemplateService>();
-            var template = templateService.GetFirst(m => m.templateName == modePath);
-            if(template == null) return;
-            string templateString = template.templateContent;
-            templateString = HttpUtility.UrlDecode(templateString);
-            //解析模板内容
-            string resultString = await Util.Maker.StartMake(jsonObj["data"], templateString);
-            //生成文件
-            string path = template.makePath.Substring(0, template.makePath.LastIndexOf("/") + 1);
-            string fileName = template.makePath.Substring(template.makePath.LastIndexOf("/") + 1);
-            path = Util.Maker.replaceKeyToValue(jsonObj["data"], path);
-            fileName = Util.Maker.replaceKeyToValue(jsonObj["data"], fileName);
-            Function.WritePage(path, fileName, resultString);
-            //生成数据入库
-            JsonData data = JsonMapper.ToObject(attach);
-            int dataId = 0;
-            if(attach.Contains("dataId")) dataId = int.Parse(Function.CheckInt(data["dataId"].ToString()));
-            string makeFileType = data["fileType"].ToString();
-            var makeDataService = App.GetRequiredService<IMakeDataService>();
-            var item = makeDataService.GetFirst(m => m.dataId == dataId && m.makeFileType == makeFileType);
-            if(item == null)
+            try
             {
-                makeDataService.Add(new MakeData()
+                Utils.WriteLog(DateTime.Now.ToString() + "\n" + content, "生成接收");
+                JsonData jsonObj = JsonMapper.ToObject(content);
+                //流水号
+                string requestId = jsonObj["requestId"].ToString();
+                //模板文件路径
+                string modePath = jsonObj["modePath"].ToString();
+                //附加数据,原样返回
+                string attach = jsonObj["attach"].ToString();
+                //读取模板文件内容
+                // string templateString = Function.ReadInstance("Template/" + modePath + ".json");
+                var templateService = App.GetRequiredService<IMakeTemplateService>();
+                var template = templateService.GetFirst(m => m.templateName == modePath);
+                if(template == null) return;
+                string templateString = template.templateContent;
+                templateString = HttpUtility.UrlDecode(templateString);
+                //解析模板内容
+                string resultString = await Util.Maker.StartMake(jsonObj["data"], templateString);
+                resultString = Util.Maker.replaceKeyToValue(jsonObj["data"], resultString);
+                //生成文件
+                string path = template.makePath.Substring(0, template.makePath.LastIndexOf("/") + 1);
+                string fileName = template.makePath.Substring(template.makePath.LastIndexOf("/") + 1);
+                path = Util.Maker.replaceKeyToValue(jsonObj["data"], path);
+                fileName = Util.Maker.replaceKeyToValue(jsonObj["data"], fileName);
+                Function.WritePage(path, fileName, resultString);
+                //生成数据入库
+                JsonData data = JsonMapper.ToObject(attach);
+                int dataId = 0;
+                if(attach.Contains("dataId")) dataId = int.Parse(Function.CheckInt(data["dataId"].ToString()));
+                string makeFileType = data["fileType"].ToString();
+                var makeDataService = App.GetRequiredService<IMakeDataService>();
+                var item = makeDataService.GetFirst(m => m.dataId == dataId && m.makeFileType == makeFileType);
+                if(item == null)
+                {
+                    makeDataService.Add(new MakeData()
+                    {
+                        dataId = dataId,
+                        makeFileName = fileName,
+                        makeFilePath = path,
+                        makeFileType = makeFileType,
+                        createTime = DateTime.Now,
+                        createBy = data["createBy"].ToString()
+                    });
+                }
+                else
                 {
-                    dataId = dataId,
-                    makeFileName = fileName,
-                    makeFilePath = path,
-                    makeFileType = makeFileType,
-                    createTime = DateTime.Now,
-                    createBy = data["createBy"].ToString()
-                });
+                    item.makeFileName = fileName;
+                    item.makeFilePath = path;
+                    item.updateTime = DateTime.Now;
+                    item.updateBy = data["createBy"].ToString();
+                    makeDataService.Update(item);
+                }
             }
-            else
+            catch (Exception ex)
             {
-                item.makeFileName = fileName;
-                item.makeFilePath = path;
-                item.updateTime = DateTime.Now;
-                item.updateBy = data["createBy"].ToString();
-                makeDataService.Update(item);
+                Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex, "生成异常");
             }
             //MQ回调解析的结果
             // RabbitMQClient.Instance.Push("MakeSqlCallBackQueue", requestId + "#cut#" + attach + "#cut#" + resultString);

+ 29 - 8
Util/Maker.cs

@@ -76,8 +76,9 @@ namespace Util
                     //匹配要循环的模板内容
                     string itemTemplate = matchValue.Replace(headValue, "").Replace("<</ym-loop:" + key + ">>", "");
                     //循环读取数组数据
-                    foreach (JsonData item in jsonObj)
+                    for (int i = 0; i < jsonObj.Count; i++)
                     {
+                        JsonData item = jsonObj[i];
                         string itemString = itemTemplate;
                         // IDictionary<string, JsonData> itemData = item as IDictionary<string, JsonData>;
                         //循环读取每个字段的数据
@@ -178,11 +179,25 @@ namespace Util
                 string[] data = matchValue.Replace("<<ym:" + key + ".", "").Replace(">>", "").Split('.');
                 if(data.Length == 1)
                 {
-                    templateString = templateString.Replace(matchValue, jsonObj[key][data[0]].ToString());
+                    if(jsonObj.ToJson().Contains("\"" + key + "\""))
+                    {
+                        templateString = templateString.Replace(matchValue, jsonObj[key][data[0]].ToString());
+                    }
+                    else
+                    {
+                        templateString = templateString.Replace(matchValue, jsonObj[data[0]].ToString());
+                    }
                 }
                 else if(data.Length == 2)
                 {
-                    templateString = templateString.Replace(matchValue, jsonObj[key][data[0]][data[1]].ToString());
+                    if(jsonObj.ToJson().Contains("\"" + key + "\""))
+                    {
+                        templateString = templateString.Replace(matchValue, jsonObj[key][data[0]][data[1]].ToString());
+                    }
+                    else
+                    {
+                        templateString = templateString.Replace(matchValue, jsonObj[data[0]][data[1]].ToString());
+                    }
                 }
             }
             return templateString;
@@ -210,9 +225,10 @@ namespace Util
                     }
                     string condition = headValue.Substring(8, headValue.Length - 10);
                     condition = condition.Replace("isEmpty", "string.IsNullOrEmpty");
+                    condition = condition.Replace(".start(", ".StartsWith(");
                     condition = condition.Replace("True", "true");
                     condition = condition.Replace("False", "false");
-                    // Console.WriteLine(condition);
+                    Console.WriteLine(condition);
                     bool result = await CSharpScript.EvaluateAsync<bool>(condition);
                     content = content.Replace(matchValue, result ? resultValue : elseValue);
                 }
@@ -224,29 +240,34 @@ namespace Util
         #region 读取模版数据,替换对应的标签内容
         public static string replaceKeyToValue(JsonData jsonObj, string templateString)
         {
+            if(templateString.Contains("<<ym.projectService.packageName>>"))
+            {
+                string packageName = "123123123";
+            }
             MatchCollection mc = Regex.Matches(templateString, "<<.*?>>");
             foreach (Match m in mc)
             {
                 string matchValue = m.Value;
                 string key = matchValue.Replace("<<", "").Replace(">>", "");
-                string[] keyArr = key.Split('.');
+                string[] keyArr = key.Replace("ym.", "").Split('.');
+                string jsonObjString = jsonObj.ToJson();
                 if (keyArr.Length == 1)
                 {
-                    if (jsonObj[key] != null)
+                    if (jsonObjString.Contains("\"" + key + "\""))
                     {
                         templateString = templateString.Replace(matchValue, jsonObj[key].ToString());
                     }
                 }
                 else if (keyArr.Length == 2)
                 { 
-                    if (jsonObj[keyArr[0]] != null && jsonObj[keyArr[0]][keyArr[1]] != null)
+                    if (jsonObjString.Contains("\"" + keyArr[0] + "\"") && jsonObjString.Contains("\"" + keyArr[1] + "\""))
                     {
                         templateString = templateString.Replace(matchValue, jsonObj[keyArr[0]][keyArr[1]].ToString());
                     }
                 }
                 else if (keyArr.Length == 3)
                 {
-                    if (jsonObj[keyArr[0]] != null && jsonObj[keyArr[0]][keyArr[1]] != null && jsonObj[keyArr[0]][keyArr[1]][keyArr[2]] != null)
+                    if (jsonObjString.Contains("\"" + keyArr[0] + "\"") && jsonObjString.Contains("\"" + keyArr[1] + "\"") && jsonObjString.Contains("\"" + keyArr[2] + "\""))
                     {
                         templateString = templateString.Replace(matchValue, jsonObj[keyArr[0]][keyArr[1]][keyArr[2]].ToString());
                     }