WYB
2021-03-22 91b8cdad021ab052e4991f3d41834a6f0ddc36b8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using JiepeiWMS.Extends;
using JiepeiWMS.IRepository.Base;
using JiepeiWMS.Model.Interfaces;
using System;
using System.Linq;
using System.Threading.Tasks;
 
namespace JiepeiWMS.Repository.Base
{
    /// <summary>
    /// 排序扩展
    /// </summary>
    public static class ExtModTree
    {
        /// <summary>
        /// 设置树节点模型的相关属性(模型中的ParentId必须存在,重点:一但调用该方法则必须要保存到数据库)需配合事务,防止错误时可回滚
        /// </summary>
        /// <typeparam name="T">模型类型</typeparam>
        /// <param name="Dal">仓储类</param>
        /// <param name="BeforeId">指定ID之前,默认0最后</param>
        /// <param name="Models">要保存的模型列表</param>
        /// <returns>返回仓储类</returns>
        public static async Task<IBaseRepository<T>> _TreeAutoSetValues<T>(this IBaseRepository<T> 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;
        }
        /// <summary>
        /// 设置树节点模型的相关属性(模型中的ParentId必须存在,重点:一但调用该方法则必须要保存到数据库)
        /// </summary>
        /// <typeparam name="T">模型类型</typeparam>
        /// <param name="Dal">仓储类</param>
        /// <param name="Models">要保存的模型列表</param>
        /// <returns>返回仓储类</returns>
        public static async Task<IBaseRepository<T>> _TreeAutoSetValues<T>(this IBaseRepository<T> Dal, params T[] Models) where T : class, IModTree
        {
            return await _TreeAutoSetValues(Dal, 0, Models);
        }
    }
 
}