You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
11 KiB
C#

12 months ago
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using DSWeb.DataAccess;
using DSWeb.Models;
namespace DSWeb.EntityDA
{
public class RoleDA
{
private const string PARM_MODULE_GID = "@gid";
private const string PARM_USER_GID = "@userid";
private const string PARM_ROLE_NAME = "@name";
private const string PARM_ROLE_DESCRIPTION = "@description";
private const string PARM_ROLE_CREATEUSER = "@createuser";
private const string PARM_ROLE_STATE = "@state";
private const string PARM_ROLE_SORT = "@sort";
private const string PARM_ROLE_MODIFIEDUSER = "@modifieduser";
private const string SQL_SELECT_ROLE = " SELECT GID,NAME,DESCRIPTION,CREATEUSER,CREATETIME,MODIFIEDUSER,MODIFIEDTIME,STATE,SORT FROM role WHERE GID = @gid ";
private const string SQL_SELECT_ROLE_ALL = " SELECT GID,NAME,DESCRIPTION,CREATEUSER,CREATETIME,MODIFIEDUSER,MODIFIEDTIME,STATE,SORT FROM role ";
private const string SQL_INSERT_ROLE = " INSERT INTO role (GID,NAME,DESCRIPTION,CREATEUSER,STATE,SORT) VALUES(@gid,@name,@description,@createuser,@state,@sort) ";
private const string SQL_UPDATE_ROLE = " UPDATE role SET NAME = @name,DESCRIPTION = @description,MODIFIEDUSER = @modifieduser, "//MODIFIEDTIME = (GETDATE()),
+ " STATE = @state,SORT = @sort WHERE GID = @gid ";
private const string SQL_SELECT_USER_ROLE = " SELECT A.GID,A.NAME,A.DESCRIPTION,A.CREATEUSER,A.CREATETIME,A.MODIFIEDUSER,A.MODIFIEDTIME,A.STATE,A.SORT,C.USERNAME FROM role as A "
+ " INNER JOIN user_role as B ON A.GID = B.ROLEID INNER JOIN [user] as C ON B.USERID = C.GID AND C.GID = @userid ";
#region 根据角色GID获取角色信息
/// <summary>
/// 根据角色GID获取角色信息
/// </summary>
/// <param name="strRoleGID"></param>
/// <returns></returns>
public RoleEntity GetRoleByID(string strRoleGID)
{
//初始化返回变量
RoleEntity roleEntity = null;
//初始化参数并赋值
SqlParameter parm = new SqlParameter(PARM_MODULE_GID, SqlDbType.VarChar, 36);
parm.Value = strRoleGID;
using (SqlDataReader sqlRead = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_ROLE, parm))
{
try
{
roleEntity = new RoleEntity();
//读取字段值
while (sqlRead.Read())
{
if (!sqlRead.IsDBNull(0))
{
roleEntity.GID = sqlRead.GetString(0);
}
if (!sqlRead.IsDBNull(1))
{
roleEntity.RoleName = sqlRead.GetString(1);
}
if (!sqlRead.IsDBNull(2))
{
roleEntity.RoleDescription = sqlRead.GetString(2);
}
if (!sqlRead.IsDBNull(3))
{
roleEntity.CreateUserID = sqlRead.GetString(3);
}
if (!sqlRead.IsDBNull(4))
{
roleEntity.CreateTime = sqlRead.GetDateTime(4);
}
if (!sqlRead.IsDBNull(5))
{
roleEntity.ModifiedUserID = sqlRead.GetString(5);
}
if (!sqlRead.IsDBNull(6))
{
roleEntity.ModifiedTime = sqlRead.GetDateTime(6);
}
if (!sqlRead.IsDBNull(7))
{
roleEntity.State = sqlRead.GetInt32(7);
}
if (!sqlRead.IsDBNull(8))
{
roleEntity.Sort = sqlRead.GetInt32(8);
}
}
}
catch (Exception exceError)
{
//抛出异常
throw exceError;
}
}
return roleEntity;
}
#endregion
#region 获取所有角色信息
/// <summary>
/// 获取所有角色信息
/// </summary>
/// <returns></returns>
public IList<RoleEntity> GetAll()
{
IList<RoleEntity> roleEntites = new List<RoleEntity>();
using (SqlDataReader sqlRead = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_ROLE_ALL, null))
{
try
{
//读取字段值
while (sqlRead.Read())
{
RoleEntity roleEntity = new RoleEntity();
if (!sqlRead.IsDBNull(0))
{
roleEntity.GID = sqlRead.GetString(0);
}
if (!sqlRead.IsDBNull(1))
{
roleEntity.RoleName = sqlRead.GetString(1);
}
if (!sqlRead.IsDBNull(2))
{
roleEntity.RoleDescription = sqlRead.GetString(2);
}
if (!sqlRead.IsDBNull(3))
{
roleEntity.CreateUserID = sqlRead.GetString(3);
}
if (!sqlRead.IsDBNull(4))
{
roleEntity.CreateTime = sqlRead.GetDateTime(4);
}
if (!sqlRead.IsDBNull(5))
{
roleEntity.ModifiedUserID = sqlRead.GetString(5);
}
if (!sqlRead.IsDBNull(6))
{
roleEntity.ModifiedTime = sqlRead.GetDateTime(6);
}
if (!sqlRead.IsDBNull(7))
{
roleEntity.State = sqlRead.GetInt32(7);
}
if (!sqlRead.IsDBNull(8))
{
roleEntity.Sort = sqlRead.GetInt32(8);
}
roleEntites.Add(roleEntity);
}
}
catch (Exception exceError)
{
//抛出异常
throw exceError;
}
}
return roleEntites;
}
#endregion
/// <summary>
/// 插入角色信息
/// </summary>
/// <param name="roleEntity">角色实体类</param>
/// <returns>值为1插入数据正常,-1操作异常</returns>
public int InserRole(RoleEntity roleEntity)
{
int iResult = 0;
//获取参数
SqlParameter[] parms = GetInsertParms();
parms[0].Value = roleEntity.GID;
parms[1].Value = roleEntity.RoleName;
parms[2].Value = roleEntity.RoleDescription;
parms[3].Value = roleEntity.State;
parms[4].Value = roleEntity.Sort;
parms[5].Value = roleEntity.CreateUserID;
using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
{
int existVal = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, SQL_INSERT_ROLE, parms);
if (existVal > 0)
{
iResult = 1;
}
else
{
iResult = -1;//执行异常
}
}
return iResult;
}
/// <summary>
/// 更新角色信息
/// </summary>
/// <param name="roleEntity"></param>
/// <returns></returns>
public int UpdateRole(RoleEntity roleEntity)
{
int iResult = 0;
SqlParameter[] parms = GetUpdateParms();
parms[0].Value = roleEntity.GID;
parms[1].Value = roleEntity.RoleName;
parms[2].Value = roleEntity.RoleDescription;
parms[3].Value = roleEntity.State;
parms[4].Value = roleEntity.Sort;
parms[5].Value = roleEntity.ModifiedUserID;
using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
{
int existVal = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, SQL_UPDATE_ROLE, parms);
if (existVal > 0)
{
iResult = 1;
}
else
{
iResult = -1;//执行异常
}
}
return iResult;
}
#region 生成插入语句参数
/// <summary>
/// 生成插入语句参数
/// </summary>
/// <returns>返回SqlParameter数组</returns>
private SqlParameter[] GetInsertParms()
{
SqlParameter[] parms = new SqlParameter[]{
new SqlParameter("@gid",SqlDbType.VarChar,36),
new SqlParameter("@name",SqlDbType.VarChar,150),
new SqlParameter("@description",SqlDbType.VarChar,300),
new SqlParameter("@state",SqlDbType.Int),
new SqlParameter("@sort",SqlDbType.Int),
new SqlParameter("@modifieduser",SqlDbType.VarChar,36)
};
return parms;
}
#endregion
#region 生成插入语句参数
/// <summary>
/// 生成插入语句参数
/// </summary>
/// <returns>返回SqlParameter数组</returns>
private SqlParameter[] GetUpdateParms()
{
SqlParameter[] parms = new SqlParameter[]{
new SqlParameter("@gid",SqlDbType.VarChar,36),
new SqlParameter("@name",SqlDbType.VarChar,150),
new SqlParameter("@description",SqlDbType.VarChar,300),
new SqlParameter("@state",SqlDbType.Int),
new SqlParameter("@sort",SqlDbType.Int),
new SqlParameter("@modifieduser",SqlDbType.VarChar,36)
};
return parms;
}
#endregion
#region 返回角色数据集
/// <summary>
/// 返回角色数据集
/// </summary>
/// <param name="strSql">查询SQL语句</param>
/// <returns>返回角色数据集</returns>
public DataSet GetRoleList(string strSql)
{
DataSet roleSet = new DataSet();
roleSet = SqlHelper.ExecuteDataset(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSql);
return roleSet;
}
#endregion
}
}