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

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 = Convert.ToInt32(MIS_BASE.Read_TagValueByTagTypeID(21, OpName));
                    qualityMask = 1; // 拧紧工位PLC不发合格标志 直接默认合格 根据MES结果判断后续合格结论即可
                    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);

                    // 拧紧工位手动触发允许保存只有在PAD在出站重试时可能 所以直接根据情况调用合格放行还是不合格放行就可以实现出站流程
                    MIS_BASE.Write_OpNameIsOk(OpName, -99); // 先复位报警信号 一秒后 再写入合格 因为如果不合格MES会判定不合格的 拧紧工位PLCS 和 PLC不判定
                    Thread.Sleep(1000);
                    MIS_BASE.Write_OpNameIsOk(OpName, 1); // 写入合格放行 触发出站流程

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

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

                    // 从数据库查出质量数据组  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
                    //{
                    //    // CallWebService.Call_sfcComplete(OpName, EngineID, qualityMask, out code, out message); // 拧紧工位无 空质量出站情况
                    //    code = "999";
                    //    message = "无拧紧数据 禁止出站！";
                    //}
                    //if (code == "0")
                    //{
                    //    MIS_BASE.Write_OpNameIsOk(OpName, qualityMask);
                    //}
                    //else if (code == "13921") // 质量判定不合格 走不合格放行
                    //{
                    //    qualityMask = 2;
                    //    MIS_BASE.Write_OpNameIsOk(OpName, qualityMask);

                    //    MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "MES保存数据完成！");
                    //    MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "MES工作完成！");
                    //}
                    //else
                    //{
                    //    string msg = "出站失败：" + EngineID + "，code：" + code + "，message：" + message;
                    //    MisDataFun.LogRecording_OP(OpName, "ERROR", "MOM", msg);
                    //    return;
                    //}

                    // 保存过站信息
                    //MisDataFun.WorkStartInsert(OpName, qualityMask);

                    //通知PLC，MES保存完成
                    //MIS_BASE.Write_MesReadOver(OpName, true);
                    //通知PLC，MES工作完成
                    //MIS_BASE.Write_MesWorkFinish(OpName, true);
                    //MisDataFun.LogRecording_OP(OpName, "INFO", "MesRead", "MES保存数据完成！");
                    //MisDataFun.LogRecording_OP(OpName, "INFO", "MesRead", "MES工作完成！");
                }
                else
                {
                    MIS_BASE.Write_MesReadOver(OpName, false);
                    MIS_BASE.Write_MesWorkFinish(OpName, false);
                    MisDataFun.LogRecording_OP(OpName, "INFO", "MesRead", "PLC允许保存数据取消！");
                }


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

        }
    }
}
