using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;
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 FalseGO(object e)
        {
            try
            {
                var msgEvent = (DeviceDriver_BasicData.CustomeEvetnArgs)e;
                string OpName = Convert.ToString(msgEvent.OpName);
                int tagValue = Convert.ToInt32(msgEvent.TagValue);

                if (tagValue == 1)
                {
                    //不合格放行信号
                    //MIS_BASE.Write_FalseGO(OpName, true);
                    //总合格标志2（不合格）
                    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_NG(OpName, 2);

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

                    // 保存过站信息
                    MisDataFun.WorkStartInsert(OpName, 2);
                    //更新订单完工数量及物料
                    Sendmessage("24", "11", OpName);
                    bool okGo = false;
                    MIS_BASE.Read_ConfirmOkGo(OpName, out okGo);
                    if (!okGo) // 如果有合格放行 那说明是质量数据不合格 从 合格放行到不合格放行的 不重新调用出站接口
                    {
                        // 从数据库查出质量数据组  MOM_质量数据_上传_查询
                        MisDataFun.GetPartQualityGroupParams(OpName, EngineID, out DataTable dt_quality);
                        // 调用工厂MES出站接口
                        if (dt_quality.Rows.Count > 0)
                        {
                            CallWebService.Call_dataCollectForSfcEx(OpName, EngineID, dt_quality, 0, out code, out message);
                        }
                        else
                        {
                            CallWebService.Call_sfcComplete(OpName, EngineID, 0, out code, out message);
                        }
                        if (code == "0")
                        {
                        }
                        else if (code == "13921") // 质量判定不合格 走不合格放行
                        {
                            MIS_BASE.Write_OpNameIsOk(OpName, 2);
                        }
                        else
                        {
                            string msg = "出站失败：" + EngineID + "，code：" + code + "，message：" + message;
                            MisDataFun.LogRecording_OP(OpName, "ERROR", "MOM", msg);
                            return;
                        }
                        MisDataFun.LogRecording_OP(OpName, "INFO", "FalseGO", "MES保存数据完成！");
                        MisDataFun.LogRecording_OP(OpName, "INFO", "FalseGO", "MES工作完成，不合格放行！");
                    }

                    MIS_BASE.Write_TagValueByTagTypeID(51, OpName, false); // 合格放行清掉
                    if (MisDataFun.GetSpecialOpExist(OpName, "人工PC工位"))
                    {
                        SendInfomation("showOpWorkFalseGoBox", "1", OpName); // 显示不合格刷卡放行弹窗
                        MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", $"{EngineID} 产品不合格 待人工确认后不合格放行！");
                        return;
                    }
                    else
                    {
                        //通知PLC，MES工作完成
                        MIS_BASE.Write_MesWorkFinish(OpName, true);
                    }
                }
            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function02:FalseGO");
            }
        }
    }
}
