using MOKA_Factory_Tools.Database;
using MOKA_Factory_Tools.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MOKA_Factory_Tools.DAL
{
public class DAL_AMResult : BaseDAL
{
public DAL_AMResult()
{
this.TableFieldNameString = "";
}
#region 属性和字段
string _tableName = "AMResult";
///
/// 获取数据表名
///
public override string TableName
{
get { return _tableName; }
set { this._tableName = value; }
}
///
/// 获取当前新的数据表模型对象
///
public override object ObjModel
{
get
{
return this.CurrentModel;
}
}
///
/// 获取当前新的MOdel
///
public AMResult CurrentModel
{
get { return new AMResult(); }
}
string _tableFieldNameString = "";
///
/// 数据表字段名数组
///
public override string TableFieldNameString
{
get { return this._tableFieldNameString; }
set { this._tableFieldNameString = value; }
}
#endregion
#region 检查记录
//基类已经实现
#endregion
#region 增加数据
///
/// 增加一条数据
///
/// Model对象
///
public bool Add(AMResult model)
{
return base.Add(model);
}
///
/// 增加一条数据
///
/// Model对象
/// 忽略字段名列表 字段名之间用“,”号分隔
///
public bool Add(AMResult model, string overlookFieldList = "ID")
{
return base.Add(model, overlookFieldList);
}
#endregion
#region 删除数据
///
/// 删除数据
///
///
///
public bool Delete(AMResult model)
{
return base.Delete(string.Format("Station = '{0}' and SN = '{1}' and ReDo = '{2}'", model.Station, model.SN, model.ReDo));
}
#endregion
#region 更新数据
///
/// 更新一条数据
///
public bool Update(AMResult model)
{
return base.Update(model);
}
///
/// 根据筛选字段和SQL筛选运算符号更新数据
///
/// Model对象
/// 筛选字段名称
/// SQL筛选运算符号
/// 忽略字段名列表 字段名之间用“,”号分隔
///
public bool Update(AMResult model, string strWhere, string IgnoreUpdateFields = "Station,SN,ODF,DSN,Line,Model,Dimension")
{
return base.UpdateAMResult(model, strWhere, IgnoreUpdateFields);
}
#endregion
#region 查询数据
///
///
///
///
///
public AMResult GetModel(string strWhere)
{
return DataRowToModel(GetDataRow(strWhere));
}
///
/// 得到一个对象实体
///
public AMResult GetModel(string strStation, string strSN)
{
return DataRowToModel(GetDataRow(string.Format("Station = '{0}' and SN = '{1}'", strStation, strSN)));
}
public HourlyResultData GetHourlyResultData(string Line, string Station, string ODF, DateTime TestDate, int TestHour)
{
DataRow row = GetHourlyDataRow(string.Format("Line='{0}' and Station='{1}' and ODF='{2}' and TestDate='{3}' and TestHour='{4}'", Line, Station, ODF, TestDate, TestHour));
if ( row != null )
{
HourlyResultData model = new HourlyResultData();
model.TotalCount = row["Total"].ToString();
model.RealTotalCount = row["RealTotal"].ToString();
model.OncePassCount = row["OncePass"].ToString();
model.OnceFailCount = row["OnceFail"].ToString();
model.NTFCount = row["NTF"].ToString();
model.TwiceFailCount = row["TwiceFail"].ToString();
model.FinalFailCount = row["FinalFail"].ToString();
model.FinalPassCount = row["FinalPass"].ToString();
model.RealFailCount = row["RealFail"].ToString();
model.RealPassCount = row["RealPass"].ToString();
model.NTFSN = row["NTFSN"].ToString();
model.FailDSN = row["FailDSN"].ToString();
return model;
}
return null;
}
public DataRow GetHourlyDataRow(string whereString)
{
if (whereString.Trim().Length > 0)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select ");
strSql.Append("count(1) as 'Total',");
strSql.Append("count(case when ReDo = '0' then 1 else null end) as 'RealTotal',");
strSql.Append("count(case when ResultType = 'OncePass' then 1 else null end) as 'OncePass',");
strSql.Append("count(case when ResultType = 'OnceFail' then 1 else null end) as 'OnceFail',");
strSql.Append("count(case when ResultType = 'NTF' then 1 else null end) as 'NTF',");
strSql.Append("count(case when ResultType = 'TwiceFail' then 1 else null end) as 'TwiceFail',");
strSql.Append("count(case when ResultType = 'FinalFail' then 1 else null end) as 'FinalFail',");
strSql.Append("count(case when ResultType = 'FinalPass' then 1 else null end) as 'FinalPass',");
strSql.Append("count(case when FinalTest = 'PASS' and ReDo = '0' then 1 else null end) as 'RealPass',");
strSql.Append("count(case when FinalTest = 'FAIL' and ReDo = '0' then 1 else null end) as 'RealFail',");
strSql.Append(string.Format("stuff((select ',' + SN from {0} where {1} and ResultType='NTF' for XML PATH('')),1,1,'') as 'NTFSN',", this.TableName, whereString));
strSql.Append(string.Format("stuff((select ',' + DSN from {0} where {1} and FinalTest='FAIL' for XML PATH('')),1,1,'') as 'FailDSN' ", this.TableName, whereString));
strSql.Append("from " + this.TableName);
strSql.Append(" where " + whereString);
DataSet ds = DbHelper.Query(cps.ConnectionString, strSql.ToString());
if (ds.Tables[0].Rows.Count > 0)
{
return ds.Tables[0].Rows[0];
}
}
return null;
}
///
/// 得到一个对象实体
///
///
///
public AMResult DataRowToModel(DataRow row)
{
return DataRowToModelObject(row) as AMResult;
}
///
/// 获得数据Model列表
///
/// 条件 不包含 where 关键字
public List GetModelList(string strWhere)
{
DataSet ds = base.GetList(strWhere);
return DataTableToList(ds.Tables[0]);
}
///
///
///
///
///
///
///
///
///
public List GetModelList(string strLine, string strStation, string ODF, DateTime TestDate, int TestHour)
{
DataSet ds = base.GetList(string.Format("Line = '{0}' and Station = '{1}' and ODF='{2}' and TestDate='{3}' and TestHour='{4}'", strLine, strStation, ODF, TestDate, TestHour));
return DataTableToList(ds.Tables[0]);
}
///
/// 获得数据列表
///
/// DataTable
public List DataTableToList(DataTable dt)
{
List modelList = new List();
List