schangxiang@126.com
2025-09-17 a32e5a5b296cab5ccc20953ca4e801ca4f27bd85
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using iWareCommon.Common.Entity;
using iWareCommon.Common.EnumType;
using iWareCommon.Common.Service;
using iWareCommon.Utils;
using iWareDataCore.ORM;
using iWareDataCore.Properties;
using iWareDataCore.RBAC.Dao;
using iWareDataCore.RBAC.Entity;
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace iWareDataCore.RBAC.Service
{
   public class ContentService : CommonService<ContentEntity, RBACContent, DbModelCore>
    {
 
        private static object Lock = new object();
 
        private ContentService() : base(ContentDao.GetInstance()) { }
 
        private static ContentService Instance = null;
 
        /// <summary>
        /// 获取单例的方法
        /// </summary>
        /// <returns>菜单服务的单例实体</returns>
        public static ContentService GetInstance()
        {
 
            if (Instance == null)
            {
                lock (Lock)
                {
                    if (Instance == null)
                    {
                        Instance = new ContentService();
                    }
                }
            }
            return Instance;
        }
 
 
 
 
 
        public override int Delete(int id, out string msg)
        {
            msg = "";
 
            using (var dbModel = new DbModelCore())
            {
                try
                {
                    var roleContents = dbModel.RBACRoleContents.Where(x => x.contentid == id).ToList();
 
                    foreach (var roleContent in roleContents)
                    {
                        dbModel.RBACRoleContents.Remove(roleContent);
                    }
 
                    var one = dbModel.RBACContents.First(x => x.id == id);
 
                    var count = dbModel.RBACContents.Where(x => x.parentid == id).ToList().Count;
 
                    if (count > 0)
                    {
                        msg = "该菜单有子菜单,请先删除子菜单!";
                        return -1;
                    }
 
                    dbModel.RBACContents.Remove(one);
                    dbModel.SaveChanges();
 
 
                    return id;
                }
                catch (DbEntityValidationException ex)
                {
                    var errs = ex.EntityValidationErrors.SelectMany(validationResult => validationResult.ValidationErrors).Select(m => m.ErrorMessage);
                    msg = string.Join(", ", errs);
                    LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "Delete", msg);
                    return -1;
                }
 
                catch (Exception ex)
                {
                    msg = ex.Message;
                    LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "Delete", ex.Message);
                    return -1;
                }
            }
        }
 
 
        /// <summary>
        /// 判断souce能否移动到target
        /// </summary>
        /// <param name="sourceId">起始位置</param>
        /// <param name="targetId">目标位置</param>
        /// <param name="msg">异常错误信息</param>
        /// <returns>能否移动</returns>
        private bool CanMove(int sourceId, int targetId, DbModelCore dbModel, out string msg)
        {
            msg = "";
 
            var contents = ContentDao.GetInstance().QueryByParam(new QueryParam { Order = new Dictionary<string, object> { { "ContentIndex", "ASC" } } }, dbModel);
 
            if (contents == null)
            {
                return false;
            }
 
            var ids = new List<int>();
 
            foreach (var content in contents)
            {
                ids.Add(content.Id);
            }
 
            var childrenDictionary = TreeHelper<ContentEntity>.GetChildrenDictionary(contents);
 
            if (!ids.Contains(sourceId) && !sourceId.Equals(-1))
            {
                msg = "起始菜单不存在";
                return false;
            }
 
            if (!ids.Contains(targetId) && !targetId.Equals(-1))
            {
                msg = "目标菜单不存在";
                return false;
            }
 
 
            if (TreeHelper<ContentEntity>.IsChildOrEq(targetId, sourceId, childrenDictionary))
            {
                msg = "不能将菜单移动到本身或其子菜单下";
                return false;
            }
            return true;
        }
 
        public override int Update(ContentEntity t, out string msg)
        {
 
            using (var dbModel = new DbModelCore())
            {
                try
                {
                    if (!CanMove(t.Id, t.ParentId, dbModel, out msg))
                    {
                        return -1;
                    }
                    return ContentDao.GetInstance().Update(t, dbModel);
 
                }
                catch (DbEntityValidationException ex)
                {
                    var errs = ex.EntityValidationErrors.SelectMany(validationResult => validationResult.ValidationErrors).Select(m => m.ErrorMessage);
                    msg = string.Join(", ", errs);
                    LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "Update", msg);
                    return -1;
                }
 
 
 
                catch (Exception ex)
                {
                    msg = ex.HResult == (int)EDbError.记录已存在 ? "此纪录已存在" : ex.Message;
                    LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "Update", msg);
                    return -1;
                }
 
            }
        }
            
    }
}