using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using QMAPP.MD.Entity ;
using QMFrameWork.Data ;
using System.Data ;
using QMAPP.DAL ;
using QMAPP.Entity ;
using QMAPP.Entity.Sys ;
using QMAPP.FJC.Entity.BZD ;
using QMAPP.FJC.DAL.BZD ;
namespace QMAPP.MD.DAL
{
/// <summary>
/// 模块名称:BZD记录
/// 作 者:宁红钢
/// 编写日期:2020年04月29日
/// </summary>
public class BZDRecorderDAL : BaseDAL
{
#region 获取信息
/// <summary>
/// 获取信息
/// </summary>
/// <param name="">条件</param>
/// <returns>*信息</returns>
public BZDRecorder2 Get ( BZDRecorder2 info )
{
try
{
using ( IDataSession session = AppDataFactory . CreateSession ( "maindbBZD" ) )
{
//获取信息
info = session . Get < BZDRecorder2 > ( info ) ;
}
return info ;
}
catch ( Exception ex )
{
throw ex ;
}
}
public BZDRecorder2 GetRecord ( BZDRecorder2 info )
{
try
{
List < DataParameter > parameters = new List < DataParameter > ( ) ;
var sql = "SELECT * FROM T_BD_BZDRecorder WHERE 1=1 " ;
if ( string . IsNullOrEmpty ( info . PID ) = = false )
{
sql + = " AND PID = @PID" ;
parameters . Add ( new DataParameter ( "PID" , info . PID ) ) ;
}
if ( string . IsNullOrEmpty ( info . PRODUCTCODE ) = = false )
{
sql + = " AND PRODUCTCODE = @PRODUCTCODE" ;
parameters . Add ( new DataParameter ( "PRODUCTCODE" , info . PRODUCTCODE ) ) ;
}
using ( IDataSession session = AppDataFactory . CreateSession ( "maindbBZD" ) )
{
//获取信息
var list = session . GetList < BZDRecorder2 > ( sql , parameters . ToArray ( ) ) . ToList ( ) ;
if ( list . Count > 0 )
return list [ 0 ] ;
else
return null ;
}
}
catch ( Exception ex )
{
throw ex ;
}
}
public BZDRecorder2 Get316Record ( BZDRecorder2 info )
{
try
{
List < DataParameter > parameters = new List < DataParameter > ( ) ;
var sql = "SELECT MAINCODE as 'BZDCODE',MATERIAL_CODE as 'MPID' FROM T_AW_MAIN WHERE 1=1 " ;
if ( string . IsNullOrEmpty ( info . PID ) = = false )
{
sql + = " AND PID = @PID" ;
parameters . Add ( new DataParameter ( "PID" , info . PID ) ) ;
}
if ( string . IsNullOrEmpty ( info . PRODUCTCODE ) = = false )
{
sql + = " AND EPIDERMISCODE = @PRODUCTCODE" ;
parameters . Add ( new DataParameter ( "PRODUCTCODE" , info . PRODUCTCODE ) ) ;
}
using ( IDataSession session = AppDataFactory . CreateSession ( "maindb" ) )
{
//获取信息
var list = session . GetList < BZDRecorder2 > ( sql , parameters . ToArray ( ) ) . ToList ( ) ;
if ( list . Count > 0 )
return list [ 0 ] ;
else
return null ;
}
}
catch ( Exception ex )
{
throw ex ;
}
}
#endregion
#region 获取列表
/// <summary>
/// 获取列表
/// </summary>
/// <param name="condition">条件</param>
/// <param name="page">数据页</param>
/// <returns>数据页</returns>
public DataPage GetList ( BZDRecorder2 condition , DataPage page )
{
string sql = null ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
sql = this . GetQuerySql2 ( condition , ref parameters ) ;
//分页关键字段及排序
page . KeyName = "PID" ;
if ( string . IsNullOrEmpty ( page . SortExpression ) )
page . SortExpression = "CREATEDATE DESC" ;
using ( IDataSession session = AppDataFactory . CreateSession ( "maindbBZD" ) )
{
// 对应多种数据库
//string sqlChange = this.ChangeSqlByDB(sql, session);
page = session . GetDataPage < BZDRecorder2 > ( sql , parameters . ToArray ( ) , page ) ;
}
return page ;
}
catch ( Exception ex )
{
throw ex ;
}
}
/// <summary>
/// 获取全部规则
/// </summary>
/// <returns></returns>
public List < BZDRecorder2 > GetAllList ( )
{
try
{
string sql = "select R.pid,r.mpid,C.MATERIALCODDE,R.BZDCODE,R.ProductCode,R.Sequence,C.Company_name,C.Line_name,R.CREATEUSER,R.CREATEDATE from T_BD_BZDRecorder R,T_BD_BZDConfig c WHERE r.MPID = c.PID" ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
using ( IDataSession session = AppDataFactory . CreateSession ( "maindbBZD" ) )
{
return session . GetList < BZDRecorder2 > ( sql , parameters . ToArray ( ) ) . ToList ( ) ;
}
}
catch ( Exception ex )
{
throw ex ;
}
}
#endregion
#region 获取查询语句
/// <summary>
/// 获取查询语句
/// </summary>
/// <param name="user">查询条件</param>
/// <param name="parameters">参数</param>
/// <returns>查询语句</returns>
private string GetQuerySql ( BZDRecorder condition , ref List < DataParameter > parameters )
{
StringBuilder sqlBuilder = new StringBuilder ( ) ;
StringBuilder whereBuilder = new StringBuilder ( ) ;
try
{
//构成查询语句//[PID],[MPID],[barcodeFist],[Company_code],[Vehicle_type],[MATERIALCODDE],[configColor],[CREATEUSER],[CREATEDATE],[FLGDEL]
sqlBuilder . Append ( "select * " ) ;
sqlBuilder . Append ( "FROM T_BD_BZDRecorder" ) ;
//whereBuilder.Append(" AND FLGDEL<> '1' ");
//查询条件
//查询条件
//if (string.IsNullOrEmpty(condition.Company_name) == false)
//{
// whereBuilder.Append(" AND C.Company_name = @Company_name ");
// parameters.Add(new DataParameter { ParameterName = "Company_name", DataType = DbType.String, Value = condition.Company_name });
//}
//if (string.IsNullOrEmpty(condition.Line_name) == false)
//{
// whereBuilder.Append(" AND C.Line_name = @Line_name ");
// parameters.Add(new DataParameter { ParameterName = "Line_name", DataType = DbType.String, Value = condition.Line_name });
//}
//if (string.IsNullOrEmpty(condition.Company_code) == false)
//{
// whereBuilder.Append(" AND C.Company_code = @Company_code ");
// parameters.Add(new DataParameter { ParameterName = "Company_code", DataType = DbType.String, Value = condition.Company_code });
//}
//if (string.IsNullOrEmpty(condition.Line_code) == false)
//{
// whereBuilder.Append(" AND C.Line_code = @Line_code ");
// parameters.Add(new DataParameter { ParameterName = "Line_code", DataType = DbType.String, Value = condition.Line_code });
//}
if ( whereBuilder . Length > 0 )
{
sqlBuilder . Append ( " WHERE " + whereBuilder . ToString ( ) . Substring ( 4 ) ) ;
}
return sqlBuilder . ToString ( ) ;
}
catch ( Exception ex )
{
throw ex ;
}
}
/// <summary>
/// 获取查询语句
/// </summary>
/// <param name="user">查询条件</param>
/// <param name="parameters">参数</param>
/// <returns>查询语句</returns>
private string GetQuerySql2 ( BZDRecorder2 condition , ref List < DataParameter > parameters )
{
StringBuilder sqlBuilder = new StringBuilder ( ) ;
StringBuilder whereBuilder = new StringBuilder ( ) ;
try
{
//构成查询语句//[PID],[MPID],[barcodeFist],[Company_code],[Vehicle_type],[MATERIALCODDE],[configColor],[CREATEUSER],[CREATEDATE],[FLGDEL]
sqlBuilder . Append ( "select R.pid as pid,r.mpid as mpid,C.MATERIALCODDE,R.BZDCODE,R.ProductCode,R.Sequence,C.Company_name,C.Line_name,R.CREATEUSER,R.CREATEDATE " ) ;
sqlBuilder . Append ( "FROM T_BD_BZDRecorder R,T_BD_BZDConfig c " ) ;
//whereBuilder.Append(" AND FLGDEL<> '1' ");
whereBuilder . Append ( " AND r.MPID = c.PID " ) ;
//查询条件
//查询条件
if ( string . IsNullOrEmpty ( condition . Company_name ) = = false )
{
whereBuilder . Append ( " AND C.Company_name like @Company_name " ) ;
parameters . Add ( new DataParameter { ParameterName = "Company_name" , DataType = DbType . String , Value = "%" + condition . Company_name + "%" } ) ;
}
if ( string . IsNullOrEmpty ( condition . Line_name ) = = false )
{
whereBuilder . Append ( " AND C.Line_name like @Line_name " ) ;
parameters . Add ( new DataParameter { ParameterName = "Line_name" , DataType = DbType . String , Value = "%" + condition . Line_name + "%" } ) ;
}
if ( string . IsNullOrEmpty ( condition . Company_code ) = = false )
{
whereBuilder . Append ( " AND C.Company_code = @Company_code " ) ;
parameters . Add ( new DataParameter { ParameterName = "Company_code" , DataType = DbType . String , Value = condition . Company_code } ) ;
}
if ( string . IsNullOrEmpty ( condition . Line_code ) = = false )
{
whereBuilder . Append ( " AND C.Line_code = @Line_code " ) ;
parameters . Add ( new DataParameter { ParameterName = "Line_code" , DataType = DbType . String , Value = condition . Line_code } ) ;
}
if ( string . IsNullOrEmpty ( condition . BZDCODE ) = = false )
{
whereBuilder . Append ( " AND R.BZDCODE = @BZDCODE " ) ;
parameters . Add ( new DataParameter { ParameterName = "BZDCODE" , DataType = DbType . String , Value = condition . BZDCODE } ) ;
}
if ( string . IsNullOrEmpty ( condition . PRODUCTCODE ) = = false )
{
whereBuilder . Append ( " AND R.ProductCode = @PRODUCTCODE " ) ;
parameters . Add ( new DataParameter { ParameterName = "PRODUCTCODE" , DataType = DbType . String , Value = condition . PRODUCTCODE } ) ;
}
if ( whereBuilder . Length > 0 )
{
sqlBuilder . Append ( " WHERE " + whereBuilder . ToString ( ) . Substring ( 4 ) ) ;
}
return sqlBuilder . ToString ( ) ;
}
catch ( Exception ex )
{
throw ex ;
}
}
#endregion
//根据总成物料号获取BZD配置信息
public BZDConfig getBZDCofig ( string planMATERIAL_CODE )
{
BZDConfigDAL bzdCofigdal = new BZDConfigDAL ( ) ;
List < BZDConfig > bzdCofig = new List < BZDConfig > ( ) ;
bzdCofig = bzdCofigdal . GetAllList ( ) ;
//将当前总成号配置取出
foreach ( var bzdconfig in bzdCofig )
{
if ( bzdconfig ! = null )
if ( bzdconfig . MATERIALCODDE = = planMATERIAL_CODE )
{
return bzdconfig ;
}
}
return null ;
}
public List < string > CreateBZDCode ( List < string > paramList , string userId )
{
try
{
List < string > list = new List < string > ( ) ;
BZDConfig bzdconfig = getBZDCofig ( paramList [ 0 ] ) ;
;
//将配置信息读出
var barcodeFirst = bzdconfig . barcodeFist ;
var cc = bzdconfig . Company_code ;
var vt = bzdconfig . Vehicle_type ;
var cfgc = bzdconfig . configColor ;
var y = DateTime . Today . Year . ToString ( ) . Substring ( 2 , 2 ) ;
var m = DateTime . Today . Month . ToString ( ) ;
//将配置信息拼接成BZD查询条件,作为查找序列最大编码条件
string BZDCODE = barcodeFirst + ' ' + cc + vt + cfgc
+ DecimalToCharX . decimalToCharX ( Convert . ToInt16 ( y ) , 3 6 )
+ DecimalToCharX . decimalToCharX ( Convert . ToInt16 ( m ) , 3 6 ) ;
int seq = 0 ;
//查询最大编码
seq = getMaxSeq ( BZDCODE ) ;
//最大编码加1
//seq = DecimalToCharX.CharXTodecimal(BZDMaxSeq, 36) + 1;
//准备打印条码数据
BZDRecorder entity = new BZDRecorder ( ) ;
//----------------------------------------------------------------------------------------
BarcodeRule bc = new BarcodeRule ( ) ;
bc . barcodeFirst = barcodeFirst ;
bc . barcodeCompnentCode = cc ;
bc . VehicleType = vt ;
bc . ConfigColor = cfgc ;
bc . Year = DecimalToCharX . decimalToCharX ( Convert . ToInt16 ( y ) , 3 6 ) ;
bc . Mon = DecimalToCharX . decimalToCharX ( Convert . ToInt16 ( m ) , 3 6 ) ;
bc . barcodeSequence = DecimalToCharX . decimalToCharX ( seq , 3 6 ) ;
//----------------------------------------------------------------------------------------
list . Add ( bc . barcode ) ;
list . Add ( bzdconfig . ColorDetail ) ;
list . Add ( bzdconfig . MATERIALCODDE ) ;
//基本信息
entity . PID = Guid . NewGuid ( ) . ToString ( ) ;
entity . CREATEUSER = userId ;
entity . CREATEDATE = DateTime . Now ;
entity . MPID = bzdconfig . PID ;
entity . BZDCODE = bc . barcode ;
entity . Sequence = seq . ToString ( ) ;
entity . PRODUCTCODE = paramList [ 1 ] ;
if ( ! Exists ( entity ) & & ! ExistsPro ( entity ) )
{
//将打印条码记录到数据库
var rst = Insert ( entity ) ;
list . Add ( rst . ToString ( ) ) ;
return list ;
}
else
{
return null ;
}
}
catch ( Exception e )
{
return null ;
}
}
#region 查询最大序列号
public int getMaxSeq ( string BZDCODE )
{
//string PID = "";
int count = 0 ;
string sql = null ;
try
{
//sql = "GetBZDSeq";
sql = @ "DECLARE @return_value int,
@Seq int
EXEC @return_value = [ dbo ] . [ GetBZDSeq ]
@KeyValue = N ' " + BZDCODE + @" ' ,
@Seq = @Seq OUTPUT
SELECT @Seq as N ' @Seq ' ";
List < DataParameter > parameters = new List < DataParameter > ( ) ;
using ( IDataSession session = AppDataFactory . CreateSession ( "maindbBZD" ) )
{
string sqlChange = ChangeSqlByDB ( sql , session ) ;
var dt = session . GetTable ( sqlChange , parameters . ToArray ( ) ) ;
return Convert . ToInt32 ( dt . Rows [ 0 ] . ItemArray [ 0 ] ) ;
}
}
catch ( Exception ex )
{
RecordExceptionLog ( ex , "查询最大序列号错误!" ) ;
throw ex ;
}
}
public string getMaxSeq_old ( string BZDCODE )
{
//string PID = "";
int count = 0 ;
string sql = null ;
try
{
sql = "select ISNULL(MAX(substring(BZDCODE,12,3)),'000') as SEQUENCE from T_BD_BZDRecorder where substring(BZDCODE,1,11) = @BZDCODE" ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
parameters . Add ( new DataParameter { ParameterName = "BZDCODE" , DataType = DbType . String , Value = BZDCODE } ) ;
using ( IDataSession session = AppDataFactory . CreateSession ( "maindbBZD" ) )
{
string sqlChange = ChangeSqlByDB ( sql , session ) ;
var dt = session . GetTable ( sqlChange , parameters . ToArray ( ) ) ;
return dt . Rows [ 0 ] [ "SEQUENCE" ] . ToString ( ) ;
}
}
catch ( Exception ex )
{
RecordExceptionLog ( ex , "查询最大序列号错误!" ) ;
throw ex ;
}
return "1" ;
}
#endregion
#region 获取导出的数据
/// <summary>
/// 获取导出的数据
/// </summary>
/// <param name="user">查询条件</param>
/// <returns>数据</returns>
public DataTable GetExportData ( BZDRecorder2 info )
{
DataTable dt = null ;
string sql = null ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
//构成查询语句
sql = this . GetQuerySql2 ( info , ref parameters ) ;
using ( IDataSession session = AppDataFactory . CreateSession ( "maindbBZD" ) )
{
dt = session . GetTable ( sql , parameters . ToArray ( ) ) ;
dt . TableName = "BZDRecorder" ;
}
return dt ;
}
catch ( Exception ex )
{
throw ex ;
}
}
#endregion
#region 信息是否重复
/// <summary>
/// 判断名称是否存在
/// </summary>
/// <param name="info"></param>
/// <returns>true:已存在;fasel:不存在。</returns>
public bool Exists ( BZDRecorder info )
{
StringBuilder sqlBuilder = new StringBuilder ( ) ;
StringBuilder whereBuilder = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
int count = 0 ;
try
{
sqlBuilder . Append ( "SELECT COUNT(0) FROM T_BD_BZDRecorder" ) ;
if ( info . PID = = null )
{
info . PID = "" ;
}
whereBuilder . Append ( " AND BZDCODE = @BZDCODE " ) ;
parameters . Add ( new DataParameter { ParameterName = "BZDCODE" , DataType = DbType . String , Value = info . BZDCODE } ) ;
//添加进行无重复字段判断代码
if ( whereBuilder . Length > 0 )
{
sqlBuilder . Append ( " WHERE " + whereBuilder . ToString ( ) . Substring ( 4 ) ) ;
}
using ( IDataSession session = AppDataFactory . CreateSession ( "maindbBZD" ) )
{
count = Convert . ToInt32 ( session . ExecuteSqlScalar ( sqlBuilder . ToString ( ) , parameters . ToArray ( ) ) ) ;
}
return count > 0 ;
}
catch ( Exception ex )
{
throw ex ;
}
}
#endregion
#region 信息是否重复
/// <summary>
/// 判断名称是否存在
/// </summary>
/// <param name="info"></param>
/// <returns>true:已存在;fasel:不存在。</returns>
public bool ExistsPro ( BZDRecorder info )
{
StringBuilder sqlBuilder = new StringBuilder ( ) ;
StringBuilder whereBuilder = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
int count = 0 ;
try
{
sqlBuilder . Append ( "SELECT COUNT(0) FROM T_BD_BZDRecorder" ) ;
if ( info . PID = = null )
{
info . PID = "" ;
}
whereBuilder . Append ( " AND ProductCode = @ProductCode " ) ;
parameters . Add ( new DataParameter { ParameterName = "ProductCode" , DataType = DbType . String , Value = info . PRODUCTCODE } ) ;
//添加进行无重复字段判断代码
if ( whereBuilder . Length > 0 )
{
sqlBuilder . Append ( " WHERE " + whereBuilder . ToString ( ) . Substring ( 4 ) ) ;
}
using ( IDataSession session = AppDataFactory . CreateSession ( "maindbBZD" ) )
{
count = Convert . ToInt32 ( session . ExecuteSqlScalar ( sqlBuilder . ToString ( ) , parameters . ToArray ( ) ) ) ;
}
return count > 0 ;
}
catch ( Exception ex )
{
throw ex ;
}
}
#endregion
#region 插入信息
/// <summary>
/// 插入信息(单表)
/// </summary>
/// <param name="">信息</param>
/// <returns>插入行数</returns>
public int Insert ( BZDRecorder info )
{
int count = 0 ;
try
{
using ( IDataSession session = AppDataFactory . CreateSession ( "maindbBZD" ) )
{
//插入基本信息
count = session . Insert < BZDRecorder > ( info ) ;
}
return count ;
}
catch ( Exception ex )
{
throw ex ;
}
}
#endregion
#region 更新信息
/// <summary>
/// 更新信息
/// </summary>
/// <param name=""></param>
/// <returns>更新行数</returns>
public int Update ( BZDRecorder info )
{
int count = 0 ;
try
{
using ( IDataSession session = AppDataFactory . CreateSession ( "maindbBZD" ) )
{
//更新基本信息
count = session . Update < BZDRecorder > ( info ) ;
}
return count ;
}
catch ( Exception ex )
{
throw ex ;
}
}
#endregion
#region 逻辑删除
/// <summary>
/// 逻辑删除信息
/// </summary>
/// <param name=""></param>
/// <returns>删除个数</returns>
public int Delete ( BZDRecorder info )
{
StringBuilder sqlBuilder = new StringBuilder ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
int count = 0 ;
try
{
using ( IDataSession session = AppDataFactory . CreateSession ( "maindbBZD" ) )
{
//删除基本信息
sqlBuilder . Append ( "UPDATE T_BD_BZDRecorder " ) ;
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 ;
}
}
#endregion
#region 导入
public ImportMessage GetImportData ( List < BZDRecorder > list )
{
ImportMessage em = new ImportMessage ( ) ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
using ( IDataSession session = AppDataFactory . CreateSession ( "maindbBZD" ) )
{
//设置祖先对象数据会话
session . OpenTs ( ) ;
foreach ( BZDRecorder info in list )
{
if ( info . IsNewInfo )
{
//插入信息
int count = session . Insert < BZDRecorder > ( info ) ;
em . insertNum + + ;
}
else
{
//更新信息
int count = session . Update < BZDRecorder > ( info ) ;
em . updateNum + + ;
}
}
session . CommitTs ( ) ;
}
}
catch ( Exception ex )
{
throw ex ;
}
return em ;
}
#endregion
}
}