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>
/// 模块名称:条码补打记录
/// 作 者:张松男
/// 编写日期:2021年05月21日
/// </summary>
public class BarCodeReplacementDAL : BaseDAL
{
#region 获取信息
/// <summary>
/// 获取信息
/// </summary>
/// <param name="">条件</param>
/// <returns>*信息</returns>
public BarCodeReplacement Get ( BarCodeReplacement info )
{
try
{
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
//获取信息
info = session . Get < BarCodeReplacement > ( info ) ;
}
return info ;
}
catch ( Exception ex )
{
throw ex ;
}
}
public BarCodeReplacement GetRecord ( BarCodeReplacement info )
{
try
{
List < DataParameter > parameters = new List < DataParameter > ( ) ;
var sql = "SELECT * FROM T_AW_BarCode_Replacement WHERE 1=1 " ;
if ( string . IsNullOrEmpty ( info . PID ) = = false )
{
sql + = " AND PID = @PID" ;
parameters . Add ( new DataParameter ( "PID" , info . PID ) ) ;
}
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
//获取信息
var list = session . GetList < BarCodeReplacement > ( 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 ( BarCodeReplacement 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 = "CreateTime DESC" ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
// 对应多种数据库
//string sqlChange = this.ChangeSqlByDB(sql, session);
page = session . GetDataPage < BarCodeReplacement > ( sql , parameters . ToArray ( ) , page ) ;
}
return page ;
}
catch ( Exception ex )
{
throw ex ;
}
}
#endregion
#region 获取查询语句
/// <summary>
/// 获取查询语句
/// </summary>
/// <param name="user">查询条件</param>
/// <param name="parameters">参数</param>
/// <returns>查询语句</returns>
private string GetQuerySql2 ( BarCodeReplacement condition , ref List < DataParameter > parameters )
{
StringBuilder sqlBuilder = new StringBuilder ( ) ;
StringBuilder whereBuilder = new StringBuilder ( ) ;
try
{
if ( condition . BeginTime = = Convert . ToDateTime ( "0001/1/1 00:00:00" ) )
condition . BeginTime = DateTime . Now . AddDays ( - 1 ) ;
if ( condition . EndTime = = Convert . ToDateTime ( "0001/1/1 00:00:00" ) )
condition . EndTime = DateTime . Now ;
//构成查询语句
sqlBuilder . Append ( "select b.PID,b.Type,b.ProductCode,m.MAINCODE as 'BZDBarcode',m.MATERIAL_CODE as 'BZDMaterial_Code',t.MATERIAL_NAME as 'BZDMaterial_CodeName',b.CreateTime,b.CreateUser " ) ;
sqlBuilder . Append ( "from T_AW_BarCode_Replacement as b left join T_AW_MAIN as m on b.ProductCode = m.EPIDERMISCODE LEFT JOIN T_MD_MATERIAL as t on m.MATERIAL_CODE = t.MATERIAL_CODE " ) ;
//查询条件
//查询条件
if ( string . IsNullOrEmpty ( condition . ProductCode ) = = false )
{
whereBuilder . Append ( " AND b.ProductCode like @ProductCode " ) ;
parameters . Add ( new DataParameter { ParameterName = "ProductCode" , DataType = DbType . String , Value = "%" + condition . ProductCode + "%" } ) ;
}
//查询条件
if ( string . IsNullOrEmpty ( condition . Type ) = = false )
{
whereBuilder . Append ( " AND b.Type like @Type " ) ;
parameters . Add ( new DataParameter { ParameterName = "Type" , DataType = DbType . String , Value = "%" + condition . Type + "%" } ) ;
}
whereBuilder . Append ( $" AND b.CreateTime >= '{condition.BeginTime}' and b.CreateTime <= '{condition.EndTime}' " ) ;
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="">信息</param>
/// <returns>插入行数</returns>
public int Insert ( BarCodeReplacement info )
{
int count = 0 ;
List < DataParameter > parameters = new List < DataParameter > ( ) ;
try
{
var sql = $" insert into T_AW_BarCode_Replacement (PID,Type,ProductCode,CreateTime,CreateUser)" +
$"VALUES('{info.PID}','{info.Type}','{info.ProductCode}','{DateTime.Now}','{info.CreateUser}')" ;
using ( IDataSession session = AppDataFactory . CreateMainSession ( ) )
{
//插入基本信息
count = session . ExecuteSql ( sql , parameters . ToArray ( ) ) ;
}
return count ;
}
catch ( Exception ex )
{
throw ex ;
}
}
#endregion
}
}