using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CB.Data;
using CB.Entity;
using CB.Common;
using System.Text.RegularExpressions;
namespace CB.TrendTool
{
public class TrendToolHtml
{
static string name = "#@@paraname", id = "#@@paraid", value = "#@@paravalue";
static string content = "#@@paracontent";
//未使用注释 static string tempTitle = "#@@paravalue";
//未使用注释 static string tempRadio = "";
//static string tempCheckbox = "";
//未使用注释 static string tempspan = "#@@paravalue";
protected static Dictionary TemplateHtml = new Dictionary();
TrendToolHtml()
{
}
public static string GetToolsHtml(int PageID)
{
StringBuilder ToolsHtml = new StringBuilder();
//List lists = TrendToolService.ToList(new TrendToolInfo() { PageID = PageID, Status = FilterStatus.Default }).ToList();
List lists = Caches.GetTrendToolList(new TrendToolInfo() { PageID = PageID, Status = FilterStatus.Default }).ToList();
List ParentLists = lists.FindAll(x => x.ParentID == -1).OrderBy(o => o.ToolOrder).ToList();
foreach (TrendToolInfo parent in ParentLists)
{
List Items = lists.FindAll(x => x.PageID == parent.PageID).OrderBy(o => o.ParentID).OrderBy(o => o.ToolOrder).ToList();
ToolsHtml.Append(GetToolHtml(parent.Id, Items));
}
return ToolsHtml.ToString();
}
public static string GetToolHtml(int ParentID, List ChildList)
{
StringBuilder ToolHtml = new StringBuilder();
//生成父节点html
TrendToolInfo parent = ChildList.Find(x => x.Id == ParentID);
List Items = ChildList.FindAll(x => x.ParentID == ParentID).OrderBy(o => o.ToolOrder).ToList();
ToolHtml.Append(GetHTML(parent, Items.Count > 0 ? true : false));
//ChildList.Remove(parent);
for (int i = 0; i < Items.Count; i++)
{
TrendToolInfo Child = Items[i];
List ChildItems = ChildList.FindAll(x => x.ParentID == Child.Id);
string ChildHtml = "";
if (ChildItems.Count > 0)
{ ChildHtml = GetToolHtml(Child.Id, ChildList); }
else
{ ChildHtml = GetHTML(Child, ChildItems.Count > 0 ? true : false); }
ToolHtml.Replace(content, ChildHtml + (i + 1 == Items.Count ? "" : content));
}
return ToolHtml.ToString();
}
private static string GetHTML(TrendToolInfo entity, bool isCreateChildItem)
{
//如果实体默认有html,直接返回原有的html
if (!string.IsNullOrEmpty(entity.HTML))
{ return entity.HTML.Trim(); }
string hHeader = "#@@Title";
string hContent = "#@@Content";
string hFooter = "#@@Remark";
string templateHtml = entity.TemplateHTML;
//主体html
List items = new List();
if (!String.IsNullOrEmpty(entity.ItemValue.Trim()))
{ items = entity.ItemValue.Split(',').ToList(); }
//if (items.Count != entity.ItemCount)
//{ throw new Exception("元素总数和和配置的总数不匹配"); }
//头部html
if (!string.IsNullOrEmpty(entity.Title))
{ templateHtml = templateHtml.Replace(hHeader, entity.Title); }
else
{ templateHtml = templateHtml.Replace(hHeader, ""); }
//匹配{}中的字符串.该字符串会循环生成
var match = Regex.Match(templateHtml, @"\{(.*)\}", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
string cycleHtml = match.Groups[1].Value;
templateHtml = templateHtml.Replace("{" + cycleHtml + "}", "");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < items.Count; i++)
{
string MainHtml = "";
if (string.IsNullOrEmpty(entity.HTML))
{
MainHtml = cycleHtml.Replace(value, items[i]);
}
else
{ MainHtml = entity.HTML; }
MainHtml = MainHtml.Replace(id, ToolUtility.FilterNamePrefix + entity.Id + "_" + i.ToString());
MainHtml = MainHtml.Replace(name, ToolUtility.FilterNamePrefix + entity.Id);
sb.Append(MainHtml);
}
if (isCreateChildItem)
{ sb.Append(content); }
templateHtml = templateHtml.Replace(hContent, sb.ToString());
//尾部html
if (!string.IsNullOrEmpty(entity.Remark))
{ templateHtml = templateHtml.Replace(hFooter, entity.Remark); }
else
{ templateHtml = templateHtml.Replace(hFooter, ""); }
templateHtml = templateHtml.Replace(name, ToolUtility.FilterNamePrefix + entity.Id);
return templateHtml;
}
}
}