using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMFrameWork.Data;
using System.Data;
using QMFrameWork.Log;
using QMAPP.MD.Entity;
using QMAPP.FJC.Entity.Basic;
namespace QMAPP.FJC.DAL.Basic
{
///
/// 模块编号:M2-2
/// 作 用:设备信息数据层
/// 作 者:王丹丹
/// 编写日期:2015年05月28日
///
public class MachineInfoDAL
{
#region 获取信息
///
/// 获取信息
///
/// 条件
/// *信息
public MachineInfo Get(MachineInfo 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;
}
}
///
/// 通过设备属性获取设备信息
///
///
///
public MachineInfo GetMachineInfo(MachineInfo model)
{
try
{
List paraList = new List();
StringBuilder sql = new StringBuilder();
sql.Append("select " + new DataQueryHelper().GetSelectColumns() + " from T_BD_MACHINEINFO WHERE 1=1 ");
//查询条件
if (string.IsNullOrEmpty(model.MACHINECODDE) == false)
{
sql.Append(" and MACHINECODDE=@MACHINECODDE ");
paraList.Add(new DataParameter() { ParameterName = "MACHINECODDE", Value = model.MACHINECODDE, DataType = DbType.String });
}
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//获取信息
model = session.Get(sql.ToString(), paraList.ToArray());
}
return model;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "设备信息数据层-获取信息"
});
throw;
}
}
#endregion
#region 获取列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(MachineInfo condition, DataPage page)
{
string sql = null;
List parameters = new List();
try
{
sql = this.GetQuerySql(condition, ref parameters);
#region 排序
//分页关键字段及排序
page.KeyName = "PID";
if (string.IsNullOrEmpty(page.SortExpression))
{
page.SortExpression = "UPDATEDATE DESC";
}
else
{
//按照标题排序
if (page.SortExpression.IndexOf("STATUSTXT") > -1)
{
page.SortExpression = page.SortExpression.Replace("STATUSTXT", "STATUS");
}
if (page.SortExpression.IndexOf("ISCONTROLTXT") > -1)
{
page.SortExpression = page.SortExpression.Replace("ISCONTROLTXT", "ISCONTROL");
}
}
#endregion
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(MachineInfo condition)
{
List list = new List();
string sql = null;
List parameters = new List();
try
{
sql = this.GetQuerySql(condition, ref parameters);
sql = sql + " ORDER BY UPDATEDATE DESC ";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw ex;
}
}
public List GetMachineInfos()
{
List list = new List();
string sql = null;
List parameters = new List();
try
{
sql = "SELECT * FROM T_BD_MACHINEINFO";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw ex;
}
}
public List GetMachineInfoMachineCheck()
{
List list = new List();
string sql = null;
List parameters = new List();
try
{
sql = " SELECT * FROM T_BD_MACHINEINFO where machinecodde in (select MACHINECODE from T_BD_MACHINECHECKURL)";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(MachineInfo condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
//构成查询语句
sqlBuilder.AppendLine(" SELECT M.PID ");
sqlBuilder.AppendLine(" ,MACHINENAME ");
sqlBuilder.AppendLine(" ,MACHINENAME_SHORT ");
sqlBuilder.AppendLine(" ,MACHINECODDE ");
sqlBuilder.AppendLine(" ,MACHINENUM ");
sqlBuilder.AppendLine(" ,ISCONTROL ");
sqlBuilder.AppendLine(" ,PRODUCELINE ");
sqlBuilder.AppendLine(" ,STATUS ");
sqlBuilder.AppendLine(" ,IPADDRESS ");
sqlBuilder.AppendLine(" ,TERMINALPORT ");
sqlBuilder.AppendLine(" ,OPERATETYPE ");
sqlBuilder.AppendLine(" ,MEMO ");
sqlBuilder.AppendLine(" ,ISSTATION ");
sqlBuilder.AppendLine(" ,MODELSTATIONS ");
sqlBuilder.AppendLine(" ,CONNECTIONSTRING ");
sqlBuilder.AppendLine(" ,OPCGROUPNAME ");
sqlBuilder.AppendLine(" ,M.CREATEUSER ");
sqlBuilder.AppendLine(" ,M.CREATEDATE ");
sqlBuilder.AppendLine(" ,M.UPDATEUSER ");
sqlBuilder.AppendLine(" ,M.UPDATEDATE ");
sqlBuilder.AppendLine(" ,M.EQUIPMENT_DIRECTORY ");
sqlBuilder.AppendLine(" ,M.EQUIPMENT_TYPE_CODE ");
sqlBuilder.AppendLine(" ,M.MATERAILPORT ");
sqlBuilder.AppendLine(" ,M.MATERAILINDEX ");
sqlBuilder.AppendLine(" ,M.WORKLOC_CODE ");
sqlBuilder.AppendLine(" ,W.WORKCELL_CODE ");
sqlBuilder.AppendLine(" ,W.WORKLOC_NAME ");
sqlBuilder.AppendLine(" ,M.MACHINECHECK_URL ");
sqlBuilder.AppendLine(" ,WC.WORKCENTER_CODE ");
sqlBuilder.AppendLine(" ,WC.WORKCELL_NAME ");
sqlBuilder.AppendLine(" ,C.USERNAME AS CREATEUSERNAME ");
sqlBuilder.AppendLine(" ,U.USERNAME AS UPDATEUSERNAME ");
sqlBuilder.AppendLine(" ,M.FACTORY_CODE ");
sqlBuilder.AppendLine(" ,F.FACTORY_NAME ");
sqlBuilder.AppendLine(" ,M.USERNAME ");
sqlBuilder.AppendLine(" ,M.PASSWORD ");
sqlBuilder.AppendLine(" FROM T_BD_MACHINEINFO M ");
sqlBuilder.AppendLine(" LEFT JOIN T_MD_WORKLOC W ON W.WORKLOC_CODE=M.WORKLOC_CODE ");
sqlBuilder.AppendLine(" LEFT JOIN T_MD_FACTORY F ON F.FACTORY_CODE=M.FACTORY_CODE ");
sqlBuilder.AppendLine(" LEFT JOIN T_MD_WORKCELL WC ON WC.WORKCELL_CODE=W.WORKCELL_CODE ");
sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER C ON C.USERID=M.CREATEUSER ");
sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER U ON U.USERID=M.UPDATEUSER ");
//查询条件
//主键
if (string.IsNullOrEmpty(condition.PID) == false)
{
whereBuilder.Append(" AND M.PID = @PID ");
parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = condition.PID });
}
//主键
if (string.IsNullOrEmpty(condition.PIDS) == false)
{
whereBuilder.Append(" AND M.PID IN " + condition.PIDS + "");
}
//工序类型
if (string.IsNullOrEmpty(condition.PROCESSTYPES) == false)
{
whereBuilder.Append(" AND M.PROCESSTYPE IN (" + condition.PROCESSTYPES + ")");
}
if (string.IsNullOrEmpty(condition.FACTORY_CODE) == false)
{
whereBuilder.Append(" AND M.FACTORY_CODE = @FACTORY_CODE");
parameters.Add(new DataParameter { ParameterName = "FACTORY_CODE", DataType = DbType.String, Value = condition.FACTORY_CODE });
}
//设备编号
if (string.IsNullOrEmpty(condition.MACHINECODDE) == false)
{
//whereBuilder.Append(" AND MACHINECODDE LIKE '%'+" + "@MACHINECODDE" + "+'%'");
whereBuilder.Append(" AND MACHINECODDE = @MACHINECODDE");
parameters.Add(new DataParameter { ParameterName = "MACHINECODDE", DataType = DbType.String, Value = condition.MACHINECODDE });
}
if (string.IsNullOrEmpty(condition.Machineids) == false)
{
whereBuilder.Append(" AND M.PID NOT in " + condition.Machineids + "");
}
//设备名称
if (string.IsNullOrEmpty(condition.MACHINENAME) == false)
{
whereBuilder.Append(" AND MACHINENAME LIKE '%'+" + "@MACHINENAME" + "+'%'");
parameters.Add(new DataParameter { ParameterName = "MACHINENAME", DataType = DbType.String, Value = condition.MACHINENAME });
}
//设备状态
if (string.IsNullOrEmpty(condition.STATUS) == false)
{
whereBuilder.Append(" AND STATUS = @STATUS ");
parameters.Add(new DataParameter { ParameterName = "STATUS", DataType = DbType.String, Value = condition.STATUS });
}
//接收控制指令
if (string.IsNullOrEmpty(condition.ISCONTROL) == false)
{
whereBuilder.Append(" AND ISCONTROL = @ISCONTROL ");
parameters.Add(new DataParameter { ParameterName = "ISCONTROL", DataType = DbType.String, Value = condition.ISCONTROL });
}
//IP地址
if (string.IsNullOrEmpty(condition.IPADDRESS) == false)
{
whereBuilder.Append(" AND IPADDRESS = @IPADDRESS ");
parameters.Add(new DataParameter { ParameterName = "IPADDRESS", DataType = DbType.String, Value = condition.IPADDRESS });
}
if (string.IsNullOrEmpty(condition.EQUIPMENT_DIRECTORY) == false)
{
whereBuilder.Append(" AND EQUIPMENT_DIRECTORY = @EQUIPMENT_DIRECTORY ");
parameters.Add(new DataParameter { ParameterName = "EQUIPMENT_DIRECTORY", DataType = DbType.String, Value = condition.EQUIPMENT_DIRECTORY });
}
if (string.IsNullOrEmpty(condition.EQUIPMENT_TYPE_CODE) == false)
{
whereBuilder.Append(" AND EQUIPMENT_TYPE_CODE = @EQUIPMENT_TYPE_CODE ");
parameters.Add(new DataParameter { ParameterName = "EQUIPMENT_TYPE_CODE", DataType = DbType.String, Value = condition.EQUIPMENT_TYPE_CODE });
}
if (!string.IsNullOrEmpty(condition.WORKCENTER_CODE))
{
whereBuilder.Append(" AND W.WORKCENTER_CODE = @WORKCENTER_CODE ");
parameters.Add(new DataParameter { ParameterName = "WORKCENTER_CODE", DataType = DbType.String, Value = condition.WORKCENTER_CODE });
}
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 ExistsMachineInfo(MachineInfo model)
{
string PID = "";
int count = 0;
string sql = null;
try
{
if (string.IsNullOrEmpty(model.PID) == false)
{
PID = model.PID;
}
sql = "SELECT COUNT(*) FROM T_BD_MACHINEINFO WHERE PID <> @PID AND FACTORY_CODE=@FACTORY_CODE AND MACHINECODDE=@MACHINECODDE";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
count = Convert.ToInt32(session.ExecuteSqlScalar(sql, new DataParameter("PID", PID),
new DataParameter("FACTORY_CODE", model.FACTORY_CODE),
new DataParameter { ParameterName = "MACHINECODDE", Value = model.MACHINECODDE }));
}
if (count > 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "设备信息数据层-判断信息是否重复"
});
throw;
}
}
#endregion
#region 插入信息
///
/// 插入信息(单表)
///
/// 信息
/// 插入行数
public int Insert(MachineInfo model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//插入基本信息
count = session.Insert(model);
}
return count;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "设备信息数据层-插入信息"
});
throw;
}
}
#endregion
#region 更新信息
///
/// 更新信息
///
///
/// 更新行数
public int Update(MachineInfo model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//更新基本信息
count = session.Update(model);
}
return count;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "设备信息数据层-更新信息"
});
throw;
}
}
#endregion
#region 删除信息
///
/// 删除信息
///
/// 物料号信息(ID)
/// 删除个数
public int Delete(MachineInfo model)
{
StringBuilder sqlBuilder = new StringBuilder();
List parameters = new List();
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//删除基本信息
count=session.Delete(model);
}
return count;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "设备信息数据层-删除信息"
});
throw;
}
}
#endregion
#region 订单导入获取设备信息
public List GetMachineForOrder(MachineInfo condition, WorkCell workcellmodel)
{
// string sql = string.Format(@"SELECT M.*
//FROM T_BD_MACHINEINFO M,T_MD_WORKLOC L,T_MD_WORKCELL C
//WHERE M.WORKLOC_CODE=L.WORKLOC_CODE AND L.WORKCELL_CODE=C.WORKCELL_CODE
//AND C.WORKCENTER_CODE IN (SELECT DISTINCT WORKCENTER FROM T_PP_ORDERINDENTITY WHERE ORDER_TYPE='{0}')", workcellmodel.WORKCELL_CODE);
string sql =
"SELECT M.* FROM T_BD_MACHINEINFO M,T_MD_WORKLOC L,T_MD_WORKCELL C WHERE M.WORKLOC_CODE=L.WORKLOC_CODE AND L.WORKCELL_CODE=C.WORKCELL_CODE AND C.WORKCENTER_CODE IN (SELECT DISTINCT WORKCENTER FROM T_PP_ORDERINDENTITY ";
if (string.IsNullOrEmpty(workcellmodel.WORKCELL_CODE))
{
sql += ")";
}
else
{
sql += "WHERE ORDER_TYPE='"+workcellmodel.WORKCELL_CODE+"')";
}
List list = new List();
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList(sql, new List().ToArray()).ToList();
}
return list;
}
public List GetMachineList()
{
string sql = @"SELECT * FROM T_BD_MACHINEINFO";
List list = new List();
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList(sql, new List().ToArray()).ToList();
}
return list;
}
#endregion
#region 设备名称下拉
///
///
///
///
///
public List GetMachineInfoList(MachineInfo 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;
}
}
public List GetAllMachineInfoList()
{
List list = new List();
string sql = null;
List parameters = new List();
try
{
sql = "SELECT * FROM T_BD_MACHINEINFO";
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
}
}