using System;
using System.Data;
using MesServerWork;
using MisDataFunDll;
using MisDataSaveDate;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <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));

                    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);

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

                    // 保存过站信息
                    MisDataFun.WorkStartInsert(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);
                    }
                    if (code == "0")
                    {
                        // 写入加工合格还是不合格 根据工位合格标志写
                        MIS_BASE.Write_OpNameIsOk(OpName, qualityMask);
                    }
                    else if (code == "13921") // 质量判定不合格 走不合格放行
                    {
                        MIS_BASE.Write_OpNameIsOk(OpName, 2);
                    }
                    else
                    {
                        string msg = "出站失败：" + EngineID + "，错误代码：" + code + "，消息：" + message;
                        MisDataFun.LogRecording_OP(OpName, "ERROR", "MOM", msg);
                        return;
                    }

                    // 如果不合格 同时 是人工PC工位 则 需要屏幕上点放行后才能放行
                    if (qualityMask != 1 && MisDataFun.GetSpecialOpExist(OpName, "人工PC工位"))
                    {
                        SendInfomation("showOpWorkFalseGoBox", "1", OpName); // 显示不合格刷卡放行弹窗
                        MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", $"{EngineID} 产品不合格 待人工确认后不合格放行！");
                        return;
                    }

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


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

        }
    }
}
