| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- 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;
- }
- }
|