NTFBLL.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using MOKA_Factory_Tools.DAL;
  2. using MOKA_Factory_Tools.Models;
  3. using SXLibrary;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace MOKA_Factory_Tools.BLL
  10. {
  11. public partial class NTFBLL
  12. {
  13. private static string strLastSN = CommonMethod.ReadProfileString("MOKAFactoryTools", "LastGSN", "");
  14. protected readonly DAL_AMResult dalAMResult = new DAL_AMResult();
  15. protected readonly DAL_AMYields dalAMYields = new DAL_AMYields();
  16. /// <summary>
  17. /// 设置数据抄写结果;
  18. /// </summary>
  19. /// <param name="strLine">线体,UI输入</param>
  20. /// <param name="strStation">默认为:DataBurning</param>
  21. /// <param name="strSN"></param>
  22. /// <param name="bResult">抄写结果:成功或失败</param>
  23. /// <param name="strErrMsg">抄写失败时的描述</param>
  24. /// <returns></returns>
  25. public bool SetDataBuringResult(string strLine, string strStation, string strSN, string strDSN, bool bResult, string strErrMsg = "")
  26. {
  27. MaInfo maInfo;
  28. DateTime dateNow = DateTime.Now;
  29. AMResult aMResult = new AMResult();
  30. // 通过SN获取MES服务器中记录的ODF、Model、Dissemination信息;
  31. if ( CommonMethod.GetMaInfo(strSN, out maInfo, null) )
  32. {
  33. // 测试日期;
  34. string strTestDate = dateNow.ToString("yyyy-MM-dd");
  35. // 测试完成时间;
  36. string strTestTime = dateNow.ToString("yyyy-MM-dd HH:mm:ss");
  37. #region 赋值model
  38. aMResult.TestDate = dateNow.Date;
  39. aMResult.Line = strLine;
  40. aMResult.Station = strStation;
  41. aMResult.DSN = strDSN;
  42. aMResult.ODF = maInfo.ODF;
  43. aMResult.Model = maInfo.Model;
  44. aMResult.Dimension = maInfo.SIZE;
  45. aMResult.TestHour = dateNow.Hour;
  46. aMResult.TestTime = strTestTime;
  47. aMResult.SN = strSN;
  48. #endregion
  49. Log.WriteInfoLog(string.Format("上一次SN={0},当前SN={1}", strLastSN, aMResult.SN));
  50. if (IsRecordExist(aMResult))
  51. {
  52. if ( !UpdateAMResults(aMResult, bResult, strErrMsg) )
  53. {
  54. // 更新失败;
  55. return false;
  56. }
  57. }
  58. else
  59. {
  60. if (!AddAMResult(aMResult, bResult, strErrMsg))
  61. {
  62. // 更新失败;
  63. return false;
  64. }
  65. }
  66. // 更新统计结果;
  67. if (!UpdateAMYields(aMResult, bResult, strErrMsg))
  68. {
  69. return false;
  70. }
  71. strLastSN = strSN;
  72. CommonMethod.WriteProfileString("MOKAFactoryTools", "LastGSN", strSN);
  73. }
  74. return false;
  75. }
  76. /// <summary>
  77. /// 记录是否存在;
  78. /// </summary>
  79. /// <param name="model"></param>
  80. /// <returns></returns>
  81. public bool IsRecordExist(AMResult model)
  82. {
  83. // 如果存在重流的话,可能不会在原来的Line上,可以流到其他Line。
  84. // dalAMResult.Exists(string.Format("Station='{0}' and SN='{1}' and Line = '{2}'", model.Station, model.SN, model.Line));
  85. return dalAMResult.Exists(string.Format("Station='{0}' and SN='{1}'", model.Station, model.SN));
  86. }
  87. /// <summary>
  88. /// 新增记录;
  89. /// </summary>
  90. /// <param name="model"></param>
  91. /// <returns></returns>
  92. public bool AddAMResult(AMResult model, bool TestResult, string strErrMsg)
  93. {
  94. // 赋新值;
  95. model.Test01 = model.FinalTest = TestResult ? "PASS" : "FAIL";
  96. model.Test02 = "NULL";
  97. model.ReDo = "0";
  98. model.ResultType = TestResult ? "OncePass" : "OnceFail";
  99. if (!TestResult)
  100. model.ErrorMsg = strErrMsg;
  101. return dalAMResult.Add(model);
  102. }
  103. public bool UpdateAMResults(AMResult model, bool TestResult, string strErrMsg)
  104. {
  105. // 先获取ReDo="0"的记录;
  106. AMResult redo0Model = dalAMResult.GetModel(string.Format("Station='{0}' and SN='{1}' and ReDo='{2}'", model.Station, model.SN, "0"));
  107. if ( strLastSN == model.SN )
  108. {// 连接抄写数据,更新ReDo="0"的记录;
  109. Log.WriteInfoLog(string.Format("上一次SN={0} 与当前SN={1} 相同,只更新数据", strLastSN, model.SN));
  110. if ( !UpdateReDo0AMResult(redo0Model, TestResult, strErrMsg) )
  111. {
  112. // 更新失败;
  113. Log.WriteInfoLog(string.Format("AMResult更新SN={0},Station={1},TestTime={2},ReDo={3} 失败", redo0Model.SN, redo0Model.Station, redo0Model.TestTime, redo0Model.ReDo));
  114. return false;
  115. }
  116. }
  117. else
  118. {// 将原ReDo="0"更新为ReDo=Max(ReDo)+1,并新增ReDo="0"的记录;
  119. int maxReDo = dalAMResult.GetMaxID("ReDo", string.Format("Station='{0}' and SN='{1}'", model.Station, model.SN));
  120. redo0Model.ReDo = maxReDo.ToString();
  121. if ( !dalAMResult.Update(redo0Model, string.Format("Station='{0}' and SN='{1}' and ReDo='{2}'", redo0Model.Station, redo0Model.SN, "0")) )
  122. {
  123. // 更新失败;
  124. Log.WriteInfoLog(string.Format("AMResult更新SN={0},Station={1},TestTime={2},ReDo={3} 失败", redo0Model.SN, redo0Model.Station, redo0Model.TestTime, redo0Model.ReDo));
  125. return false;
  126. }
  127. // 新增ReDo=0记录;
  128. if ( !AddAMResult(model, TestResult, strErrMsg) )
  129. {
  130. // 添加失败;
  131. Log.WriteInfoLog(string.Format("AMResult新增SN={0},Station={1},TestTime={2},ReDo={3} 失败", model.SN, model.Station, model.TestTime, model.ReDo));
  132. return false;
  133. }
  134. }
  135. return true;
  136. }
  137. public bool UpdateReDo0AMResult(AMResult redo0Model, bool TestResult, string strErrMsg)
  138. {
  139. if (redo0Model.Test01 == "PASS" && redo0Model.Test02 == "NULL" && redo0Model.FinalTest == "PASS")
  140. {// OncePass 的情况;
  141. if (TestResult)
  142. {
  143. redo0Model.Test02 = "PASS";
  144. redo0Model.ResultType = "FinalPass";
  145. }
  146. else
  147. {
  148. redo0Model.Test02 = "FAIL";
  149. redo0Model.FinalTest = "FAIL";
  150. redo0Model.ResultType = "FinalFail";
  151. }
  152. }
  153. else if (redo0Model.Test01 == "FAIL" && redo0Model.Test02 == "NULL" && redo0Model.FinalTest == "FAIL")
  154. {// OnceFail 的情况;
  155. if (TestResult)
  156. {
  157. redo0Model.Test02 = "PASS";
  158. redo0Model.FinalTest = "PASS";
  159. redo0Model.ResultType = "NTF";
  160. }
  161. else
  162. {
  163. redo0Model.Test02 = "FAIL";
  164. redo0Model.FinalTest = "FAIL";
  165. redo0Model.ResultType = "TwiceFail";
  166. }
  167. }
  168. else if (redo0Model.Test01 == "FAIL" && redo0Model.Test02 == "PASS" && (redo0Model.FinalTest == "PASS" || redo0Model.FinalTest == "FAIL"))
  169. {// NTF + FinalFail 的情况;
  170. if (TestResult)
  171. {
  172. redo0Model.FinalTest = "PASS";
  173. redo0Model.ResultType = "NTF";
  174. }
  175. else
  176. {
  177. redo0Model.FinalTest = "FAIL";
  178. redo0Model.ResultType = "FinalFail";
  179. }
  180. }
  181. else
  182. {
  183. if (TestResult)
  184. {
  185. redo0Model.FinalTest = "PASS";
  186. redo0Model.ResultType = "FinalPass";
  187. }
  188. else
  189. {
  190. redo0Model.FinalTest = "FAIL";
  191. redo0Model.ResultType = "FinalFail";
  192. }
  193. }
  194. if (!TestResult)
  195. redo0Model.ErrorMsg = strErrMsg;
  196. return dalAMResult.Update(redo0Model, string.Format("Station='{0}' and SN='{1}' and ReDo='{2}'", redo0Model.Station, redo0Model.SN, redo0Model.ReDo));
  197. }
  198. public bool UpdateAMYields(AMResult model, bool TestResult, string strErrMsg)
  199. {
  200. bool IsNewRecord = false;
  201. // 查询出统计的记录;
  202. AMYields yields = dalAMYields.GetModel(model.Line, model.Station, model.ODF, model.TestDate, model.TestHour);
  203. // 根据当前model所在的Line、Station、ODF、Model、TestDate、TestHour,统计该TestHour内的数据;
  204. List<AMResult> mResults = dalAMResult.GetModelList(model.Line, model.Station, model.ODF, model.TestDate, model.TestHour);
  205. // 查询每小时统计值;
  206. HourlyResultData hourlydata = dalAMResult.GetHourlyResultData(model.Line, model.Station, model.ODF, model.TestDate, model.TestHour);
  207. if ( yields == null || yields.IsValueEmpty() )
  208. {// 添加新记录;
  209. IsNewRecord = true;
  210. if (yields == null)
  211. yields = new AMYields();
  212. yields.TestDate = model.TestDate;
  213. yields.TestHour = model.TestHour;
  214. yields.Line = model.Line;
  215. yields.Station = model.Station;
  216. yields.ODF = model.ODF;
  217. yields.Dimension = model.Dimension;
  218. yields.Model = model.Model;
  219. }
  220. yields.Total = hourlydata.TotalCount;
  221. yields.OncePass = hourlydata.OncePassCount;
  222. yields.TwiceFail = hourlydata.TwiceFailCount;
  223. yields.NTF = hourlydata.NTFCount;
  224. yields.RealFail = hourlydata.FinalFailCount; // hourlydata.RealFailCount;
  225. yields.NTF_SN = hourlydata.NTFSN;
  226. yields.FailDSN = hourlydata.FailDSN;
  227. // 计算百分比;
  228. try
  229. {
  230. int total = int.Parse(yields.Total);
  231. int realtotal = int.Parse(hourlydata.RealTotalCount);
  232. int oncepassCount = int.Parse(yields.OncePass);
  233. int twicefailCount = int.Parse(yields.TwiceFail);
  234. int ntfCount = int.Parse(yields.NTF);
  235. int realfailCount = int.Parse(yields.RealFail);
  236. int realpassCount = int.Parse(hourlydata.RealPassCount);
  237. int finalpassCount = int.Parse(hourlydata.FinalPassCount);
  238. int finalfailCount = int.Parse(hourlydata.FinalFailCount);
  239. // 统计合格率;
  240. yields.FPY = (oncepassCount / (double)total).ToString("P");
  241. yields.SPY = (oncepassCount + ntfCount / (double)total).ToString("P");
  242. yields.RPY = (finalpassCount / (double)total).ToString("P");
  243. yields.YieldRate = (realpassCount / (double)realtotal).ToString("P");
  244. }
  245. catch
  246. {
  247. return false;
  248. }
  249. return IsNewRecord ? dalAMYields.Add(yields) : dalAMYields.Update(yields);
  250. }
  251. }
  252. }