using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMAPP.Entity.Sys;
using QMFrameWork.Data;
using System.Data;
namespace QMAPP.DAL.Sys
{
///
/// 文件管理数据层
/// 创建者:王丹丹
/// 创建时间:2015-03-17
///
public class FileInfoDAL : BaseDAL
{
#region 获取信息
///
/// 获取信息
///
/// 条件
/// *信息
public FileInfo Get(FileInfo model)
{
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//获取信息
model = session.Get(model);
}
return model;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取信息
///
/// 获取信息
///
/// 条件
/// *信息
public IList GetList(FileType model)
{
IList list = new List();
StringBuilder sqlBuilder = new StringBuilder();
List parameters = new List();
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//删除基本信息
sqlBuilder.Append(" SELECT FI.PID,FI.FILENAME,FI.FILESTORAGENAME,FI.REMARK, ");
sqlBuilder.Append(" FI.TYPENO,FI.CREATEUSER,FI.CREATEDATE FROM T_QM_FILEINFO FI ");
sqlBuilder.Append(" LEFT JOIN T_QM_FILETYPE FT ON FT.TYPENO=FI.TYPENO ");
sqlBuilder.Append(" WHERE FT.TYPENO=@TYPENO");
parameters.Add(new DataParameter { ParameterName = "TYPENO", DataType = DbType.String, Value = model.TYPENO });
string sql = this.ChangeSqlByDB(sqlBuilder.ToString(), session);
list = session.GetList(sql, parameters.ToArray());
}
return list;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(FileInfo 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 = "CREATEDATE 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 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(FileInfo condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
//构成查询语句
sqlBuilder.Append(" SELECT F.PID,F.FILENAME,F.FILESTORAGENAME,F.REMARK,F.TYPENO,F.CREATEUSER, ");
sqlBuilder.Append(" F.CREATEDATE AS CREATEDATE,C.USERNAME AS CREATEUSERNAME,FT.TYPENAME");
sqlBuilder.Append(" FROM T_QM_FILEINFO F ");
sqlBuilder.Append(" LEFT JOIN T_QM_USER C ON C.USERID=F.CREATEUSER ");
sqlBuilder.Append(" LEFT JOIN T_QM_FILETYPE FT ON FT.TYPENO=F.TYPENO ");
//查询条件
//文件名
if (string.IsNullOrEmpty(condition.FILENAMETXT) == false)
{
whereBuilder.Append(" AND F.FILENAME LIKE @FILENAMETXT");
parameters.Add(new DataParameter { ParameterName = "FILENAMETXT", DataType = DbType.String, Value = "%" + condition.FILENAMETXT + "%" });
}
//文件类型
if (string.IsNullOrEmpty(condition.TYPENO) == false)
{
whereBuilder.Append(" AND F.TYPENO =@TYPENO");
parameters.Add(new DataParameter { ParameterName = "TYPENO", DataType = DbType.String, Value = condition.TYPENO });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 信息是否重复
///
/// 判断名称是否存在
///
///
/// true:已存在;fasel:不存在。
public bool ExistsFileInfo(FileInfo model)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
List parameters = new List();
int count = 0;
try
{
sqlBuilder.Append("SELECT COUNT(PID) FROM T_QM_FILEINFO");
if (string.IsNullOrEmpty(model.PID) == false)
{
whereBuilder.Append(" AND PID =@PID ");
parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = model.PID });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
using (IDataSession session = AppDataFactory.CreateMainSession())
{
string sql = this.ChangeSqlByDB(sqlBuilder.ToString(), session);
count = Convert.ToInt32(session.ExecuteSqlScalar(sql, parameters.ToArray()));
}
if (count > 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 插入信息
///
/// 插入信息(单表)
///
/// 信息
/// 插入行数
public int Insert(FileInfo 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(FileInfo model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//更新基本信息
count = session.Update(model);
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 删除文件
///
/// 删除文件
///
///
/// 删除个数
public int Delete(FileInfo model)
{
StringBuilder sqlBuilder = new StringBuilder();
List parameters = new List();
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//删除基本信息
sqlBuilder.Append("DELETE FROM T_QM_FILEINFO ");
sqlBuilder.Append("WHERE PID = @PID ");
parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = model.PID });
string sql = this.ChangeSqlByDB(sqlBuilder.ToString(), session);
count = session.ExecuteSql(sql, parameters.ToArray());
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 删除文件类型下的文件
///
/// 删除文件类型下的文件
///
///
/// 删除个数
public int DeleteByTypeNo(FileInfo model)
{
StringBuilder sqlBuilder = new StringBuilder();
List parameters = new List();
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//删除基本信息
sqlBuilder.Append("DELETE FROM T_QM_FILEINFO ");
sqlBuilder.Append("WHERE TYPENO = @TYPENO ");
parameters.Add(new DataParameter { ParameterName = "TYPENO", DataType = DbType.String, Value = model.TYPENO });
string sql = this.ChangeSqlByDB(sqlBuilder.ToString(), session);
count = session.ExecuteSql(sql, parameters.ToArray());
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取导出文件信息
///
/// 获取导出文件信息
///
/// 查询条件
/// 数据
public DataTable GetExportData(FileInfo model)
{
DataTable dt = null;
string sql = null;
List parameters = new List();
try
{
//构成查询语句
sql = GetQuerySql(model, ref parameters);
sql += " ORDER BY CREATEDATE DESC";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sql = this.ChangeSqlByDB(sql, session);
dt = session.GetTable(sql, parameters.ToArray());
}
dt.TableName = "FileInfo";
return dt;
}
catch (Exception ex)
{
throw;
}
}
#endregion
}
}