using JiepeiWMS.Extends; using JiepeiWMS.IRepository.Base; using JiepeiWMS.Model.Interfaces; using System; using System.Linq; using System.Threading.Tasks; namespace JiepeiWMS.Repository.Base { /// /// 排序扩展 /// public static class ExtModTree { /// /// 设置树节点模型的相关属性(模型中的ParentId必须存在,重点:一但调用该方法则必须要保存到数据库)需配合事务,防止错误时可回滚 /// /// 模型类型 /// 仓储类 /// 指定ID之前,默认0最后 /// 要保存的模型列表 /// 返回仓储类 public static async Task> _TreeAutoSetValues(this IBaseRepository Dal, int BeforeId, params T[] Models) where T : class, IModTree { var tablename = typeof(T).Name; foreach (var mod in Models) { //必须先保存获取主键 if (mod.Id <= 0) { throw new Exception(mod.Name + "不能小于等于0"); } //顶级节点时 if (mod.ParentId <= 0) { mod.ParentId = 0; mod.Depth = 1; mod.Path = mod.Id.ToString(); continue; } //读取父节点 var srcmod = (await Dal.Query(t => t.Id == mod.Id)).FirstOrDefault(); if (srcmod == null) { throw new Exception("信息不存在(Id=" + mod.Id + ")"); } //当父级ID和原来的相同则跳过 if (mod.ParentId == srcmod.ParentId && mod.Depth > 0) { continue; } //兄弟节点存在重名时 var tb = await Dal.QueryTable("select Id from [" + tablename + "] where ParentID=" + mod.ParentId + " and Name='" + mod.Name._ToSqlStr() + "' and Id!=" + mod.Id); var pidobj = tb.Rows.Count > 0 ? tb.Rows[0][0] : null; if (pidobj != null && pidobj != DBNull.Value) { throw new Exception(mod.Name + "已经存在"); } //读取父节点 var parent = (await Dal.Query(t => t.Id == mod.ParentId)).FirstOrDefault(); if (parent == null) { throw new Exception("父级不存在(Id=" + mod.ParentId + ")"); } //当前节点的父节点不允许为自身的子节点(节点无限循环) if (!string.IsNullOrEmpty(srcmod.Path) && parent.Path.StartsWith(srcmod.Path)) { throw new Exception("父级不能是自身子孙集"); } //更新子集 var pathnew = parent.Path + "," + mod.Id;//新路径 var pathcurrentcount = srcmod.Path.Length;//原路径长度 var sqlup = string.Format("update [{0}] set [Path]='{1}'+substring([Path],{2}+1,len([Path])-{2}),Depth=Depth+{4} where [Path] like '{3},%'", tablename, pathnew, pathcurrentcount, srcmod.Path, parent.Depth + 1 - srcmod.Depth); var count = Dal.ExecuteSqlCommand(sqlup); //更新信息 mod.Path = pathnew; mod.Depth = parent.Depth + 1; } foreach (var gmods in Models.GroupBy(t => t.ParentId)) { await Dal._SortByMoveTo(BeforeId, "ParentId=" + gmods.Key, gmods.ToArray()); } return Dal; } /// /// 设置树节点模型的相关属性(模型中的ParentId必须存在,重点:一但调用该方法则必须要保存到数据库) /// /// 模型类型 /// 仓储类 /// 要保存的模型列表 /// 返回仓储类 public static async Task> _TreeAutoSetValues(this IBaseRepository Dal, params T[] Models) where T : class, IModTree { return await _TreeAutoSetValues(Dal, 0, Models); } } }