using MOKA_Factory_Tools.DAL;
using MOKA_Factory_Tools.Models;
using SXLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MOKA_Factory_Tools.BLL
{
public partial class NTFBLL
{
private static string strLastSN = CommonMethod.ReadProfileString("MOKAFactoryTools", "LastGSN", "");
protected readonly DAL_AMResult dalAMResult = new DAL_AMResult();
protected readonly DAL_AMYields dalAMYields = new DAL_AMYields();
///
/// 设置数据抄写结果;
///
/// 线体,UI输入
/// 默认为:DataBurning
///
/// 抄写结果:成功或失败
/// 抄写失败时的描述
///
public bool SetDataBuringResult(string strLine, string strStation, string strSN, string strDSN, bool bResult, string strErrMsg = "")
{
MaInfo maInfo;
DateTime dateNow = DateTime.Now;
AMResult aMResult = new AMResult();
// 通过SN获取MES服务器中记录的ODF、Model、Dissemination信息;
if ( CommonMethod.GetMaInfo(strSN, out maInfo, null) )
{
// 测试日期;
string strTestDate = dateNow.ToString("yyyy-MM-dd");
// 测试完成时间;
string strTestTime = dateNow.ToString("yyyy-MM-dd HH:mm:ss");
#region 赋值model
aMResult.TestDate = dateNow.Date;
aMResult.Line = strLine;
aMResult.Station = strStation;
aMResult.DSN = strDSN;
aMResult.ODF = maInfo.ODF;
aMResult.Model = maInfo.Model;
aMResult.Dimension = maInfo.SIZE;
aMResult.TestHour = dateNow.Hour;
aMResult.TestTime = strTestTime;
aMResult.SN = strSN;
#endregion
Log.WriteInfoLog(string.Format("上一次SN={0},当前SN={1}", strLastSN, aMResult.SN));
if (IsRecordExist(aMResult))
{
if ( !UpdateAMResults(aMResult, bResult, strErrMsg) )
{
// 更新失败;
return false;
}
}
else
{
if (!AddAMResult(aMResult, bResult, strErrMsg))
{
// 更新失败;
return false;
}
}
// 更新统计结果;
if (!UpdateAMYields(aMResult, bResult, strErrMsg))
{
return false;
}
strLastSN = strSN;
CommonMethod.WriteProfileString("MOKAFactoryTools", "LastGSN", strSN);
}
return false;
}
///
/// 记录是否存在;
///
///
///
public bool IsRecordExist(AMResult model)
{
// 如果存在重流的话,可能不会在原来的Line上,可以流到其他Line。
// dalAMResult.Exists(string.Format("Station='{0}' and SN='{1}' and Line = '{2}'", model.Station, model.SN, model.Line));
return dalAMResult.Exists(string.Format("Station='{0}' and SN='{1}'", model.Station, model.SN));
}
///
/// 新增记录;
///
///
///
public bool AddAMResult(AMResult model, bool TestResult, string strErrMsg)
{
// 赋新值;
model.Test01 = model.FinalTest = TestResult ? "PASS" : "FAIL";
model.Test02 = "NULL";
model.ReDo = "0";
model.ResultType = TestResult ? "OncePass" : "OnceFail";
if (!TestResult)
model.ErrorMsg = strErrMsg;
return dalAMResult.Add(model);
}
public bool UpdateAMResults(AMResult model, bool TestResult, string strErrMsg)
{
// 先获取ReDo="0"的记录;
AMResult redo0Model = dalAMResult.GetModel(string.Format("Station='{0}' and SN='{1}' and ReDo='{2}'", model.Station, model.SN, "0"));
if ( strLastSN == model.SN )
{// 连接抄写数据,更新ReDo="0"的记录;
Log.WriteInfoLog(string.Format("上一次SN={0} 与当前SN={1} 相同,只更新数据", strLastSN, model.SN));
if ( !UpdateReDo0AMResult(redo0Model, TestResult, strErrMsg) )
{
// 更新失败;
Log.WriteInfoLog(string.Format("AMResult更新SN={0},Station={1},TestTime={2},ReDo={3} 失败", redo0Model.SN, redo0Model.Station, redo0Model.TestTime, redo0Model.ReDo));
return false;
}
}
else
{// 将原ReDo="0"更新为ReDo=Max(ReDo)+1,并新增ReDo="0"的记录;
int maxReDo = dalAMResult.GetMaxID("ReDo", string.Format("Station='{0}' and SN='{1}'", model.Station, model.SN));
redo0Model.ReDo = maxReDo.ToString();
if ( !dalAMResult.Update(redo0Model, string.Format("Station='{0}' and SN='{1}' and ReDo='{2}'", redo0Model.Station, redo0Model.SN, "0")) )
{
// 更新失败;
Log.WriteInfoLog(string.Format("AMResult更新SN={0},Station={1},TestTime={2},ReDo={3} 失败", redo0Model.SN, redo0Model.Station, redo0Model.TestTime, redo0Model.ReDo));
return false;
}
// 新增ReDo=0记录;
if ( !AddAMResult(model, TestResult, strErrMsg) )
{
// 添加失败;
Log.WriteInfoLog(string.Format("AMResult新增SN={0},Station={1},TestTime={2},ReDo={3} 失败", model.SN, model.Station, model.TestTime, model.ReDo));
return false;
}
}
return true;
}
public bool UpdateReDo0AMResult(AMResult redo0Model, bool TestResult, string strErrMsg)
{
if (redo0Model.Test01 == "PASS" && redo0Model.Test02 == "NULL" && redo0Model.FinalTest == "PASS")
{// OncePass 的情况;
if (TestResult)
{
redo0Model.Test02 = "PASS";
redo0Model.ResultType = "FinalPass";
}
else
{
redo0Model.Test02 = "FAIL";
redo0Model.FinalTest = "FAIL";
redo0Model.ResultType = "FinalFail";
}
}
else if (redo0Model.Test01 == "FAIL" && redo0Model.Test02 == "NULL" && redo0Model.FinalTest == "FAIL")
{// OnceFail 的情况;
if (TestResult)
{
redo0Model.Test02 = "PASS";
redo0Model.FinalTest = "PASS";
redo0Model.ResultType = "NTF";
}
else
{
redo0Model.Test02 = "FAIL";
redo0Model.FinalTest = "FAIL";
redo0Model.ResultType = "TwiceFail";
}
}
else if (redo0Model.Test01 == "FAIL" && redo0Model.Test02 == "PASS" && (redo0Model.FinalTest == "PASS" || redo0Model.FinalTest == "FAIL"))
{// NTF + FinalFail 的情况;
if (TestResult)
{
redo0Model.FinalTest = "PASS";
redo0Model.ResultType = "NTF";
}
else
{
redo0Model.FinalTest = "FAIL";
redo0Model.ResultType = "FinalFail";
}
}
else
{
if (TestResult)
{
redo0Model.FinalTest = "PASS";
redo0Model.ResultType = "FinalPass";
}
else
{
redo0Model.FinalTest = "FAIL";
redo0Model.ResultType = "FinalFail";
}
}
if (!TestResult)
redo0Model.ErrorMsg = strErrMsg;
return dalAMResult.Update(redo0Model, string.Format("Station='{0}' and SN='{1}' and ReDo='{2}'", redo0Model.Station, redo0Model.SN, redo0Model.ReDo));
}
public bool UpdateAMYields(AMResult model, bool TestResult, string strErrMsg)
{
bool IsNewRecord = false;
// 查询出统计的记录;
AMYields yields = dalAMYields.GetModel(model.Line, model.Station, model.ODF, model.TestDate, model.TestHour);
// 根据当前model所在的Line、Station、ODF、Model、TestDate、TestHour,统计该TestHour内的数据;
List mResults = dalAMResult.GetModelList(model.Line, model.Station, model.ODF, model.TestDate, model.TestHour);
// 查询每小时统计值;
HourlyResultData hourlydata = dalAMResult.GetHourlyResultData(model.Line, model.Station, model.ODF, model.TestDate, model.TestHour);
if ( yields == null || yields.IsValueEmpty() )
{// 添加新记录;
IsNewRecord = true;
if (yields == null)
yields = new AMYields();
yields.TestDate = model.TestDate;
yields.TestHour = model.TestHour;
yields.Line = model.Line;
yields.Station = model.Station;
yields.ODF = model.ODF;
yields.Dimension = model.Dimension;
yields.Model = model.Model;
}
yields.Total = hourlydata.TotalCount;
yields.OncePass = hourlydata.OncePassCount;
yields.TwiceFail = hourlydata.TwiceFailCount;
yields.NTF = hourlydata.NTFCount;
yields.RealFail = hourlydata.FinalFailCount; // hourlydata.RealFailCount;
yields.NTF_SN = hourlydata.NTFSN;
yields.FailDSN = hourlydata.FailDSN;
// 计算百分比;
try
{
int total = int.Parse(yields.Total);
int realtotal = int.Parse(hourlydata.RealTotalCount);
int oncepassCount = int.Parse(yields.OncePass);
int twicefailCount = int.Parse(yields.TwiceFail);
int ntfCount = int.Parse(yields.NTF);
int realfailCount = int.Parse(yields.RealFail);
int realpassCount = int.Parse(hourlydata.RealPassCount);
int finalpassCount = int.Parse(hourlydata.FinalPassCount);
int finalfailCount = int.Parse(hourlydata.FinalFailCount);
// 统计合格率;
yields.FPY = (oncepassCount / (double)total).ToString("P");
yields.SPY = (oncepassCount + ntfCount / (double)total).ToString("P");
yields.RPY = (finalpassCount / (double)total).ToString("P");
yields.YieldRate = (realpassCount / (double)realtotal).ToString("P");
}
catch
{
return false;
}
return IsNewRecord ? dalAMYields.Add(yields) : dalAMYields.Update(yields);
}
}
}