You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
417 lines
14 KiB
417 lines
14 KiB
4 years ago
|
using System;
|
||
|
using System.Data;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Text;
|
||
|
using QMFrameWork.Data;
|
||
|
using QMAPP.Entity.Sys;
|
||
|
|
||
|
namespace QMAPP.DAL.Sys
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 角色管理
|
||
|
/// 创建者:郭佳伟
|
||
|
/// 创建日期:2013.1.29
|
||
|
/// </summary>
|
||
|
public class RoleManageDAL:BaseDAL
|
||
|
{
|
||
|
#region 获取角色信息
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取角色信息
|
||
|
/// </summary>
|
||
|
/// <param name="role">条件</param>
|
||
|
/// <returns>角色信息</returns>
|
||
|
public Role Get(Role role)
|
||
|
{
|
||
|
string sql = null;
|
||
|
List<DataParameter> parameters = new List<DataParameter>();
|
||
|
Role r = new Role();
|
||
|
try
|
||
|
{
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
//获取角色信息
|
||
|
sql = "SELECT ROLEID,ORGAID,(SELECT ORGADESC FROM T_QM_ORGANIZATION WHERE ORGAID = T_QM_ROLE.ORGAID) AS ORGADESC ,ROLEDESC,CREATEUSER,CREATEDATE,UPDATEUSER,UPDATEDATE FROM T_QM_ROLE WHERE ROLEID=@ROLEID";
|
||
|
sql = this.ChangeSqlByDB(sql, session);
|
||
|
parameters.Add(new DataParameter { ParameterName = "ROLEID", DataType = DbType.String, Value = role.RoleID });
|
||
|
|
||
|
r = session.Get<Role>(sql, parameters.ToArray());
|
||
|
|
||
|
//获取权限信息
|
||
|
sql.Remove(0,sql.Length);
|
||
|
parameters = new List<DataParameter>();
|
||
|
sql = "SELECT * FROM T_QM_ROLEAUTHORITY WHERE ROLEID=@ROLEID";
|
||
|
sql = this.ChangeSqlByDB(sql, session);
|
||
|
parameters.Add(new DataParameter { ParameterName = "ROLEID", DataType = DbType.String, Value = role.RoleID });
|
||
|
|
||
|
r.Powers = session.GetList<RoleAuthority>(sql, parameters.ToArray()).ToList();
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
return r;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 获取角色列表
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取角色列表
|
||
|
/// </summary>
|
||
|
/// <param name="condition">条件</param>
|
||
|
/// <param name="page">数据页</param>
|
||
|
/// <returns>数据页</returns>
|
||
|
public DataPage GetList(Role condition, DataPage page)
|
||
|
{
|
||
|
StringBuilder sqlBuilder = new StringBuilder();
|
||
|
StringBuilder whereBuilder = new StringBuilder();
|
||
|
List<DataParameter> parameters = new List<DataParameter>();
|
||
|
string sql = null;
|
||
|
try
|
||
|
{
|
||
|
|
||
|
sqlBuilder.Append("SELECT T1.ROLEID,T1.ORGAID,(SELECT ORGADESC FROM T_QM_ORGANIZATION WHERE ORGAID = T1.ORGAID) ");
|
||
|
sqlBuilder.Append(" AS ORGADESC ,T1.ROLEDESC,U1.USERNAME AS CREATEUSER,T1.CREATEDATE,U2.USERNAME AS UPDATEUSER,T1.UPDATEDATE FROM T_QM_ROLE T1 ");
|
||
|
sqlBuilder.Append(",T_QM_USER U1,T_QM_USER U2 WHERE T1.CREATEUSER = U1.USERID AND T1.UPDATEUSER = U2.USERID ");
|
||
|
//查询条件
|
||
|
if (string.IsNullOrEmpty(condition.RoleDESC) == false)
|
||
|
{
|
||
|
whereBuilder.Append(" AND T1.ROLEDESC LIKE @ROLEDESC");
|
||
|
parameters.Add(new DataParameter { ParameterName = "ROLEDESC", DataType = DbType.String, Value = "%"+condition.RoleDESC+"%" });
|
||
|
}
|
||
|
//查询条件
|
||
|
if (string.IsNullOrEmpty(condition.OrgaID) == false)
|
||
|
{
|
||
|
whereBuilder.Append(" AND T1.ORGAID = @ORGAID");
|
||
|
parameters.Add(new DataParameter { ParameterName = "ORGAID", DataType = DbType.String, Value = condition.OrgaID });
|
||
|
}
|
||
|
if (whereBuilder.Length > 0)
|
||
|
{
|
||
|
sqlBuilder.Append(whereBuilder.ToString());
|
||
|
}
|
||
|
|
||
|
//分页关键字段及排序
|
||
|
page.KeyName = "ROLEID";
|
||
|
if (string.IsNullOrEmpty(page.SortExpression))
|
||
|
{
|
||
|
page.SortExpression = "UPDATEDATE DESC";
|
||
|
}
|
||
|
|
||
|
if (page.SortExpression.IndexOf("CreateTime") > -1)
|
||
|
{
|
||
|
page.SortExpression = page.SortExpression.Replace("CreateTime", "CREATEDATE");
|
||
|
}
|
||
|
if (page.SortExpression.IndexOf("UpdateTime") > -1)
|
||
|
{
|
||
|
page.SortExpression = page.SortExpression.Replace("UpdateTime", "UPDATEDATE");
|
||
|
}
|
||
|
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
sql = sqlBuilder.ToString();
|
||
|
sql = this.ChangeSqlByDB(sql, session);
|
||
|
page = session.GetDataPage<Role>(sql, parameters.ToArray(), page);
|
||
|
}
|
||
|
|
||
|
return page;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 获取角色列表
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取全部角色信息
|
||
|
/// </summary>
|
||
|
/// <returns>数据列表</returns>
|
||
|
public Role GetAll()
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
Role role = new Role();
|
||
|
List<DataParameter> parameters = new List<DataParameter>();
|
||
|
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
|
||
|
string sql = "SELECT * FROM T_QM_ROLE ";
|
||
|
role.Roles = session.GetList<Role>(sql, parameters.ToArray()).ToList();
|
||
|
}
|
||
|
|
||
|
return role;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 根据机构获取角色
|
||
|
/// </summary>
|
||
|
/// <param name="organID">机构主键</param>
|
||
|
/// <returns>数据列表</returns>
|
||
|
public List<Role> GetRolesByOrgan(string organID)
|
||
|
{
|
||
|
List<Role> list;
|
||
|
string sql;
|
||
|
List<DataParameter> parameters = new List<DataParameter>();
|
||
|
try
|
||
|
{
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
|
||
|
sql = @"SELECT T1.ROLEID,T1.ROLEDESC FROM T_QM_ROLE T1
|
||
|
INNER JOIN T_QM_ORGANROLE T2 ON T1.ROLEID = T2.ROLEID AND T2.ORGANID = @ORGANID";
|
||
|
parameters.Add(new DataParameter("ORGANID",organID));
|
||
|
sql = this.ChangeSqlByDB(sql, session);
|
||
|
list = session.GetList<Role>(sql, parameters.ToArray()).ToList();
|
||
|
}
|
||
|
|
||
|
return list;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 角色信息是否重复
|
||
|
|
||
|
/// <summary>
|
||
|
/// 判断角色名称是否存在
|
||
|
/// </summary>
|
||
|
/// <param name="role">角色信息</param>
|
||
|
/// <returns>true:已存在;fasel:不存在。</returns>
|
||
|
public bool ExistsRole(Role role)
|
||
|
{
|
||
|
string roleID = "none";
|
||
|
int count = 0;
|
||
|
string sql = null;
|
||
|
try
|
||
|
{
|
||
|
if (string.IsNullOrEmpty(role.RoleID) == false)
|
||
|
{
|
||
|
roleID = role.RoleID;
|
||
|
}
|
||
|
|
||
|
sql = "SELECT COUNT(*) FROM T_QM_ROLE WHERE ROLEID <> @ROLEID AND ROLEDESC=@ROLEDESC";
|
||
|
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
sql = this.ChangeSqlByDB(sql, session);
|
||
|
count = int.Parse(session.ExecuteSqlScalar(sql, new DataParameter { ParameterName = "ROLEID", Value = roleID }
|
||
|
, new DataParameter { ParameterName = "ROLEDESC", Value = role.RoleDESC }).ToString());
|
||
|
}
|
||
|
|
||
|
if (count > 0)
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 插入角色
|
||
|
|
||
|
/// <summary>
|
||
|
/// 插入角色
|
||
|
/// </summary>
|
||
|
/// <param name="role">角色信息</param>
|
||
|
/// <returns>插入数</returns>
|
||
|
public int Insert(Role role)
|
||
|
{
|
||
|
int count = 0;
|
||
|
|
||
|
try
|
||
|
{
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
session.OpenTs();
|
||
|
|
||
|
//插入基本信息
|
||
|
session.Insert<Role>(role);
|
||
|
|
||
|
if (role.Powers != null)
|
||
|
{
|
||
|
foreach(RoleAuthority ra in role.Powers){
|
||
|
ra.RoleID = role.RoleID;
|
||
|
}
|
||
|
//插入权限信息
|
||
|
session.Insert<RoleAuthority>(role.Powers);
|
||
|
}
|
||
|
session.CommitTs();
|
||
|
}
|
||
|
return count;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 删除角色
|
||
|
|
||
|
/// <summary>
|
||
|
/// 删除角色信息
|
||
|
/// </summary>
|
||
|
/// <param name="role">角色信息</param>
|
||
|
/// <returns>删除个数</returns>
|
||
|
public int Delete(Role role)
|
||
|
{
|
||
|
int count = 0;
|
||
|
string sql = null;
|
||
|
try
|
||
|
{
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
session.OpenTs();
|
||
|
|
||
|
//删除权限信息,子表
|
||
|
sql = "DELETE T_QM_ROLEAUTHORITY WHERE ROLEID=@ROLEID";
|
||
|
sql = this.ChangeSqlByDB(sql, session);
|
||
|
session.ExecuteSql(sql, new DataParameter { ParameterName = "ROLEID", DataType = DbType.String, Value = role.RoleID });
|
||
|
|
||
|
//删除基本信息,主表
|
||
|
sql.Remove(0, sql.Length);
|
||
|
sql = "DELETE T_QM_ROLE WHERE ROLEID=@ROLEID";
|
||
|
sql = this.ChangeSqlByDB(sql, session);
|
||
|
session.ExecuteSql(sql, new DataParameter { ParameterName = "ROLEID", DataType = DbType.String, Value = role.RoleID });
|
||
|
|
||
|
session.CommitTs();
|
||
|
}
|
||
|
return count;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 更新角色
|
||
|
|
||
|
/// <summary>
|
||
|
/// 更新角色
|
||
|
/// </summary>
|
||
|
/// <param name="role">角色信息</param>
|
||
|
/// <returns>更新个数</returns>
|
||
|
public int Update(Role role)
|
||
|
{
|
||
|
int count = 0;
|
||
|
string sql = null;
|
||
|
List<DataParameter> parameters = new List<DataParameter>();
|
||
|
try
|
||
|
{
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
session.OpenTs();
|
||
|
|
||
|
//更新基本信息
|
||
|
session.Update<Role>(role);
|
||
|
|
||
|
//更新权限信息
|
||
|
sql = "DELETE FROM T_QM_ROLEAUTHORITY WHERE ROLEID=@ROLEID";
|
||
|
sql = this.ChangeSqlByDB(sql, session);
|
||
|
session.ExecuteSql(sql, new DataParameter { ParameterName = "ROLEID", DataType = DbType.String, Value = role.RoleID });
|
||
|
|
||
|
if (role.Powers != null)
|
||
|
session.Insert<RoleAuthority>(role.Powers);
|
||
|
|
||
|
|
||
|
session.CommitTs();
|
||
|
}
|
||
|
return count;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 获取导出的数据
|
||
|
/// <summary>
|
||
|
/// 获取导出的数据
|
||
|
/// </summary>
|
||
|
/// <param name="condition">查询条件</param>
|
||
|
/// <returns>数据</returns>
|
||
|
public DataTable GetExportData(Role condition)
|
||
|
{
|
||
|
StringBuilder sqlBuilder = new StringBuilder();
|
||
|
StringBuilder whereBuilder = new StringBuilder();
|
||
|
DataTable dt = null;
|
||
|
string sql = null;
|
||
|
List<DataParameter> parameters = new List<DataParameter>();
|
||
|
try
|
||
|
{
|
||
|
//构成查询语句
|
||
|
sqlBuilder.Append("SELECT T1.ROLEID,T1.ORGAID,(SELECT ORGADESC FROM T_QM_ORGANIZATION WHERE ORGAID = T1.ORGAID) ");
|
||
|
sqlBuilder.Append(" AS ORGADESC ,T1.ROLEDESC,U1.USERNAME AS CREATEUSER,T1.CREATEDATE,U2.USERNAME AS UPDATEUSER,T1.UPDATEDATE FROM T_QM_ROLE T1 ");
|
||
|
sqlBuilder.Append(",T_QM_USER U1,T_QM_USER U2 WHERE T1.CREATEUSER = U1.USERID AND T1.UPDATEUSER = U2.USERID ");
|
||
|
//查询条件
|
||
|
if (string.IsNullOrEmpty(condition.RoleDESC) == false)
|
||
|
{
|
||
|
whereBuilder.Append(" AND T1.ROLEDESC LIKE @ROLEDESC");
|
||
|
parameters.Add(new DataParameter { ParameterName = "ROLEDESC", DataType = DbType.String, Value = "%" + condition.RoleDESC + "%" });
|
||
|
}
|
||
|
//查询条件
|
||
|
if (string.IsNullOrEmpty(condition.OrgaID) == false)
|
||
|
{
|
||
|
whereBuilder.Append(" AND T1.ORGAID = @ORGAID");
|
||
|
parameters.Add(new DataParameter { ParameterName = "ORGAID", DataType = DbType.String, Value = condition.OrgaID });
|
||
|
}
|
||
|
|
||
|
if (whereBuilder.Length > 0)
|
||
|
{
|
||
|
sqlBuilder.Append(whereBuilder.ToString());
|
||
|
}
|
||
|
|
||
|
sqlBuilder.Append(" ORDER BY UPDATEDATE DESC ");
|
||
|
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
sql = this.ChangeSqlByDB(sqlBuilder.ToString(), session);
|
||
|
dt = session.GetTable(sql, parameters.ToArray());
|
||
|
dt.TableName = "T_QM_ROLE";
|
||
|
}
|
||
|
|
||
|
return dt;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
#endregion
|
||
|
}
|
||
|
}
|