using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using QMAPP.DAL ;
using QMAPP.MD.Entity ;
using QMFrameWork.Data ;
using QMFrameWork.Log ;
using System.Data ;
using System.Net.Sockets ;
using QMAPP.MD.Entity.View ;
namespace QMAPP.MD.DAL
{
/// <summary>
/// 模块名称:Bom信息
/// 作 者:郭兆福
/// 编写日期:2017年05月10日
/// </summary>
public class PbomDAL : BaseDAL
{
#region 获取信息
/// <summary>
/// 获取信息
/// </summary>
/// <param name="">条件</param>
/// <returns>信息</returns>
public Pbom Get ( Pbom model )
{
StringBuilder sqlBuilder = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
sqlBuilder . Append ( " SELECT * " ) ;
sqlBuilder . Append ( " FROM T_MD_PBOM " ) ;
sqlBuilder . Append ( " WHERE FLGDEL = '0'" ) ;
if ( string . IsNullOrEmpty ( model . PBOM_CODE ) = = false )
{
sqlBuilder . Append ( " AND PBOM_CODE= @PBOM_CODE " ) ;
parameters . Add ( new DataParameter { ParameterName = "PBOM_CODE" , DataType = DbType . String , Value = model . PBOM_CODE } ) ;
}
if ( string . IsNullOrEmpty ( model . MATERIAL_CODE ) = = false )
{
sqlBuilder . Append ( " AND MATERIAL_CODE= @MATERIAL_CODE " ) ;
parameters . Add ( new DataParameter { ParameterName = "MATERIAL_CODE" , DataType = DbType . String , Value = model . MATERIAL_CODE } ) ;
}
if ( string . IsNullOrEmpty ( model . PID ) = = false )
{
sqlBuilder . Append ( " AND PID= @PID " ) ;
parameters . Add ( new DataParameter { ParameterName = "PID" , DataType = DbType . String , Value = model . PID } ) ;
}
try
{
if ( null ! = BaseSession )
{
string sqlChange = ChangeSqlByDB ( sqlBuilder . ToString ( ) , BaseSession ) ;
model = BaseSession . Get < Pbom > ( sqlChange , parameters . ToArray ( ) ) ;
}
else
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
string sqlChange = ChangeSqlByDB ( sqlBuilder . ToString ( ) , session ) ;
//获取信息
model = session . Get < Pbom > ( sqlChange , parameters . ToArray ( ) ) ;
}
}
return model ;
}
catch ( Exception ex )
{
RecordExceptionLog ( ex , "Bom信息维护数据层-获取信息" ) ;
throw ex ;
}
}
/// <summary>
/// 获取信息
/// </summary>
/// <param name="">条件</param>
/// <returns>信息</returns>
public Pbom TJGet ( Pbom model )
{
StringBuilder sqlBuilder = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
sqlBuilder . Append ( " SELECT * " ) ;
sqlBuilder . Append ( " FROM T_MD_PBOM " ) ;
sqlBuilder . Append ( " WHERE FLGDEL = '0'" ) ;
if ( string . IsNullOrEmpty ( model . PBOM_CODE ) = = false )
{
sqlBuilder . Append ( " AND PBOM_CODE = @PBOM_CODE " ) ;
parameters . Add ( new DataParameter { ParameterName = "PBOM_CODE" , DataType = DbType . String , Value = model . PBOM_CODE } ) ;
}
if ( string . IsNullOrEmpty ( model . MATERIAL_CODE ) = = false )
{
sqlBuilder . Append ( " AND MATERIAL_CODE LIKE @MATERIAL_CODE " ) ;
parameters . Add ( new DataParameter { ParameterName = "MATERIAL_CODE" , DataType = DbType . String , Value = "%" + model . MATERIAL_CODE + "%" } ) ;
}
if ( string . IsNullOrEmpty ( model . PID ) = = false )
{
sqlBuilder . Append ( " AND PID= @PID " ) ;
parameters . Add ( new DataParameter { ParameterName = "PID" , DataType = DbType . String , Value = model . PID } ) ;
}
try
{
if ( null ! = BaseSession )
{
string sqlChange = ChangeSqlByDB ( sqlBuilder . ToString ( ) , BaseSession ) ;
model = BaseSession . Get < Pbom > ( sqlChange , parameters . ToArray ( ) ) ;
}
else
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
string sqlChange = ChangeSqlByDB ( sqlBuilder . ToString ( ) , session ) ;
//获取信息
model = session . Get < Pbom > ( sqlChange , parameters . ToArray ( ) ) ;
}
}
return model ;
}
catch ( Exception ex )
{
RecordExceptionLog ( ex , "Bom信息维护数据层-获取信息" ) ;
throw ex ;
}
}
#endregion
#region 获取列表
/// <summary>
/// 获取列表
/// </summary>
/// <param name="condition">条件</param>
/// <param name="page">数据页</param>
/// <returns>数据页</returns>
public DataPage GetList ( Pbom 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" ;
}
#endregion
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
string sqlChange = base . ChangeSqlByDB ( sql , session ) ;
page = session . GetDataPage < Pbom > ( sqlChange , parameters . ToArray ( ) , page ) ;
}
return page ;
}
catch ( Exception ex )
{
RecordExceptionLog ( ex , "Bom信息维护数据层-获取列表" ) ;
throw ex ;
}
}
/// <summary>
/// 获取列表信息
/// </summary>
/// <param name="condition"></param>
/// <returns></returns>
public List < Pbom > GetList ( Pbom condition )
{
List < Pbom > list = new List < Pbom > ( ) ;
StringBuilder sqlBuilder = new StringBuilder ( ) ;
StringBuilder whereBuilder = new StringBuilder ( ) ;
List < DataParameter > paraList = new List < DataParameter > ( ) ;
//构成查询语句
sqlBuilder . Append ( "SELECT PID,PBOM_CODE,MATERIAL_CODE,PROPERTY_CODE,EOA_DATE,EXP_DATE,STATUS,ITEM_NUM,VER_NUM,REMARK,CREATEUSER,CREATEDATE,UPDATEUSER,UPDATEDATE " ) ;
sqlBuilder . Append ( "FROM T_MD_PBOM where " ) ;
if ( string . IsNullOrEmpty ( condition . PBOM_CODE ) = = false )
{
whereBuilder . AppendFormat ( "and PBOM_CODE=@PBOM_CODE " ) ;
paraList . Add ( new DataParameter ( ) { ParameterName = "PBOM_CODE" , DataType = DbType . String , Value = condition . PBOM_CODE } ) ;
}
if ( string . IsNullOrEmpty ( condition . MATERIAL_CODE ) = = false )
{
whereBuilder . AppendFormat ( "and MATERIAL_CODE=@MATERIAL_CODE " ) ;
paraList . Add ( new DataParameter ( ) { ParameterName = "MATERIAL_CODE" , DataType = DbType . String , Value = condition . MATERIAL_CODE } ) ;
}
sqlBuilder . Append ( whereBuilder . ToString ( ) . Substring ( 3 ) ) ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
list = session . GetList < Pbom > ( sqlBuilder . ToString ( ) , paraList . ToArray ( ) ) . ToList < Pbom > ( ) ;
}
return list ;
}
#endregion
#region 获取查询语句
/// <summary>
/// 获取查询语句
/// </summary>
/// <param name="user">查询条件</param>
/// <param name="parameters">参数</param>
/// <returns>查询语句</returns>
private string GetQuerySql ( Pbom condition , ref List < DataParameter > parameters )
{
StringBuilder sqlBuilder = new StringBuilder ( ) ;
StringBuilder whereBuilder = new StringBuilder ( ) ;
try
{
//构成查询语句
sqlBuilder . AppendLine ( " SELECT B.PID " ) ;
sqlBuilder . AppendLine ( " ,B.PBOM_CODE " ) ;
sqlBuilder . AppendLine ( " ,B.MATERIAL_CODE " ) ;
sqlBuilder . AppendLine ( " ,M.MATERIAL_NAME " ) ;
sqlBuilder . AppendLine ( " ,B.PROPERTY_CODE " ) ;
sqlBuilder . AppendLine ( " ,R.ROUTE_CODE " ) ;
sqlBuilder . AppendLine ( " ,R.ROUTE_NAME " ) ;
sqlBuilder . AppendLine ( " ,B.EOA_DATE " ) ;
sqlBuilder . AppendLine ( " ,B.EXP_DATE " ) ;
sqlBuilder . AppendLine ( " ,B.STATUS " ) ;
sqlBuilder . AppendLine ( " ,B.ITEM_NUM " ) ;
sqlBuilder . AppendLine ( " ,B.VER_NUM " ) ;
sqlBuilder . AppendLine ( " ,B.REMARK " ) ;
sqlBuilder . AppendLine ( " ,B.CREATEUSER " ) ;
sqlBuilder . AppendLine ( " ,B.CREATEDATE " ) ;
sqlBuilder . AppendLine ( " ,B.UPDATEUSER " ) ;
sqlBuilder . AppendLine ( " ,B.UPDATEDATE " ) ;
sqlBuilder . AppendLine ( " ,B.FLGDEL " ) ;
sqlBuilder . AppendLine ( " ,B.FACTORY_CODE " ) ;
sqlBuilder . AppendLine ( " ,F.FACTORY_NAME " ) ;
sqlBuilder . AppendLine ( " FROM T_MD_PBOM B " ) ;
sqlBuilder . AppendLine ( " LEFT JOIN T_MD_MATERIAL M ON M.MATERIAL_CODE=B.MATERIAL_CODE AND M.FLGDEL ='0' " ) ;
sqlBuilder . AppendLine ( " LEFT JOIN T_MD_MATERIAL_ROUTE MR ON B.MATERIAL_CODE=MR.MATERIAL_CODE AND MR.FLGDEL ='0' " ) ;
sqlBuilder . AppendLine ( " LEFT JOIN T_MD_PROCESS_ROUTE R ON MR.ROUTE_CODE=R.ROUTE_CODE AND R.FLGDEL ='0' " ) ;
sqlBuilder . AppendLine ( " LEFT JOIN T_MD_FACTORY F ON F.FACTORY_CODE=B.FACTORY_CODE " ) ;
whereBuilder . Append ( " AND B.FLGDEL='0'" ) ;
//查询条件
//工厂
if ( string . IsNullOrEmpty ( condition . FACTORY_CODE ) = = false )
{
whereBuilder . Append ( " AND B.FACTORY_CODE = @FACTORY_CODE" ) ;
parameters . Add ( new DataParameter { ParameterName = "FACTORY_CODE" , DataType = DbType . String , Value = condition . FACTORY_CODE } ) ;
}
//PBOM号
if ( string . IsNullOrEmpty ( condition . PBOM_CODE ) = = false )
{
whereBuilder . Append ( " AND B.PBOM_CODE = @PBOM_CODE " ) ;
parameters . Add ( new DataParameter { ParameterName = "PBOM_CODE" , DataType = DbType . String , Value = condition . PBOM_CODE } ) ;
}
//物料号
if ( string . IsNullOrEmpty ( condition . MATERIAL_CODE ) = = false )
{
whereBuilder . Append ( " AND B.MATERIAL_CODE = @MATERIAL_CODE " ) ;
parameters . Add ( new DataParameter { ParameterName = "MATERIAL_CODE" , DataType = DbType . String , Value = condition . MATERIAL_CODE } ) ;
}
//BOM版本
if ( string . IsNullOrEmpty ( condition . VER_NUM ) = = false )
{
whereBuilder . Append ( " AND B.VER_NUM= @VER_NUM " ) ;
parameters . Add ( new DataParameter { ParameterName = "VER_NUM" , DataType = DbType . String , Value = condition . VER_NUM } ) ;
}
//BOM特征码
if ( string . IsNullOrEmpty ( condition . PROPERTY_CODE ) = = false )
{
whereBuilder . Append ( " AND B.PROPERTY_CODE= @PROPERTY_CODE " ) ;
parameters . Add ( new DataParameter { ParameterName = "PROPERTY_CODE" , DataType = DbType . String , Value = condition . PROPERTY_CODE } ) ;
}
if ( whereBuilder . Length > 0 )
{
sqlBuilder . Append ( " WHERE " + whereBuilder . ToString ( ) . Substring ( 4 ) ) ;
}
return sqlBuilder . ToString ( ) ;
}
catch ( Exception ex )
{
throw ex ;
}
}
#endregion
#region BOM号,物料号信息是否重复
/// <summary>
/// 判断是否存在
/// </summary>
/// <param name="info"></param>
/// <returns>true:已存在;fasel:不存在。</returns>
public bool ExistsBomHdr ( Pbom model )
{
string PID = "" ;
int count = 0 ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
StringBuilder sqlBuilder = new StringBuilder ( ) ;
try
{
if ( string . IsNullOrEmpty ( model . PID ) = = false )
{
PID = model . PID ;
}
sqlBuilder . Append ( " SELECT COUNT(*) FROM T_MD_PBOM WHERE PID <> @PID AND FACTORY_CODE <> @FACTORY_CODE AND (PBOM_CODE =@PBOM_CODE OR MATERIAL_CODE =@MATERIAL_CODE ) " ) ;
parameters . Add ( new DataParameter { ParameterName = "PID" , DataType = DbType . String , Value = PID } ) ;
parameters . Add ( new DataParameter { ParameterName = "FACTORY_CODE" , DataType = DbType . String , Value = model . FACTORY_CODE } ) ;
parameters . Add ( new DataParameter { ParameterName = "PBOM_CODE" , DataType = DbType . String , Value = model . PBOM_CODE } ) ;
parameters . Add ( new DataParameter { ParameterName = "MATERIAL_CODE" , DataType = DbType . String , Value = model . MATERIAL_CODE } ) ;
if ( this . BaseSession ! = null )
{
string sqlChange = base . ChangeSqlByDB ( sqlBuilder . ToString ( ) , BaseSession ) ;
count = Convert . ToInt32 ( BaseSession . ExecuteSqlScalar ( sqlChange , parameters . ToArray ( ) ) ) ;
}
else
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
string sqlChange = ChangeSqlByDB ( sqlBuilder . ToString ( ) , session ) ;
count = Convert . ToInt32 ( session . ExecuteSqlScalar ( sqlChange , parameters . ToArray ( ) ) ) ;
}
}
if ( count > 0 )
{
return true ;
}
else
{
return false ;
}
}
catch ( Exception ex )
{
RecordExceptionLog ( ex , "Bom信息维护数据层-获取列表" ) ;
throw ex ;
}
}
#endregion
#region 判断物料是否存在
/// <summary>
/// 判断是否存在
/// </summary>
/// <param name="materialCode">物料号</param>
/// <param name="isItem">是否PBOM项目</param>
/// <returns>true:已存在;fasel:不存在。</returns>
public bool IsExists ( string materialCode , bool isItem )
{
int count = 0 ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
StringBuilder sqlBuilder = new StringBuilder ( ) ;
try
{
sqlBuilder . Append ( " SELECT COUNT(*) FROM " ) ;
if ( isItem = = true )
{
sqlBuilder . Append ( " T_MD_PBOM_ITEM " ) ;
}
else
{
sqlBuilder . Append ( " T_MD_PBOM " ) ;
}
sqlBuilder . Append ( " WHERE MATERIAL_CODE =@MATERIAL_CODE " ) ;
sqlBuilder . Append ( " AND FLGDEL = '0' " ) ;
parameters . Add ( new DataParameter { ParameterName = "MATERIAL_CODE" , DataType = DbType . String , Value = materialCode } ) ;
if ( this . BaseSession ! = null )
{
string sqlChange = base . ChangeSqlByDB ( sqlBuilder . ToString ( ) , BaseSession ) ;
count = Convert . ToInt32 ( BaseSession . ExecuteSqlScalar ( sqlChange , parameters . ToArray ( ) ) ) ;
}
else
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
string sqlChange = ChangeSqlByDB ( sqlBuilder . ToString ( ) , session ) ;
count = Convert . ToInt32 ( session . ExecuteSqlScalar ( sqlChange , parameters . ToArray ( ) ) ) ;
}
}
return count = = 0 ? false : true ;
}
catch ( Exception ex )
{
RecordExceptionLog ( ex , "判断物料号是否在BOM存在" ) ;
throw ex ;
}
}
#endregion
#region 插入信息
/// <summary>
/// 插入信息(单表)
/// </summary>
/// <param name="">信息</param>
/// <returns>插入行数</returns>
public int Insert ( Pbom model )
{
int count = 0 ;
try
{
if ( BaseSession ! = null )
{
//插入基本信息
count = BaseSession . Insert < Pbom > ( model ) ;
}
else
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
//插入基本信息
count = session . Insert < Pbom > ( model ) ;
}
}
return count ;
}
catch ( Exception ex )
{
RecordExceptionLog ( ex , "Bom信息维护数据层-插入信息" ) ;
throw ex ;
}
}
#endregion
#region 删除Bom信息
/// <summary>
/// 删除信息
/// </summary>
/// <param name="model">Bom信息(ID)</param>
/// <returns>删除个数</returns>
public int DeleteBomHdr ( Pbom model )
{
StringBuilder sqlBuilder = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
StringBuilder whereBuilder = new StringBuilder ( ) ;
int count = 0 ;
try
{
//构成查询语句
sqlBuilder . AppendLine ( " UPDATE T_MD_PBOM SET FLGDEL = '1' " ) ;
if ( string . IsNullOrEmpty ( model . PID ) = = false )
{
whereBuilder . Append ( " AND PID=@PID " ) ;
parameters . Add ( new DataParameter { ParameterName = "PID" , DataType = DbType . String , Value = model . PID } ) ;
}
if ( whereBuilder . Length > 0 )
{
sqlBuilder . Append ( " WHERE " + whereBuilder . ToString ( ) . Substring ( 4 ) ) ;
}
if ( this . BaseSession ! = null )
{
string sqlChange = base . ChangeSqlByDB ( sqlBuilder . ToString ( ) , BaseSession ) ;
//删除基本信息
count = BaseSession . ExecuteSql ( sqlChange , parameters . ToArray ( ) ) ;
}
else
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
string sqlChange = base . ChangeSqlByDB ( sqlBuilder . ToString ( ) , session ) ;
//删除基本信息
count = session . ExecuteSql ( sqlChange , parameters . ToArray ( ) ) ;
}
}
return count ;
}
catch ( Exception ex )
{
RecordExceptionLog ( ex , "Bom信息维护数据层-删除Bom信息" ) ;
throw ex ;
}
}
#endregion
#region Bom信息下拉
/// <summary>
///
/// </summary>
/// <param name="condition"></param>
/// <returns></returns>
public List < Pbom > GetBomHdrList ( Pbom condition )
{
List < Pbom > list = new List < Pbom > ( ) ;
string sql = null ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql = this . GetQuerySql ( condition , ref parameters ) ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
string sqlChange = base . ChangeSqlByDB ( sql , session ) ;
list = session . GetList < Pbom > ( sqlChange , parameters . ToArray ( ) ) . ToList ( ) ;
}
return list ;
}
catch ( Exception ex )
{
RecordExceptionLog ( ex , "BOM信息-下拉列表" ) ;
throw ex ;
}
}
#endregion
#region 根据BOM明细查找BOM头
/// <summary>
/// 根据BOM明细查找BOM头
/// </summary>
/// <param name="materialCode"></param>
/// <returns>BOM头列表</returns>
public List < Pbom > GetPbomByItem ( string materialCode )
{
StringBuilder sqlBuilder = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
sqlBuilder . Append ( " SELECT H.* " ) ;
sqlBuilder . Append ( " FROM T_MD_PBOM H WHERE H.FLGDEL = '0' AND EXISTS " ) ;
sqlBuilder . Append ( " ( " ) ;
sqlBuilder . Append ( " SELECT 1 FROM T_MD_PBOM_ITEM I " ) ;
sqlBuilder . Append ( " WHERE H.PBOM_CODE = I.PBOM_CODE AND I.FLGDEL = '0' AND I.MATERIAL_CODE = @MATERIAL_CODE " ) ;
sqlBuilder . Append ( " ) " ) ;
parameters . Add ( new DataParameter ( "MATERIAL_CODE" , DbType . String , materialCode ) ) ;
try
{
if ( null ! = BaseSession )
{
string sqlChange = ChangeSqlByDB ( sqlBuilder . ToString ( ) , BaseSession ) ;
return BaseSession . GetList < Pbom > ( sqlChange , parameters . ToArray ( ) ) . ToList ( ) ;
}
else
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
string sqlChange = ChangeSqlByDB ( sqlBuilder . ToString ( ) , session ) ;
//获取信息
return session . GetList < Pbom > ( sqlChange , parameters . ToArray ( ) ) . ToList ( ) ;
}
}
}
catch ( Exception ex )
{
RecordExceptionLog ( ex , "根据BOM明细查找BOM头 -出错!" ) ;
throw ex ;
}
}
/// <summary>
/// 根据物料号查找BOM头
/// </summary>
/// <param name="materialCode"></param>
/// <returns>BOM头列表</returns>
public List < Pbom > GetPbomByMaterialCode ( string materialCode )
{
StringBuilder sqlBuilder = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
sqlBuilder . AppendLine ( "SELECT H.* " ) ;
sqlBuilder . AppendLine ( " FROM T_MD_PBOM AS H " ) ;
sqlBuilder . AppendLine ( " WHERE H.FLGDEL = '0' " ) ;
sqlBuilder . AppendLine ( " AND (EXISTS (SELECT 1 " ) ;
sqlBuilder . AppendLine ( " FROM T_MD_PBOM_ITEM AS I " ) ;
sqlBuilder . AppendLine ( " WHERE H.PBOM_CODE = I.PBOM_CODE " ) ;
sqlBuilder . AppendLine ( " AND I.FLGDEL = '0' " ) ;
sqlBuilder . AppendLine ( " AND I.MATERIAL_CODE = @materialcode) " ) ;
sqlBuilder . AppendLine ( " OR H.[MATERIAL_CODE]=@materialcode) " ) ;
parameters . Add ( new DataParameter ( "materialcode" , DbType . String , materialCode ) ) ;
try
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
string sqlChange = ChangeSqlByDB ( sqlBuilder . ToString ( ) , session ) ;
//获取信息
return session . GetList < Pbom > ( sqlChange , parameters . ToArray ( ) ) . ToList ( ) ;
}
}
catch ( Exception ex )
{
throw ex ;
}
}
/// <summary>
/// 根据物料号查找BOM头
/// </summary>
/// <param name="materialCode"></param>
/// <returns>BOM头列表</returns>
public List < Pbom > GetPbomByMaterialCodeWithPlan ( string materialCode , string BomCode )
{
StringBuilder sqlBuilder = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
sqlBuilder . AppendLine ( "SELECT H.* " ) ;
sqlBuilder . AppendLine ( " FROM T_MD_PBOM AS H " ) ;
sqlBuilder . AppendLine ( " WHERE H.FLGDEL = '0' " ) ;
sqlBuilder . AppendLine ( " AND (EXISTS (SELECT 1 " ) ;
sqlBuilder . AppendLine ( " FROM T_MD_PBOM_ITEM AS I " ) ;
sqlBuilder . AppendLine ( " WHERE " ) ;
sqlBuilder . AppendLine ( " I.FLGDEL = '0' " ) ;
sqlBuilder . AppendLine ( " AND I.MATERIAL_CODE = @materialcode) " ) ;
sqlBuilder . AppendLine ( " ) and H.PBOM_CODE = @BomCode " ) ;
parameters . Add ( new DataParameter ( "materialcode" , DbType . String , materialCode ) ) ;
parameters . Add ( new DataParameter ( "BomCode" , DbType . String , BomCode ) ) ;
try
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
string sqlChange = ChangeSqlByDB ( sqlBuilder . ToString ( ) , session ) ;
//获取信息
return session . GetList < Pbom > ( sqlChange , parameters . ToArray ( ) ) . ToList ( ) ;
}
}
catch ( Exception ex )
{
throw ex ;
}
}
/// <summary>
/// 根据物料号查找最终的总成BOM头
/// </summary>
/// <param name="materialCode"></param>
/// <returns>BOM头列表</returns>
public List < Pbom > GetFinalPbom ( string materialCode )
{
StringBuilder sqlBuilder = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
sqlBuilder . AppendLine ( "WITH CTE AS ( " ) ;
sqlBuilder . AppendLine ( " SELECT H.*,0 as N " ) ;
sqlBuilder . AppendLine ( " FROM T_MD_PBOM AS H " ) ;
sqlBuilder . AppendLine ( " WHERE H.FLGDEL = '0' " ) ;
sqlBuilder . AppendLine ( " AND (EXISTS (SELECT 1 " ) ;
sqlBuilder . AppendLine ( " FROM T_MD_PBOM_ITEM AS I " ) ;
sqlBuilder . AppendLine ( " WHERE H.PBOM_CODE = I.PBOM_CODE " ) ;
sqlBuilder . AppendLine ( " AND I.FLGDEL = '0' " ) ;
sqlBuilder . AppendLine ( " AND I.MATERIAL_CODE = @materialcode) " ) ;
sqlBuilder . AppendLine ( " OR H.[MATERIAL_CODE]=@materialcode) " ) ;
sqlBuilder . AppendLine ( " UNION ALL " ) ;
sqlBuilder . AppendLine ( " SELECT B.*,C.N+1 AS N " ) ;
sqlBuilder . AppendLine ( " FROM CTE C " ) ;
sqlBuilder . AppendLine ( " JOIN T_MD_PBOM_ITEM AS BI " ) ;
sqlBuilder . AppendLine ( " ON BI.MATERIAL_CODE=c.MATERIAL_CODE " ) ;
sqlBuilder . AppendLine ( " JOIN T_MD_PBOM AS B " ) ;
sqlBuilder . AppendLine ( " ON B.PBOM_CODE=BI.PBOM_CODE " ) ;
sqlBuilder . AppendLine ( " WHERE B.FLGDEL='0' " ) ;
sqlBuilder . AppendLine ( ") " ) ;
sqlBuilder . AppendLine ( "SELECT * FROM CTE " ) ;
sqlBuilder . AppendLine ( " WHERE N=(SELECT MAX(N) FROM CTE ) " ) ;
parameters . Add ( new DataParameter ( "materialcode" , DbType . String , materialCode ) ) ;
try
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
string sqlChange = ChangeSqlByDB ( sqlBuilder . ToString ( ) , session ) ;
//获取信息
return session . GetList < Pbom > ( sqlChange , parameters . ToArray ( ) ) . ToList ( ) ;
}
}
catch ( Exception ex )
{
throw ex ;
}
}
#endregion
#region 配置BOM明细
#region 获取BOM明细列表
/// <summary>
/// 获取列表
/// </summary>
/// <param name="condition">条件</param>
/// <param name="page">数据页</param>
/// <returns>数据页</returns>
public DataPage BomHdrConfigList ( PbomItem condition , DataPage page )
{
string sql = "" ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql = this . GetBomDetailListSql ( condition , ref parameters ) ;
//分页关键字段及排序
page . KeyName = "PID" ;
if ( string . IsNullOrEmpty ( page . SortExpression ) )
{
page . SortExpression = "UPDATEDATE DESC" ;
}
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
string sqlChange = base . ChangeSqlByDB ( sql , session ) ;
page = session . GetDataPage < PbomItem > ( sqlChange , parameters . ToArray ( ) , page ) ;
}
return page ;
}
catch ( Exception ex )
{
RecordExceptionLog ( ex , "Bom信息维护数据层-获取明细列表" ) ;
throw ex ;
}
}
#endregion
#region 获取查询语句
/// <summary>
/// 获取查询语句
/// </summary>
/// <param name="user">查询条件</param>
/// <param name="parameters">参数</param>
/// <returns>查询语句</returns>
private string GetBomDetailListSql ( PbomItem condition , ref List < DataParameter > parameters )
{
StringBuilder sqlBuilder = new StringBuilder ( ) ;
StringBuilder whereBuilder = new StringBuilder ( ) ;
try
{
//构成查询语句
sqlBuilder . AppendLine ( " SELECT B.PID " ) ;
sqlBuilder . AppendLine ( " ,B.PBOM_CODE " ) ;
sqlBuilder . AppendLine ( " ,B.PROPERTY_CODE " ) ;
sqlBuilder . AppendLine ( " ,B.MATERIAL_CODE " ) ;
sqlBuilder . AppendLine ( " ,M.MATERIAL_NAME " ) ;
sqlBuilder . AppendLine ( " ,B.SEQ_NUM " ) ;
sqlBuilder . AppendLine ( " ,B.UP_SQ_NUM " ) ;
sqlBuilder . AppendLine ( " ,B.UNIT_SUM " ) ;
sqlBuilder . AppendLine ( " ,B.GMP " ) ;
sqlBuilder . AppendLine ( " ,C.WORKCELL_NAME AS GMP_NAME " ) ;
sqlBuilder . AppendLine ( " ,B.FLAG_CSP " ) ;
sqlBuilder . AppendLine ( " ,B.MFRS " ) ;
sqlBuilder . AppendLine ( " ,B.REMARK " ) ;
sqlBuilder . AppendLine ( " ,B.FLGDEL " ) ;
sqlBuilder . AppendLine ( " ,B.CREATEUSER " ) ;
sqlBuilder . AppendLine ( " ,B.CREATEDATE " ) ;
sqlBuilder . AppendLine ( " ,B.UPDATEUSER " ) ;
sqlBuilder . AppendLine ( " ,B.UPDATEDATE " ) ;
sqlBuilder . AppendLine ( " FROM T_MD_PBOM_ITEM B " ) ;
sqlBuilder . AppendLine ( " LEFT JOIN T_MD_WORKCELL C ON B.GMP = C.WORKCELL_CODE AND C.FLGDEL = '0' " ) ;
sqlBuilder . AppendLine ( " LEFT JOIN T_MD_MATERIAL M ON B.MATERIAL_CODE = M.MATERIAL_CODE AND M.FLGDEL = '0' " ) ;
whereBuilder . Append ( " AND B.FLGDEL ='0' " ) ;
//查询条件
if ( string . IsNullOrEmpty ( condition . PBOM_CODE ) = = false )
{
whereBuilder . Append ( " AND B.PBOM_CODE= @PBOM_CODE " ) ;
parameters . Add ( new DataParameter { ParameterName = "PBOM_CODE" , DataType = DbType . String , Value = condition . PBOM_CODE } ) ;
}
if ( whereBuilder . Length > 0 )
{
sqlBuilder . Append ( " WHERE " + whereBuilder . ToString ( ) . Substring ( 4 ) ) ;
}
return sqlBuilder . ToString ( ) ;
}
catch ( Exception ex )
{
throw ex ;
}
}
#endregion
#region 获取Bom明细列表
/// <summary>
/// 获取列表
/// </summary>
/// <param name="condition">条件</param>
/// <param name="page">数据页</param>
/// <returns>数据页</returns>
public List < PbomItem > BomDetailList ( PbomItem condition )
{
string sql = "" ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
List < PbomItem > list = new List < PbomItem > ( ) ;
try
{
sql = this . GetBomDetailListSql ( condition , ref parameters ) ;
if ( null ! = BaseSession )
{
string sqlChange = base . ChangeSqlByDB ( sql , BaseSession ) ;
list = BaseSession . GetList < PbomItem > ( sqlChange , parameters . ToArray ( ) ) . ToList ( ) ;
}
else
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
string sqlChange = base . ChangeSqlByDB ( sql , session ) ;
list = session . GetList < PbomItem > ( sqlChange , parameters . ToArray ( ) ) . ToList ( ) ;
}
}
return list ;
}
catch ( Exception ex )
{
RecordExceptionLog ( ex , "Bom信息维护数据层-获取Bom明细列表" ) ;
throw ex ;
}
}
#endregion
#region 插入Bom明细信息
/// <summary>
/// 插入信息(单表)
/// </summary>
/// <param name="">信息</param>
/// <returns>插入行数</returns>
public int InsertBomDetail ( PbomItem model )
{
int count = 0 ;
try
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
//插入基本信息
count = session . Insert < PbomItem > ( model ) ;
}
return count ;
}
catch ( Exception ex )
{
RecordExceptionLog ( ex , "Bom信息维护数据层-插入Bom明细信息" ) ;
throw ex ;
}
}
#endregion
#region 获取Bom明细
/// <summary>
/// 获取Bom明细
/// </summary>
/// <param name="">条件</param>
/// <returns>*信息</returns>
public PbomItem GetBomDetail ( PbomItem model )
{
StringBuilder sqlBuilder = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
sqlBuilder . Append ( " SELECT * " ) ;
sqlBuilder . Append ( " FROM T_MD_PBOM_ITEM " ) ;
sqlBuilder . Append ( " WHERE FLGDEL = '0'" ) ;
if ( string . IsNullOrEmpty ( model . PBOM_CODE ) = = false & & string . IsNullOrEmpty ( model . MATERIAL_CODE ) = = false )
{
sqlBuilder . Append ( " AND PBOM_CODE= @PBOM_CODE " ) ;
sqlBuilder . Append ( " AND MATERIAL_CODE= @MATERIAL_CODE " ) ;
parameters . Add ( new DataParameter { ParameterName = "PBOM_CODE" , DataType = DbType . String , Value = model . PBOM_CODE } ) ;
parameters . Add ( new DataParameter { ParameterName = "MATERIAL_CODE" , DataType = DbType . String , Value = model . MATERIAL_CODE } ) ;
}
if ( string . IsNullOrEmpty ( model . PID ) = = false )
{
sqlBuilder . Append ( " AND PID= @PID " ) ;
parameters . Add ( new DataParameter { ParameterName = "PID" , DataType = DbType . String , Value = model . PID } ) ;
}
if ( string . IsNullOrEmpty ( model . GMP ) = = false )
{
sqlBuilder . Append ( " AND GMP= @GMP " ) ;
parameters . Add ( new DataParameter { ParameterName = "GMP" , DataType = DbType . String , Value = model . GMP } ) ;
}
try
{
if ( null ! = BaseSession )
{
string sqlChange = ChangeSqlByDB ( sqlBuilder . ToString ( ) , BaseSession ) ;
model = BaseSession . Get < PbomItem > ( sqlChange , parameters . ToArray ( ) ) ;
}
else
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
string sqlChange = ChangeSqlByDB ( sqlBuilder . ToString ( ) , session ) ;
//获取信息
model = session . Get < PbomItem > ( sqlChange , parameters . ToArray ( ) ) ;
}
}
return model ;
}
catch ( Exception ex )
{
RecordExceptionLog ( ex , "Bom信息维护数据层-获取信息" ) ;
throw ex ;
}
}
#endregion
#region 删除Bom明细
/// <summary>
/// 删除信息
/// </summary>
/// <param name="model">Bom信息(ID)</param>
/// <returns>删除个数</returns>
public int DeleteBomDetail ( PbomItem model )
{
StringBuilder sqlBuilder = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
StringBuilder whereBuilder = new StringBuilder ( ) ;
int count = 0 ;
try
{
//构成查询语句
sqlBuilder . AppendLine ( " UPDATE T_MD_PBOM_ITEM SET FLGDEL = '1' " ) ;
if ( string . IsNullOrEmpty ( model . PID ) = = false )
{
whereBuilder . Append ( " AND PID=@PID " ) ;
parameters . Add ( new DataParameter { ParameterName = "PID" , DataType = DbType . String , Value = model . PID } ) ;
}
if ( string . IsNullOrEmpty ( model . PBOM_CODE ) = = false )
{
whereBuilder . Append ( " AND PBOM_CODE=@PBOM_CODE " ) ;
parameters . Add ( new DataParameter { ParameterName = "PBOM_CODE" , DataType = DbType . String , Value = model . PBOM_CODE } ) ;
}
if ( whereBuilder . Length > 0 )
{
sqlBuilder . Append ( " WHERE " + whereBuilder . ToString ( ) . Substring ( 4 ) ) ;
}
if ( this . BaseSession ! = null )
{
string sqlChange = base . ChangeSqlByDB ( sqlBuilder . ToString ( ) , BaseSession ) ;
//删除基本信息
count = BaseSession . ExecuteSql ( sqlChange , parameters . ToArray ( ) ) ;
}
else
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
string sqlChange = base . ChangeSqlByDB ( sqlBuilder . ToString ( ) , session ) ;
//删除基本信息
count = session . ExecuteSql ( sqlChange , parameters . ToArray ( ) ) ;
}
}
return count ;
}
catch ( Exception ex )
{
RecordExceptionLog ( ex , "Bom信息维护数据层-删除BOM详细信息" ) ;
throw ex ;
}
}
/// <summary>
/// 删除信息
/// </summary>
/// <param name="model">Bom信息(ID)</param>
/// <returns>删除个数</returns>
public int DeleteBomDetailByPbomId ( string pid )
{
StringBuilder sqlBuilder = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
StringBuilder whereBuilder = new StringBuilder ( ) ;
int count = 0 ;
try
{
//构成查询语句
sqlBuilder . AppendLine ( " UPDATE T_MD_PBOM_ITEM SET FLGDEL = '1' WHERE PBOM_CODE IN (SELECT T.PBOM_CODE FROM T_MD_PBOM T WHERE T.PID = @PID)" ) ;
parameters . Add ( new DataParameter { ParameterName = "PID" , DataType = DbType . String , Value = pid } ) ;
if ( this . BaseSession ! = null )
{
string sqlChange = base . ChangeSqlByDB ( sqlBuilder . ToString ( ) , BaseSession ) ;
//删除基本信息
count = BaseSession . ExecuteSql ( sqlChange , parameters . ToArray ( ) ) ;
}
else
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
string sqlChange = base . ChangeSqlByDB ( sqlBuilder . ToString ( ) , session ) ;
//删除基本信息
count = session . ExecuteSql ( sqlChange , parameters . ToArray ( ) ) ;
}
}
return count ;
}
catch ( Exception ex )
{
RecordExceptionLog ( ex , "Bom信息维护数据层-删除BOM详细信息" ) ;
throw ex ;
}
}
#endregion
#endregion
/// <summary>
/// 获取Bom信息
/// </summary>
/// <param name="BomCode">Bom编码</param>
/// <returns></returns>
public Pbom GetByCode ( string BomCode )
{
StringBuilder sql = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql . Append ( "SELECT * FROM [T_MD_PBOM] WHERE [PBOM_CODE]=@bomcode" ) ;
parameters . Add ( new DataParameter ( "bomcode" , BomCode ) ) ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
return session . Get < Pbom > ( sql . ToString ( ) , parameters . ToArray ( ) ) ;
}
}
catch ( Exception ex )
{
throw ex ;
}
}
/// <summary>
/// 根据BOM定位查找BOM明细项
/// </summary>
/// <param name="locate">BOM定位信息</param>
/// <returns></returns>
public PbomItem GetByLocate ( BomLocation locate )
{
StringBuilder sql = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql . Append ( "SELECT * FROM [T_MD_PBOM_ITEM] WHERE [PBOM_CODE]=@bomcode AND [SEQ_NUM]=@itemno" ) ;
parameters . Add ( new DataParameter ( "bomcode" , locate . BOMCode ) ) ;
parameters . Add ( new DataParameter ( "itemno" , locate . ItemNo ) ) ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
return session . Get < PbomItem > ( sql . ToString ( ) , parameters . ToArray ( ) ) ;
}
}
catch ( Exception ex )
{
throw ex ;
}
}
/// <summary>
/// 根据BOM定位查找BOM明细项列表
/// </summary>
/// <param name="locate">BOM定位信息</param>
/// <returns></returns>
public List < PbomItem > GetBomItemsByLocate ( BomLocation locate )
{
StringBuilder sql = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql . Append ( "SELECT * FROM [T_MD_PBOM_ITEM] WHERE [PBOM_CODE]=@bomcode AND [UP_SQ_NUM]=@itemno" ) ;
parameters . Add ( new DataParameter ( "bomcode" , locate . BOMCode ) ) ;
parameters . Add ( new DataParameter ( "itemno" , locate . ItemNo ) ) ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
return session . GetList < PbomItem > ( sql . ToString ( ) , parameters . ToArray ( ) ) . ToList ( ) ;
}
}
catch ( Exception ex )
{
throw ex ;
}
}
public List < PbomItem > GetBomItemsByLocateWithType ( BomLocation locate )
{
StringBuilder sql = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql . AppendLine ( "SELECT B.* " ) ;
sql . AppendLine ( " ,M.MATERIAL_TYPE_CODE " ) ;
sql . AppendLine ( " ,C.UP_MATERIAL_TYPE_CODE " ) ;
sql . AppendLine ( "FROM [T_MD_PBOM_ITEM] AS B " ) ;
sql . AppendLine ( "LEFT JOIN T_MD_MATERIAL AS M " ) ;
sql . AppendLine ( "ON B.MATERIAL_CODE=M.MATERIAL_CODE " ) ;
sql . AppendLine ( "LEFT JOIN T_MD_MATERIAL_CLASS AS C " ) ;
sql . AppendLine ( "ON C.MATERIAL_TYPE_CODE=m.MATERIAL_TYPE_CODE " ) ;
sql . AppendLine ( "WHERE [PBOM_CODE]=@bomcode AND [UP_SQ_NUM]=@itemno" ) ;
parameters . Add ( new DataParameter ( "bomcode" , locate . BOMCode ) ) ;
parameters . Add ( new DataParameter ( "itemno" , locate . ItemNo ) ) ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
return session . GetList < PbomItem > ( sql . ToString ( ) , parameters . ToArray ( ) ) . ToList ( ) ;
}
}
catch ( Exception ex )
{
throw ex ;
}
}
/// <summary>
/// 定位BOM
/// </summary>
/// <param name="materials"></param>
/// <returns></returns>
public List < BomLocation > LocateBom ( params string [ ] materials )
{
if ( materials . Length = = 0 )
{
throw new Exception ( "至少需要一种物料号才能确定BOM!" ) ;
}
StringBuilder sql = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql . AppendLine ( "SELECT [PBOM_CODE] AS [BOMCode]" ) ;
sql . AppendLine ( " ,[UP_SQ_NUM] AS [ItemNo] " ) ;
sql . AppendLine ( " ,(SELECT COUNT(*) " ) ;
sql . AppendLine ( " FROM [T_MD_PBOM_ITEM] AS A " ) ;
sql . AppendLine ( " LEFT JOIN T_MD_MATERIAL M ON M.MATERIAL_CODE=A.MATERIAL_CODE " ) ;
sql . AppendLine ( " LEFT JOIN T_MD_MATERIAL_CLASS MC ON MC.MATERIAL_TYPE_CODE=M.MATERIAL_TYPE_CODE " ) ;
sql . AppendLine ( " WHERE A.[PBOM_CODE]=W.[PBOM_CODE] " ) ;
sql . AppendLine ( " AND A.[UP_SQ_NUM]=W.[UP_SQ_NUM] " ) ;
sql . AppendLine ( " AND MC.MATERIAL_ATTRIBUTE NOT IN ('3') " ) ;
sql . AppendLine ( " )AS [SubCount] " ) ;
sql . AppendLine ( " ,(SELECT TOP 1 MATERIAL_CODE " ) ;
sql . AppendLine ( " FROM [T_MD_PBOM_ITEM] AS B " ) ;
sql . AppendLine ( " WHERE B.[PBOM_CODE]=W.[PBOM_CODE] " ) ;
sql . AppendLine ( " AND B.[SEQ_NUM]=W.[UP_SQ_NUM] " ) ;
sql . AppendLine ( " )AS [UP_MATERIAL_CODE] " ) ;
sql . AppendLine ( " FROM [T_MD_PBOM_ITEM] AS W" ) ;
sql . Append ( " WHERE W.[MATERIAL_CODE] IN (" ) ;
for ( int i = 0 ; i < materials . Length ; i + + )
{
if ( i > 0 )
{
sql . Append ( "," ) ;
}
sql . Append ( string . Format ( "@material{0}" , i ) ) ;
parameters . Add ( new DataParameter ( string . Format ( "@material{0}" , i ) , materials [ i ] ) ) ;
}
sql . AppendLine ( ") " ) ;
sql . AppendLine ( " GROUP BY W.[PBOM_CODE],W.[UP_SQ_NUM]" ) ;
sql . AppendLine ( "HAVING COUNT(*)>=@materialcount" ) ;
parameters . Add ( new DataParameter ( "materialcount" , materials . Length ) ) ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
return session . GetList < Entity . View . BomLocation > ( sql . ToString ( ) , parameters . ToArray ( ) ) . ToList ( ) ;
}
}
catch ( Exception ex )
{
throw ex ;
}
}
/// <summary>
/// 定位BOM
/// </summary>
/// <param name="materials"></param>
/// <returns></returns>
public List < BomLocation > LocateBomByUpMaterial ( string upmaterial )
{
StringBuilder sql = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql . AppendLine ( "SELECT [PBOM_CODE] AS [BOMCode] " ) ;
sql . AppendLine ( " ,[SEQ_NUM] AS [ItemNo] " ) ;
sql . AppendLine ( " ,(SELECT COUNT(*) " ) ;
sql . AppendLine ( " FROM [T_MD_PBOM_ITEM] AS A " ) ;
sql . AppendLine ( " WHERE A.[PBOM_CODE]=B.[PBOM_CODE] " ) ;
sql . AppendLine ( " AND A.[UP_SQ_NUM]=B.[SEQ_NUM])AS [SubCount] " ) ;
sql . AppendLine ( " ,[MATERIAL_CODE] AS [UP_MATERIAL_CODE] " ) ;
sql . AppendLine ( " FROM T_MD_PBOM_ITEM B " ) ;
sql . AppendLine ( " WHERE MATERIAL_CODE=@upmaterial " ) ;
sql . AppendLine ( "UNION " ) ;
sql . AppendLine ( "SELECT M.PBOM_CODE AS [BOMCode] " ) ;
sql . AppendLine ( " ,0 AS [ItemNo] " ) ;
sql . AppendLine ( " ,(SELECT COUNT(*) " ) ;
sql . AppendLine ( " FROM [T_MD_PBOM_ITEM] AS A " ) ;
sql . AppendLine ( " WHERE A.[PBOM_CODE]=M.[PBOM_CODE] " ) ;
sql . AppendLine ( " AND A.[UP_SQ_NUM]=0)AS [SubCount] " ) ;
sql . AppendLine ( " ,M.[MATERIAL_CODE] AS [UP_MATERIAL_CODE] " ) ;
sql . AppendLine ( " FROM T_MD_PBOM AS M " ) ;
sql . AppendLine ( " WHERE M.MATERIAL_CODE=@upmaterial " ) ;
parameters . Add ( new DataParameter ( "upmaterial" , upmaterial ) ) ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
return session . GetList < Entity . View . BomLocation > ( sql . ToString ( ) , parameters . ToArray ( ) ) . ToList ( ) ;
}
}
catch ( Exception ex )
{
throw ex ;
}
}
public List < PbomItem > GetSubItem ( string upmaterial )
{
StringBuilder sql = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql . AppendLine ( "WITH CTE AS (" ) ;
sql . AppendLine ( " SELECT TOP 1 *" ) ;
sql . AppendLine ( " FROM T_MD_PBOM_ITEM " ) ;
sql . AppendLine ( " WHERE MATERIAL_CODE=@upmaterial" ) ;
sql . AppendLine ( " UNION ALL" ) ;
sql . AppendLine ( " SELECT TOP 1 NULL AS [PID]" ) ;
sql . AppendLine ( " ,[PBOM_CODE]" ) ;
sql . AppendLine ( " ,[MATERIAL_CODE]" ) ;
sql . AppendLine ( " ,0 AS [SEQ_NUM]" ) ;
sql . AppendLine ( " ,0 AS [UP_SQ_NUM]" ) ;
sql . AppendLine ( " ,1 AS [UNIT_SUM]" ) ;
sql . AppendLine ( " ,'' AS [GMP]" ) ;
sql . AppendLine ( " ,NULL AS [FLAG_CSP]" ) ;
sql . AppendLine ( " ,'' AS [MFRS]" ) ;
sql . AppendLine ( " ,[REMARK]" ) ;
sql . AppendLine ( " ,[CREATEUSER]" ) ;
sql . AppendLine ( " ,[CREATEDATE]" ) ;
sql . AppendLine ( " ,[UPDATEUSER]" ) ;
sql . AppendLine ( " ,[UPDATEDATE]" ) ;
sql . AppendLine ( " ,[PROPERTY_CODE]" ) ;
sql . AppendLine ( " ,[FLGDEL]" ) ;
sql . AppendLine ( " ,[FACTORY_CODE]" ) ;
sql . AppendLine ( " FROM T_MD_PBOM" ) ;
sql . AppendLine ( " WHERE MATERIAL_CODE=@upmaterial" ) ;
sql . AppendLine ( " UNION ALL" ) ;
sql . AppendLine ( " SELECT B.* " ) ;
sql . AppendLine ( " FROM CTE AS C " ) ;
sql . AppendLine ( " JOIN T_MD_PBOM_ITEM AS B " ) ;
sql . AppendLine ( " ON B.PBOM_CODE=C.PBOM_CODE" ) ;
sql . AppendLine ( " AND B.UP_SQ_NUM=C.SEQ_NUM" ) ;
sql . AppendLine ( ")" ) ;
sql . AppendLine ( "SELECT * FROM CTE" ) ;
parameters . Add ( new DataParameter ( "upmaterial" , upmaterial ) ) ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
return session . GetList < PbomItem > ( sql . ToString ( ) , parameters . ToArray ( ) ) . ToList ( ) ;
}
}
catch ( Exception ex )
{
throw ex ;
}
}
public List < string > GetSubMaterial ( string upmaterial )
{
StringBuilder sql = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql . AppendLine ( "SELECT C.* " ) ;
sql . AppendLine ( " FROM T_MD_PBOM_ITEM AS C " ) ;
sql . AppendLine ( " JOIN (SELECT PBOM_CODE " ) ;
sql . AppendLine ( " ,SEQ_NUM " ) ;
sql . AppendLine ( " FROM T_MD_PBOM_ITEM " ) ;
sql . AppendLine ( " WHERE MATERIAL_CODE=@upmaterial " ) ;
sql . AppendLine ( " UNION ALL " ) ;
sql . AppendLine ( " SELECT [PBOM_CODE] " ) ;
sql . AppendLine ( " ,0 AS [SEQ_NUM] " ) ;
sql . AppendLine ( " FROM T_MD_PBOM " ) ;
sql . AppendLine ( " WHERE MATERIAL_CODE=@upmaterial) AS B " ) ;
sql . AppendLine ( " ON C.PBOM_CODE=B.PBOM_CODE " ) ;
sql . AppendLine ( " AND C.UP_SQ_NUM=B.SEQ_NUM" ) ;
parameters . Add ( new DataParameter ( "upmaterial" , upmaterial ) ) ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
var list = session . GetList < PbomItem > ( sql . ToString ( ) , parameters . ToArray ( ) ) . ToList ( ) ;
return list . Select ( p = > p . MATERIAL_CODE ) . Distinct ( ) . ToList ( ) ;
}
}
catch ( Exception ex )
{
throw ex ;
}
}
/// <summary>
/// 根据物料组查询BOM
/// </summary>
/// <param name="materials">物料组</param>
/// <returns></returns>
public Pbom GetBomWithMaterials ( params string [ ] materials )
{
if ( materials . Length = = 0 )
{
throw new Exception ( "至少需要一种物料号才能确定BOM!" ) ;
}
StringBuilder sql = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql . AppendLine ( "SELECT * " ) ;
sql . AppendLine ( " FROM T_MD_PBOM AS B " ) ;
sql . AppendLine ( " WHERE EXISTS (SELECT PBOM_CODE,UP_SQ_NUM FROM T_MD_PBOM_ITEM AS W " ) ;
sql . AppendLine ( " WHERE W.[MATERIAL_CODE] IN (" ) ;
for ( int i = 0 ; i < materials . Length ; i + + )
{
if ( i > 0 )
{
sql . Append ( "," ) ;
}
sql . Append ( string . Format ( "@material{0}" , i ) ) ;
parameters . Add ( new DataParameter ( string . Format ( "@material{0}" , i ) , materials [ i ] ) ) ;
}
sql . AppendLine ( ") " ) ;
sql . AppendLine ( " AND (SELECT COUNT(1) " ) ;
sql . AppendLine ( " FROM T_MD_PBOM_ITEM AS I " ) ;
sql . AppendLine ( " WHERE I.PBOM_CODE=W.PBOM_CODE " ) ;
sql . AppendLine ( " AND I.UP_SQ_NUM=W.UP_SQ_NUM " ) ;
sql . AppendLine ( " AND I.UNIT_SUM>0)=@materialcount " ) ;
sql . AppendLine ( " AND W.PBOM_CODE=B.PBOM_CODE " ) ;
sql . AppendLine ( " GROUP BY PBOM_CODE,UP_SQ_NUM HAVING COUNT(*)>=@materialcount)" ) ;
parameters . Add ( new DataParameter ( "materialcount" , materials . Length ) ) ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
return session . Get < Pbom > ( sql . ToString ( ) , parameters . ToArray ( ) ) ;
}
}
catch ( Exception ex )
{
throw ex ;
}
}
/// <summary>
/// 根据物料类型和BOM编号查找BOM明细项
/// </summary>
/// <param name="p"></param>
/// <param name="p_2"></param>
/// <returns></returns>
public PbomItem GetBomItemByMaterilType ( string bomcode , string materialtype )
{
StringBuilder sql = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql . Append ( "SELECT TOP 1 P.* " ) ;
sql . Append ( " FROM T_MD_PBOM_ITEM AS P " ) ;
sql . Append ( " LEFT JOIN T_MD_MATERIAL AS M " ) ;
sql . Append ( " ON P.MATERIAL_CODE=M.MATERIAL_CODE " ) ;
sql . Append ( " WHERE P.PBOM_CODE=@bomcode " ) ;
sql . Append ( " AND M.MATERIAL_TYPE_CODE=@materialtype " ) ;
parameters . Add ( new DataParameter ( "bomcode" , bomcode ) ) ;
parameters . Add ( new DataParameter ( "materialtype" , materialtype ) ) ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
return session . Get < PbomItem > ( sql . ToString ( ) , parameters . ToArray ( ) ) ;
}
}
catch ( Exception ex )
{
throw ex ;
}
}
/// <summary>
/// 根据物料类型和BOM编号查找BOM明细项
/// </summary>
/// <param name="p"></param>
/// <param name="p_2"></param>
/// <returns></returns>
public PbomItem GetBomItemByMaterilType ( string bomcode , string materialtype , bool parenttype = false )
{
StringBuilder sql = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql . Append ( "SELECT TOP 1 P.* " ) ;
sql . Append ( " FROM T_MD_PBOM_ITEM AS P " ) ;
sql . Append ( " LEFT JOIN T_MD_MATERIAL AS M " ) ;
sql . Append ( " ON P.MATERIAL_CODE=M.MATERIAL_CODE " ) ;
sql . Append ( " WHERE P.PBOM_CODE=@bomcode " ) ;
if ( parenttype )
{
sql . Append ( " AND M.MATERIAL_TYPE_CODE LIKE @materialtype + '%' " ) ;
}
else
{
sql . Append ( " AND M.MATERIAL_TYPE_CODE=@materialtype " ) ;
}
parameters . Add ( new DataParameter ( "bomcode" , bomcode ) ) ;
parameters . Add ( new DataParameter ( "materialtype" , materialtype ) ) ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
return session . Get < PbomItem > ( sql . ToString ( ) , parameters . ToArray ( ) ) ;
}
}
catch ( Exception ex )
{
throw ex ;
}
}
#region 获取全部列表
/// <summary>
/// 获取全部列表信息
/// </summary>
/// <param name="condition"></param>
/// <returns></returns>
public List < PbomItem > GetPbomItemList ( PbomItem model )
{
List < DataParameter > parameters = new List < DataParameter > ( ) ;
var sql = this . GetQuerySqlPbom ( model , ref parameters ) ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
return session . GetList < PbomItem > ( sql , parameters . ToArray ( ) ) . ToList ( ) ;
}
}
#endregion
#region 获取pbom查询语句
/// <summary>
/// 获取查询语句
/// </summary>
/// <param name="user">查询条件</param>
/// <param name="parameters">参数</param>
/// <returns>查询语句</returns>
private string GetQuerySqlPbom ( PbomItem condition , ref List < DataParameter > parameters )
{
StringBuilder sqlBuilder = new StringBuilder ( ) ;
StringBuilder whereBuilder = new StringBuilder ( ) ;
try
{
//构成查询语句
sqlBuilder . AppendLine ( " select t1.*,t2.MATERIAL_NAME from T_MD_PBOM_ITEM t1,T_MD_MATERIAL t2 " ) ;
sqlBuilder . AppendLine ( " where t1.MATERIAL_CODE=t2.MATERIAL_CODE " ) ;
//查询条件
if ( string . IsNullOrEmpty ( condition . PBOM_CODE ) = = false )
{
whereBuilder . Append ( " AND PBOM_CODE=@PBOM_CODE " ) ;
parameters . Add ( new DataParameter { ParameterName = "PBOM_CODE" , DataType = DbType . String , Value = condition . PBOM_CODE } ) ;
}
//UP_SQ_NUM号
if ( condition . UP_SQ_NUM > = 0 )
{
whereBuilder . Append ( " AND UP_SQ_NUM = @UP_SQ_NUM " ) ;
parameters . Add ( new DataParameter { ParameterName = "UP_SQ_NUM" , DataType = DbType . String , Value = condition . UP_SQ_NUM } ) ;
}
//查询条件
if ( string . IsNullOrEmpty ( condition . GMP ) = = false )
{
whereBuilder . Append ( " AND GMP=@GMP " ) ;
parameters . Add ( new DataParameter { ParameterName = "GMP" , DataType = DbType . String , Value = condition . GMP } ) ;
}
if ( whereBuilder . Length > 0 )
{
//sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
sqlBuilder . Append ( whereBuilder ) ;
}
return sqlBuilder . ToString ( ) ;
}
catch ( Exception ex )
{
throw ex ;
}
}
#endregion
#region 获取信息
/// <summary>
/// 获取信息
/// </summary>
/// <param name="">条件</param>
/// <returns>*信息</returns>
public PbomItem GetPbomItemOne ( PbomItem model )
{
try
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
//获取信息
model = session . Get < PbomItem > ( model ) ;
}
return model ;
}
catch ( Exception ex )
{
throw ex ;
}
}
#endregion
#region 获取SEQ_NUM最大号
/// <summary>
/// 获取信息
/// </summary>
/// <param name="">条件</param>
/// <returns>*信息</returns>
public PbomItem GetMaxSeqNum ( PbomItem model )
{
try
{
List < DataParameter > parameters = new List < DataParameter > ( ) ;
var sql = " SELECT max(seq_num) as seq_num FROM T_MD_PBOM_ITEM where PBOM_CODE ='" + model . PBOM_CODE + "' " ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
return session . Get < PbomItem > ( sql , parameters . ToArray ( ) ) ;
}
}
catch ( Exception ex )
{
throw ex ;
}
}
#endregion
#region 插入信息
/// <summary>
/// 插入信息(单表)
/// </summary>
/// <param name="">信息</param>
/// <returns>插入行数</returns>
public int InsertPbomItem ( PbomItem model )
{
int count = 0 ;
try
{
if ( BaseSession ! = null )
{
//插入基本信息
count = BaseSession . Insert < PbomItem > ( model ) ;
}
else
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
//插入基本信息
count = session . Insert < PbomItem > ( model ) ;
}
}
return count ;
}
catch ( Exception ex )
{
RecordExceptionLog ( ex , "Bom信息维护数据层-插入信息" ) ;
throw ex ;
}
}
#endregion
#region 更新信息
/// <summary>
/// 更新信息
/// </summary>
/// <param name="">信息</param>
/// <returns>插入行数</returns>
public int UpdatePbomItem ( PbomItem model )
{
int count = 0 ;
try
{
if ( BaseSession ! = null )
{
//插入基本信息
count = BaseSession . Update < PbomItem > ( model ) ;
}
else
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
//插入基本信息
count = session . Update < PbomItem > ( model ) ;
}
}
return count ;
}
catch ( Exception ex )
{
RecordExceptionLog ( ex , "Bom信息维护数据层-更新信息" ) ;
throw ex ;
}
}
#endregion
#region 删除信息
/// <summary>
/// 删除信息
/// </summary>
/// <param name="">信息</param>
/// <returns>插入行数</returns>
public int DelPbomItem ( string pid )
{
List < DataParameter > parameters = new List < DataParameter > ( ) ;
int count = 0 ;
try
{
var sql = "delete from T_MD_PBOM_ITEM WHERE PID = '" + pid + "'" ;
if ( this . BaseSession ! = null )
{
//删除基本信息
count = BaseSession . ExecuteSql ( sql , parameters . ToArray ( ) ) ;
}
else
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
//删除基本信息
count = session . ExecuteSql ( sql , parameters . ToArray ( ) ) ;
}
}
return count ;
}
catch ( Exception ex )
{
RecordExceptionLog ( ex , "Bom信息维护数据层-删除BOM详细信息" ) ;
throw ex ;
}
}
#endregion
public PbomItem GetUPLevelItem ( string bomcode , string submaterial )
{
StringBuilder sql = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql . Append ( "SELECT * " ) ;
sql . Append ( " FROM T_MD_PBOM_ITEM " ) ;
sql . Append ( " WHERE PBOM_CODE=@bomcode " ) ;
sql . Append ( " AND SEQ_NUM=(SELECT UP_SQ_NUM " ) ;
sql . Append ( " FROM T_MD_PBOM_ITEM AS I " ) ;
sql . Append ( " WHERE I.PBOM_CODE=@bomcode " ) ;
sql . Append ( " AND I.MATERIAL_CODE=@submaterial)" ) ;
parameters . Add ( new DataParameter ( "bomcode" , bomcode ) ) ;
parameters . Add ( new DataParameter ( "submaterial" , submaterial ) ) ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
var bomitem = session . Get < PbomItem > ( sql . ToString ( ) , parameters . ToArray ( ) ) ;
return bomitem ;
}
}
catch ( Exception ex )
{
throw ex ;
}
}
public List < PbomItem > GetWorkingBom ( string WorklocCode , string OrderID )
{
StringBuilder sql = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql . Append ( "SELECT B.*" ) ;
sql . Append ( " ,M.MATERIAL_NAME" ) ;
sql . Append ( " ,M.MATERIAL_TYPE_CODE" ) ;
sql . Append ( " FROM T_MD_PBOM_ITEM AS B" ) ;
sql . Append ( " LEFT JOIN T_MD_MATERIAL AS M" ) ;
sql . Append ( " ON M.MATERIAL_CODE=B.MATERIAL_CODE" ) ;
sql . Append ( " WHERE PBOM_CODE=(SELECT PBOM_CODE " ) ;
sql . Append ( " FROM T_PP_WORKORDER " ) ;
sql . Append ( " WHERE PID=@OrderID ) " ) ;
//sql.Append(" AND STATE='2')");
sql . Append ( " AND GMP=@WorklocCode" ) ;
sql . Append ( " ORDER BY B.MATERIAL_CODE" ) ;
parameters . Add ( new DataParameter ( "WorklocCode" , WorklocCode ) ) ;
parameters . Add ( new DataParameter ( "OrderID" , OrderID ) ) ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
var bomitem = session . GetList < PbomItem > ( sql . ToString ( ) , parameters . ToArray ( ) ) ;
return bomitem . ToList ( ) ;
}
}
catch ( Exception ex )
{
throw ex ;
}
}
public List < PbomItem > GetRelatedItems ( string itemmaterial )
{
StringBuilder sql = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql . Append ( "SELECT * " ) ;
sql . Append ( " FROM T_MD_PBOM_ITEM " ) ;
sql . Append ( " WHERE PBOM_CODE IN (SELECT PBOM_CODE " ) ;
sql . Append ( " FROM T_MD_PBOM_ITEM " ) ;
sql . Append ( " WHERE MATERIAL_CODE=@itemmaterial) " ) ;
parameters . Add ( new DataParameter ( "itemmaterial" , itemmaterial ) ) ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
var bomitem = session . GetList < PbomItem > ( sql . ToString ( ) , parameters . ToArray ( ) ) ;
return bomitem . ToList ( ) ;
}
}
catch ( Exception ex )
{
throw ex ;
}
}
public WorkCell GetGMP ( string materialcode )
{
WorkCell cell = new WorkCell ( ) ;
StringBuilder sql = new StringBuilder ( ) ;
sql . AppendLine ( "SELECT * " ) ;
sql . AppendLine ( " FROM T_MD_WORKCELL " ) ;
sql . AppendLine ( " WHERE WORKCELL_CODE = (SELECT TOP 1 GMP " ) ;
sql . AppendLine ( " FROM T_MD_PBOM_ITEM " ) ;
sql . AppendLine ( " WHERE MATERIAL_CODE=@materialcode)" ) ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
cell = session . Get < WorkCell > ( sql . ToString ( ) , new DataParameter ( "materialcode" , materialcode ) ) ;
}
return cell ;
}
}
}