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, "<>[\\s\\S]*?<>"); foreach(Match m in mc) { string listString = ""; string matchValue = m.Value; Match head = Regex.Match(matchValue, "<>"); if(head.Success) { string headValue = head.Value; string itemTemplate = matchValue.Replace(headValue, "").Replace("<>", ""); foreach(JsonData item in obj) { string itemString = itemTemplate; IDictionary itemData = item as IDictionary; 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, "<>[\\s\\S]*?<>"); foreach(Match m in mc) { string listString = ""; string matchValue = m.Value; Match head = Regex.Match(matchValue, "<>"); if(head.Success) { string headValue = head.Value; string itemTemplate = matchValue.Replace(headValue, "").Replace("<>", ""); string itemString = itemTemplate; IDictionary itemData = obj as IDictionary; 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("<>", jsonObj[key].ToString()); } } return templateString; } public async Task PublicMakeAsync(string content) { MatchCollection mc = Regex.Matches(content, "<>.*?<>"); foreach(Match m in mc) { string matchValue = m.Value; Match head = Regex.Match(matchValue, "<>"); if(head.Success) { string headValue = head.Value; string resultValue = matchValue.Replace(headValue, "").Replace("<>", ""); string condition = headValue.Substring(8, headValue.Length - 10); condition = condition.Replace("isEmpty", "string.IsNullOrEmpty"); bool result = await CSharpScript.EvaluateAsync(condition); content = content.Replace(matchValue, result ? resultValue : ""); } } return content; } }