using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using QMFrameWork.Data;
using QMAPP.Entity;
using QMAPP.MD.Entity;
using QMAPP.DAL;
namespace QMAPP.MD.DAL
{
///
/// 模块名称:采集数据借口
/// 作 者:QMMES
/// 编写日期:2017年09月05日
///
public class DaiDAL:BaseDAL
{
#region 获取信息
///
/// 获取信息
///
/// 条件
/// *信息
public Dai Get(Dai info)
{
string sql = null;
List parameters = new List();
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//获取信息
info = session.Get(info);
//获取明细信息
//sql = "SELECT * FROM T_QT_DAI_DATA WHERE DA_CODE = @DA_CODE";
//parameters.Add(new DataParameter("DA_CODE", info.DA_CODE));
//info.Details = session.GetList(sql, parameters.ToArray()).ToList();
}
return info;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 根据采集点编码获得采集点相关信息
///
/// 条件
/// *信息
public Dai GetDaiByDaCode(string daCode)
{
string sql = null;
List parameters = new List();
try
{
Dai info = new Dai {DA_CODE = daCode};
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//获取信息
string daiChangeSql = ChangeSqlByDB(GetDaiInfoSql(info,ref parameters),session);
info = session.Get(daiChangeSql, parameters.ToArray());
if (null == info)
{
throw new Exception("没有找到相应的采集点!");
}
parameters.Clear();
//获取明细信息
//sql = "SELECT * FROM T_QT_DAI_DATA WHERE DA_CODE = @DA_CODE";
//parameters.Add(new DataParameter("DA_CODE", info.DA_CODE));
//string detailChangeSql = ChangeSqlByDB(sql,session);
//info.Details = session.GetList(detailChangeSql, parameters.ToArray()).ToList();
}
return info;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(Dai 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())
{
page = session.GetDataPage(sql, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(Dai condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
//构成查询语句
sqlBuilder.Append(" SELECT T.* ,F.FACTORY_NAME ");
sqlBuilder.Append(" FROM T_QT_DAI T ");
sqlBuilder.Append(" LEFT JOIN T_MD_FACTORY F ON F.FACTORY_CODE=T.FACTORY_CODE ");
//查询条件
//工厂
if (string.IsNullOrEmpty(condition.FACTORY_CODE) == false)
{
whereBuilder.Append(" AND T.FACTORY_CODE = @FACTORY_CODE");
parameters.Add(new DataParameter { ParameterName = "FACTORY_CODE", DataType = DbType.String, Value = condition.FACTORY_CODE });
}
//采集点编码
if (string.IsNullOrEmpty(condition.DA_CODE) == false)
{
whereBuilder.Append(" AND DA_CODE like @DA_CODE");
parameters.Add(new DataParameter { ParameterName = "DA_CODE", DataType = DbType.String, Value = "%"+ condition.DA_CODE+"%" });
}
if (string.IsNullOrEmpty(condition.DA_NAME) == false)
{
whereBuilder.Append(" AND DA_NAME like @DA_NAME");
parameters.Add(new DataParameter { ParameterName = "DA_NAME", DataType = DbType.String, Value = "%" + condition.DA_NAME + "%" });
}
if (string.IsNullOrEmpty(condition.WORKCELL_CODE) == false)
{
whereBuilder.Append(" AND WORKCELL_CODE = @WORKCELL_CODE");
parameters.Add(new DataParameter { ParameterName = "WORKCELL_CODE", DataType = DbType.String, Value = condition.WORKCELL_CODE });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetDaiInfoSql(Dai condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
//构成查询语句
sqlBuilder.Append("SELECT");
sqlBuilder.Append(" A.PID");
sqlBuilder.Append(" , A.WORKCELL_CODE");
sqlBuilder.Append(" , B.WORKCELL_NAME");
sqlBuilder.Append(" , A.EQUIPMENT_CODE");
sqlBuilder.Append(" , C.EQUIPMENT_NAME");
sqlBuilder.Append(" , A.DA_CODE");
sqlBuilder.Append(" , A.DA_NAME");
sqlBuilder.Append(" , A.DA_STATUS");
sqlBuilder.Append(" , A.MATERIAL_CODE");
sqlBuilder.Append(" , A.MATERIAL_TYPE");
sqlBuilder.Append(" , A.ANALYZE_CODE");
sqlBuilder.Append(" , A.DATA_TYPE");
sqlBuilder.Append(" , A.STATE_CODE");
sqlBuilder.Append(" , A.DA_SEQ");
sqlBuilder.Append(" , A.PIVOTAL");
sqlBuilder.Append(" , A.DA_MODEL");
sqlBuilder.Append(" , A.ISPROCESSCODE");
sqlBuilder.Append(" , A.REMARK");
sqlBuilder.Append(" , A.CREATEUSER");
sqlBuilder.Append(" , A.CREATEDATE");
sqlBuilder.Append(" , A.UPDATEUSER");
sqlBuilder.Append(" , A.UPDATEDATE");
sqlBuilder.Append(" , A.FLGDEL ");
sqlBuilder.Append(" FROM ");
sqlBuilder.Append(" T_QT_DAI A ");
sqlBuilder.Append(" LEFT JOIN T_MD_WORKCELL B ");
sqlBuilder.Append(" ON A.WORKCELL_CODE = B.WORKCELL_CODE ");
sqlBuilder.Append(" AND B.FLGDEL = '0' ");
sqlBuilder.Append(" LEFT JOIN T_MD_EQUIPMENT C ");
sqlBuilder.Append(" ON A.EQUIPMENT_CODE = C.EQUIPMENT_CODE ");
sqlBuilder.Append(" AND C.FLGDEL = '0'");
whereBuilder.Append(" AND A.FLGDEL = '0' ");
//查询条件
//采集点编码
if (string.IsNullOrEmpty(condition.DA_CODE) == false)
{
whereBuilder.Append(" AND A.DA_CODE = @DA_CODE");
parameters.Add(new DataParameter { ParameterName = "DA_CODE", DataType = DbType.String, Value = condition.DA_CODE });
}
//主键
if (string.IsNullOrEmpty(condition.PID) == false)
{
whereBuilder.Append(" AND A.PID = @PID");
parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = condition.PID });
}
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 Exists(Dai info)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
List parameters = new List();
int count = 0;
try
{
sqlBuilder.Append("SELECT COUNT(0) FROM T_QT_DAI");
if (info.PID==null)
{
info.PID = "";
}
whereBuilder.Append(" AND PID <> @PID ");
parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = info.PID });
//添加进行无重复字段判断代码
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
using (IDataSession session = AppDataFactory.CreateMainSession())
{
count = Convert.ToInt32(session.ExecuteSqlScalar(sqlBuilder.ToString(), parameters.ToArray()));
}
return count > 0;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 插入信息
///
/// 插入信息(单表)
///
/// 信息
/// 插入行数
public int Insert(Dai info)
{
int count=0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
session.OpenTs();
//插入基本信息
count = session.Insert(info);
//插入明细
//if (info.Details != null)
// session.Insert(info.Details);
session.CommitTs();
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 更新信息
///
/// 更新信息
///
///
/// 更新行数
public int Update(Dai info)
{
string sql = null;
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
session.OpenTs();
//更新基本信息
count = session.Update(info);
////更新明细信息
//sql = "DELETE FROM T_QT_DAI_DATA WHERE DA_CODE = @DA_CODE";
//session.ExecuteSql(sql, new DataParameter("DA_CODE", info.PID));
//if (info.Details != null)
// session.Insert(info.Details);
session.CommitTs();
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 逻辑删除
///
/// 逻辑删除信息
///
///
/// 删除个数
public int Delete(Dai info)
{
StringBuilder sqlBuilder = new StringBuilder();
List parameters = new List();
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//删除基本信息
sqlBuilder.Append("UPDATE T_QT_DAI ");
sqlBuilder.Append("SET FLGDEL = '1' ");
sqlBuilder.Append("WHERE PID = @PID ");
parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = info.PID });
count = session.ExecuteSql(sqlBuilder.ToString(), parameters.ToArray());
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
public int Del(Dai info)
{
StringBuilder sqlBuilder = new StringBuilder();
List parameters = new List();
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//删除基本信息
sqlBuilder.Append("delete from T_QT_DAI ");
sqlBuilder.Append("WHERE PID = @PID ");
parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = info.PID });
count = session.ExecuteSql(sqlBuilder.ToString(), parameters.ToArray());
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
//获取状态码的下拉框
public List GetWorkCellStateCodeList(string workCellCode)
{
List parameters = new List();
var sql = "";
try
{
if (string.IsNullOrEmpty(workCellCode))
{
sql = " SELECT STATE_CODE,STATE_NAME FROM T_MD_WORKCELL_STATE group by STATE_CODE,STATE_NAME";
}
else
{
sql = " SELECT STATE_CODE,STATE_NAME FROM T_MD_WORKCELL_STATE where WORKCELL_CODE='" + workCellCode + "' ";
}
using (IDataSession session = AppDataFactory.CreateMainSession())
{
return session.GetList(sql, parameters.ToArray()).ToList();
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}