懒羊羊
2023-12-28 e46d3baaf3e8d7d85f4bafec3aad75e52b078408
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Transactions;
using YX.Entity;
 
namespace YX.DAL
{
    public class SystemRole_Dal
    {
      
        /// <summary>
        /// 通过角色ID 获取角色
        /// </summary>
        /// <returns></returns>
        public List<Base_Roles> GetRoleByRoleId(string Roles_ID)
        {
            DataTable dt = new DataTable();
            try
            {
                using (Sam_DBEntities db=new Sam_DBEntities())
                {
                    //string sql = @"SELECT 
                    //        Roles_ID as '角色ID', 
                    //        ParentId as '节点位置', 
                    //        Roles_Name as  '角色名称', 
                    //        Roles_Remark as '角色描述',
                    //        SortCode as '排序',                          
                    //        CreateDate as '创建日期',
                    //        CreateUserName as '创建人',
                    //        ModifyDate as '修改日期', 
                    //        ModifyUserName as '修改人'
                    //        FROM Base_Roles WHERE DeleteMark != 0 and Roles_ID=@Roles_ID ORDER BY SortCode ASC";
 
                    //SqlParameter[] par = new SqlParameter[]
                    //{
                    //new SqlParameter("@Roles_ID", Roles_ID)
                    //};
                    //dt = SqlHelper.ExecuteDataset(db.Database.Connection.ConnectionString, CommandType.Text, sql, par).Tables[0];
                   return    db.Base_Roles.Where(o=>o.DeleteMark!=0 && o.Roles_ID==Roles_ID).OrderBy(o=>o.SortCode).ToList();
                }
                    
 
            }
            catch (Exception)
            {
 
                throw;
            }
        }
        /// <summary>
        /// 获取全部角色
        /// </summary>
        /// <returns></returns>
        public List<Base_Roles> GetRoles()
        {
            try
            {
                using (Sam_DBEntities db = new Sam_DBEntities())
                {
                    return db.Base_Roles.Where(o => o.DeleteMark != 0).OrderBy(o => o.SortCode).ToList();
                }
 
            }
            catch (Exception ex)
            {
 
                throw ex;
            }
        }
        /// <summary>
        /// 获取角色节点
        /// </summary>
        /// <returns></returns>
        public DataTable GetRoleParentId()
        {
            DataTable dt = new DataTable();
            try
            {
                using (Sam_DBEntities db = new Sam_DBEntities())
                {
                    var sql = @"SELECT Roles_ID,
                            Roles_Name + ' - ' + CASE ParentId WHEN '0' THEN '父节' ELSE  '子节' END AS Roles_Name
                            FROM Base_Roles WHERE DeleteMark = 1 ORDER BY SortCode ASC";
                    dt = SqlHelper.ExecuteDataset(db.Database.Connection.ConnectionString, CommandType.Text, sql).Tables[0];
                }
 
            }
            catch (Exception ex)
            {
 
                throw ex;
            }
            return dt;
        }
        public List<Base_UserRole> GetUserRoleByUserID(string UserID)
        {
            try
            {
                using (Sam_DBEntities db=new Sam_DBEntities())
                {
                    return db.Base_UserRole.Where(o => o.Base_UserInfo.User_ID == UserID).ToList();
                }
            }
            catch (Exception ex)
            {
 
                throw ex;
            }
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int AddRoles(Base_Roles info)
        {
            try
            {
                int result = 0;
                using (Sam_DBEntities db=new Sam_DBEntities())
                {
                    using (TransactionScope trans = new TransactionScope())
                    {
                        // UserInfo.User_Pwd = Md5Helper.Md5("123456");
                        db.Base_Roles.Add(info);
                        result = db.SaveChanges();
                        trans.Complete();
                        return result;
                    }
                }
            }
            catch (Exception ex)
            {
 
                throw ex;
            }
        }
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int UpdateRoles(Base_Roles info)
        {
            try
            {
                int result = 0;
                using (Sam_DBEntities db=new Sam_DBEntities())
                {
                    using (TransactionScope trans=new TransactionScope())
                    {
                        db.Database.ExecuteSqlCommand("delete from Base_RoleRight where Roles_ID={0}",info.Roles_ID);
 
                        if (info.Base_RoleRight != null && info.Base_RoleRight.Count>0)
                        {
                            string sql_roleright = "";
                            foreach (var item in info.Base_RoleRight)
                            {
                                sql_roleright += string.Format(@"insert into Base_RoleRight ([RoleRight_ID]
                                      ,[Roles_ID]
                                      ,[Menu_Id]
                                      ,[CreateDate]
                                      ,[CreateUserId]
                                      ,[CreateUserName])
                                        values('{0}','{1}','{2}','{3}','{4}','{5}');"
                                    , Guid.NewGuid().ToString(), info.Roles_ID, item.Menu_Id, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), item.CreateUserId, item.CreateUserName);
                            }
                            db.Database.ExecuteSqlCommand(sql_roleright);
                        }
 
                        //把当前实体的状态改为Modified
                        db.Base_Roles.Attach(info);
                        db.Entry(info).State = EntityState.Modified;//会全部字段修改
                        db.Entry(info).Property("CreateDate").IsModified = false;//不修改某字段
                        db.Entry(info).Property("CreateUserId").IsModified = false;//不修改某字段
                        db.Entry(info).Property("CreateUserName").IsModified = false;//不修改某字段
                        result = db.SaveChanges();
                        trans.Complete();
                        return result;
                    }
                }
            }
            catch (Exception ex)
            {
 
                throw ex;
            }
        }
        public int DeleteRole(string  RoleID)
        {
            try
            {
                using (Sam_DBEntities db=new Sam_DBEntities())
                {
                    var obj = new Base_Roles {  Roles_ID=RoleID };
                    db.Base_Roles.Attach(obj);
                    db.Base_Roles.Remove(obj);
                    return db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
 
                throw ex;
            }
        }
    }
}