using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMFrameWork.Data;
using QMAPP.Entity.QueryTL;
namespace QMAPP.DAL.QueryTL
{
///
/// 查询模板管理
/// 创建者:李炳海
/// 创建日期:2014.8.13
///
public class QueryTemplateDAL:BaseDAL
{
#region 新增模板信息
///
/// 新增模板信息
///
/// 模板信息
public void Insert(QueryTemplate templateInfo)
{
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
session.OpenTs();
//保存基本信息
session.Insert(templateInfo);
//保存列信息
if (templateInfo.Columns!=null)
session.Insert(templateInfo.Columns);
//保存排序信息
if (templateInfo.Sorts!=null)
session.Insert(templateInfo.Sorts);
//保存查询条件
if (templateInfo.Wheres!=null)
session.Insert(templateInfo.Wheres);
session.CommitTs();
}
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 更新模板信息
///
/// 更新模板信息
///
/// 模板信息
public void Update(QueryTemplate templateInfo)
{
string sql = null;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
session.OpenTs();
//保存基本信息
session.Update(templateInfo);
//删除原列信息
sql = "DELETE FROM T_QM_TEMPLATECOLUMN WHERE TEMPLATEID = @TEMPLATEID";
sql = this.ChangeSqlByDB(sql, session);
session.ExecuteSql(sql, new DataParameter("TEMPLATEID", templateInfo.TEMPLATEID));
//保存列信息
if (templateInfo.Columns!=null)
session.Insert(templateInfo.Columns);
//删除原排序信息
sql = "DELETE FROM T_QM_TEMPLATESORT WHERE TEMPLATEID = @TEMPLATEID";
sql = this.ChangeSqlByDB(sql, session);
session.ExecuteSql(sql, new DataParameter("TEMPLATEID", templateInfo.TEMPLATEID));
//保存排序信息
if (templateInfo.Sorts!=null)
session.Insert(templateInfo.Sorts);
//删除原查询条件
sql = "DELETE FROM T_QM_TEMPLATEWHERE WHERE TEMPLATEID = @TEMPLATEID";
sql = this.ChangeSqlByDB(sql, session);
session.ExecuteSql(sql, new DataParameter("TEMPLATEID", templateInfo.TEMPLATEID));
//保存查询条件
if (templateInfo.Wheres!=null)
session.Insert(templateInfo.Wheres);
session.CommitTs();
}
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 删除模板信息
///
/// 删除模板信息
///
/// 模板信息
public void Delete(List list)
{
string sql = null;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
session.OpenTs();
foreach (string id in list)
{
//删除基本信息
session.Delete(new QueryTemplate {TEMPLATEID=id });
//删除列信息
sql = "DELETE FROM T_QM_TEMPLATECOLUMN WHERE TEMPLATEID = @TEMPLATEID";
sql = this.ChangeSqlByDB(sql, session);
session.ExecuteSql(sql, new DataParameter("TEMPLATEID", id));
//删除排序信息
sql = "DELETE FROM T_QM_TEMPLATESORT WHERE TEMPLATEID = @TEMPLATEID";
sql = this.ChangeSqlByDB(sql, session);
session.ExecuteSql(sql, new DataParameter("TEMPLATEID", id));
//删除查询条件
sql = "DELETE FROM T_QM_TEMPLATEWHERE WHERE TEMPLATEID = @TEMPLATEID";
sql = this.ChangeSqlByDB(sql, session);
session.ExecuteSql(sql, new DataParameter("TEMPLATEID", id));
}
session.CommitTs();
}
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 获取模板信息
///
/// 获取模板信息
///
/// 获取条件
/// 模板信息
public QueryTemplate Get(QueryTemplate templateInfo)
{
string sql = null;
List parameters = new List();
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sql = "SELECT T1.*,T2.USERNAME AS CREATEUSERNAME FROM T_QM_QUERYTEMPLATE T1,T_QM_USER T2 "
+ "WHERE T1.CREATEUSER = T2.USERID AND T1.TEMPLATEID = @TEMPLATEID";
//获取基本信息
sql = this.ChangeSqlByDB(sql, session);
templateInfo = session.Get(sql, new DataParameter("TEMPLATEID",templateInfo.TEMPLATEID));
if (templateInfo != null)
{
//查询条件设置
sql = "SELECT * FROM T_QM_TEMPLATEWHERE WHERE TEMPLATEID = @TEMPLATEID";
sql = this.ChangeSqlByDB(sql, session);
parameters.Add(new DataParameter("TEMPLATEID", templateInfo.TEMPLATEID));
templateInfo.Wheres = session.GetList(sql, parameters.ToArray()).ToList();
//获取列设置
sql = "SELECT * FROM T_QM_TEMPLATECOLUMN WHERE TEMPLATEID = @TEMPLATEID ORDER BY SEQ";
sql = this.ChangeSqlByDB(sql, session);
parameters.Clear();
parameters.Add(new DataParameter("TEMPLATEID", templateInfo.TEMPLATEID));
templateInfo.Columns = session.GetList(sql, parameters.ToArray()).ToList();
//获取排序设置
sql = "SELECT * FROM T_QM_TEMPLATESORT WHERE TEMPLATEID = @TEMPLATEID ORDER BY SEQ";
sql = this.ChangeSqlByDB(sql, session);
parameters.Clear();
parameters.Add(new DataParameter("TEMPLATEID", templateInfo.TEMPLATEID));
templateInfo.Sorts = session.GetList(sql, parameters.ToArray()).ToList();
}
}
return templateInfo;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 获取模板列表
///
/// 获取模板列表
///
/// 查询条件
/// 数据页
/// 数据页
public DataPage GetList(QueryTemplate condition, DataPage page)
{
string sql = null;
List parameters = new List();
string where = "";
try
{
sql = "SELECT T1.TEMPLATEID,T1.TEMPLATENAME,T1.QUERYPROGRAM,T1.ISPUBLISH, "
+ " T1.REMARK,T1.CREATEDATE,T2.UserName AS CREATEUSER,T1.UPDATEDATE,T1.UPDATEUSER "
+ " FROM T_QM_QUERYTEMPLATE T1,T_QM_USER T2 WHERE T1.CREATEUSER = T2.USERID ";
if (string.IsNullOrEmpty(condition.QUERYPROGRAM) == false)
{
where += " AND T1.QUERYPROGRAM = @QUERYPROGRAM";
parameters.Add(new DataParameter("QUERYPROGRAM",condition.QUERYPROGRAM));
}
if (string.IsNullOrEmpty(condition.TEMPLATENAME) == false)
{
where += " AND T1.TEMPLATENAME = @TEMPLATENAME";
parameters.Add(new DataParameter("TEMPLATENAME", "%"+condition.TEMPLATENAME+"%"));
}
if (string.IsNullOrEmpty(condition.CREATEUSER) == false)
{
where += " AND T1.CREATEUSER = @CREATEUSER";
parameters.Add(new DataParameter("CREATEUSER", condition.CREATEUSER));
}
else
{
where += " AND T1.ISPUBLISH = '1'";
}
sql += where;
//分页关键字段及排序
page.KeyName = "TEMPLATEID";
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;
}
}
#endregion
#region 设置查询模板默认模板
///
/// 设置查询模板默认模板
///
/// 用户主键
/// 查询模块
/// 模板主键
public void SetProgramTemplate(ProgramTemplate pt)
{
string sql = null;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
session.OpenTs();
//删除原信息
sql = @"DELETE FROM T_QM_PROGRAMTEMPLATE
WHERE QUERYPROGRAM = @QUERYPROGRAM AND CREATEUSER = @USERID";
sql = this.ChangeSqlByDB(sql, session);
session.ExecuteSql(sql, new DataParameter("USERID", pt.CREATEUSER)
, new DataParameter("QUERYPROGRAM",pt.QUERYPROGRAM));
//插入默认模板信息
session.Insert(pt);
session.CommitTs();
}
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 取消应用查询模板
///
/// 取消应用查询模板
///
/// 需要取消的模板信息
public void RemoveQueryTemplate(ProgramTemplate pt)
{
string sql = null;
try
{
sql = "DELETE FROM T_QM_PROGRAMTEMPLATE WHERE CREATEUSER = @USERID AND QUERYPROGRAM = @QUERYPROGRAM";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sql = this.ChangeSqlByDB(sql, session);
session.ExecuteSql(sql, new DataParameter("USERID", pt.CREATEUSER), new DataParameter("QUERYPROGRAM",pt.QUERYPROGRAM));
}
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 获取查询模板默认模板
///
/// 获取查询模板默认模板基本信息
///
/// 用户主键
/// 查询模板
/// 模板基本信息
public QueryTemplate GetProgramTemplateBase(string userID, string queryProgram)
{
string sql = null;
QueryTemplate template = new QueryTemplate();
try
{
sql = @"SELECT T1.TEMPLATEID,T2.TEMPLATENAME FROM T_QM_PROGRAMTEMPLATE T1,T_QM_QUERYTEMPLATE T2
WHERE T1.QUERYPROGRAM = @QUERYPROGRAM AND T1.CREATEUSER = @USERID
AND T1.TEMPLATEID = T2.TEMPLATEID";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sql = this.ChangeSqlByDB(sql, session);
template = session.Get(sql
, new DataParameter("QUERYPROGRAM", queryProgram)
, new DataParameter("USERID", userID));
}
return template;
}
catch (Exception ex)
{
throw;
}
}
///
/// 获取查询模板默认模板
///
/// 用户主键
/// 查询模块
/// 默认模板
public string GetProgramTemplate(string userID, string queryProgram)
{
string templateID = null;
object value = null;
string sql = null;
try
{
sql = @"SELECT TEMPLATEID FROM T_QM_PROGRAMTEMPLATE
WHERE QUERYPROGRAM = @QUERYPROGRAM AND CREATEUSER = @USERID";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sql = this.ChangeSqlByDB(sql, session);
value = session.ExecuteSqlScalar(sql
, new DataParameter("QUERYPROGRAM",queryProgram)
,new DataParameter("USERID",userID));
}
if (value != null)
templateID = (string)value;
return templateID;
}
catch (Exception ex)
{
throw;
}
}
#endregion
}
}