TrendToolHtml.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using CB.Data;
  6. using CB.Entity;
  7. using CB.Common;
  8. using System.Text.RegularExpressions;
  9. namespace CB.TrendTool
  10. {
  11. public class TrendToolHtml
  12. {
  13. static string name = "#@@paraname", id = "#@@paraid", value = "#@@paravalue";
  14. static string content = "#@@paracontent";
  15. //未使用注释 static string tempTitle = "<span>#@@paravalue</span>";
  16. //未使用注释 static string tempRadio = "<input name='#@@paraname' id='#@@paraid' type='radio' value='#@@paravalue' /><label for='#@@paraid'>#@@paravalue</label>";
  17. //static string tempCheckbox = "<input name='#@@paraname' id='#@@paraid' type='checkbox' value='#@@paravalue'/><label for='#@@paraid'>#@@paravalue</label>";
  18. //未使用注释 static string tempspan = "<span for='#@@paraid' class='frame white' onclick='spanOnclick(this);'>#@@paravalue</span><input type='hidden' name='#@@paraname' id='#@@paraid' hiddenvalue='#@@paravalue' />";
  19. protected static Dictionary<int, string> TemplateHtml = new Dictionary<int, string>();
  20. TrendToolHtml()
  21. {
  22. }
  23. public static string GetToolsHtml(int PageID)
  24. {
  25. StringBuilder ToolsHtml = new StringBuilder();
  26. //List<TrendToolInfo> lists = TrendToolService.ToList(new TrendToolInfo() { PageID = PageID, Status = FilterStatus.Default }).ToList();
  27. List<TrendToolInfo> lists = Caches.GetTrendToolList(new TrendToolInfo() { PageID = PageID, Status = FilterStatus.Default }).ToList();
  28. List<TrendToolInfo> ParentLists = lists.FindAll(x => x.ParentID == -1).OrderBy(o => o.ToolOrder).ToList();
  29. foreach (TrendToolInfo parent in ParentLists)
  30. {
  31. List<TrendToolInfo> Items = lists.FindAll(x => x.PageID == parent.PageID).OrderBy(o => o.ParentID).OrderBy(o => o.ToolOrder).ToList();
  32. ToolsHtml.Append(GetToolHtml(parent.Id, Items));
  33. }
  34. return ToolsHtml.ToString();
  35. }
  36. public static string GetToolHtml(int ParentID, List<TrendToolInfo> ChildList)
  37. {
  38. StringBuilder ToolHtml = new StringBuilder();
  39. //生成父节点html
  40. TrendToolInfo parent = ChildList.Find(x => x.Id == ParentID);
  41. List<TrendToolInfo> Items = ChildList.FindAll(x => x.ParentID == ParentID).OrderBy(o => o.ToolOrder).ToList();
  42. ToolHtml.Append(GetHTML(parent, Items.Count > 0 ? true : false));
  43. //ChildList.Remove(parent);
  44. for (int i = 0; i < Items.Count; i++)
  45. {
  46. TrendToolInfo Child = Items[i];
  47. List<TrendToolInfo> ChildItems = ChildList.FindAll(x => x.ParentID == Child.Id);
  48. string ChildHtml = "";
  49. if (ChildItems.Count > 0)
  50. { ChildHtml = GetToolHtml(Child.Id, ChildList); }
  51. else
  52. { ChildHtml = GetHTML(Child, ChildItems.Count > 0 ? true : false); }
  53. ToolHtml.Replace(content, ChildHtml + (i + 1 == Items.Count ? "" : content));
  54. }
  55. return ToolHtml.ToString();
  56. }
  57. private static string GetHTML(TrendToolInfo entity, bool isCreateChildItem)
  58. {
  59. //如果实体默认有html,直接返回原有的html
  60. if (!string.IsNullOrEmpty(entity.HTML))
  61. { return entity.HTML.Trim(); }
  62. string hHeader = "#@@Title";
  63. string hContent = "#@@Content";
  64. string hFooter = "#@@Remark";
  65. string templateHtml = entity.TemplateHTML;
  66. //主体html
  67. List<string> items = new List<string>();
  68. if (!String.IsNullOrEmpty(entity.ItemValue.Trim()))
  69. { items = entity.ItemValue.Split(',').ToList(); }
  70. //if (items.Count != entity.ItemCount)
  71. //{ throw new Exception("元素总数和和配置的总数不匹配"); }
  72. //头部html
  73. if (!string.IsNullOrEmpty(entity.Title))
  74. { templateHtml = templateHtml.Replace(hHeader, entity.Title); }
  75. else
  76. { templateHtml = templateHtml.Replace(hHeader, ""); }
  77. //匹配{}中的字符串.该字符串会循环生成
  78. var match = Regex.Match(templateHtml, @"\{(.*)\}", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
  79. string cycleHtml = match.Groups[1].Value;
  80. templateHtml = templateHtml.Replace("{" + cycleHtml + "}", "");
  81. StringBuilder sb = new StringBuilder();
  82. for (int i = 0; i < items.Count; i++)
  83. {
  84. string MainHtml = "";
  85. if (string.IsNullOrEmpty(entity.HTML))
  86. {
  87. MainHtml = cycleHtml.Replace(value, items[i]);
  88. }
  89. else
  90. { MainHtml = entity.HTML; }
  91. MainHtml = MainHtml.Replace(id, ToolUtility.FilterNamePrefix + entity.Id + "_" + i.ToString());
  92. MainHtml = MainHtml.Replace(name, ToolUtility.FilterNamePrefix + entity.Id);
  93. sb.Append(MainHtml);
  94. }
  95. if (isCreateChildItem)
  96. { sb.Append(content); }
  97. templateHtml = templateHtml.Replace(hContent, sb.ToString());
  98. //尾部html
  99. if (!string.IsNullOrEmpty(entity.Remark))
  100. { templateHtml = templateHtml.Replace(hFooter, entity.Remark); }
  101. else
  102. { templateHtml = templateHtml.Replace(hFooter, ""); }
  103. templateHtml = templateHtml.Replace(name, ToolUtility.FilterNamePrefix + entity.Id);
  104. return templateHtml;
  105. }
  106. }
  107. }