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
{
/// <summary>
/// 模块编号:M2-2
/// 作 用:设备信息数据层
/// 作 者:王丹丹
/// 编写日期:2015年05月28日
///</summary>
public class MachineInfoDAL
{
#region 获取信息
/// <summary>
/// 获取信息
/// </summary>
/// <param name="">条件</param>
/// <returns>*信息</returns>
public MachineInfo Get ( MachineInfo model )
{
try
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
//获取信息
model = session . Get < MachineInfo > ( model ) ;
}
return model ;
}
catch ( Exception ex )
{
LogManager . LogHelper . Error ( new LogInfo ( )
{
ErrorInfo = ex ,
Tag = ex . StackTrace ,
Info = "设备信息数据层-获取信息"
} ) ;
throw ;
}
}
/// <summary>
/// 通过设备属性获取设备信息
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public MachineInfo GetMachineInfo ( MachineInfo model )
{
try
{
List < DataParameter > paraList = new List < DataParameter > ( ) ;
StringBuilder sql = new StringBuilder ( ) ;
sql . Append ( "select " + new DataQueryHelper ( ) . GetSelectColumns < MachineInfo > ( ) + " 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 < MachineInfo > ( sql . ToString ( ) , paraList . ToArray ( ) ) ;
}
return model ;
}
catch ( Exception ex )
{
LogManager . LogHelper . Error ( new LogInfo ( )
{
ErrorInfo = ex ,
Tag = ex . StackTrace ,
Info = "设备信息数据层-获取信息"
} ) ;
throw ;
}
}
#endregion
#region 获取列表
/// <summary>
/// 获取列表
/// </summary>
/// <param name="condition">条件</param>
/// <param name="page">数据页</param>
/// <returns>数据页</returns>
public DataPage GetList ( MachineInfo condition , DataPage page )
{
string sql = null ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
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 < MachineInfo > ( sql , parameters . ToArray ( ) , page ) ;
}
return page ;
}
catch ( Exception ex )
{
LogManager . LogHelper . Error ( new LogInfo ( )
{
ErrorInfo = ex ,
Tag = ex . StackTrace ,
Info = "设备信息数据层-获取列表"
} ) ;
throw ;
}
}
/// <summary>
/// 获取列表
/// </summary>
/// <param name="condition">条件</param>
/// <returns>全部集合</returns>
public List < MachineInfo > GetList ( MachineInfo condition )
{
List < MachineInfo > list = new List < MachineInfo > ( ) ;
string sql = null ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql = this . GetQuerySql ( condition , ref parameters ) ;
sql = sql + " ORDER BY UPDATEDATE DESC " ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
list = session . GetList < MachineInfo > ( sql , parameters . ToArray ( ) ) . ToList ( ) ;
}
return list ;
}
catch ( Exception ex )
{
throw ex ;
}
}
public List < MachineInfo > GetMachineInfos ( )
{
List < MachineInfo > list = new List < MachineInfo > ( ) ;
string sql = null ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql = "SELECT * FROM T_BD_MACHINEINFO" ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
list = session . GetList < MachineInfo > ( sql , parameters . ToArray ( ) ) . ToList ( ) ;
}
return list ;
}
catch ( Exception ex )
{
throw ex ;
}
}
public List < MachineInfo > GetMachineInfoMachineCheck ( )
{
List < MachineInfo > list = new List < MachineInfo > ( ) ;
string sql = null ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql = " SELECT * FROM T_BD_MACHINEINFO where machinecodde in (select MACHINECODE from T_BD_MACHINECHECKURL)" ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
list = session . GetList < MachineInfo > ( sql , parameters . ToArray ( ) ) . ToList ( ) ;
}
return list ;
}
catch ( Exception ex )
{
throw ex ;
}
}
#endregion
#region 获取查询语句
/// <summary>
/// 获取查询语句
/// </summary>
/// <param name="user">查询条件</param>
/// <param name="parameters">参数</param>
/// <returns>查询语句</returns>
private string GetQuerySql ( MachineInfo condition , ref List < DataParameter > 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 判断信息是否重复
/// <summary>
/// 判断信息是否重复
/// </summary>
/// <param name="info"></param>
/// <returns>true:已存在;fasel:不存在。</returns>
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 插入信息
/// <summary>
/// 插入信息(单表)
/// </summary>
/// <param name="">信息</param>
/// <returns>插入行数</returns>
public int Insert ( MachineInfo model )
{
int count = 0 ;
try
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
//插入基本信息
count = session . Insert < MachineInfo > ( model ) ;
}
return count ;
}
catch ( Exception ex )
{
LogManager . LogHelper . Error ( new LogInfo ( )
{
ErrorInfo = ex ,
Tag = ex . StackTrace ,
Info = "设备信息数据层-插入信息"
} ) ;
throw ;
}
}
#endregion
#region 更新信息
/// <summary>
/// 更新信息
/// </summary>
/// <param name=""></param>
/// <returns>更新行数</returns>
public int Update ( MachineInfo model )
{
int count = 0 ;
try
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
//更新基本信息
count = session . Update < MachineInfo > ( model ) ;
}
return count ;
}
catch ( Exception ex )
{
LogManager . LogHelper . Error ( new LogInfo ( )
{
ErrorInfo = ex ,
Tag = ex . StackTrace ,
Info = "设备信息数据层-更新信息"
} ) ;
throw ;
}
}
#endregion
#region 删除信息
/// <summary>
/// 删除信息
/// </summary>
/// <param name="model">物料号信息(ID)</param>
/// <returns>删除个数</returns>
public int Delete ( MachineInfo model )
{
StringBuilder sqlBuilder = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
int count = 0 ;
try
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
//删除基本信息
count = session . Delete < MachineInfo > ( model ) ;
}
return count ;
}
catch ( Exception ex )
{
LogManager . LogHelper . Error ( new LogInfo ( )
{
ErrorInfo = ex ,
Tag = ex . StackTrace ,
Info = "设备信息数据层-删除信息"
} ) ;
throw ;
}
}
#endregion
#region 订单导入获取设备信息
public List < MachineInfo > 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 < MachineInfo > list = new List < MachineInfo > ( ) ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
list = session . GetList < MachineInfo > ( sql , new List < DataParameter > ( ) . ToArray ( ) ) . ToList < MachineInfo > ( ) ;
}
return list ;
}
public List < MachineInfo > GetMachineList ( )
{
string sql = @"SELECT * FROM T_BD_MACHINEINFO" ;
List < MachineInfo > list = new List < MachineInfo > ( ) ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
list = session . GetList < MachineInfo > ( sql , new List < DataParameter > ( ) . ToArray ( ) ) . ToList < MachineInfo > ( ) ;
}
return list ;
}
#endregion
#region 设备名称下拉
/// <summary>
///
/// </summary>
/// <param name="condition"></param>
/// <returns></returns>
public List < MachineInfo > GetMachineInfoList ( MachineInfo condition )
{
List < MachineInfo > list = new List < MachineInfo > ( ) ;
string sql = null ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql = this . GetQuerySql ( condition , ref parameters ) ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
list = session . GetList < MachineInfo > ( sql , parameters . ToArray ( ) ) . ToList ( ) ;
}
return list ;
}
catch ( Exception ex )
{
LogManager . LogHelper . Error ( new LogInfo ( )
{
ErrorInfo = ex ,
Tag = ex . StackTrace ,
Info = "设备名称-列表"
} ) ;
throw ;
}
}
public List < MachineInfo > GetAllMachineInfoList ( )
{
List < MachineInfo > list = new List < MachineInfo > ( ) ;
string sql = null ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql = "SELECT * FROM T_BD_MACHINEINFO" ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
list = session . GetList < MachineInfo > ( sql , parameters . ToArray ( ) ) . ToList ( ) ;
}
return list ;
}
catch ( Exception ex )
{
LogManager . LogHelper . Error ( new LogInfo ( )
{
ErrorInfo = ex ,
Tag = ex . StackTrace ,
Info = "设备名称-列表"
} ) ;
throw ;
}
}
#endregion
}
}