using NetLibrary.Data; using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Linq; using System.Text; using TradeModel; using NetLibrary; namespace TradeData { public class JC_RoleInfoService { #region 读取角色列表 public static List GetList(int CompanyID) { string tsql = @" select * from JC_RoleInfo where CompanyID=@CompanyID order by RoleId desc "; Database db = DatabaseFactory.CreateDatabase(); DbCommand cmd = db.GetSqlStringCommand(tsql); db.AddInParameter(cmd, "@CompanyID", DbType.Int32, CompanyID); DataTable tb= db.ExecuteDataTable(cmd); return tb.ToList(); } #endregion #region 读取角色列表 public static List GetUserRoleList(int UserId) { string tsql = @" select * from JC_RoleUser where UserId=@UserId and RoleType=1 "; Database db = DatabaseFactory.CreateDatabase(); DbCommand cmd = db.GetSqlStringCommand(tsql); db.AddInParameter(cmd, "@UserId", DbType.Int32, UserId); DataTable tb = db.ExecuteDataTable(cmd); return tb.ToList(); } #endregion #region 删除角色 public static void Delete(int RoleId) { string tsql = @" delete from JC_RoleInfo where RoleId=@RoleId delete from JC_RoleUser where RoleID=@RoleID delete from JC_RoleMenu where RoleID=@RoleID "; Database db = DatabaseFactory.CreateDatabase(); DbCommand cmd = db.GetSqlStringCommand(tsql); db.AddInParameter(cmd, "@RoleId", DbType.Int32, RoleId); db.ExecuteNonQuery(cmd); } #endregion #region 保存角色 public static void Save(int RoleID,int CompanyID,string RoleName) { string tsql = @" if @RoleID>0 begin update JC_RoleInfo set RoleName=@RoleName where RoleID=@RoleID end else begin INSERT INTO [JC_RoleInfo](RoleName,CompanyID)values(@RoleName,@CompanyID) end "; Database db = DatabaseFactory.CreateDatabase(); DbCommand cmd = db.GetSqlStringCommand(tsql); db.AddInParameter(cmd, "@RoleID", DbType.Int32, RoleID); db.AddInParameter(cmd, "@CompanyID", DbType.Int32, CompanyID); db.AddInParameter(cmd, "@RoleName", DbType.String, RoleName); db.ExecuteNonQuery(cmd); } #endregion #region 保存角色明细 public static void SaveDetail(int CompanyID, JC_RoleInfo Model) { string tsql = @" delete from JC_RoleUser where RoleID=@RoleID delete from JC_RoleMenu where RoleID=@RoleID "; Database db = DatabaseFactory.CreateDatabase(); DbCommand cmd = db.GetSqlStringCommand(tsql); db.AddInParameter(cmd, "@RoleID", DbType.Int32, Model.RoleID); db.ExecuteNonQuery(cmd); tsql = "insert JC_RoleUser(RoleId,RoleType,UserId,UserName)values(@RoleId,1,@UserId,@UserName)"; cmd = db.GetSqlStringCommand(tsql); foreach (var item in Model.ListRoleUser) { cmd.Parameters.Clear(); db.AddInParameter(cmd, "@RoleId", DbType.Int32, Model.RoleID); db.AddInParameter(cmd, "@UserId", DbType.Int32, item.UserId); db.AddInParameter(cmd, "@UserName", DbType.String, item.UserName); db.ExecuteNonQuery(cmd); } tsql = @" declare @ParentModuleNo1 int,@ParentModuleNo2 int select @ParentModuleNo1=ParentModuleNo from JC_MenuList where ModuleNo=@ModuleNo if @ParentModuleNo1 is not null and @PopedomType>0 begin insert JC_RoleMenu(RoleID,ModuleNo,PopedomType)values(@RoleID,@ParentModuleNo1,@PopedomType) select @ParentModuleNo2=ParentModuleNo from JC_MenuList where ModuleNo=@ParentModuleNo1 if @ParentModuleNo2 is not null begin insert JC_RoleMenu(RoleID,ModuleNo,PopedomType)values(@RoleID,@ParentModuleNo2,@PopedomType) end end insert JC_RoleMenu(RoleID,ModuleNo,PopedomType)values(@RoleID,@ModuleNo,@PopedomType)"; cmd = db.GetSqlStringCommand(tsql); foreach (var item in Model.ListRoleMenu) { cmd.Parameters.Clear(); db.AddInParameter(cmd, "@RoleID", DbType.Int32, Model.RoleID); db.AddInParameter(cmd, "@ModuleNo", DbType.Int32, item.ModuleNo); db.AddInParameter(cmd, "@PopedomType", DbType.Int32, item.PopedomType); db.ExecuteNonQuery(cmd); } } #endregion #region 读取菜单父节点 public static List GetParentMenuList(int CompanyID) { string tsql = @" select * from JC_MenuList where (isnull(CompanyID,0)=0 or CompanyID=@CompanyID) and isnull(ParentModuleNo,0)<=0 and IsUse=1 order by SortNo "; Database db = DatabaseFactory.CreateDatabase(); DbCommand cmd = db.GetSqlStringCommand(tsql); db.AddInParameter(cmd, "@CompanyID", DbType.Int32, CompanyID); DataTable tb= db.ExecuteDataTable(cmd); return tb.ToList(); } #endregion #region 读取角色对象 public static JC_RoleInfo GetModel(int CompanyID,int RoleID) { List ListMenu = JC_RoleInfoService.GetParentMenuList(CompanyID); string tsql = @" select * from JC_RoleInfo where RoleID=@RoleID select a.*,UserName=b.Name from JC_RoleUser a inner join JC_UserInfo b on a.UserID=b.UserID and b.State=1 where a.RoleID=@RoleID select a.ParentModuleNo,a.ModuleNo,a.ModuleName,b.PopedomType,IsUrl=case when isnull(a.ModuleUrl,'')='' then 0 else 1 end from JC_MenuList a left join JC_RoleMenu b on a.ModuleNo=b.ModuleNo and b.RoleID=@RoleID where a.IsUse=1 order by a.SortNo "; Database db = DatabaseFactory.CreateDatabase(); DbCommand cmd = db.GetSqlStringCommand(tsql); db.AddInParameter(cmd, "@RoleID", DbType.Int32, RoleID); DataSet ds = db.ExecuteDataSet(cmd); JC_RoleInfo Model = ds.Tables[0].Rows[0].ToModel(); Model.ListRoleUser = ds.Tables[1].ToList(); Model.ListRoleMenu = new List(); List ListMenu2 = ds.Tables[2].ToList(); foreach (var item in ListMenu) { List ListMenu3 = new List(); JC_RoleInfoService.GetChildMenu(ListMenu2, item.ModuleNo.Value, ListMenu3); foreach (var item2 in ListMenu3) { JC_RoleMenu model = new JC_RoleMenu(); model.ModuleName = item2.ModuleName; model.ModuleNo = item2.ModuleNo; model.ParentModuleNo = item2.ParentModuleNo; model.PopedomType = item2.PopedomType; Model.ListRoleMenu.Add(model); } } return Model; } #endregion #region 读取子菜单 public static void GetChildMenu(List ListMenu, int ParentModuleNo, List OutListMenu) { var query = ListMenu.Where(m => m.ParentModuleNo == ParentModuleNo); foreach (var item in query) { if (item.IsUrl == 0) { GetChildMenu(ListMenu, item.ModuleNo, OutListMenu); } else { JC_RoleMenu model = new JC_RoleMenu(); model.ModuleName = item.ModuleName; model.ModuleNo = item.ModuleNo; model.ParentModuleNo = item.ParentModuleNo; model.PopedomType = item.PopedomType; OutListMenu.Add(model); } } } #endregion #region 保存角色成员 public static void SaveRoleUser(JC_RoleUser Model) { string tsql = @" select top 1 @Id=Id from JC_RoleUser where RoleId=@RoleId and RoleType=@RoleType and UserId=@UserId if @Id is null begin INSERT INTO [JC_RoleUser]([RoleId],[RoleType],[UserId],[UserName])values(@RoleId,@RoleType,@UserId,@UserName) end "; Database db = DatabaseFactory.CreateDatabase(); DbCommand cmd = db.GetSqlStringCommand(tsql); db.AddInParameter(cmd, "@Id", DbType.Int32, Model.Id); db.AddInParameter(cmd, "@RoleId", DbType.Int32, Model.RoleId); db.AddInParameter(cmd, "@RoleType", DbType.Int32, Model.RoleType); db.AddInParameter(cmd, "@UserId", DbType.Int32, Model.UserId); db.AddInParameter(cmd, "@UserName", DbType.String, Model.UserName); db.ExecuteNonQuery(cmd); } #endregion } }