yantian yue
2023-12-28 74222333166387c1e0508f8ccaf8347b861649a5
提交 | 用户 | 时间
e46d3b 1 using System;
2 using System.Collections.Generic;
3 using System.Data;
4 using System.Data.Entity;
5 using System.Data.SqlClient;
6 using System.Linq;
7 using System.Text;
8 using System.Transactions;
9 using YX.Entity;
10
11 namespace YX.DAL
12 {
13     public class SystemRole_Dal
14     {
15       
16         /// <summary>
17         /// 通过角色ID 获取角色
18         /// </summary>
19         /// <returns></returns>
20         public List<Base_Roles> GetRoleByRoleId(string Roles_ID)
21         {
22             DataTable dt = new DataTable();
23             try
24             {
25                 using (Sam_DBEntities db=new Sam_DBEntities())
26                 {
27                     //string sql = @"SELECT 
28                     //        Roles_ID as '角色ID', 
29                     //        ParentId as '节点位置', 
30                     //        Roles_Name as  '角色名称', 
31                     //        Roles_Remark as '角色描述',
32                     //        SortCode as '排序',                          
33                     //        CreateDate as '创建日期',
34                     //        CreateUserName as '创建人',
35                     //        ModifyDate as '修改日期', 
36                     //        ModifyUserName as '修改人'
37                     //        FROM Base_Roles WHERE DeleteMark != 0 and Roles_ID=@Roles_ID ORDER BY SortCode ASC";
38
39                     //SqlParameter[] par = new SqlParameter[]
40                     //{
41                     //new SqlParameter("@Roles_ID", Roles_ID)
42                     //};
43                     //dt = SqlHelper.ExecuteDataset(db.Database.Connection.ConnectionString, CommandType.Text, sql, par).Tables[0];
44                    return    db.Base_Roles.Where(o=>o.DeleteMark!=0 && o.Roles_ID==Roles_ID).OrderBy(o=>o.SortCode).ToList();
45                 }
46                     
47
48             }
49             catch (Exception)
50             {
51
52                 throw;
53             }
54         }
55         /// <summary>
56         /// 获取全部角色
57         /// </summary>
58         /// <returns></returns>
59         public List<Base_Roles> GetRoles()
60         {
61             try
62             {
63                 using (Sam_DBEntities db = new Sam_DBEntities())
64                 {
65                     return db.Base_Roles.Where(o => o.DeleteMark != 0).OrderBy(o => o.SortCode).ToList();
66                 }
67
68             }
69             catch (Exception ex)
70             {
71
72                 throw ex;
73             }
74         }
75         /// <summary>
76         /// 获取角色节点
77         /// </summary>
78         /// <returns></returns>
79         public DataTable GetRoleParentId()
80         {
81             DataTable dt = new DataTable();
82             try
83             {
84                 using (Sam_DBEntities db = new Sam_DBEntities())
85                 {
86                     var sql = @"SELECT Roles_ID,
87                             Roles_Name + ' - ' + CASE ParentId WHEN '0' THEN '父节' ELSE  '子节' END AS Roles_Name
88                             FROM Base_Roles WHERE DeleteMark = 1 ORDER BY SortCode ASC";
89                     dt = SqlHelper.ExecuteDataset(db.Database.Connection.ConnectionString, CommandType.Text, sql).Tables[0];
90                 }
91
92             }
93             catch (Exception ex)
94             {
95
96                 throw ex;
97             }
98             return dt;
99         }
100         public List<Base_UserRole> GetUserRoleByUserID(string UserID)
101         {
102             try
103             {
104                 using (Sam_DBEntities db=new Sam_DBEntities())
105                 {
106                     return db.Base_UserRole.Where(o => o.Base_UserInfo.User_ID == UserID).ToList();
107                 }
108             }
109             catch (Exception ex)
110             {
111
112                 throw ex;
113             }
114         }
115         /// <summary>
116         /// 添加角色
117         /// </summary>
118         /// <param name="info"></param>
119         /// <returns></returns>
120         public int AddRoles(Base_Roles info)
121         {
122             try
123             {
124                 int result = 0;
125                 using (Sam_DBEntities db=new Sam_DBEntities())
126                 {
127                     using (TransactionScope trans = new TransactionScope())
128                     {
129                         // UserInfo.User_Pwd = Md5Helper.Md5("123456");
130                         db.Base_Roles.Add(info);
131                         result = db.SaveChanges();
132                         trans.Complete();
133                         return result;
134                     }
135                 }
136             }
137             catch (Exception ex)
138             {
139
140                 throw ex;
141             }
142         }
143         /// <summary>
144         /// 修改角色
145         /// </summary>
146         /// <param name="info"></param>
147         /// <returns></returns>
148         public int UpdateRoles(Base_Roles info)
149         {
150             try
151             {
152                 int result = 0;
153                 using (Sam_DBEntities db=new Sam_DBEntities())
154                 {
155                     using (TransactionScope trans=new TransactionScope())
156                     {
157                         db.Database.ExecuteSqlCommand("delete from Base_RoleRight where Roles_ID={0}",info.Roles_ID);
158
159                         if (info.Base_RoleRight != null && info.Base_RoleRight.Count>0)
160                         {
161                             string sql_roleright = "";
162                             foreach (var item in info.Base_RoleRight)
163                             {
164                                 sql_roleright += string.Format(@"insert into Base_RoleRight ([RoleRight_ID]
165                                       ,[Roles_ID]
166                                       ,[Menu_Id]
167                                       ,[CreateDate]
168                                       ,[CreateUserId]
169                                       ,[CreateUserName])
170                                         values('{0}','{1}','{2}','{3}','{4}','{5}');"
171                                     , Guid.NewGuid().ToString(), info.Roles_ID, item.Menu_Id, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), item.CreateUserId, item.CreateUserName);
172                             }
173                             db.Database.ExecuteSqlCommand(sql_roleright);
174                         }
175
176                         //把当前实体的状态改为Modified
177                         db.Base_Roles.Attach(info);
178                         db.Entry(info).State = EntityState.Modified;//会全部字段修改
179                         db.Entry(info).Property("CreateDate").IsModified = false;//不修改某字段
180                         db.Entry(info).Property("CreateUserId").IsModified = false;//不修改某字段
181                         db.Entry(info).Property("CreateUserName").IsModified = false;//不修改某字段
182                         result = db.SaveChanges();
183                         trans.Complete();
184                         return result;
185                     }
186                 }
187             }
188             catch (Exception ex)
189             {
190
191                 throw ex;
192             }
193         }
194         public int DeleteRole(string  RoleID)
195         {
196             try
197             {
198                 using (Sam_DBEntities db=new Sam_DBEntities())
199                 {
200                     var obj = new Base_Roles {  Roles_ID=RoleID };
201                     db.Base_Roles.Attach(obj);
202                     db.Base_Roles.Remove(obj);
203                     return db.SaveChanges();
204                 }
205             }
206             catch (Exception ex)
207             {
208
209                 throw ex;
210             }
211         }
212     }
213 }