using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using MesDataFunction;


//using SystemFramework;
using MesServerWork;
using MisDataFunDll;
using MisDataSaveDate;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// 合格放行
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void ConfirmOkGo(object e)
        {
            try
            {
                var msgEvent = (DeviceDriver_BasicData.CustomeEvetnArgs)e;
                string OpName = Convert.ToString(msgEvent.OpName);
                int tagValue = Convert.ToInt32(msgEvent.TagValue);

                if (tagValue == 1)
                {
                    string code;
                    string message;
                    MIS_BASE.GetEngineTypeFromPLC(OpName, out int EngineTypeID, out string EngineType, out string EngineID, out int PartPallet_Code, out string OrderForm);

                    MisDataFun.Material_Insert(OpName, 1);

                    if (MisDataFun.GetSpecialOpExist(OpName, "涂胶时间差校验")) MisDataFun.CheckTJTimeAndSave(OpName, EngineID, "", 1);

                    //更新订单完工数量及物料
                    Sendmessage("24", "11", OpName);
                    // 保存过站信息
                    MisDataFun.WorkStartInsert(OpName, 1);
                    // 从数据库查出质量数据组  MOM_质量数据_上传_查询
                    MisDataFun.GetPartQualityGroupParams(OpName, EngineID, out DataTable dt_quality);
                    // 调用工厂MES出站接口
                    if (dt_quality.Rows.Count > 0)
                    {
                        CallWebService.Call_dataCollectForSfcEx(OpName, EngineID, dt_quality, 1, out code, out message);
                    }
                    else
                    {
                        CallWebService.Call_sfcComplete(OpName, EngineID, 1, out code, out message);
                    }
                    if (code == "0")
                    {
                        //通知PLC，MES工作完成
                        MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "MES保存数据完成！");
                        MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "MES工作完成！");
                        MIS_BASE.Write_MesWorkFinish(OpName, true);
                    }
                    else if (code == "13921") // 质量判定不合格 走不合格放行
                    {
                        //MIS_BASE.Write_OpNameIsOk(OpName, 2);
                        //MIS_BASE.Write_TagValueByTagTypeID(51, OpName, false);  // 合格放行 先不清 用于不合格放行时做校验
                        MIS_BASE.Write_TagValueByTagTypeID(8, OpName, true); // 不合格放行
                        MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "质量判定不合格！");
                        //if (MisDataFun.GetSpecialOpExist(OpName, "人工PC工位"))
                        //{
                        //    SendInfomation("showOpWorkFalseGoBox", "1", OpName); // 显示不合格刷卡放行弹窗
                        //    MisDataFun.LogRecording_OP(OpName, "ERROR", "MIS", $"{EngineID} 产品不合格 待人工确认后不合格放行！");
                        //    return;
                        //}
                        //else
                        //{
                        //    //通知PLC，MES工作完成
                        //    MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "MES保存数据完成！");
                        //    MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "MES工作完成！");
                        //    MIS_BASE.Write_MesWorkFinish(OpName, true);
                        //}
                    }
                    else
                    {
                        string msg = "出站失败：" + EngineID + "，code：" + code + "，message：" + message;
                        MisDataFun.LogRecording_OP(OpName, "ERROR", "MOM", msg);
                        return;
                    }
                }
            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function02:ConfirmOkGo");
            }
        }
    }
}
