using System;
using System.Data;
using System.Data.SqlClient;
using DSWeb.Models;
using WebSqlHelper;
using System.Collections;
using System.Collections.Generic;
namespace DSWeb.EntityDA
{
public class UserActionDA
{
private const string PARM_USER_ACTION_GID = "@gid";
private const string PARM_USER_ACTION_ACTION_ID = "@action_id";
private const string PARM_USER_ACTION_USER_ID = "@user_id";
private const string PARM_USER_ACTION_CREATE_USER = "@create_user";
private const string PARM_USER_ACTION_CREATE_TIME = "@create_time";
private const string PARM_USER_ACTION_MODIFIED_USER = "@modified_user";
private const string PARM_USER_ACTION_MODIFIED_TIME = "@modified_time";
private const string PARM_ACTION_MODULE_ID = "@module_id";
private const string SQL_SELECT_USER_ACTION_BY_GID = "SELECT GID, ACTIONID, USERID, CREATEUSER, CREATETIME, MODIFIEDUSER, MODIFIEDTIME FROM user_action WHERE GID = @gid";
//private const string SQL_SELECT_USER_ACTION_BY_ACTION_ID = "SELECT GID, ACTIONID, USERID, CREATEUSER, CREATETIME, MODIFIEDUSER, MODIFIEDTIME FROM user_action WHERE ACTIONID = @action_id ";
private const string SQL_SELECT_USER_ACTION_BY_ACTION_ID = "SELECT GID, ACTIONID,USERID,CREATEUSER,CREATETIME,MODIFIEDUSER,MODIFIEDTIME FROM user_action WHERE ACTIONID = @action_id and USERID in (select USERID from user_company where COMPANYID in (SELECT COMPANYID FROM dbo.user_company WHERE USERID = @USERID))";
private const string SQL_SELECT_USER_ACTION_BY_USER_ID = "SELECT GID, ACTIONID, USERID, CREATEUSER, CREATETIME, MODIFIEDUSER, MODIFIEDTIME FROM user_action WHERE USERID = @user_id";
private const string SQL_SELECT_USER_ACTION_BY_USERID_AND_MODULEID = " SELECT COUNT(*) FROM [action] as A INNER JOIN user_action AS B ON A.GID = B.ACTIONID WHERE A.MODULEID = @module_id AND B.USERID = @user_id ";
private const string SQL_INSERT_USER_ACTION_ADD = " INSERT INTO user_action(GID,ACTIONID,USERID,CREATEUSER,CREATETIME) "
+ " SELECT newid(),GID,@user_id,@create_user,GETDATE() FROM [action] WHERE MODULEID = @module_id ";
private const string SQL_DELETE_USER_ACTION = " DELETE user_action WHERE ACTIONID IN (SELECT GID FROM [action] WHERE MODULEID = @module_id) AND USERID = @user_id ";
private const string SQL_SELECT_USER_ACTION_VALIDATE = " SELECT COUNT(*) FROM user_action WHERE ACTIONID IN (SELECT GID FROM [action] WHERE MODULEID = @module_id) ";
#region 根据Action表GID获取行为信息
///
/// 根据UserAction表GID获取行为信息
///
/// 用户行为表GID
/// 行为实体类
public UserActionEntity GetUserActionByGID(string tempUserActionGID)
{
SqlParameter parm = new SqlParameter(PARM_USER_ACTION_GID, SqlDbType.VarChar, 36);
parm.Value = tempUserActionGID;
UserActionEntity userActionEntity = null;
using (SqlDataReader sqlRead = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_USER_ACTION_BY_GID, parm))
{
try
{
//读取字段值
while (sqlRead.Read())
{
userActionEntity = new UserActionEntity();
if (!sqlRead.IsDBNull(0))
{
userActionEntity.GID = sqlRead.GetString(0);
}
if (!sqlRead.IsDBNull(1))
{
userActionEntity.ActionID = sqlRead.GetString(1);
}
if (!sqlRead.IsDBNull(2))
{
userActionEntity.UserID = sqlRead.GetString(2);
}
if (!sqlRead.IsDBNull(3))
{
userActionEntity.CreateUser = sqlRead.GetString(3);
}
if (!sqlRead.IsDBNull(4))
{
userActionEntity.CreateTime = sqlRead.GetDateTime(4);
}
if (!sqlRead.IsDBNull(5))
{
userActionEntity.ModifiedUser = sqlRead.GetString(5);
}
if (!sqlRead.IsDBNull(6))
{
userActionEntity.ModifiedTime = sqlRead.GetDateTime(6);
}
}
}
catch (Exception exceError)
{
//抛出异常
throw exceError;
}
}
return userActionEntity;
}
#endregion
#region 根据Action表GID获取行为信息
///
/// 根据UserAction表ActionID获取行为信息
///
/// 用户行为表GID
/// 行为实体类
public UserActionEntity GetUserActionByActionID(string tempActionGID)
{
SqlParameter parm = new SqlParameter(PARM_USER_ACTION_GID, SqlDbType.VarChar, 36);
parm.Value = tempActionGID;
UserActionEntity userActionEntity = null;
using (SqlDataReader sqlRead = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_USER_ACTION_BY_ACTION_ID, parm))
{
try
{
//读取字段值
while (sqlRead.Read())
{
userActionEntity = new UserActionEntity();
if (!sqlRead.IsDBNull(0))
{
userActionEntity.GID = sqlRead.GetString(0);
}
if (!sqlRead.IsDBNull(1))
{
userActionEntity.ActionID = sqlRead.GetString(1);
}
if (!sqlRead.IsDBNull(2))
{
userActionEntity.UserID = sqlRead.GetString(2);
}
if (!sqlRead.IsDBNull(3))
{
userActionEntity.CreateUser = sqlRead.GetString(3);
}
if (!sqlRead.IsDBNull(4))
{
userActionEntity.CreateTime = sqlRead.GetDateTime(4);
}
if (!sqlRead.IsDBNull(5))
{
userActionEntity.ModifiedUser = sqlRead.GetString(5);
}
if (!sqlRead.IsDBNull(6))
{
userActionEntity.ModifiedTime = sqlRead.GetDateTime(6);
}
}
}
catch (Exception exceError)
{
//抛出异常
throw exceError;
}
}
return userActionEntity;
}
#endregion
#region 根据用户GID获取行为信息
///
/// 根据用户GID获取行为信息
///
/// 用户GID
/// 行为实体类
public IList GetUserActionByActionIDALL(string tempActionGID,string strUserID)
{
SqlParameter[] parms = new SqlParameter[] {
new SqlParameter(PARM_USER_ACTION_ACTION_ID,SqlDbType.VarChar,36),
new SqlParameter("@USERID",SqlDbType.VarChar,36)
};
parms[0].Value = tempActionGID;
parms[1].Value = strUserID;
IList userActionEntities = new List();
using (SqlDataReader sqlRead = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_USER_ACTION_BY_ACTION_ID, parms))
{
try
{
//读取字段值
while (sqlRead.Read())
{
UserActionEntity userActionEntity = new UserActionEntity();
if (!sqlRead.IsDBNull(0))
{
userActionEntity.GID = sqlRead.GetString(0);
}
if (!sqlRead.IsDBNull(1))
{
userActionEntity.ActionID = sqlRead.GetString(1);
}
if (!sqlRead.IsDBNull(2))
{
userActionEntity.UserID = sqlRead.GetString(2);
}
if (!sqlRead.IsDBNull(3))
{
userActionEntity.CreateUser = sqlRead.GetString(3);
}
if (!sqlRead.IsDBNull(4))
{
userActionEntity.CreateTime = sqlRead.GetDateTime(4);
}
if (!sqlRead.IsDBNull(5))
{
userActionEntity.ModifiedUser = sqlRead.GetString(5);
}
if (!sqlRead.IsDBNull(6))
{
userActionEntity.ModifiedTime = sqlRead.GetDateTime(6);
}
userActionEntities.Add(userActionEntity);
}
}
catch (Exception exceError)
{
//抛出异常
throw exceError;
}
}
return userActionEntities;
}
#endregion
#region 根据用户GID获取行为信息
///
/// 根据用户GID获取行为信息
///
/// 用户GID
/// 行为实体类
public IList GetUserActionByUserID(string tempUserGID)
{
SqlParameter parm = new SqlParameter(PARM_USER_ACTION_USER_ID, SqlDbType.VarChar, 36);
parm.Value = tempUserGID;
IList userActionEntities = new List();
using (SqlDataReader sqlRead = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_USER_ACTION_BY_USER_ID, parm))
{
try
{
//读取字段值
while (sqlRead.Read())
{
UserActionEntity userActionEntity = new UserActionEntity();
if (!sqlRead.IsDBNull(0))
{
userActionEntity.GID = sqlRead.GetString(0);
}
if (!sqlRead.IsDBNull(1))
{
userActionEntity.ActionID = sqlRead.GetString(1);
}
if (!sqlRead.IsDBNull(2))
{
userActionEntity.UserID = sqlRead.GetString(2);
}
if (!sqlRead.IsDBNull(3))
{
userActionEntity.CreateUser = sqlRead.GetString(3);
}
if (!sqlRead.IsDBNull(4))
{
userActionEntity.CreateTime = sqlRead.GetDateTime(4);
}
if (!sqlRead.IsDBNull(5))
{
userActionEntity.ModifiedUser = sqlRead.GetString(5);
}
if (!sqlRead.IsDBNull(6))
{
userActionEntity.ModifiedTime = sqlRead.GetDateTime(6);
}
userActionEntities.Add(userActionEntity);
}
}
catch (Exception exceError)
{
//抛出异常
throw exceError;
}
}
return userActionEntities;
}
#endregion
#region 查看用户是否已经指定了模块权限
///
/// 查看用户是否已经指定了模块权限
///
/// 用户GID
/// 模块GID
/// true-表示存在 false-表示不存在
public bool IsExistUserModule(string tempUserID,string tempModuleID)
{
bool isExist = false;
SqlParameter[] parms = new SqlParameter[] {
new SqlParameter(PARM_ACTION_MODULE_ID,SqlDbType.VarChar,36),
new SqlParameter(PARM_USER_ACTION_USER_ID,SqlDbType.VarChar,36)
};
parms[0].Value = tempModuleID;
parms[1].Value = tempUserID;
using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
int iTempResult = (int)SqlHelper.ExecuteScalar(conn, CommandType.Text, SQL_SELECT_USER_ACTION_BY_USERID_AND_MODULEID, parms);
if (iTempResult > 0)
{
isExist = true;
}
}
catch (Exception error)
{
throw (error);
}
}
return isExist;
}
#endregion
#region 获取所有使用模块的用户ACTION总数
///
/// 获取所有使用模块的用户ACTION总数
///
/// 模块GID
/// 返回使用模块总数
public int GetExistUserActionModule(string tempModuleID)
{
int iResult = 0;
SqlParameter parm = new SqlParameter(PARM_ACTION_MODULE_ID, SqlDbType.VarChar, 36);
parm.Value = tempModuleID;
using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
iResult = (int)SqlHelper.ExecuteScalar(conn, CommandType.Text, SQL_SELECT_USER_ACTION_VALIDATE, parm);
}
catch (Exception error)
{
throw (error);
}
}
return iResult;
}
#endregion
#region 为用户指定模块权限
///
/// 为用户指定模块权限
///
/// 用户GID
/// 模块GID
/// 操作人GID
/// 值1表示插入成功 值不等于1表示插入失败
public int InsertUserAction(string tempUserID,string tempModuleID,string tempCreateUserID)
{
int iResult = 0;
SqlParameter[] parms = new SqlParameter[] {
new SqlParameter(PARM_USER_ACTION_USER_ID,SqlDbType.VarChar,36),
new SqlParameter(PARM_ACTION_MODULE_ID,SqlDbType.VarChar,36),
new SqlParameter(PARM_USER_ACTION_CREATE_USER,SqlDbType.VarChar,36)
};
parms[0].Value = tempUserID;
parms[1].Value = tempModuleID;
parms[2].Value = tempCreateUserID;
using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
iResult = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, SQL_INSERT_USER_ACTION_ADD, parms);
}
catch (Exception error)
{
throw (error);
}
}
return iResult;
}
#endregion
#region 删除用户模块权限
///
/// 删除用户模块权限
///
/// 用户GID
/// 模块GID
/// 值1表示删除成功 值不等于1表示删除失败
public int DeleteUserAction(string tempUserID, string tempModuleID)
{
int iResult = 0;
SqlParameter[] parms = new SqlParameter[] {
new SqlParameter(PARM_USER_ACTION_USER_ID,SqlDbType.VarChar,36),
new SqlParameter(PARM_ACTION_MODULE_ID,SqlDbType.VarChar,36)
};
parms[0].Value = tempUserID;
parms[1].Value = tempModuleID;
using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
iResult = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, SQL_DELETE_USER_ACTION, parms);
}
catch (Exception error)
{
throw (error);
}
}
return iResult;
}
#endregion
}
}