using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMFrameWork.Data;
using QMAPP.Entity.Sys;
using System.Data;
namespace QMAPP.DAL.Sys
{
///
/// 支持交互问题类型数据层对象
/// 创建者:wangyf
/// 创建日期:2014.03.30
///
public class SIProblemTypeDAL : BaseDAL
{
#region 获取信息
///
/// 获取信息
///
/// 条件
/// *信息
public SIProblemType Get(SIProblemType model)
{
string sql = null;
StringBuilder sb = new StringBuilder();
List parameters = new List();
SIProblemType r = new SIProblemType();
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//获取角色信息
sb.Append(" SELECT PID,PTYPENAME,SDEPT,SUSER,REMARK,ZPTYPE.CREATEDATE,ZPTYPE.UPDATEDATE,QMUSER.USERNAME AS SUserName, ");
sb.Append(" ZPTYPE.CREATEUSER,ZPTYPE.UPDATEUSER,ORGA.ORGADESC AS SOrgaDesc");
sb.Append(" FROM T_QM_ZPTYPE ZPTYPE LEFT JOIN T_QM_USER QMUSER ON ZPTYPE.SUSER = QMUSER.USERID ");
sb.Append(" LEFT JOIN T_QM_ORGANIZATION ORGA ON ZPTYPE.SDEPT = ORGA.ORGAID WHERE ZPTYPE.PID=@PIDT ");
parameters.Add(new DataParameter { ParameterName = "PIDT", DataType = DbType.String, Value = model.PID });
sql = this.ChangeSqlByDB(sb.ToString(), session);
r = session.Get(sql, parameters.ToArray());
}
return r;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(SIProblemType condition, DataPage page)
{
string sql = null;
List parameters = new List();
try
{
sql = this.GetQuerySql(condition, ref parameters);
//分页关键字段及排序
page.KeyName = "PID";
if (string.IsNullOrEmpty(page.SortExpression))
{
page.SortExpression = "UPDATEDATE DESC";
}
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sql = this.ChangeSqlByDB(sql, session);
page = session.GetDataPage(sql, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 判断类型名称是否已经存在
///
/// 判断类型名称是否已经存在
///
/// 验证结果
public bool CheckProblemTypeNameExist(SIProblemType condition)
{
string sql = null;
StringBuilder sb = new StringBuilder();
List parameters = new List();
try
{
sb.Append(" SELECT COUNT(*) FROM T_QM_ZPTYPE WHERE FLGDEL = 0 AND PTYPENAME = @PTYPENAME ");
parameters.Add(new DataParameter { ParameterName = "PTYPENAME", DataType = DbType.String, Value = condition.PTypeName });
if (!string.IsNullOrEmpty(condition.PID))
{
sb.Append(" AND PID <> @PID ");
parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = condition.PID });
}
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sql = this.ChangeSqlByDB(sb.ToString(), session);
return Convert.ToInt32(session.ExecuteSqlScalar(sql, parameters.ToArray())) > 0;
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(SIProblemType condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
try
{
//构成查询语句
sqlBuilder.Append(" SELECT PID,PTYPENAME,SDEPT,SUSER,REMARK,ZPTYPE.CREATEDATE,ZPTYPE.UPDATEDATE,QMUSER.USERNAME AS SUserName, ");
sqlBuilder.Append(" ORGA.ORGADESC AS SOrgaDesc, ");
sqlBuilder.Append(" (SELECT USERNAME FROM T_QM_USER CUSER WHERE CUSER.USERID = ZPTYPE.CREATEUSER) AS CREATEUSER, ");
sqlBuilder.Append(" (SELECT USERNAME FROM T_QM_USER CUSER WHERE CUSER.USERID = ZPTYPE.UPDATEUSER) AS UPDATEUSER ");
sqlBuilder.Append(" FROM T_QM_ZPTYPE ZPTYPE LEFT OUTER JOIN T_QM_USER QMUSER ON ZPTYPE.SUSER = QMUSER.USERID ");
sqlBuilder.Append(" LEFT JOIN T_QM_ORGANIZATION ORGA ON ZPTYPE.SDEPT = ORGA.ORGAID ");
sqlBuilder.Append(" WHERE ZPTYPE.FLGDEL = '0' ");
if (!string.IsNullOrEmpty(condition.PTypeName))
{
sqlBuilder.Append(" AND PTYPENAME LIKE '%" + condition.PTypeName + "%' ");
}
if (!string.IsNullOrEmpty(condition.SDept))
{
sqlBuilder.Append(" AND SDEPT = @SDEPT ");
parameters.Add(new DataParameter { ParameterName = "SDEPT", DataType = DbType.String, Value = condition.SDept });
}
if (!string.IsNullOrEmpty(condition.SUser))
{
sqlBuilder.Append(" AND QMUSER.USERID = @SUSER ");
parameters.Add(new DataParameter { ParameterName = "SUSER", DataType = DbType.String, Value = condition.SUser });
}
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 插入信息
///
/// 插入信息(单表)
///
/// 信息
/// 插入行数
public int Insert(SIProblemType model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//插入基本信息
count = session.Insert(model);
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 更新信息
///
/// 更新信息
///
///
/// 更新行数
public int Update(SIProblemType model)
{
StringBuilder sqlBuilder = new StringBuilder();
List parameters = new List();
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
string sql = " UPDATE T_QM_ZPTYPE SET PTYPENAME=@PTYPENAME,SUSER=@SUSER,REMARK=@REMARK,SDEPT=@SDEPT,UPDATEUSER=@UPDATEUSER,UPDATEDATE=@UPDATEDATE WHERE PID=@PID ";
string updateUser = "";
string updateRemark = "";
//处理人不为空时
if (!string.IsNullOrEmpty(model.SUser))
{
updateUser = model.SUser;
}
//备注不为空时
if (!string.IsNullOrEmpty(model.Remark))
{
updateRemark = model.Remark;
}
parameters.Add(new DataParameter { ParameterName = "SUSER", DataType = DbType.String, Value = updateUser });
parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = model.PID });
parameters.Add(new DataParameter { ParameterName = "PTYPENAME", DataType = DbType.String, Value = model.PTypeName });
parameters.Add(new DataParameter { ParameterName = "SDEPT", DataType = DbType.String, Value = model.SDept });
parameters.Add(new DataParameter { ParameterName = "REMARK", DataType = DbType.String, Value = updateRemark });
parameters.Add(new DataParameter { ParameterName = "UPDATEUSER", DataType = DbType.String, Value = model.UpdateUser});
parameters.Add(new DataParameter { ParameterName = "UPDATEDATE", DataType = DbType.DateTime, Value = model.UpdateDate });
//更新基本信息
sql = this.ChangeSqlByDB(sql, session);
count = session.ExecuteSql(sql, parameters.ToArray());
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 逻辑删除
///
/// 逻辑删除信息
///
///
/// 删除个数
public int Delete(SIProblemType model)
{
StringBuilder sqlBuilder = new StringBuilder();
List parameters = new List();
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//删除基本信息
string sql = " UPDATE T_QM_ZPTYPE SET FLGDEL=@FLGDEL WHERE PID=@PID ";
parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = model.PID });
parameters.Add(new DataParameter { ParameterName = "FLGDEL", DataType = DbType.String, Value = model.FlgDel });
//更新基本信息
sql = this.ChangeSqlByDB(sql, session);
count = session.ExecuteSql(sql, parameters.ToArray());
}
return count;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 获取导出的数据
///
/// 获取导出的数据
///
/// 查询条件
/// 数据
public DataTable GetExportData(SIProblemType entity)
{
DataTable dt = null;
string sql = null;
List parameters = new List();
try
{
dt = new DataTable();
sql = this.GetExportSql(entity, ref parameters);
sql += " ORDER BY UPDATEDATE DESC";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
dt = session.GetTable(sql, parameters.ToArray());
}
return dt;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取导出查询语句
///
/// 获取导出查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetExportSql(SIProblemType condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
sqlBuilder.Append(" SELECT PID,PTYPENAME,REMARK,ZPTYPE.CREATEDATE,ZPTYPE.UPDATEDATE,QMUSER.USERNAME AS SUserName,ORGA.ORGADESC AS SOrgaDesc, ");
sqlBuilder.Append(" (SELECT USERNAME FROM T_QM_USER CUSER WHERE CUSER.USERID = ZPTYPE.CREATEUSER) AS CREATEUSER, ");
sqlBuilder.Append(" (SELECT USERNAME FROM T_QM_USER CUSER WHERE CUSER.USERID = ZPTYPE.UPDATEUSER) AS UPDATEUSER ");
sqlBuilder.Append(" FROM T_QM_ZPTYPE ZPTYPE LEFT OUTER JOIN T_QM_USER QMUSER ON ZPTYPE.SUSER = QMUSER.USERID ");
sqlBuilder.Append(" LEFT JOIN T_QM_ORGANIZATION ORGA ON ZPTYPE.SDEPT = ORGA.ORGAID ");
sqlBuilder.Append(" WHERE ZPTYPE.FLGDEL = '0' ");
if (!string.IsNullOrEmpty(condition.PTypeName))
{
sqlBuilder.Append(" AND PTYPENAME LIKE '%" + condition.PTypeName + "%' ");
}
if (!string.IsNullOrEmpty(condition.SDept))
{
sqlBuilder.Append(" AND SDEPT = @SDEPT ");
parameters.Add(new DataParameter { ParameterName = "SDEPT", DataType = DbType.String, Value = condition.SDept });
}
if (!string.IsNullOrEmpty(condition.SUser))
{
sqlBuilder.Append(" AND SUSER = @SUSER ");
parameters.Add(new DataParameter { ParameterName = "SUSER", DataType = DbType.String, Value = condition.SUser });
}
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}