lixiaojun
2023-12-14 b802ff5e1f903b526326c85d5658565a8dba95ec
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
namespace IStation.DAL
{
    /// <summary>
    /// 业务清单
    /// </summary>
    public partial class LogicTree : Yw.DAL.BaseDAL_TreeSorter<Entity.LogicTree>
    {
        /// <summary>
        /// 
        /// </summary>
        public override ConnectionConfig ConnectionConfig
        {
            get { return ConfigHelper.DefaultConnectionConfig; }
        }
 
        /// <summary>
        /// 通过 PolicyID 获取
        /// </summary>
        public List<Entity.LogicTree> GetByPolicyID(long PolicyID)
        {
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
                return db.Queryable<Entity.LogicTree>()
                    .Where(x => x.PolicyID == PolicyID)
                    .ToList();
            }
        }
 
        /// <summary>
        /// 保存
        /// </summary>
        public bool Save(long PolicyID, List<Entity.LogicTreeSaveTree> treeList)
        {
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
                try
                {
                    if (treeList == null || treeList.Count < 1)
                    {
                        return db.Deleteable<Entity.LogicTree>()
                            .Where(x => x.PolicyID == PolicyID)
                            .ExecuteCommandHasChange();
                    }
                    db.BeginTran();
 
                    var idList = new List<long>();
 
                    #region 迭代函数
 
                    long Append(Entity.LogicTreeSaveTree tree, List<long> parentIds, int sortCode)
                    {
                        if (parentIds == null)
                            parentIds = new List<long>();
                        sortCode++;
 
                        //构造
                        var entity = new Entity.LogicTree()
                        {
                            ID = tree.ID,
                            PolicyID = PolicyID,
                            ParentIds = TreeParentIdsHelper.ToString(parentIds),
                            LogicType = tree.LogicType,
                            LogicID = tree.LogicID,
                            SortCode = sortCode
                        };
 
                        if (entity.ID < 1)
                        {
                            entity.ID = db.Insertable(entity).ExecuteReturnSnowflakeId();
                            if (entity.ID < 1)
                            {
                                return default;
                            }
                        }
                        else
                        {
                            var bol = db.Updateable(entity).ExecuteCommandHasChange();
                            if (!bol)
                            {
                                return default;
                            }
                        }
 
                        idList.Add(entity.ID);
 
                        if (tree.Children != null && tree.Children.Count > 0)
                        {
                            var childParentIds = parentIds.Append(entity.ID).ToList();
                            var childSortCode = 0;
                            foreach (var child in tree.Children)
                            {
                                var id = Append(child, childParentIds, childSortCode);
                                if (id < 1)
                                    return default;
                            }
                        }
 
                        return entity.ID;
                    }
 
                    #endregion
 
                    #region 遍历处理
 
                    foreach (var tree in treeList)
                    {
                        var id = Append(tree, null, 0);
                        if (id < 1)
                        {
                            db.RollbackTran();
                            return false;
                        }
                    }
 
                    #endregion
 
                    #region 清理目录
 
                    if (idList.Count > 0)
                    {
                        db.Deleteable<Entity.LogicTree>()
                           .Where(x => x.PolicyID == PolicyID && !idList.Contains(x.ID))
                           .ExecuteCommandHasChange();
                    }
                    else
                    {
                        db.Deleteable<Entity.LogicTree>()
                            .Where(x => x.PolicyID == PolicyID)
                            .ExecuteCommandHasChange();
                    }
 
                    #endregion
 
                    db.CommitTran();
                    return true;
                }
                catch
                {
                    db.RollbackTran();
                    throw;
                }
            }
        }
 
 
 
 
 
 
 
 
    }
}