TrendToolInfoPage.aspx.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using CB.Framework;
  8. using CB.Common;
  9. using CB.Data;
  10. using CB.Entity;
  11. using System.Text.RegularExpressions;
  12. using System.Text;
  13. using CB.TrendTool;
  14. using System.Reflection;
  15. namespace CB.Admin.Plugins.TrendTool
  16. {
  17. public partial class TrendToolInfoPage : AdminPage
  18. {
  19. protected int PageId = 0;
  20. protected void Page_Load(object sender, EventArgs e)
  21. {
  22. PageId = WRequest.GetInt("PageID", 0);
  23. if (!IsPostBack)
  24. {
  25. List<ToolFunctionAttr> list = new List<ToolFunctionAttr>();
  26. BindList<TrendToolRule>(ref list);
  27. foreach (ToolFunctionAttr item in list)
  28. {
  29. ListItem li = new ListItem(item.Name, ((int)item.FilterType).ToString());
  30. ddlFilterFunEnum.Items.Add(li);
  31. }
  32. BindRepeaterList();
  33. }
  34. }
  35. protected override void BindRepeaterList()
  36. {
  37. int id = WRequest.GetInt("ID", 0);
  38. List<Entity.TrendToolInfo> lists = new List<Entity.TrendToolInfo>();
  39. lists = TrendToolService.ChildToList(new Entity.TrendToolInfo() { Id = id }).ToList();
  40. //lists = Caches.GetTrendToolChildList(new Entity.TrendToolInfo() { Id = id }).ToList();
  41. repeaterList.DataSource = lists;
  42. repeaterList.DataBind();
  43. BindDropDownListToolName(lists.ToList());
  44. }
  45. int levelNum = 1;
  46. private void BindDropDownListToolName(List<TrendToolInfo> list, int parentId = -1)
  47. {
  48. TrendToolInfo parent = new TrendToolInfo();
  49. parent = parentId == -1 ? list.Find(x => x.ParentID == parentId) : list.Find(x => x.Id == parentId);
  50. if (parentId == -1)
  51. {
  52. ddlParentID.Items.Clear();
  53. ddlParentID.Items.Insert(0, new ListItem("--请选择--", "-1"));
  54. }
  55. if (parent != null)
  56. {
  57. ListItem li = new ListItem(GetPrefix() + parent.ToolName, parent.Id.ToString());
  58. ddlParentID.Items.Add(li);
  59. }
  60. List<TrendToolInfo> childs = list.FindAll(x => parent != null && x.ParentID == parent.Id);
  61. foreach (TrendToolInfo item in childs)
  62. {
  63. List<TrendToolInfo> childItems = list.FindAll(x => x.ParentID == item.Id);
  64. levelNum += 1;
  65. if (childItems.Count > 0)
  66. {
  67. BindDropDownListToolName(list, item.Id);
  68. }
  69. else
  70. {
  71. ListItem li = new ListItem(GetPrefix() + item.ToolName, item.Id.ToString());
  72. ddlParentID.Items.Add(li);
  73. }
  74. levelNum -= 1;
  75. }
  76. }
  77. private string GetPrefix()
  78. {
  79. string prefix = "";
  80. for (int i = 0; i < levelNum; i++)
  81. {
  82. prefix += "-";
  83. }
  84. return prefix;
  85. }
  86. protected void repeaterList_ItemCommand(object source, RepeaterCommandEventArgs e)
  87. {
  88. switch (e.CommandName)
  89. {
  90. case "dnedit":
  91. {
  92. int id = TypeConverter.ObjectToInt(e.CommandArgument, 0);
  93. Entity.TrendToolInfo entity = TrendToolService.Get(id);
  94. BindEntityToPage(entity);
  95. }
  96. break;
  97. case "dndel":
  98. {
  99. int id = TypeConverter.ObjectToInt(e.CommandArgument, 0);
  100. TrendToolService.Delete(id);
  101. BindRepeaterList();
  102. }
  103. break;
  104. }
  105. }
  106. protected void btnUpdate_Click(object sender, EventArgs e)
  107. {
  108. }
  109. protected void btnClear_Click(object sender, EventArgs e)
  110. {
  111. ClearFormElement();
  112. }
  113. protected void btnSave_Click(object sender, EventArgs e)
  114. {
  115. TrendToolInfo nowEntity = PageToEntity(false);
  116. TrendToolService.Save(nowEntity);
  117. ClearFormElement();
  118. BindRepeaterList();
  119. //ShowMessageBox("保存成功!");
  120. }
  121. protected void btnCreateHTML_Click(object sender, EventArgs e)
  122. {
  123. if (this.ddlParentID.SelectedValue == "")
  124. {
  125. ShowMessageBox("请选择父节点");
  126. return;
  127. }
  128. TrendToolInfo nowEntity = PageToEntity(true);
  129. List<Entity.TrendToolInfo> lists = new List<Entity.TrendToolInfo>();
  130. //lists = TrendToolService.ChildToList(new Entity.TrendToolInfo() { Id = nowEntity.Id }).ToList();
  131. lists = Caches.GetTrendToolChildList(new Entity.TrendToolInfo() { Id = nowEntity.Id }).ToList();
  132. lists.Remove(lists.Find(x => x.Id == nowEntity.Id));
  133. lists.Add(nowEntity);
  134. lists = lists.OrderBy(x => x.ToolOrder).ToList();
  135. string html = TrendToolHtml.GetToolHtml(nowEntity.Id, lists);
  136. if (html.Length > 4)
  137. {
  138. if (html.Trim().Substring(0, 4) == "<tr>")
  139. { html = "<table>" + html + "</table>"; }
  140. }
  141. this.txtOldHTML.Value = html;
  142. }
  143. /// <summary>
  144. /// 实体绑定到页面
  145. /// </summary>
  146. /// <param name="entity"></param>
  147. private void BindEntityToPage(TrendToolInfo entity)
  148. {
  149. //this.txtNewHTML.Value = "";
  150. this.txtID.Value = entity.Id.ToString();
  151. this.txtToolName.Text = entity.ToolName;
  152. this.txtTitle.Text = entity.Title;
  153. this.txtRemark.Text = entity.Remark;
  154. this.txtPageID.Value = entity.PageID.ToString();
  155. this.txtItemValue.Text = entity.ItemValue;
  156. this.txtItemCount.Text = entity.ItemCount.ToString();
  157. this.txtIndexStart.Text = entity.IndexStart.ToString();
  158. this.txtIndexEnd.Text = entity.IndexEnd.ToString();
  159. string html = entity.HTML;
  160. if (entity.HTML.Length > 0 && html.Trim().Substring(0, 4) == "<tr>")
  161. { html = "<table>" + html + "</table>"; }
  162. this.txtOldHTML.Value = html;
  163. this.txtTemplateHtml.Value = entity.TemplateHTML;
  164. this.txtToolOrder.Text = entity.ToolOrder.ToString();
  165. this.ddlStatus.ClearSelection();
  166. ListItem li0 = this.ddlStatus.Items.FindByValue(Convert.ToInt32(entity.Status).ToString());
  167. if (li0 != null) li0.Selected = true;
  168. this.ddlIsSaveData.ClearSelection();
  169. ListItem li1 = this.ddlIsSaveData.Items.FindByValue(entity.IsSaveData.ToString());
  170. if (li1 != null) li1.Selected = true;
  171. this.ddlFilterFunEnum.ClearSelection();
  172. ListItem li2 = ddlFilterFunEnum.Items.FindByValue(Convert.ToInt32(entity.FilterFunEnum).ToString());
  173. if (li2 != null) li2.Selected = true;
  174. //this.ddlToolType.ClearSelection();
  175. //this.ddlToolType.SelectedValue = Convert.ToInt32(entity.ToolType).ToString();
  176. this.ddlParentID.ClearSelection();
  177. ListItem li3 = this.ddlParentID.Items.FindByValue(entity.ParentID.ToString());
  178. if (li3 != null) li3.Selected = true;
  179. this.ddlFilterCodeType.ClearSelection();
  180. ListItem li4 = this.ddlFilterCodeType.Items.FindByValue(Convert.ToInt32(entity.FilterCodeType).ToString());
  181. if (li4 != null) li4.Selected = true;
  182. }
  183. /// <summary>
  184. /// 页面转换为实体
  185. /// </summary>
  186. /// <param name="isCreateHtml">是否是重新生成HTML</param>
  187. /// <returns></returns>
  188. private TrendToolInfo PageToEntity(bool isCreateHtml)
  189. {
  190. TrendToolInfo entity = new TrendToolInfo();
  191. entity.Id = TypeConverter.ObjectToInt(this.txtID.Value, 0);
  192. entity.ToolName = this.txtToolName.Text.Trim();
  193. entity.ItemValue = this.txtItemValue.Text.Trim();
  194. entity.Title = this.txtTitle.Text.Trim();
  195. entity.Remark = this.txtRemark.Text.Trim();
  196. string html = this.txtOldHTML.Value.Trim();
  197. if (!isCreateHtml && html.Length > 7)
  198. {
  199. if (html.Substring(0, 7) == "<table>")
  200. html = html.Substring(7, html.Length - 7).Trim();
  201. if (html.Substring(0, 7) == "<tbody>")
  202. html = html.Substring(7, html.Length - 7).Trim();
  203. if (html.Substring(html.Length - 8, 8) == "</table>")
  204. html = html.Substring(0, html.Length - 8).Trim();
  205. if (html.Substring(html.Length - 8, 8) == "</tbody>")
  206. html = html.Substring(0, html.Length - 8).Trim();
  207. entity.HTML = html.Trim();
  208. }
  209. entity.TemplateHTML = this.txtTemplateHtml.Value.Trim();
  210. if (string.IsNullOrEmpty(this.txtPageID.Value.Trim()))
  211. {
  212. PageId = WRequest.GetInt("PageID", 0);
  213. }
  214. entity.PageID = PageId;
  215. entity.ParentID = TypeConverter.ObjectToInt(this.ddlParentID.SelectedValue, -1);
  216. entity.ItemCount = TypeConverter.ObjectToInt(this.txtItemCount.Text.Trim());
  217. entity.IndexStart = TypeConverter.ObjectToInt(this.txtIndexStart.Text.Trim());
  218. entity.IndexEnd = TypeConverter.ObjectToInt(this.txtIndexEnd.Text.Trim());
  219. entity.ToolOrder = TypeConverter.ObjectToInt(this.txtToolOrder.Text.Trim());
  220. string statusValue = this.ddlStatus.SelectedValue;
  221. entity.Status = TypeConverter.StringToEnum<FilterStatus>(statusValue);
  222. string funValue = ddlFilterFunEnum.SelectedValue;
  223. entity.FilterFunEnum = TypeConverter.StringToEnum<FilterType>(funValue);
  224. //string toolTypeValue = ddlToolType.SelectedValue;
  225. //entity.ToolType = TypeConverter.StringToEnum<ToolType>(toolTypeValue);
  226. string filterCodeType = ddlFilterCodeType.SelectedValue;
  227. entity.FilterCodeType = TypeConverter.StringToEnum<FilterCodeType>(filterCodeType);
  228. entity.IsSaveData = TypeConverter.ObjectToBool(this.txtTitle.Text.Trim(), false);
  229. return entity;
  230. }
  231. private void ClearFormElement()
  232. {
  233. this.form1.Controls.OfType<TextBox>().ToList().ForEach(t => t.Text = "");
  234. this.form1.Controls.OfType<DropDownList>().ToList().ForEach(t => { t.ClearSelection(); t.SelectedIndex = 0; });
  235. this.form1.Controls.OfType<HiddenField>().ToList().ForEach(t => t.Value = "");
  236. this.form1.Controls.OfType<Label>().ToList().ForEach(t => t.Text = "");
  237. this.txtTemplateHtml.Value = "";
  238. this.txtOldHTML.Value = "";
  239. }
  240. protected void Button1_Click(object sender, EventArgs e)
  241. {
  242. TrendToolHtml.GetToolsHtml(PageId);
  243. }
  244. //绑定方法列表方法
  245. private void BindList<T>(ref List<ToolFunctionAttr> lists)
  246. {
  247. MethodInfo[] methodList = typeof(T).GetMethods();
  248. if (null == methodList || 0 >= methodList.Length) return;
  249. foreach (MethodInfo item in methodList)
  250. {
  251. ToolFunctionAttr[] description = item.GetCustomAttributes(typeof(ToolFunctionAttr), false) as ToolFunctionAttr[];
  252. if (null != description && 1 <= description.Length)
  253. lists.Add(new ToolFunctionAttr(description[0].Name, description[0].FilterType));
  254. }
  255. }
  256. }
  257. }