/// <summary> /// 新建实体时使用 /// </summary> public static Discipline New() { Discipline discipline = new Discipline() { Name = string.Empty, PostCode = string.Empty }; return discipline; }
/// <summary> /// 插入地区数据 /// </summary> /// <param name="discipline"></param> public void Insert(Discipline discipline) { Database database = CreateDAO(); database.OpenSharedConnection(); if (string.IsNullOrEmpty(discipline.ParentCode)) { discipline.Depth = 0; discipline.ChildCount = 0; } else { Discipline disciplineParent = Get(discipline.ParentCode); if (disciplineParent == null) return; discipline.Depth = disciplineParent.Depth + 1; discipline.ChildCount = 0; } object disciplineCode = database.Insert(discipline); if (disciplineCode != null) { var sql = PetaPoco.Sql.Builder; sql.Append("update tn_Disciplines set ChildCount=ChildCount+1 where DisciplineCode=@0", discipline.ParentCode); database.Execute(sql); } var sql_selectMaxDisplayOrder = Sql.Builder.Append("select MAX(DisplayOrder) from tn_Disciplines", discipline.DisciplineCode); int maxvalue = database.Execute(sql_selectMaxDisplayOrder); string sqltext = string.Format("update tn_Disciplines set DisplayOrder= {0}+1 where DisciplineCode = @0", maxvalue); var sql_SetDisplayOrder = Sql.Builder .Append(sqltext, discipline.DisciplineCode); database.Execute(sql_SetDisplayOrder); database.CloseSharedConnection(); //清空缓存 ClearChache(); }
/// <summary> /// 更新子节点信息 /// </summary> /// <param name="discipline">要更新的地区实体</param> /// <returns>更新之后的实体</returns> public void Update(Discipline discipline) { Database database = CreateDAO(); int newParentDepth = 0; //过滤错误:判定,如果不为空但是取不到就是错的 if (!string.IsNullOrEmpty(discipline.ParentCode)) { Discipline newParentDiscipline = Get(discipline.ParentCode); if (newParentDiscipline != null) newParentDepth = newParentDiscipline.Depth; else return; } var sql_selete = PetaPoco.Sql.Builder; sql_selete.Select("*").From("tn_Disciplines") .Where("DisciplineCode = @0", discipline.DisciplineCode); Discipline oldDiscipline = database.FirstOrDefault<Discipline>(sql_selete); discipline.Depth = newParentDepth + 1; IList<PetaPoco.Sql> sql_updates = new List<PetaPoco.Sql>(); //在没有更新父节点的情况下,仅更新自身的属性。 sql_updates.Add(new PetaPoco.Sql("update tn_Disciplines set Name = @1,PostCode = @2,DisplayOrder = @3 where DisciplineCode= @0", discipline.DisciplineCode, discipline.Name, discipline.PostCode, discipline.DisplayOrder)); //如果用户调整了父节点 if (discipline.ParentCode.ToLower() != oldDiscipline.ParentCode.ToLower()) { //如果用户更新了其父节点,更新自己的深度,更新原来的父节点和新的父节点的childcount sql_updates.Add(new PetaPoco.Sql("update tn_Disciplines set Depth = @1,ParentCode = @2 where DisciplineCode = @0", discipline.DisciplineCode, discipline.Depth, discipline.ParentCode)); sql_updates.Add(new PetaPoco.Sql("update tn_Disciplines set ChildCount = ChildCount - 1 where DisciplineCode = @0", oldDiscipline.ParentCode)); sql_updates.Add(new PetaPoco.Sql("update tn_Disciplines set ChildCount = ChildCount + 1 where DisciplineCode = @0", discipline.ParentCode)); int differenceDepth = discipline.Depth - oldDiscipline.Depth; //如果原来的父节点与新的父节点不是在同一等级上,更新所有的子节点的深度。 if (differenceDepth != 0) { IEnumerable<Discipline> childDisciplines = GetDescendants(discipline.DisciplineCode); if (childDisciplines != null && childDisciplines.Count() > 0) { foreach (Discipline childDiscipline in childDisciplines) sql_updates.Add(new PetaPoco.Sql("update tn_Disciplines set Depth = Depth + @1 where DisciplineCode = @0", childDiscipline.DisciplineCode, differenceDepth)); } } } database.Execute(sql_updates); ClearChache(); }
/// <summary> /// 递归获取parentDiscipline所有子Discipline /// </summary> /// <param name="parentDiscipline">父地区</param> /// <param name="allChildDisciplines">递归获取的所有子地区</param> private void RecursiveGetAllDisciplines(Discipline parentDiscipline, ref Dictionary<string, Discipline> allChildDisciplines) { if (parentDiscipline.Children.Count() > 0) { foreach (Discipline discipline in parentDiscipline.Children) { allChildDisciplines[discipline.DisciplineCode] = discipline; RecursiveGetAllDisciplines(discipline, ref allChildDisciplines); } } }
public void AppendChild(Discipline discipline) { if (children == null) children = new List<Discipline>(); children.Add(discipline); }
/// <summary> /// 更新地区 /// </summary> /// <param name="discipline">要更新的地区</param> /// <returns></returns> public void Update(Discipline discipline) { EventBus<Discipline>.Instance().OnBefore(discipline, new CommonEventArgs(EventOperationType.Instance().Update())); disciplineRepository.Update(discipline); EventBus<Discipline>.Instance().OnAfter(discipline, new CommonEventArgs(EventOperationType.Instance().Update())); }
/// <summary> /// 获取所有的父级地区 /// </summary> /// <param name="discipline"></param> /// <param name="disciplines"></param> private void RecursiveGetAllParentDiscipline(Discipline discipline, ref List<Discipline> disciplines) { if (discipline == null || string.IsNullOrEmpty(discipline.ParentCode.Trim())) return; Discipline parentDiscipline = disciplineRepository.Get(discipline.ParentCode); disciplines.Add(parentDiscipline); RecursiveGetAllParentDiscipline(parentDiscipline, ref disciplines); }