using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMAPP.FJC.Entity.Basic;
using QMFrameWork.Data;
using QMFrameWork.Log;
using QMAPP.Entity;
using System.Data;
namespace QMAPP.FJC.DAL.Basic
{
public class ProductInfoDAL
{
#region 获取信息
///
/// 获取信息
///
/// 条件
/// 信息
public ProductInfo Get(ProductInfo model)
{
string sql = null;
List parameters = new List();
try
{
sql = "SELECT * FROM T_BD_PRODUCTINFO WHERE '1'='1'";
if (string.IsNullOrEmpty(model.PID) == false)
{
sql += " AND PID = @PID";
parameters.Add(new DataParameter("PID", model.PID));
}
if (string.IsNullOrEmpty(model.PRODUCTMARK) == false)
{
sql += " AND PRODUCTMARK = @PRODUCTMARK";
parameters.Add(new DataParameter("PRODUCTMARK", model.PRODUCTMARK));
}
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//获取信息
model = session.Get(sql, parameters.ToArray());
}
return model;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "零件号数据层-获取信息"
});
throw;
}
}
#endregion
#region 获取列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(ProductInfo condition, DataPage page)
{
string sql = null;
List parameters = new List();
try
{
sql = this.GetQuerySql(condition, ref parameters);
//分页关键字段及排序
page.KeyName = "PID";
page.SortExpression = "PRODUCTMARK ASC";
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(ProductInfo condition)
{
List list = new List();
string sql = null;
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 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(ProductInfo condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
//构成查询语句
sqlBuilder.AppendLine(" SELECT P.PID ");
sqlBuilder.AppendLine(" ,P.PRODUCTMARK ");
sqlBuilder.AppendLine(" ,P.PRODUCTDESCRIPTION ");
//sqlBuilder.AppendLine(" ,P.AREAID ");
//sqlBuilder.AppendLine(" ,A.AREACODE ");
//sqlBuilder.AppendLine(" ,A.AREANAME ");
sqlBuilder.AppendLine(" ,P.FACTORYID ");
sqlBuilder.AppendLine(" ,PL.FACTORY_CODE ");
sqlBuilder.AppendLine(" ,PL.FACTORY_NAME ");
sqlBuilder.AppendLine(" ,P.PRODUCTBASICID ");
sqlBuilder.AppendLine(" ,PC.PRODUCTTYPECODE ");
sqlBuilder.AppendLine(" ,PC.PRODUCTNAME ");
sqlBuilder.AppendLine(" ,P.MEMO ");
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_PRODUCTINFO P ");
sqlBuilder.AppendLine(" LEFT JOIN T_BD_PRODUCTBASIC PC ON PC.PID=P.PRODUCTBASICID ");
//sqlBuilder.AppendLine(" LEFT JOIN T_BD_AREA A ON A.PID=P.AREAID ");
sqlBuilder.AppendLine(" LEFT JOIN T_BD_FACTORY PL ON PL.PID=P.FACTORYID ");
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 ");
//查询条件
if (string.IsNullOrEmpty(condition.PRODUCTMARK) == false)
{
whereBuilder.Append(" AND PRODUCTMARK = @PRODUCTMARK ");
parameters.Add(new DataParameter { ParameterName = "PRODUCTMARK", DataType = DbType.String, Value = condition.PRODUCTMARK });
}
//if (string.IsNullOrEmpty(condition.AREAID) == false)
//{
// whereBuilder.Append(" AND P.AREAID = @AREAID ");
// parameters.Add(new DataParameter { ParameterName = "AREAID", DataType = DbType.String, Value = condition.AREAID });
//}
if (string.IsNullOrEmpty(condition.FACTORYID) == false)
{
whereBuilder.Append(" AND P.FACTORYID = @FACTORYID ");
parameters.Add(new DataParameter { ParameterName = "FACTORYID", DataType = DbType.String, Value = condition.FACTORYID });
}
if (string.IsNullOrEmpty(condition.PRODUCTBASICID) == false)
{
whereBuilder.Append(" AND P.PRODUCTBASICID = @PRODUCTBASICID ");
parameters.Add(new DataParameter { ParameterName = "PRODUCTBASICID", DataType = DbType.String, Value = condition.PRODUCTBASICID });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 插入信息
///
/// 插入信息(单表)
///
/// 信息
/// 插入行数
public int Insert(ProductInfo 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 ExistsProductInfo(ProductInfo 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_PRODUCTINFO ");
sqlBuilder.AppendLine(" WHERE PID <> @PID AND PRODUCTMARK=@PRODUCTMARK");
using (IDataSession session = AppDataFactory.CreateMainSession())
{
count = Convert.ToInt32(session.ExecuteSqlScalar(sqlBuilder.ToString(),
new DataParameter("PID", PID),
new DataParameter { ParameterName = "PRODUCTMARK", Value = model.PRODUCTMARK }));
}
if (count > 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 更新信息
///
/// 更新信息
///
///
/// 更新行数
public int Update(ProductInfo 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(ProductInfo model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//删除基本信息
count = session.Delete(model);
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取导出的数据
///
/// 获取导出的数据
///
/// 查询条件
/// 数据
public DataTable GetExportData(ProductInfo 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 = "ProductInfoExp";
dt.TableName = "T_BD_PRODUCTINFO";
}
return dt;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 导入
public ImportMessage GetImportData(List list)
{
ImportMessage em = new ImportMessage();
List parameters = new List();
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//设置祖先对象数据会话
session.OpenTs();
foreach (ProductInfo info in list)
{
if (info.IsNewInfo == true)
{
//插入信息
int count = session.Insert(info);
em.insertNum++;
}
else
{
//更新信息
int count = session.Update(info);
em.updateNum++;
}
}
session.CommitTs();
}
}
catch (Exception ex)
{
throw ex;
}
return em;
}
#endregion
#region 导入校验工厂区域信息是否匹配
/////
///// 导入校验工厂区域信息是否匹配
/////
///// 工厂
///// 区域
///// 结果
//public int ImportCheck(Plant plantmodel, Area areamodel)
//{
// int num = 0;
// StringBuilder sqlBuilder = new StringBuilder();
// try
// {
// sqlBuilder.AppendLine("SELECT COUNT(1) FROM T_BD_FACTORY ");
// sqlBuilder.AppendLine(" WHERE PID=@PID AND AREAID=@AREAID");
// using (IDataSession session = AppDataFactory.CreateMainSession())
// {
// num = Convert.ToInt32(session.ExecuteSqlScalar(sqlBuilder.ToString(), new DataParameter("PID", plantmodel.PID),
// new DataParameter { ParameterName = "AREAID", Value = areamodel.PID }));
// }
// return num;
// }
// catch (Exception ex)
// {
// throw ex;
// }
//}
#endregion
#region 获取零件列表(下拉列表数据源)王丹丹 2017.03.13
///
/// 获取零件列表(下拉列表数据源)
///
/// 条件
/// 全部集合
public List GetProductInfoList(ProductInfo condition)
{
List list = new List();
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
List parameters = new List();
try
{
#region 构成查询语句
//构成查询语句
sqlBuilder.AppendLine(" SELECT P.PID ");
sqlBuilder.AppendLine(" ,P.PRODUCTMARK ");
sqlBuilder.AppendLine(" ,P.PRODUCTDESCRIPTION ");
//sqlBuilder.AppendLine(" ,P.AREAID ");
sqlBuilder.AppendLine(" ,P.FACTORYID ");
sqlBuilder.AppendLine(" ,P.MEMO ");
sqlBuilder.AppendLine(" ,P.CREATEUSER ");
sqlBuilder.AppendLine(" ,P.CREATEDATE ");
sqlBuilder.AppendLine(" ,P.UPDATEUSER ");
sqlBuilder.AppendLine(" ,P.UPDATEDATE ");
sqlBuilder.AppendLine(" FROM T_BD_PRODUCTINFO P ");
////区域
//if (!string.IsNullOrEmpty(condition.AREAID))
//{
// whereBuilder.AppendLine(" AND P.AREAID=@AREAID");
// parameters.Add(new DataParameter { ParameterName = "AREAID", DataType = DbType.String, Value = condition.AREAID });
//}
//工厂
if (!string.IsNullOrEmpty(condition.FACTORYID))
{
whereBuilder.AppendLine(" AND P.FACTORYID=@FACTORYID");
parameters.Add(new DataParameter { ParameterName = "FACTORYID", DataType = DbType.String, Value = condition.FACTORYID });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
#endregion
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
}
}