MakeSqlHelper.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System.Collections;
  2. using System.Text.RegularExpressions;
  3. using Common;
  4. using LitJson;
  5. using Microsoft.CodeAnalysis.CSharp.Scripting;
  6. //营业额日汇总统计
  7. public class MakeSqlHelper
  8. {
  9. public readonly static MakeSqlHelper Instance = new MakeSqlHelper();
  10. private MakeSqlHelper()
  11. { }
  12. public void Start(string QueueName)
  13. {
  14. RabbitMQClient.Instance.StartReceive(QueueName, content => DoWorks(content));
  15. }
  16. public void DoWorks(string content)
  17. {
  18. try
  19. {
  20. if(!string.IsNullOrEmpty(content))
  21. {
  22. DoQueue(content);
  23. }
  24. }
  25. catch (Exception ex)
  26. {
  27. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex, "生成SQL异常");
  28. }
  29. }
  30. public void DoQueue(string content)
  31. {
  32. JsonData jsonObj = JsonMapper.ToObject(content);
  33. string templateString = Function.ReadInstance("Template/Sql/db.sql");
  34. string resultString = StartMake(jsonObj, templateString);
  35. }
  36. public string StartMake(JsonData jsonObj, string templateString)
  37. {
  38. foreach(string key in jsonObj.Keys)
  39. {
  40. JsonData obj = jsonObj[key];
  41. if(obj.IsArray)
  42. {
  43. MatchCollection mc = Regex.Matches(templateString, "<<ym-loop:" + key + ".*?>>[\\s\\S]*?<</ym-loop:" + key + ">>");
  44. foreach(Match m in mc)
  45. {
  46. string listString = "";
  47. string matchValue = m.Value;
  48. Match head = Regex.Match(matchValue, "<<ym-loop:" + key + ".*>>");
  49. if(head.Success)
  50. {
  51. string headValue = head.Value;
  52. string itemTemplate = matchValue.Replace(headValue, "").Replace("<</ym-loop:" + key + ">>", "");
  53. foreach(JsonData item in obj)
  54. {
  55. string itemString = itemTemplate;
  56. IDictionary<string, JsonData> itemData = item as IDictionary<string, JsonData>;
  57. foreach(string itemKey in itemData.Keys)
  58. {
  59. JsonData itemObj = itemData[itemKey];
  60. if(itemObj.IsArray || itemObj.IsObject)
  61. {
  62. itemString = StartMake(itemObj, itemTemplate);
  63. }
  64. else
  65. {
  66. itemString = itemString.Replace("<<" + itemKey + ">>", itemObj[itemKey].ToString());
  67. }
  68. }
  69. listString += itemString;
  70. }
  71. }
  72. templateString.Replace(matchValue, listString);
  73. }
  74. }
  75. else if(obj.IsObject)
  76. {
  77. MatchCollection mc = Regex.Matches(templateString, "<<ym-item:" + key + ".*?>>[\\s\\S]*?<</ym-item:" + key + ">>");
  78. foreach(Match m in mc)
  79. {
  80. string listString = "";
  81. string matchValue = m.Value;
  82. Match head = Regex.Match(matchValue, "<<ym-item:" + key + ".*>>");
  83. if(head.Success)
  84. {
  85. string headValue = head.Value;
  86. string itemTemplate = matchValue.Replace(headValue, "").Replace("<</ym-item:" + key + ">>", "");
  87. string itemString = itemTemplate;
  88. IDictionary<string, JsonData> itemData = obj as IDictionary<string, JsonData>;
  89. foreach(string itemKey in itemData.Keys)
  90. {
  91. JsonData itemObj = itemData[itemKey];
  92. if(itemObj.IsArray || itemObj.IsObject)
  93. {
  94. itemString = StartMake(itemObj, itemTemplate);
  95. }
  96. else
  97. {
  98. itemString = itemString.Replace("<<" + itemKey + ">>", itemObj[itemKey].ToString());
  99. }
  100. }
  101. listString += itemString;
  102. }
  103. templateString.Replace(matchValue, listString);
  104. }
  105. }
  106. else
  107. {
  108. templateString = templateString.Replace("<<ym:" + key + ">>", jsonObj[key].ToString());
  109. }
  110. }
  111. return templateString;
  112. }
  113. public async Task<string> PublicMakeAsync(string content)
  114. {
  115. MatchCollection mc = Regex.Matches(content, "<<ym-if:.*?>>.*?<</ym-if>>");
  116. foreach(Match m in mc)
  117. {
  118. string matchValue = m.Value;
  119. Match head = Regex.Match(matchValue, "<<ym-if:.*>>");
  120. if(head.Success)
  121. {
  122. string headValue = head.Value;
  123. string resultValue = matchValue.Replace(headValue, "").Replace("<</ym-if>>", "");
  124. string condition = headValue.Substring(8, headValue.Length - 10);
  125. condition = condition.Replace("isEmpty", "string.IsNullOrEmpty");
  126. bool result = await CSharpScript.EvaluateAsync<bool>(condition);
  127. content = content.Replace(matchValue, result ? resultValue : "");
  128. }
  129. }
  130. return content;
  131. }
  132. }