//using SystemFramework;
using MesServerWork;
using MisDataFunDll;
using MisDataSaveDate;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        object lockMesRead = new object();
        /// <summary>
        /// 保存完成(针对半自动和手动压装工位)
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void MesRead(object e)
        {
            try
            {
                var msgEvent = (DeviceDriver_BasicData.CustomeEvetnArgs)e;
                string OpName = Convert.ToString(msgEvent.OpName);
                int tagValue = Convert.ToInt32(msgEvent.TagValue);

                int EngineTypeID;
                string EngineType;
                string EngineID;
                int PartPallet_Code;
                string OrderForm;

                if (tagValue == 1)
                {
                    MisDataFun.LogRecording_OP(OpName, "INFO", "MesRead", "PLC允许保存数据！");
                    int qualityMask;
                    MIS_BASE.GetEngineTypeFromPLC(OpName, out EngineTypeID, out EngineType, out EngineID, out PartPallet_Code, out OrderForm);

                    // PDC产线plc只写了发动机号和机型代码，其余要从数据库中读取
                    MisDataFun.Moby_Select_PDC(EngineID, out OrderForm, out EngineType, out EngineTypeID);

                    // 保存质量数据
                    qualityMask = MisDataSaveDateMesRead.Funtion.MesRead(OpName, tagValue.ToString(), msgEvent.TagID.ToString());

                    // 保存物料数据
                    MisDataFun.Material_Insert(OpName, qualityMask);

                    // 保存过站信息
                    MisDataFun.WorkStartInsert(OpName, qualityMask);
                    // 写入加工合格还是不合格 根据工位合格标志写
                    MIS_BASE.Write_OpNameIsOk(OpName, qualityMask);

                    // 查看测试设备是否完成测试，如果完成测试的话流程结束，否则的话要等测试设备把数据传输过来
                    DataTable dt2 = MisDataFun.CheckAllFinish(OpName, EngineID, 1);
                    // EngineID = "001MB0D0012C003QF9910001";
                    string msg = "查询测试设备数据出错";
                    if (dt2 != null && dt2.Rows.Count > 0)
                    {
                        string result = dt2.Rows[0]["result"].ToString();
                        msg = dt2.Rows[0]["msg"].ToString();

                        if (EngineTypeID != 1 && EngineTypeID != 5) result = "1"; // 非PDC 和 W05 可直接放行
                        if (result == "1") //已完成
                        {
                            // 从数据库查出质量数据组  MOM_质量数据_上传_查询
                            MisDataFun.GetPartQualityGroupParams(OpName, EngineID, out DataTable dt_quality);

                            // 调用工厂MES出站接口
                            string code;
                            string message;
                            if (dt_quality.Rows.Count > 0)
                            {
                                CallWebService.Call_dataCollectForSfcEx(OpName, EngineID, dt_quality, qualityMask, out code, out message);
                            }
                            else
                            {
                                if(OpName == "OP230") CallWebService.Call_sfcComplete(OpName, EngineID, qualityMask, out code, out message); //测试设备不允许无质量数据出站
                                else // 除了 OP230 以外其他测试设备得带数据出站
                                {
                                    code = "999";
                                    message = "未读取到测试数据 未成功出站！";
                                }
                            } 

                            if (code == "0")
                            {
                            }
                            else if (code == "13921") // 质量判定不合格 走不合格放行
                            {
                                MIS_BASE.Write_TagValueByTagTypeID(51, OpName, false); // 合格放行复位掉
                                MIS_BASE.Write_OpNameIsOk(OpName, 2);
                                MisDataFun.WorkStartInsert(OpName, 2);
                            }
                            else
                            {
                                string msg1 = "出站失败：" + EngineID + "，code：" + code + "，message：" + message;
                                MisDataFun.LogRecording_OP(OpName, "ERROR", "MOM", msg1);
                                return;
                            }

                            MisDataBaseFun.WriteMesReadOver(OpName, true); // 保存完成工作完成
                        }
                        else
                        {
                            MisDataFun.LogRecording_OP(OpName, "INFO", "MesRead", msg);
                        }
                    }
                    else
                    {
                        MisDataFun.LogRecording_OP(OpName, "INFO", "MesRead", msg);
                    }
                }
                else
                {                   
                    // 写入加工合格还是不合格 根据工位合格标志写
                    MIS_BASE.Write_OpNameIsOk(OpName, -99);
                    MisDataBaseFun.WriteMesReadOver(OpName, false); // 保存完成工作完成
                    MisDataFun.LogRecording_OP(OpName, "INFO", "MesRead", "PLC允许保存数据取消！");
                }


            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function08:MesReadOver");
            }

        }
    }
}
