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
{
///
/// 角色管理
/// 创建者:郭佳伟
/// 创建日期:2013.1.29
///
public class RoleManageDAL:BaseDAL
{
#region 获取角色信息
///
/// 获取角色信息
///
/// 条件
/// 角色信息
public Role Get(Role role)
{
string sql = null;
List parameters = new List();
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(sql, parameters.ToArray());
//获取权限信息
sql.Remove(0,sql.Length);
parameters = new List();
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(sql, parameters.ToArray()).ToList();
}
return r;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 获取角色列表
///
/// 获取角色列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(Role condition, DataPage page)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
List parameters = new List();
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(sql, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 获取角色列表
///
/// 获取全部角色信息
///
/// 数据列表
public Role GetAll()
{
try
{
Role role = new Role();
List parameters = new List();
using (IDataSession session = AppDataFactory.CreateMainSession())
{
string sql = "SELECT * FROM T_QM_ROLE ";
role.Roles = session.GetList(sql, parameters.ToArray()).ToList();
}
return role;
}
catch (Exception ex)
{
throw;
}
}
///
/// 根据机构获取角色
///
/// 机构主键
/// 数据列表
public List GetRolesByOrgan(string organID)
{
List list;
string sql;
List parameters = new List();
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(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 角色信息是否重复
///
/// 判断角色名称是否存在
///
/// 角色信息
/// true:已存在;fasel:不存在。
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 插入角色
///
/// 插入角色
///
/// 角色信息
/// 插入数
public int Insert(Role role)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
session.OpenTs();
//插入基本信息
session.Insert(role);
if (role.Powers != null)
{
foreach(RoleAuthority ra in role.Powers){
ra.RoleID = role.RoleID;
}
//插入权限信息
session.Insert(role.Powers);
}
session.CommitTs();
}
return count;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 删除角色
///
/// 删除角色信息
///
/// 角色信息
/// 删除个数
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 更新角色
///
/// 更新角色
///
/// 角色信息
/// 更新个数
public int Update(Role role)
{
int count = 0;
string sql = null;
List parameters = new List();
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
session.OpenTs();
//更新基本信息
session.Update(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(role.Powers);
session.CommitTs();
}
return count;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 获取导出的数据
///
/// 获取导出的数据
///
/// 查询条件
/// 数据
public DataTable GetExportData(Role condition)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
DataTable dt = null;
string sql = null;
List parameters = new List();
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
}
}