using System;
using System.Data;
using System.Data.SqlClient;
using DSWeb.Models;
using WebSqlHelper;
namespace DSWeb.EntityDA
{
public class WorkFlowDA
{
private const string PARM_WORKFLOW_GID = "@gid";
private const string PARM_WORKFLOW_NAME = "@name";
private const string PARM_WORKFLOW_DESCRIPTION = "@description";
private const string PARM_WORKFLOW_MODULE_ID = "@module_id";
private const string PARM_WORKFLOW_TYPE = "@type";
private const string PARM_WORKFLOW_CREATE_USER = "@create_user";
private const string PARM_WORKFLOW_CREATE_TIME = "@create_time";
private const string PARM_WORKFLOW_MODIFIED_USER = "@modified_user";
private const string PARM_WORKFLOW_MODIFIED_TIME = "@modified_time";
private const string PARM_WORKFLOW_STATE = "@state";
private const string PARM_WORKFLOW_SORT = "@sort";
private const string PARM_WORKFLOW_COMPANY_ID = "@company_id";
//private const string PARM_WORKFLOW_REMARK = "@remark";
private const string SQL_SELECT_WORKFLOW_BY_GID = "SELECT GID, NAME, DESCRIPTION, MODULEID, TYPE, CREATEUSER, CREATETIME, MODIFIEDUSER, MODIFIEDTIME, STATE, SORT, ISDELETE,COMPANYID FROM workflow WHERE GID = @gid";
private const string SQL_SELECT_WORKFLOW_BY_MODULEID = "SELECT GID, NAME, DESCRIPTION, MODULEID, TYPE, CREATEUSER, CREATETIME, MODIFIEDUSER, MODIFIEDTIME, STATE, SORT, ISDELETE FROM workflow WHERE MODULEID = @module_id";
private const string SQL_SELECT_WORKFLOW_BY_TYPE_AND_COMPANYID = "SELECT GID, NAME, DESCRIPTION, MODULEID, TYPE, CREATEUSER, CREATETIME, MODIFIEDUSER, MODIFIEDTIME, STATE, SORT, ISDELETE,COMPANYID FROM workflow WHERE TYPE = @type AND ISNULL(COMPANYID,'') = @company_id ";
private const string SQL_INSERT_WORKFLOW = " INSERT workflow(GID, NAME, DESCRIPTION, MODULEID, TYPE, CREATEUSER, CREATETIME,STATE, SORT, ISDELETE,COMPANYID) "
+ " VALUES(@gid,@name,@description,@module_id,@type,@create_user,GETDATE(),@state,@sort,0,@company_id) ";
private const string SQL_UPDATE_WORKFLOW = " UPDATE workflow SET NAME = @name,DESCRIPTION = @description,MODULEID = @module_id,TYPE = @type,MODIFIEDUSER = @modified_user,"
+ " MODIFIEDTIME = GETDATE(),STATE = @state,SORT = @sort,COMPANYID = @company_id WHERE GID = @gid ";
private const string SQL_SELECT_WORKFLOW_COMPANYID_AND_TYPE = " SELECT COUNT(*) FROM workflow WHERE TYPE = @type AND COMPANYID = @company_id AND ISNULL(ISDELETE,0) <> 1 ";
private const string SQL_SELECT_WORKFLOW_COMPANYID_AND_TYPE_GID = " SELECT COUNT(*) FROM workflow WHERE TYPE = @type AND COMPANYID = @company_id AND GID <> @gid AND ISNULL(ISDELETE,0) <> 1 ";
private const string SQL_Delete_WORKFLOW_By_GID = "delete from workflow where gid=@gid";
private const string SQL_Update_WORKFLOW_SetDelete_By_GID = "update workflow set isdelete=1 where gid=@gid";
#region 更新工作流信息
///
/// 更新工作流信息
///
/// 工作流实体类
/// 值1表示更新成功 值不等于1表示更新失败
public int DeleteWorkFlowByID(string tempWorkFlowID,int iType )
{
int iResult = 0;
using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
{
SqlParameter[] updateParms = new SqlParameter[] {
new SqlParameter(PARM_WORKFLOW_GID,SqlDbType.VarChar,36)
};
updateParms[0].Value = tempWorkFlowID;
try
{
if (iType == 0)
{ //工作流已经生效,可以设置isdelete=1
iResult = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, SQL_Update_WORKFLOW_SetDelete_By_GID, updateParms);
}
else if (iType == 1)
{ //工作流没有生效,可直接删除
iResult = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, SQL_Delete_WORKFLOW_By_GID, updateParms);
}
}
catch (Exception error)
{
iResult = -1;//插入操作异常
throw error;
}
}
return iResult;
}
#endregion
#region 更新工作流信息
///
/// 更新工作流信息
///
/// 工作流实体类
/// 值1表示更新成功 值不等于1表示更新失败
public int UpdateWorkFlow(WorkFlowEntity tempWorkFlowEntity)
{
int iResult = 0;
using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
{
SqlParameter[] updateParms = new SqlParameter[] {
new SqlParameter(PARM_WORKFLOW_GID,SqlDbType.VarChar,36),
new SqlParameter(PARM_WORKFLOW_NAME,SqlDbType.VarChar,50),
new SqlParameter(PARM_WORKFLOW_DESCRIPTION,SqlDbType.VarChar,50),
new SqlParameter(PARM_WORKFLOW_MODULE_ID,SqlDbType.VarChar,36),
new SqlParameter(PARM_WORKFLOW_TYPE,SqlDbType.Int),
new SqlParameter(PARM_WORKFLOW_MODIFIED_USER,SqlDbType.VarChar,36),
new SqlParameter(PARM_WORKFLOW_STATE,SqlDbType.Int),
new SqlParameter(PARM_WORKFLOW_SORT,SqlDbType.Int),
new SqlParameter(PARM_WORKFLOW_COMPANY_ID,SqlDbType.VarChar,36)
};
updateParms[0].Value = tempWorkFlowEntity.GID;
updateParms[1].Value = tempWorkFlowEntity.Name;
updateParms[2].Value = tempWorkFlowEntity.Description;
updateParms[3].Value = tempWorkFlowEntity.ModuleID;
updateParms[4].Value = tempWorkFlowEntity.Type;
updateParms[5].Value = tempWorkFlowEntity.ModifiedUser;
updateParms[6].Value = tempWorkFlowEntity.State;
updateParms[7].Value = tempWorkFlowEntity.Sort;
updateParms[8].Value = tempWorkFlowEntity.CompanyID;
try
{
iResult = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, SQL_UPDATE_WORKFLOW, updateParms);
}
catch (Exception error)
{
iResult = -1;//插入操作异常
throw error;
}
}
return iResult;
}
#endregion
#region 插入新的工作流信息
///
/// 插入新的工作流信息
///
/// 工作流实体类
/// 值1表示插入成功 值不等于1表示插入异常
public int InsertWorkFlow(WorkFlowEntity tempWorkFlowEntity)
{
int iResult = 0;
using(SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
{
SqlParameter[] insertParms = new SqlParameter[]{
new SqlParameter(PARM_WORKFLOW_GID,SqlDbType.VarChar,36),
new SqlParameter(PARM_WORKFLOW_NAME,SqlDbType.VarChar,50),
new SqlParameter(PARM_WORKFLOW_DESCRIPTION,SqlDbType.VarChar,50),
new SqlParameter(PARM_WORKFLOW_MODULE_ID,SqlDbType.VarChar,36),
new SqlParameter(PARM_WORKFLOW_TYPE,SqlDbType.Int),
new SqlParameter(PARM_WORKFLOW_CREATE_USER,SqlDbType.VarChar,36),
new SqlParameter(PARM_WORKFLOW_STATE,SqlDbType.Int),
new SqlParameter(PARM_WORKFLOW_SORT,SqlDbType.Int),
new SqlParameter(PARM_WORKFLOW_COMPANY_ID,SqlDbType.VarChar,36)
};
insertParms[0].Value = tempWorkFlowEntity.GID;
insertParms[1].Value = tempWorkFlowEntity.Name;
insertParms[2].Value = tempWorkFlowEntity.Description;
insertParms[3].Value = tempWorkFlowEntity.ModuleID;
insertParms[4].Value = tempWorkFlowEntity.Type;
insertParms[5].Value = tempWorkFlowEntity.CreateUser;
insertParms[6].Value = tempWorkFlowEntity.State;
insertParms[7].Value = tempWorkFlowEntity.Sort;
insertParms[8].Value = tempWorkFlowEntity.CompanyID;
try
{
iResult = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, SQL_INSERT_WORKFLOW, insertParms);
}
catch (Exception error)
{
iResult = -1;//插入操作异常
throw error;
}
}
return iResult;
}
#endregion
#region 验证同一分公司下是否存在相同类型的工作流
///
/// 验证同一分公司下是否存在相同类型的工作流
///
/// 分公司GID
/// 工作流类型
/// 验证操作类型 值1表示插入验证 值2表示更新验证
/// 工作流GID
/// 值1表示同一分公司下已经指定了此类型的工作流 值等于0表示同一分公司下不存在相同的工作流
public int ExistSameWorkFlowInCompanyAndType(string tempCompanyID,int tempType,int tempOperateType,string tempWorkFlowID)
{
int iResult = 0;
using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
//值1表示做插入前验证,同一分公司下只能存在一种类型的工作流,不可重复创建
if (tempOperateType == 1)
{
SqlParameter[] insertParms = new SqlParameter[] {
new SqlParameter(PARM_WORKFLOW_COMPANY_ID,SqlDbType.VarChar,36),
new SqlParameter(PARM_WORKFLOW_TYPE,SqlDbType.Int)
};
insertParms[0].Value = tempCompanyID;
insertParms[1].Value = tempType;
iResult = (int)SqlHelper.ExecuteScalar(conn, CommandType.Text, SQL_SELECT_WORKFLOW_COMPANYID_AND_TYPE, insertParms);
}
else
{
SqlParameter[] updateParms = new SqlParameter[] {
new SqlParameter(PARM_WORKFLOW_GID,SqlDbType.VarChar,36),
new SqlParameter(PARM_WORKFLOW_COMPANY_ID,SqlDbType.VarChar,36),
new SqlParameter(PARM_WORKFLOW_TYPE,SqlDbType.Int)
};
updateParms[0].Value = tempWorkFlowID;
updateParms[1].Value = tempCompanyID;
updateParms[2].Value = tempType;
iResult = (int)SqlHelper.ExecuteScalar(conn, CommandType.Text, SQL_SELECT_WORKFLOW_COMPANYID_AND_TYPE_GID, updateParms);
}
}
catch (Exception error)
{
throw error;
}
}
return iResult;
}
#endregion
#region 根据工作流类型获取工作流信息
///
/// 根据工作流类型获取工作流信息
///
/// 工作流类型
/// 分公司GID
/// 返回工作流实体类
public WorkFlowEntity GetSingleWorkFlowByTypeNo(int tempTypeNo,string tempCompanyID)
{
//初始化返回变量
WorkFlowEntity workFlowEntity = null;
//初始化参数并赋值
SqlParameter[] parms = new SqlParameter[] {
new SqlParameter(PARM_WORKFLOW_TYPE, SqlDbType.Int),
new SqlParameter(PARM_WORKFLOW_COMPANY_ID,SqlDbType.VarChar,36)
};
parms[0].Value = tempTypeNo;
parms[1].Value = tempCompanyID;
using (SqlDataReader sqlRead = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_WORKFLOW_BY_TYPE_AND_COMPANYID, parms))
{
try
{
workFlowEntity = new WorkFlowEntity();
//读取字段值
while (sqlRead.Read())
{
if (!sqlRead.IsDBNull(0))
{
workFlowEntity.GID = sqlRead.GetString(0);
}
if (!sqlRead.IsDBNull(1))
{
workFlowEntity.Name = sqlRead.GetString(1);
}
if (!sqlRead.IsDBNull(2))
{
workFlowEntity.Description = sqlRead.GetString(2);
}
if (!sqlRead.IsDBNull(3))
{
workFlowEntity.ModuleID = sqlRead.GetString(3);
}
if (!sqlRead.IsDBNull(4))
{
workFlowEntity.Type = sqlRead.GetInt32(4);
}
if (!sqlRead.IsDBNull(5))
{
workFlowEntity.CreateUser = sqlRead.GetString(5);
}
if (!sqlRead.IsDBNull(6))
{
workFlowEntity.CreateTime = sqlRead.GetDateTime(6);
}
if (!sqlRead.IsDBNull(7))
{
workFlowEntity.ModifiedUser = sqlRead.GetString(7);
}
if (!sqlRead.IsDBNull(8))
{
workFlowEntity.ModifiedTime = sqlRead.GetDateTime(8);
}
if (!sqlRead.IsDBNull(9))
{
workFlowEntity.State = sqlRead.GetInt32(9);
}
if (!sqlRead.IsDBNull(10))
{
workFlowEntity.Sort = sqlRead.GetInt32(10);
}
if (!sqlRead.IsDBNull(11))
{
workFlowEntity.IsDelete = sqlRead.GetBoolean(11);
}
if (!sqlRead.IsDBNull(12))
{
workFlowEntity.CompanyID = sqlRead.GetString(12);
}
}
}
catch (Exception exceError)
{
//抛出异常
throw exceError;
}
}
return workFlowEntity;
}
#endregion
public WorkFlowEntity GetSingleWorkFlow(string tempWorkFlowGID)
{
//初始化返回变量
WorkFlowEntity workFlowEntity = null;
//初始化参数并赋值
SqlParameter parm = new SqlParameter(PARM_WORKFLOW_GID, SqlDbType.VarChar, 36);
parm.Value = tempWorkFlowGID;
using (SqlDataReader sqlRead = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_WORKFLOW_BY_GID, parm))
{
try
{
workFlowEntity = new WorkFlowEntity();
//读取字段值
while (sqlRead.Read())
{
if (!sqlRead.IsDBNull(0))
{
workFlowEntity.GID = sqlRead.GetString(0);
}
if (!sqlRead.IsDBNull(1))
{
workFlowEntity.Name = sqlRead.GetString(1);
}
if (!sqlRead.IsDBNull(2))
{
workFlowEntity.Description = sqlRead.GetString(2);
}
if (!sqlRead.IsDBNull(3))
{
workFlowEntity.ModuleID = sqlRead.GetString(3);
}
if (!sqlRead.IsDBNull(4))
{
workFlowEntity.Type = sqlRead.GetInt32(4);
}
if (!sqlRead.IsDBNull(5))
{
workFlowEntity.CreateUser = sqlRead.GetString(5);
}
if (!sqlRead.IsDBNull(6))
{
workFlowEntity.CreateTime = sqlRead.GetDateTime(6);
}
if (!sqlRead.IsDBNull(7))
{
workFlowEntity.ModifiedUser = sqlRead.GetString(7);
}
if (!sqlRead.IsDBNull(8))
{
workFlowEntity.ModifiedTime = sqlRead.GetDateTime(8);
}
if (!sqlRead.IsDBNull(9))
{
workFlowEntity.State = sqlRead.GetInt32(9);
}
if (!sqlRead.IsDBNull(10))
{
workFlowEntity.Sort = sqlRead.GetInt32(10);
}
if (!sqlRead.IsDBNull(11))
{
workFlowEntity.IsDelete = sqlRead.GetBoolean(11);
}
if (!sqlRead.IsDBNull(12))
{
workFlowEntity.CompanyID = sqlRead.GetString(12);
}
}
}
catch (Exception exceError)
{
//抛出异常
throw exceError;
}
}
return workFlowEntity;
}
#region 通过SQL语句获取数据
///
/// 通过SQL语句获取数据
///
/// 要执行查询的SQL语句
/// 返回DataSet数据
public DataSet GetDataSetBySql(string strSql)
{
DataSet dataSet = new DataSet();
dataSet = SqlHelper.ExecuteDataset(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSql);
return dataSet;
}
#endregion
#region 根据MODULEID获取工作流信息
///
/// 根据MODULEID获取工作流信息
///
///
///
public WorkFlowEntity GetSingleWorkFlowByModuleID(string tempModuleID)
{
//初始化返回变量
WorkFlowEntity workFlowEntity = null;
//初始化参数并赋值
SqlParameter parm = new SqlParameter(PARM_WORKFLOW_MODULE_ID, SqlDbType.VarChar, 36);
parm.Value = tempModuleID;
using (SqlDataReader sqlRead = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_WORKFLOW_BY_MODULEID, parm))
{
try
{
workFlowEntity = new WorkFlowEntity();
//读取字段值
while (sqlRead.Read())
{
if (!sqlRead.IsDBNull(0))
{
workFlowEntity.GID = sqlRead.GetString(0);
}
if (!sqlRead.IsDBNull(1))
{
workFlowEntity.Name = sqlRead.GetString(1);
}
if (!sqlRead.IsDBNull(2))
{
workFlowEntity.Description = sqlRead.GetString(2);
}
if (!sqlRead.IsDBNull(3))
{
workFlowEntity.ModuleID = sqlRead.GetString(3);
}
if (!sqlRead.IsDBNull(4))
{
workFlowEntity.Type = sqlRead.GetInt32(4);
}
if (!sqlRead.IsDBNull(5))
{
workFlowEntity.CreateUser = sqlRead.GetString(5);
}
if (!sqlRead.IsDBNull(6))
{
workFlowEntity.CreateTime = sqlRead.GetDateTime(6);
}
if (!sqlRead.IsDBNull(7))
{
workFlowEntity.ModifiedUser = sqlRead.GetString(7);
}
if (!sqlRead.IsDBNull(8))
{
workFlowEntity.ModifiedTime = sqlRead.GetDateTime(8);
}
if (!sqlRead.IsDBNull(9))
{
workFlowEntity.State = sqlRead.GetInt32(9);
}
if (!sqlRead.IsDBNull(10))
{
workFlowEntity.Sort = sqlRead.GetInt32(10);
}
if (!sqlRead.IsDBNull(11))
{
workFlowEntity.IsDelete = sqlRead.GetBoolean(11);
}
}
}
catch (Exception exceError)
{
//抛出异常
throw exceError;
}
}
return workFlowEntity;
}
#endregion
#region 查看是否有相同的工作流信息
///
/// 查看是否有相同的工作流信息
///
/// WorkFlowCheckValueType 类型 可以检索NAME和DESCRIPTION
/// 检索值
/// 工作流GID
/// True表示有相同值 False表示没有相同值
public bool IsExistSameValueWorkFlow(WorkFlowCheckValueType tempType,string tempCheckValue,string tempWorkFlowID)
{
bool IsExist = false;
string strCheckName = "";
switch (tempType)
{
case WorkFlowCheckValueType.NAME:
strCheckName = "NAME";
break;
case WorkFlowCheckValueType.DESCRIPTION:
strCheckName = "DESCRIPTION";
break;
}
string strSql = String.Format("SELECT COUNT(*) FROM workflow WHERE {0} = '{1}' {2}", strCheckName, tempCheckValue, (tempWorkFlowID.Trim().Equals("") ? "" : " AND GID <> '" + tempWorkFlowID + "' "));
using (SqlDataReader sqlRead = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSql,null))
{
try
{
if (sqlRead.Read())
{
if (!sqlRead.IsDBNull(0))
{
if (sqlRead.GetInt32(0) > 0)
{
IsExist = true;//如果存在相同的值则返回true表示工作流表中已经有了相关信息
}
}
}
}
catch (Exception exceError)
{
//抛出异常
throw exceError;
}
}
return IsExist;
}
#endregion
}
public enum WorkFlowCheckValueType
{
NAME = 0,
DESCRIPTION = 1
}
}