using System; using System.Text; using System.Collections; using BP.DA; namespace BP.En { /// /// 属性列表 /// public class EntityTreeAttr { /// /// 树结构编号 /// public const string No = "No"; /// /// 名称 /// public const string Name = "Name"; /// /// 父节编号 /// public const string ParentNo = "ParentNo"; /// /// 顺序号 /// public const string Idx = "Idx"; /// /// 控制参数 /// public const string CtrlWayPara = "CtrlWayPara"; /// /// 图标 /// public const string ICON = "ICON"; /// /// 节点类型 /// public const string MenuType = "MenuType"; } /// /// 树实体 /// abstract public class EntityTree : Entity { #region 属性 public bool ItIsRoot { get { if (this.ParentNo == "-1" || this.ParentNo == "0") return true; if (this.ParentNo == this.No) return true; return false; } } /// /// 唯一标示 /// public string No { get { return this.GetValStringByKey(EntityTreeAttr.No); } set { this.SetValByKey(EntityTreeAttr.No, value); } } public void setNo(string val) { this.SetValByKey(EntityTreeAttr.No, val); } /// /// 名称 /// public string Name { get { return this.GetValStringByKey(EntityTreeAttr.Name); } set { this.SetValByKey(EntityTreeAttr.Name, value); } } public void setName(string val) { this.SetValByKey(EntityTreeAttr.Name, val); } /// /// 父节点编号 /// public string ParentNo { get { return this.GetValStringByKey(EntityTreeAttr.ParentNo); } set { this.SetValByKey(EntityTreeAttr.ParentNo, value); } } public void setParentNo(string val) { this.SetValByKey(EntityTreeAttr.ParentNo, val); } /// /// 图标 /// public string ICON { get { return this.GetValStringByKey(EntityTreeAttr.ICON); } set { this.SetValByKey(EntityTreeAttr.ICON, value); } } /// /// 顺序号 /// public int Idx { get { return this.GetValIntByKey(EntityTreeAttr.Idx); } set { this.SetValByKey(EntityTreeAttr.Idx, value); } } #endregion #region 构造函数 /// /// 主键 /// public override string PK { get { return EntityTreeAttr.No; } } /// /// 树结构编号 /// public EntityTree() { } /// /// 树结构编号 /// /// 编号 public EntityTree(string no) { if (DataType.IsNullOrEmpty(no)) throw new Exception(this.EnDesc + "@对表[" + this.EnDesc + "]进行查询前必须指定编号。"); this.No = no; if (this.Retrieve() == 0) throw new Exception("@没有" + this._enMap.PhysicsTable + ", No = " + this.No + "的记录。"); } #endregion #region 业务逻辑处理 /// /// 重新设置treeNo /// public void ResetTreeNo() { } /// /// 检查名称的问题. /// /// protected override bool beforeInsert() { if (this.EnMap.ItIsAllowRepeatName == false) { if (this.PKCount == 1) { if (this.ExitsValueNum("Name", this.Name) >= 1) throw new Exception("@插入失败[" + this.EnMap.EnDesc + "] 编号[" + this.No + "]名称[" + Name + "]重复."); } } if (DataType.IsNullOrEmpty(this.No)) this.No = this.GenerNewNoByKey("No"); return base.beforeInsert(); } protected override bool beforeUpdate() { if (this.EnMap.ItIsAllowRepeatName == false) { if (this.PKCount == 1) { if (this.ExitsValueNum("Name", this.Name) >= 2) throw new Exception("@更新失败[" + this.EnMap.EnDesc + "] 编号[" + this.No + "]名称[" + Name + "]重复."); } } return base.beforeUpdate(); } #endregion #region 可让子类调用的方法 /// /// 新建同级节点 /// /// public EntityTree DoCreateSameLevelNode(string name = null) { EntityTree en = this.CreateInstance() as EntityTree; if (this.ClassID.Contains("FlowSort") || this.ClassID.Contains("SysFormTree")) en.No = DBAccess.GenerGUID(); else en.No = DBAccess.GenerOID(this.ToString()).ToString(); // en.GenerNewNoByKey(EntityTreeAttr.No); if (name == null) en.Name = "新建节点" + en.No; else en.Name = name; en.ParentNo = this.ParentNo; en.Insert(); return en; } /// /// 新建子节点 /// /// public EntityTree DoCreateSubNode(string name = null) { EntityTree en = this.CreateInstance() as EntityTree; en.Copy(this); if (this.ClassID.Contains("FlowSort") || this.ClassID.Contains("SysFormTree")) en.No = DBAccess.GenerGUID(10, null, null); else en.No = DBAccess.GenerOID(this.ToString()).ToString(); if (name == null) en.Name = "新建节点" + en.No; else en.Name = name; en.ParentNo = this.No; en.Insert(); return en; } /// /// 上移 /// /// public string DoUp() { this.DoOrderUp(EntityTreeAttr.ParentNo, this.ParentNo, EntityTreeAttr.Idx); return "执行成功."; } /// /// 下移 /// /// public string DoDown() { this.DoOrderDown(EntityTreeAttr.ParentNo, this.ParentNo, EntityTreeAttr.Idx); return "执行成功."; } #endregion } /// /// 树实体s /// abstract public class EntitiesTree : Entities { #region 转化为树结构的tree. private StringBuilder appendMenus = null; private StringBuilder appendMenuSb = null; /// /// 转化为json树 /// /// public string ToJsonOfTree(string rootNo = "0") { appendMenus = new StringBuilder(); appendMenuSb = new StringBuilder(); EntityTree root = this.GetEntityByKey(EntityTreeAttr.ParentNo, rootNo) as EntityTree; if (root == null) return "err@没有找到rootNo=" + rootNo + "的entity."; appendMenus.Append("[{"); appendMenus.Append("'id':'" + root.No + "',"); appendMenus.Append("'pid':'" + root.ParentNo + "',"); appendMenus.Append("'text':'" + root.Name + "'"); // appendMenus.Append(IsPermissionsNodes(ens, dms, root.No)); // 增加它的子级. appendMenus.Append(",'children':"); AddChildren(root, this); appendMenus.Append(appendMenuSb); appendMenus.Append("}]"); return ReplaceIllgalChart(appendMenus.ToString()); } public void AddChildren(EntityTree parentEn, EntitiesTree ens) { appendMenus.Append(appendMenuSb); appendMenuSb.Clear(); appendMenuSb.Append("["); foreach (EntityTree item in ens) { if (item.ParentNo != parentEn.No) continue; appendMenuSb.Append("{'id':'" + item.No + "','pid':'" + item.ParentNo + "','text':'" + item.Name + "'"); //appendMenuSb.Append(IsPermissionsNodes(ens, dms, item.No)); EntityTree treeNode = item as EntityTree; // 增加它的子级. appendMenuSb.Append(",'children':"); AddChildren(item, ens); appendMenuSb.Append("},"); } if (appendMenuSb.Length > 1) appendMenuSb = appendMenuSb.Remove(appendMenuSb.Length - 1, 1); appendMenuSb.Append("]"); appendMenus.Append(appendMenuSb); appendMenuSb.Clear(); } public string ReplaceIllgalChart(string s) { StringBuilder sb = new StringBuilder(); for (int i = 0, j = s.Length; i < j; i++) { char c = s[i]; switch (c) { case '\"': sb.Append("\\\""); break; case '\\': sb.Append("\\\\"); break; case '/': sb.Append("\\/"); break; case '\b': sb.Append("\\b"); break; case '\f': sb.Append("\\f"); break; case '\n': sb.Append("\\n"); break; case '\r': sb.Append("\\r"); break; case '\t': sb.Append("\\t"); break; default: sb.Append(c); break; } } return sb.ToString(); } #endregion 转化为树结构的tree. #region 构造. /// /// 查询他的子节点 /// /// /// public int RetrieveHisChinren(EntityTree en) { int i = this.Retrieve(EntityTreeAttr.ParentNo, en.No); this.AddEntity(en); return i + 1; } /// /// 获取它的子节点 /// /// /// public EntitiesTree GenerHisChinren(EntityTree en) { EntitiesTree ens = this.CreateInstance() as EntitiesTree; foreach (EntityTree item in ens) { if (en.ParentNo == en.No) ens.AddEntity(item); } return ens; } /// /// 根据位置取得数据 /// public new EntityTree this[int index] { get { return (EntityTree)this.InnerList[index]; } } /// /// 构造 /// public EntitiesTree() { } #endregion 构造. } }