schangxiang@126.com
2024-08-29 f3ac4e7983dd475d800acc31cbbb8bbaf2e6c638
DataCapture/iWare_SCADA_DataCapture/iWare_SCADA_BusinessLogical/BLL/WorkPieceInfoManager.cs
@@ -1,4 +1,5 @@
using iWare_SCADA_BusinessLogical.Utils;
using HslCommunication.BasicFramework;
using iWare_SCADA_BusinessLogical.Utils;
using iWare_SCADA_Model;
using iWare_SCADA_Model.MiddleModel;
using iWare_SCADA_Model.TableModel;
@@ -7,6 +8,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.Validation;
using System.Diagnostics;
//using System.Data.Entity.Infrastructure;
using System.Linq;
@@ -237,6 +239,7 @@
                            //每次扫描上线都插入追溯表
                            WorkPieceProcess new_process = new WorkPieceProcess();
                            new_process = EntityPropHelper.Mapper<WorkPieceProcess, WorkPieceInfo>(info);
                            QualityStateHelper.ResetQualityNoOkForNewProcess(ref new_process);
                            new_process.StartTime = DateTimeHelper.GetDateTime();
                            //不再默认赋值为合格 【Editby shaocx,2024-08-16】
@@ -371,12 +374,38 @@
                catch (Exception e)
                {
                    loginfo.Remarks = (loginfo.Remarks ?? "") + $"读取二维码{loginfo.WorkPieceID ?? "空"}更新数据异常{e.Message}";
                    Log4NetHelper.WriteErrorLog(type, $" {loginfo.WorkingProcedure}上线监控读码标记 读取工件码数据[{loginfo.WorkPieceID ?? "空"}]时异常:", e);
                }
                finally
                {
                    loginfo.Id = Yitter.IdGenerator.YitIdHelper.NextId();
                    db.WorkPieceLog.Add(loginfo);
                    db.SaveChanges();
                    //保存数据库的异常捕捉 [Editby shaocx,2024-08-29]
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (DbEntityValidationException exception)
                    {
                        var errorMessages =
                            exception.EntityValidationErrors
                                .SelectMany(validationResult => validationResult.ValidationErrors)
                                .Select(m => m.ErrorMessage);
                        var fullErrorMessage = string.Join(", ", errorMessages);
                        var exceptionMessage = string.Concat(exception.Message, " 验证异常消息是:", fullErrorMessage);
                        Log4NetHelper.WriteErrorLog(type, $" {loginfo.WorkingProcedure}上线监控读码标记 读取工件码数据[{loginfo.WorkPieceID ?? "空"}],保存数据库时异常:" + exceptionMessage, exception);
                        throw new DbEntityValidationException(exceptionMessage, exception.EntityValidationErrors);
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
@@ -734,10 +763,17 @@
                        var pro = db.WorkPieceProcess.Where(o => o.WorkPieceID == logMiddle.WorkPieceID && o.WorkingProcedureCurrent == pieceInfo.WorkingProcedureCurrent && o.OperationType == OperationType.生产.ToString()).OrderByDescending(o => o.StartTime).FirstOrDefault();
                        if (pro == null || pro.WorkPieceID.Length < 1 || logMiddle.MonitoringPoint.Equals("OP1002") || logMiddle.MonitoringPoint.Equals("OP1003") || logMiddle.MonitoringPoint.Contains("OP2002"))
                        {// OP10 没有上线扫码枪,约定用测量完成信号当上线标记,所以上线需要修改的字段放这里
                            //上线是每次都插入
                         //上线是每次都插入
                            WorkPieceProcess process_05 = WorkPieceProcessHelper.CreateWorkPieceProcessForOP05(logMiddle, db, pieceInfo);
                            if (process_05 != null)
                            {//创建OP05工序,当没有05工序时,才创建 【Editby shaocx,2024-08-29】
                                db.WorkPieceProcess.Add(process_05);
                            }
                            WorkPieceProcess process = new WorkPieceProcess();
                            process = EntityPropHelper.Mapper<WorkPieceProcess, WorkPieceInfo>(pieceInfo);
                            QualityStateHelper.ResetQualityNoOkForNewProcess(ref process);
                            process.StartTime = DateTimeHelper.GetDateTime();
                            //不再默认赋值为合格 【Editby shaocx,2024-08-16】
@@ -916,9 +952,9 @@
                {
                    if (loginfo.WorkPieceID != null && loginfo.WorkPieceID.Trim('\0').Length == 22)
                    {
                        WorkPieceInfo info = new WorkPieceInfo();
                        info = db.WorkPieceInfo.Where(o => o.WorkPieceID == loginfo.WorkPieceID).FirstOrDefault();
                        if (info == null || info.WorkPieceID.Length < 1)
                        WorkPieceInfo cur_db_info = new WorkPieceInfo();
                        cur_db_info = db.WorkPieceInfo.Where(o => o.WorkPieceID == loginfo.WorkPieceID).FirstOrDefault();
                        if (cur_db_info == null || cur_db_info.WorkPieceID.Length < 1)
                        {//没有工件信息,不做更新
                            Log4NetHelper.WriteErrorLog(type, $"工件{loginfo.WorkPieceID} 工件推出工序{loginfo.WorkingProcedure} 没有获取到工件信息不做更新");
                            return;
@@ -927,43 +963,58 @@
                        {//更新表WorkPieceInfo
                            if (loginfo.WorkingProcedure.Equals("OP10"))
                            {
                                /*
                                info.WorkPieceCurrentPosition = info.WorkingProcedureCurrent + "NOOK";
                                //var WorkingProcedure = (loginfo.MonitoringPoint.Equals("OP1002") ? "OP10" : "OP40");
                                loginfo2.WorkingProcedure = info.WorkingProcedureCurrent;//解决OP10,OP40同一个的问题
                                loginfo.WorkingProcedure = info.WorkingProcedureCurrent; //解决OP10,OP40同一个的问题
                                //*/
                                //修复 错误记录成OP05的问题 【Editby shaocx,2024-08-26】
                                var set_WorkingProcedureCurrent = "";
                                if (cur_db_info.WorkingProcedureCurrent != "OP10" && cur_db_info.WorkingProcedureCurrent != "OP40")
                                {
                                    SetWorkingProcedureCurrentForOP10(cur_db_info, ref set_WorkingProcedureCurrent);
                                }
                                else
                                {
                                    set_WorkingProcedureCurrent = cur_db_info.WorkingProcedureCurrent;
                                }
                                cur_db_info.WorkingProcedureCurrent = set_WorkingProcedureCurrent;
                                cur_db_info.WorkPieceCurrentPosition = set_WorkingProcedureCurrent + "NOOK";
                                loginfo2.WorkingProcedure = set_WorkingProcedureCurrent;//解决OP10,OP40同一个的问题
                                loginfo.WorkingProcedure = set_WorkingProcedureCurrent; //解决OP10,OP40同一个的问题
                            }
                            else
                            {
                                info.WorkingProcedureCurrent = loginfo.WorkingProcedure;
                                info.WorkPieceCurrentPosition = loginfo.WorkingProcedure + "NOOK";
                                cur_db_info.WorkingProcedureCurrent = loginfo.WorkingProcedure;
                                cur_db_info.WorkPieceCurrentPosition = loginfo.WorkingProcedure + "NOOK";
                            }
                            info.WorkPieceState = (int)WorkPieceState.NOOKPush;
                            info.UpdatedUserName = loginfo.MonitoringPoint;
                            info.UpdatedTime = DateTime.Now;
                            info.Remarks = "工件NOOK推出";
                            if (!(info.WorkingProcedureStartTime.HasValue && info.WorkingProcedureStartTime > DateTime.Parse("1900/01/01")))
                            cur_db_info.WorkPieceState = (int)WorkPieceState.NOOKPush;
                            cur_db_info.UpdatedUserName = loginfo.MonitoringPoint;
                            cur_db_info.UpdatedTime = DateTime.Now;
                            cur_db_info.Remarks = "工件NOOK推出";
                            if (!(cur_db_info.WorkingProcedureStartTime.HasValue && cur_db_info.WorkingProcedureStartTime > DateTime.Parse("1900/01/01")))
                            {
                                info.WorkingProcedureStartTime = DateTimeHelper.GetDateTime();
                                cur_db_info.WorkingProcedureStartTime = DateTimeHelper.GetDateTime();
                            }
                            info.WorkingProcedureEndTime = DateTimeHelper.GetDateTime();
                            info.WorkingProcedureCompleted = info.WorkingProcedureCompleted + loginfo.WorkingProcedure;//已完成工序
                            info.WorkPieceCurrentPositionOrder = info.WorkingProcedurePlan.IndexOf(loginfo.WorkingProcedure) / 2;
                            cur_db_info.WorkingProcedureEndTime = DateTimeHelper.GetDateTime();
                            cur_db_info.WorkingProcedureCompleted = cur_db_info.WorkingProcedureCompleted + loginfo.WorkingProcedure;//已完成工序
                            cur_db_info.WorkPieceCurrentPositionOrder = cur_db_info.WorkingProcedurePlan.IndexOf(loginfo.WorkingProcedure) / 2;
                        }
                        QualityDataInfo qualityData = null;
                        qualityData = db.QualityDataInfo.Where(o => o.WorkPieceID == loginfo.WorkPieceID).FirstOrDefault();
                        if (loginfo.WorkingProcedure.Equals("OP35"))
                        {//为了解决OP35的采集到的质量信息是OK还NOOK的情况,OP35 NOOK时更新质量信息
                            loginfo2.Remarks = (loginfo2.Remarks ?? "") + $"OP35NOOK原质量信息{(info.QualityState.HasValue ? info.QualityState.Value.ToString() : "空")}";
                            info.QualityState = (int)QualityState.NG;
                            if (!info.QualityState.Equals(((int)QualityState.OK).ToString()))
                            loginfo2.Remarks = (loginfo2.Remarks ?? "") + $"OP35NOOK原质量信息{(cur_db_info.QualityState.HasValue ? cur_db_info.QualityState.Value.ToString() : "空")}";
                            cur_db_info.QualityState = (int)QualityState.NG;
                            if (!cur_db_info.QualityState.Equals(((int)QualityState.OK).ToString()))
                            {
                                info.QualityErrorInfo = $"{loginfo.WorkingProcedure}工序质量NOOK采集数据不合格{info.QualityState}";
                                cur_db_info.QualityErrorInfo = $"{loginfo.WorkingProcedure}工序质量NOOK采集数据不合格{cur_db_info.QualityState}";
                            }
                            else
                            {
                                info.QualityErrorInfo = "";
                                cur_db_info.QualityErrorInfo = "";
                            }
                            loginfo.QualityState = info.QualityState;
                            loginfo.QualityState = cur_db_info.QualityState;
                            if (qualityData != null)
@@ -983,9 +1034,9 @@
                            pro.UpdatedTime = DateTimeHelper.GetDateTime();
                        }
                        QualityStateHelper.SetQualityStateForNoOk(ref info, ref pro, ref qualityData);
                        QualityStateHelper.SetQualityStateForNoOk(ref cur_db_info, ref pro, ref qualityData);
                        db.WorkPieceInfoLog.Add(GetAddWorkPieceInfoLog(info));
                        db.WorkPieceInfoLog.Add(GetAddWorkPieceInfoLog(cur_db_info));
                    }
                    else
@@ -1045,6 +1096,7 @@
                                {
                                    set_WorkingProcedureCurrent = cur_db_info.WorkingProcedureCurrent;
                                }
                                cur_db_info.WorkingProcedureCurrent = set_WorkingProcedureCurrent;
                                cur_db_info.WorkPieceCurrentPosition = set_WorkingProcedureCurrent + "SPC";
                                loginfo2.WorkingProcedure = set_WorkingProcedureCurrent;//解决OP10,OP40同一个的问题
                                loginfo.WorkingProcedure = set_WorkingProcedureCurrent; //解决OP10,OP40同一个的问题
@@ -1082,6 +1134,7 @@
                        //每次SPC 都插入追溯表
                        WorkPieceProcess process = new WorkPieceProcess();
                        process = EntityPropHelper.Mapper<WorkPieceProcess, WorkPieceInfo>(cur_db_info);
                        QualityStateHelper.ResetQualityNoOkForNewProcess(ref process);
                        process.StartTime = DateTimeHelper.GetDateTime();
                        process.OperationType = OperationType.SPC.ToString();
                        process.QualityState = (int)QualityState.Suspected;
@@ -1234,6 +1287,7 @@
                            info.QualityStateUpdateMode = QualityStateUpdateMode.Auto.ToString();
                            //*/
                            var qualityData = db.QualityDataInfo.Where(o => o.WorkPieceID == logMiddle.WorkPieceID).FirstOrDefault();
                            if (qualityData == null || qualityData.WorkPieceID.Length < 1 || qualityData.WorkingProcedure.Length < 1)
                            {//插入QualityDataInfo表
@@ -1314,11 +1368,14 @@
                        {
                            WorkPieceProcess process = new WorkPieceProcess();
                            process = EntityPropHelper.Mapper<WorkPieceProcess, WorkPieceInfo>(info);
                            QualityStateHelper.ResetQualityNoOkForNewProcess(ref process);
                            process.Id = Yitter.IdGenerator.YitIdHelper.NextId();
                            process.StartTime = DateTimeHelper.GetDateTime();
                            process.EndTime = DateTimeHelper.GetDateTime();
                            if (logMiddle.WorkingProcedure.Equals("OP80"))
                            {//OP80的质量数据是后来获取文件搞的
                             //质检数据,再考虑 另外两种状态  【Editby shaocx,2024-08-27】
                                QualityStateHelper.SetQualityStateForOP80(logMiddle, ref info, ref process);
                            }
                            else
                            {
@@ -1344,6 +1401,11 @@
                        }
                        else
                        {
                            if (logMiddle.WorkingProcedure.Equals("OP80"))
                            {//OP80的质量数据是后来获取文件搞的
                             //质检数据,再考虑 另外两种状态  【Editby shaocx,2024-08-27】
                                QualityStateHelper.SetQualityStateForOP80(logMiddle, ref info, ref pro);
                            }
                            if (op80id > 0)
                            {
                                pro.QualityDataInfoID = op80id;