/// <summary> /// 递归获取parentArea所有子Area /// </summary> /// <param name="parentArea">父地区</param> /// <param name="allChildAreas">递归获取的所有子地区</param> private void RecursiveGetAllAreas(Area parentArea, ref Dictionary<string, Area> allChildAreas) { if (parentArea.Children.Count() > 0) { foreach (Area area in parentArea.Children) { allChildAreas[area.AreaCode] = area; RecursiveGetAllAreas(area, ref allChildAreas); } } }
/// <summary> /// 迭代获取地区 /// </summary> /// <param name="area"></param> /// <param name="rootAreaCode"></param> /// <param name="allParentAreas"></param> private void RecursiveGetAllParentArea(Area area, string rootAreaCode, ref List<Area> allParentAreas) { if (area == null || area.AreaCode == rootAreaCode) return; allParentAreas.Insert(0, area); Area parent = areaService.Get(area.ParentCode); if (parent != null) { RecursiveGetAllParentArea(parent, rootAreaCode, ref allParentAreas); } }
/// <summary> /// 插入地区数据 /// </summary> /// <param name="area"></param> public void Insert(Area area) { Database database = CreateDAO(); database.OpenSharedConnection(); if (string.IsNullOrEmpty(area.ParentCode)) { area.Depth = 0; area.ChildCount = 0; } else { Area areaParent = Get(area.ParentCode); if (areaParent == null) return; area.Depth = areaParent.Depth + 1; area.ChildCount = 0; } object areaCode = database.Insert(area); if (areaCode != null) { var sql = PetaPoco.Sql.Builder; sql.Append("update tn_Areas set ChildCount=ChildCount+1 where AreaCode=@0", area.ParentCode); database.Execute(sql); } var sql_selectMaxDisplayOrder = Sql.Builder.Append("select MAX(DisplayOrder) from tn_Areas", area.AreaCode); int maxvalue = database.Execute(sql_selectMaxDisplayOrder); string sqltext = string.Format("update tn_Areas set DisplayOrder= {0}+1 where AreaCode = @0", maxvalue); var sql_SetDisplayOrder = Sql.Builder .Append(sqltext, area.AreaCode); database.Execute(sql_SetDisplayOrder); database.CloseSharedConnection(); //清空缓存 ClearChache(); }
/// <summary> /// 更新子节点信息 /// </summary> /// <param name="area">要更新的地区实体</param> /// <returns>更新之后的实体</returns> public void Update(Area area) { Database database = CreateDAO(); int newParentDepth = 0; //过滤错误:判定,如果不为空但是取不到就是错的 if (!string.IsNullOrEmpty(area.ParentCode)) { Area newParentArea = Get(area.ParentCode); if (newParentArea != null) newParentDepth = newParentArea.Depth; else return; } var sql_selete = PetaPoco.Sql.Builder; sql_selete.Select("*").From("tn_Areas") .Where("AreaCode = @0", area.AreaCode); Area oldArea = database.FirstOrDefault<Area>(sql_selete); area.Depth = newParentDepth + 1; IList<PetaPoco.Sql> sql_updates = new List<PetaPoco.Sql>(); //在没有更新父节点的情况下,仅更新自身的属性。 sql_updates.Add(new PetaPoco.Sql("update tn_Areas set Name = @1,PostCode = @2,DisplayOrder = @3 where AreaCode= @0", area.AreaCode, area.Name, area.PostCode, area.DisplayOrder)); //如果用户调整了父节点 if (area.ParentCode.ToLower() != oldArea.ParentCode.ToLower()) { //如果用户更新了其父节点,更新自己的深度,更新原来的父节点和新的父节点的childcount sql_updates.Add(new PetaPoco.Sql("update tn_Areas set Depth = @1,ParentCode = @2 where AreaCode = @0", area.AreaCode, area.Depth, area.ParentCode)); sql_updates.Add(new PetaPoco.Sql("update tn_Areas set ChildCount = ChildCount - 1 where AreaCode = @0", oldArea.ParentCode)); sql_updates.Add(new PetaPoco.Sql("update tn_Areas set ChildCount = ChildCount + 1 where AreaCode = @0", area.ParentCode)); int differenceDepth = area.Depth - oldArea.Depth; //如果原来的父节点与新的父节点不是在同一等级上,更新所有的子节点的深度。 if (differenceDepth != 0) { IEnumerable<Area> childAreas = GetDescendants(area.AreaCode); if (childAreas != null && childAreas.Count() > 0) { foreach (Area childArea in childAreas) sql_updates.Add(new PetaPoco.Sql("update tn_Areas set Depth = Depth + @1 where AreaCode = @0", childArea.AreaCode, differenceDepth)); } } } database.Execute(sql_updates); ClearChache(); }
/// <summary> /// 添加地区 /// </summary> public void Create(Area area) { EventBus<Area>.Instance().OnBefore(area, new CommonEventArgs(EventOperationType.Instance().Create())); areaRepository.Insert(area); EventBus<Area>.Instance().OnAfter(area, new CommonEventArgs(EventOperationType.Instance().Create())); }
/// <summary> /// 获取所有的父级地区 /// </summary> /// <param name="area"></param> /// <param name="areas"></param> private void RecursiveGetAllParentArea(Area area, ref List<Area> areas) { if (area == null || string.IsNullOrEmpty(area.ParentCode.Trim())) return; Area parentArea = areaRepository.Get(area.ParentCode); areas.Add(parentArea); RecursiveGetAllParentArea(parentArea, ref areas); }
public void AppendChild(Area area) { if (children == null) children = new List<Area>(); children.Add(area); }
/// <summary> /// 新建实体时使用 /// </summary> public static Area New() { Area area = new Area() { Name = string.Empty, PostCode = string.Empty }; return area; }
/// <summary> /// 获取所有的父地区的集合(包含当前地区) /// </summary> /// <param name="area">当前地区</param> /// <param name="areas">所有的父地区</param> private static void GetAllArea(Area area, ref List<Area> areas) { if (string.IsNullOrEmpty(area.ParentCode) || area.AreaCode == "A1560000") return; areas.Insert(0, area); Area areaParent = areaService.Get(area.ParentCode); if (areaParent != null) GetAllArea(areaParent, ref areas); }