ke_junjie
2025-06-04 101c57ec4c28bc3c36e49c50a926e9e7c0dd0247
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
184
185
186
187
188
189
190
191
192
193
using IWareDataAccess.EF;
using IWareDataAccess.Entity.Sys;
using IWareDataAccess.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IWareDataAccess.Sys.ROLE_VS_PERMISSION
{
    public static class Role_Vs_PermissionSqlFunc
    {
        /// <summary>
        /// 搜索视图
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public static List<View_ROLE_VS_PERMISSION> SearchView(PermissionVsRoleWebEntity webEntity,out string msg)
        {
            msg = "";
            using (Model edm = new Model())
            {
                var f = PredicateBuilder.True<View_ROLE_VS_PERMISSION>();
 
                //便利所有属性
                Type type = webEntity.GetType();
                foreach (var i in type.GetProperties())
                {
                    object v = Helper.Helper.GetFieldValueByName(webEntity, i.Name);
                    if (i.PropertyType == typeof(String))
                    {
                        string value;
                        if (v != null)
                        {
                            value = v.ToString();
                            f = f.And(x => Helper.Helper.GetFieldValueByName(x, i.Name) != null ? Helper.Helper.GetFieldValueByName(x, i.Name).ToString().Contains(value) : false);
                        }
                    }
                    else
                    {
                        if (v != null)
                        {
                            f = f.And(x => Object.Equals(Helper.Helper.GetFieldValueByName(x, i.Name), v));
                        }
                    }
                }
 
                List<View_ROLE_VS_PERMISSION> list = edm.View_ROLE_VS_PERMISSION.Where(f.Compile()).ToList();
 
                return list;
            }
        }
 
        /// <summary>
        /// 搜索视图
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public static int SearchViewNum(PermissionVsRoleWebEntity webEntity)
        {
            using (Model edm = new Model())
            {
                var f = PredicateBuilder.True<View_ROLE_VS_PERMISSION>();
 
                //便利所有属性
                Type type = webEntity.GetType();
                foreach (var i in type.GetProperties())
                {
                    object v = Helper.Helper.GetFieldValueByName(webEntity, i.Name);
                    if (i.PropertyType == typeof(String))
                    {
                        string value;
                        if (v != null)
                        {
                            value = v.ToString();
                            f = f.And(x => Helper.Helper.GetFieldValueByName(x, i.Name) != null ? Helper.Helper.GetFieldValueByName(x, i.Name).ToString().Contains(value) : false);
                        }
                    }
                    else
                    {
                        if (v != null)
                        {
                            f = f.And(x => Object.Equals(Helper.Helper.GetFieldValueByName(x, i.Name), v));
                        }
                    }
                }
 
                int num = edm.View_ROLE_VS_PERMISSION.Where(f.Compile()).Count();
 
                return num;
            }
        }
 
        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public static bool AddOrUpdate(PermissionVsRoleWebEntity webRoleVsPermission,out string msg)
        {
            msg = "";
            using (Model edm = new Model())
            {
                RoleWebEntity searchEntity = new RoleWebEntity();
                //查询是否存在此角色
                SYS_ROLE role = edm.SYS_ROLE.FirstOrDefault(x => x.ROLENAME == webRoleVsPermission.roleName);
                if (role == null)
                {
                    //无此角色
                    msg = "无此角色";
                    return false;
                }
                int type = 1;
                if (webRoleVsPermission.permissionNameList != null && webRoleVsPermission.permissionNameList.Count > 0)
                {
                    int id = webRoleVsPermission.permissionNameList[0];
                    type=(int)edm.SYS_PERMISSION.FirstOrDefault(x=>x.ID==id).TYPE;
                }
                else 
                {
                    msg = "获取该角色的权限失败!";
                    return false;
                }
                if (DeleteAll(webRoleVsPermission.roleName, type, out msg))
                {
                    foreach (var i in webRoleVsPermission.permissionNameList)
                    {
                        //如果输入权限包含遍历的权限,添加
 
                        SYS_ROLE_VS_PERMISSION rvp = new SYS_ROLE_VS_PERMISSION();
                        rvp.PERMISSIONID = i;
                        rvp.ROLEID = role.ID;
                        edm.SYS_ROLE_VS_PERMISSION.AddOrUpdateExtension(rvp);
 
                    }
 
                }
                else 
                {
                    msg = "保存失败!";
                    return false;
                }
             
                if (edm.SaveChanges() > 0)
                {
                    return true;
                }
                else
                {
                    msg = "修改失败";
                    return false;
                }
            }
        }
 
        /// <summary>
        /// 初始化角色权限
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public static bool DeleteAll(string roleName,int type,out string msg)
        {
            try
            {
                msg = "";
                using (Model edm = new Model())
                {
                    List<SYS_ROLE_VS_PERMISSION> rvplst = new List<SYS_ROLE_VS_PERMISSION>();
                    var list = edm.SYS_ROLE_VS_PERMISSION.Where(x => x.SYS_ROLE.ROLENAME == roleName);
                    var plst = edm.SYS_PERMISSION.Where(x => x.TYPE == type).ToList();
                    plst.ForEach(x =>
                    {
                        var rvp = list.FirstOrDefault(y => y.PERMISSIONID == x.ID);
                        if (rvp != null)
                        {
                            rvplst.Add(rvp);
                        }
                    });
                    edm.SYS_ROLE_VS_PERMISSION.RemoveRange(rvplst);
                    edm.SaveChanges();
                    return true;
 
                }
            }
            catch (Exception ex)
            {
                msg = "保存失败";
                return false;
            }
        }
    }
}