using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMFrameWork.Data;
using System.Data;
using QMAPP.FJC.Entity.Basic;
using QMFrameWork.Log;
namespace QMAPP.FJC.DAL.Basic
{
///
/// 模块编号:M13-1
/// 作 用:零件基本类别
/// 作 者:王庆男
/// 编写日期:2015年06月09日
///
public class ProductBasicDAL
{
#region 获取信息
///
/// 获取信息
///
/// 条件
/// *信息
public ProductBasic Get(ProductBasic model)
{
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//获取信息
model = session.Get(model);
}
return model;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "零件类别数据层-获取信息"
});
throw;
}
}
#endregion
#region 获取列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(ProductBasic condition, DataPage page)
{
string sql = null;
List parameters = new List();
try
{
sql = this.GetQuerySql(condition, ref parameters);
//分页关键字段及排序
page.KeyName = "PID";
page.SortExpression = "UPDATEDATE DESC";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
page = session.GetDataPage(sql, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "零件类别数据层-获取列表"
});
throw;
}
}
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public List GetList(ProductBasic condition)
{
string sql = null;
List list = new List();
List parameters = new List();
try
{
sql = this.GetQuerySql(condition, ref parameters);
//分页关键字段及排序
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "操作者数据层-获取零件类别列表"
});
throw;
}
}
#endregion
#region 获取查询语句
///
/// 获取查询语句(2017.03.07 郭兆福修改)
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(ProductBasic condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
//构成查询语句
sqlBuilder.AppendLine(" SELECT P.PID ");
sqlBuilder.AppendLine(" ,P.PRODUCTTYPE ");
sqlBuilder.AppendLine(" ,P.PRODUCTTYPECODE ");
sqlBuilder.AppendLine(" ,P.PRODUCTNAME ");
sqlBuilder.AppendLine(" ,CASE P.PRODUCTUNIT WHEN '0' THEN '单体' WHEN '1' THEN '集合' ELSE '' END AS PRODUCTUNIT ");
sqlBuilder.AppendLine(" ,CASE P.PRODUCTSOURCE WHEN '0' THEN '自制' WHEN '1' THEN '外购' ELSE '' END AS PRODUCTSOURCE ");
sqlBuilder.AppendLine(" ,P.ISPARENT ");
sqlBuilder.AppendLine(" ,P.CAPATITY ");
sqlBuilder.AppendLine(" ,P.DELFLAG ");
sqlBuilder.AppendLine(" ,P.CREATEUSER ");
sqlBuilder.AppendLine(" ,P.CREATEDATE ");
sqlBuilder.AppendLine(" ,P.UPDATEUSER ");
sqlBuilder.AppendLine(" ,P.UPDATEDATE ");
sqlBuilder.AppendLine(" ,C.USERNAME AS CREATEUSERNAME ");
sqlBuilder.AppendLine(" ,U.USERNAME AS UPDATEUSERNAME ");
sqlBuilder.AppendLine(" FROM T_BD_PRODUCTBASIC P ");
sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER C ON C.USERID=P.CREATEUSER ");
sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER U ON U.USERID=P.UPDATEUSER ");
whereBuilder.Append(" AND P.DELFLAG='0'");
//查询条件
if (string.IsNullOrEmpty(condition.PRODUCTTYPECODE) == false)
{
whereBuilder.Append(" AND PRODUCTTYPECODE = @PRODUCTTYPECODE");
parameters.Add(new DataParameter { ParameterName = "PRODUCTTYPECODE", DataType = DbType.String, Value = condition.PRODUCTTYPECODE });
}
if (string.IsNullOrEmpty(condition.PRODUCTTYPE) == false)
{
whereBuilder.Append(" AND PRODUCTTYPE = @PRODUCTTYPE");
parameters.Add(new DataParameter { ParameterName = "PRODUCTTYPE", DataType = DbType.String, Value = condition.PRODUCTTYPE });
}
if (string.IsNullOrEmpty(condition.PRODUCTSOURCE) == false)
{
whereBuilder.Append(" AND PRODUCTSOURCE = @PRODUCTSOURCE");
parameters.Add(new DataParameter { ParameterName = "PRODUCTSOURCE", DataType = DbType.String, Value = condition.PRODUCTSOURCE });
}
if (string.IsNullOrEmpty(condition.PRODUCTUNIT) == false)
{
whereBuilder.Append(" AND PRODUCTUNIT = @PRODUCTUNIT");
parameters.Add(new DataParameter { ParameterName = "PRODUCTUNIT", DataType = DbType.String, Value = condition.PRODUCTUNIT });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 插入信息
///
/// 插入信息(单表)(2017.03.07 郭兆福)
///
/// 信息
/// 插入行数
public int Insert(ProductBasic model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
count = session.Insert(model);
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 信息是否重复
///
/// 判断名称是否存在
///
///
/// true:已存在;fasel:不存在。
public bool ExistsProductBasic(ProductBasic model)
{
string PID = "";
int count = 0;
StringBuilder sqlBuilder = new StringBuilder();
try
{
if (string.IsNullOrEmpty(model.PID) == false)
{
PID = model.PID;
}
sqlBuilder.AppendLine("SELECT COUNT(*) FROM T_BD_PRODUCTBASIC ");
sqlBuilder.AppendLine(" WHERE PID <> @PID AND (PRODUCTTYPECODE=@PRODUCTTYPECODE OR PRODUCTNAME=@PRODUCTNAME)");
using (IDataSession session = AppDataFactory.CreateMainSession())
{
count = Convert.ToInt32(session.ExecuteSqlScalar(sqlBuilder.ToString(), new DataParameter("PID", PID),
new DataParameter { ParameterName = "PRODUCTTYPECODE", Value = model.PRODUCTTYPECODE },
new DataParameter { ParameterName = "PRODUCTNAME", Value = model.PRODUCTNAME }));
}
if (count > 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 更新信息
///
/// 更新信息
///
///
/// 更新行数
public int Update(ProductBasic model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//更新基本信息
count = session.Update(model);
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 逻辑删除
///
/// 逻辑删除信息(2017.03.07 郭兆福)
///
///
/// 删除个数
public int Delete(ProductBasic model)
{
StringBuilder sqlBuilder = new StringBuilder();
List parameters = new List();
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//删除基本信息
sqlBuilder.Append("UPDATE T_BD_PRODUCTBASIC ");
sqlBuilder.Append("SET DELFLAG = '1'");
sqlBuilder.Append("WHERE PID = @PID");
parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = model.PID });
count = session.ExecuteSql(sqlBuilder.ToString(), parameters.ToArray());
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 删除校验
///
/// 删除校验
///
///
/// 结果
public int DelCheck(ProductBasic model)
{
int num1 = 0;
StringBuilder sqlBuilder1 = new StringBuilder();
try
{
sqlBuilder1.AppendLine(" SELECT COUNT(1) ");
sqlBuilder1.AppendLine(" FROM T_BD_PRODUCTINFO ");
sqlBuilder1.AppendLine(" WHERE ProductBasicID = @PID ");
using (IDataSession session = AppDataFactory.CreateMainSession())
{
num1 = Convert.ToInt32(session.ExecuteSqlScalar(sqlBuilder1.ToString(), new DataParameter("PID", model.PID)));
}
return num1;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取导出的数据
///
/// 获取导出的数据
///
/// 查询条件
/// 数据
public DataTable GetExportData(ProductBasic model)
{
DataTable dt = null;
string sql = null;
List parameters = new List();
try
{
//构成查询语句
sql = this.GetQuerySql(model, ref parameters);
using (IDataSession session = AppDataFactory.CreateMainSession())
{
dt = session.GetTable(sql, parameters.ToArray());
dt.TableName = "T_BD_PRODUCTBASIC";
}
return dt;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取最大零件类别值
///
/// 获取最大零件类别值
///
///
/// 结果
public int GetType(ProductBasic condition)
{
int num = 0;
string sql = null;
List parameters = new List();
try
{
sql = "SELECT ISNULL(MAX(CONVERT(INT,PRODUCTTYPE)),0) FROM T_BD_PRODUCTBASIC";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
num = Convert.ToInt32(session.ExecuteSqlScalar(sql, parameters.ToArray()));
}
return num;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取工厂列表(下拉列表数据源)
///
/// 获取工厂列表(下拉列表数据源)
///
/// 条件
/// 全部集合
public List GetProductBasicList(ProductBasic condition)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
List list = new List();
List parameters = new List();
try
{
sqlBuilder.AppendLine(" SELECT P.PID ");
sqlBuilder.AppendLine(" ,P.PRODUCTTYPE ");
sqlBuilder.AppendLine(" ,P.PRODUCTTYPECODE ");
sqlBuilder.AppendLine(" ,P.PRODUCTNAME ");
sqlBuilder.AppendLine(" ,P.PRODUCTUNIT ");
sqlBuilder.AppendLine(" ,P.PRODUCTSOURCE ");
sqlBuilder.AppendLine(" ,P.ISPARENT ");
sqlBuilder.AppendLine(" ,P.CAPATITY ");
sqlBuilder.AppendLine(" ,P.DELFLAG ");
sqlBuilder.AppendLine(" ,P.CREATEUSER ");
sqlBuilder.AppendLine(" ,P.CREATEDATE ");
sqlBuilder.AppendLine(" ,P.UPDATEUSER ");
sqlBuilder.AppendLine(" ,P.UPDATEDATE ");
sqlBuilder.AppendLine(" FROM T_BD_PRODUCTBASIC P ");
sqlBuilder.AppendLine(" WHERE P.DELFLAG = '0'");
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList(sqlBuilder.ToString(), parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "零件类别信息-获取零件类别列表"
});
throw;
}
}
#endregion
}
}