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

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

                if (tagValue == 1)
                {
                    MisDataFun.LogRecording_OP(OpName, "INFO", "MesRead", "PLC允许保存数据！");

                    // 1.读取产品的静置信息，1/2个产品的，无产品不交互
                    string part1;
                    string part1_setAside_time;
                    string part1_setAside_isOk;
                    string tagID1 = "74200001";
                    string part2;
                    string part2_setAside_time;
                    string part2_setAside_isOk;
                    string tagID2 = "74200009";
                    MIS_BASE.Read_TagValueByTagTypeID(205, OpName, out part1);
                    MIS_BASE.Read_TagValueByTagTypeID(17, OpName, out part1_setAside_time);
                    MIS_BASE.Read_TagValueByTagTypeID(18, OpName, out part1_setAside_isOk);

                    MIS_BASE.Read_TagValueByTagTypeID(206, OpName, out part2);
                    MIS_BASE.Read_TagValueByTagTypeID(201, OpName, out part2_setAside_time);
                    MIS_BASE.Read_TagValueByTagTypeID(202, OpName, out part2_setAside_isOk);

                    bool part1_IsOk = true;
                    bool part2_IsOk = true;
                    // 2.判断PLC工件是否写入
                    if ((part2 == "" && part1 == "") || (part2 == null && part1 == null) || (part2.Length < 5 && part1.Length < 5))
                    {
                        // MES报警
                        MIS_BASE.Write_TagValueByTagTypeID(34, OpName, true);
                        string msg = "【" + OpName + "】" + "PLC两个产品信息为空，part1：" + part1 + "，part2：" + part2;
                        SendMessage.AsyncSendMessage_Notice(OpName, msg);
                        MisDataFun.LogRecording_OP(OpName, "ERROR", "MesRead", msg);
                        return;
                    }

                    // 3.保存质量数据（静置时间，静置结果） 保存过站信息
                    if (part1 != null && part1 != "" && part1.Length > 5)
                    {
                        // 保存过站信息
                        MisDataFun.WorkStartInsert_JZ(OpName, part1, Convert.ToInt32(part1_setAside_isOk));

                        // 从数据库查出质量数据组  MOM_质量数据_上传_查询
                        MisDataFun.GetPartQualityGroupParams(OpName, part1, out DataTable dt_quality);

                        // 调用工厂MES出站接口
                        string code;
                        string message;
                        if (dt_quality.Rows.Count > 0)
                        {
                            CallWebService.Call_dataCollectForSfcEx(OpName, part1, dt_quality, Convert.ToInt32(part1_setAside_isOk), out code, out message);
                        }
                        else
                        {
                            CallWebService.Call_sfcComplete(OpName, part1, Convert.ToInt32(part1_setAside_isOk), out code, out message);
                        }
                        if (code == "0")
                        {
                        }
                        else if (code == "13921") // 质量判定不合格 走不合格放行
                        {
                            MIS_BASE.Write_OpNameIsOk(OpName, 2);
                        }
                        else
                        {
                            part1_IsOk = false;
                            string msg = "出站失败：" + part1 + "，code：" + code + "，message：" + message;
                            MisDataFun.LogRecording_OP(OpName, "ERROR", "MOM", msg);
                        }

                    }
                    if (part2 != null && part2 != "" && part2.Length > 5)
                    {
                       
                        // 保存过站信息
                        MisDataFun.WorkStartInsert_JZ(OpName, part2, Convert.ToInt32(part2_setAside_isOk));

                        // 从数据库查出质量数据组  MOM_质量数据_上传_查询
                        MisDataFun.GetPartQualityGroupParams(OpName, part2, out DataTable dt_quality);

                        // 调用工厂MES出站接口
                        string code;
                        string message;
                        if (dt_quality.Rows.Count > 0)
                        {
                            CallWebService.Call_dataCollectForSfcEx(OpName, part2, dt_quality, Convert.ToInt32(part2_setAside_isOk), out code, out message);
                        }
                        else
                        {
                            CallWebService.Call_sfcComplete(OpName, part2, Convert.ToInt32(part2_setAside_isOk), out code, out message);
                        }
                        if (code == "0")
                        {
                        }
                        else if (code == "13921") // 质量判定不合格 走不合格放行
                        {
                            MIS_BASE.Write_OpNameIsOk(OpName, 2);
                        }
                        else
                        {
                            part2_IsOk = false;
                            string msg = "出站失败：" + part2 + "，code：" + code + "，message：" + message;
                            MisDataFun.LogRecording_OP(OpName, "ERROR", "MOM", msg);
                        }
                    }
                    // 都出站成功了 才给PLC信号
                    if (part1_IsOk && part2_IsOk)
                    {
                        //通知PLC，MES工作完成
                        MIS_BASE.Write_MesWorkFinish(OpName, true);
                        //通知PLC，MES保存完成
                        MIS_BASE.Write_MesReadOver(OpName, true);
                        MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "MES保存数据完成！");
                        MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "MES工作完成！");

                    }

                }
                else
                {
                    MisDataBaseFun.WriteMesReadOver(OpName, false); // 保存完成工作完成
                    MisDataFun.LogRecording_OP(OpName, "INFO", "MesRead", "PLC允许保存数据取消！");
                }
            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function08:MesReadOver");
            }

        }
    }
}
