 using MesServerWork;
using MesWork;
using MisDataFunDll;
using MisDataSaveDate;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
//using SystemFramework;

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)
                {
                    // 1.读取所有的电芯码，总合格标志
                   var cell_code = new string[8];
                   cell_code[0] = MIS_BASE.Read_TagValueByTagTypeID(200, OpName).ToString();
                   cell_code[1] = MIS_BASE.Read_TagValueByTagTypeID(201, OpName).ToString();
                   cell_code[2] = MIS_BASE.Read_TagValueByTagTypeID(202, OpName).ToString();
                   cell_code[3] = MIS_BASE.Read_TagValueByTagTypeID(203, OpName).ToString();
                   cell_code[4] = MIS_BASE.Read_TagValueByTagTypeID(204, OpName).ToString();
                   cell_code[5] = MIS_BASE.Read_TagValueByTagTypeID(205, OpName).ToString();
                   cell_code[6] = MIS_BASE.Read_TagValueByTagTypeID(206, OpName).ToString();
                   cell_code[7] = MIS_BASE.Read_TagValueByTagTypeID(207, OpName).ToString();
                   var operationTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                   var qualityMask = Convert.ToInt32(MIS_BASE.Read_TagValueByTagTypeID(21, OpName));
                    
                    // 2.保存质量数据
                    MisDataSaveDateMesRead.Funtion.MesRead_Cell(cell_code, OpName, tagValue.ToString(), msgEvent.TagID.ToString(), qualityMask);

                    for (int i = 0; i < cell_code.Length ; i++)
                    {
                        string EngineID = cell_code[i];
                        // 3.保存过站数据
                        MisDataFun.Cell_PassStation(OpName, EngineID, operationTime, 2, qualityMask);

                        // 4.上传数据至工厂MES  如果不合格 则在 114—121 8个电芯合格标志 写入 每颗电芯的合格状态
                        if (MisDataFun.GetSpecialOpExist(OpName, "角度检测工位"))
                        {
                            string code;
                            string message;
                            // 从数据库查出质量数据组  MOM_质量数据_上传_查询
                            MisDataFun.GetPartQualityGroupParams(OpName, EngineID, out DataTable dt_quality);

                            CallWebService.Call_cellCustomOneDCCheck(OpName, EngineID, dt_quality, out code, out message,out string checkResult);
                            if (code == "0")
                            {
                                MisDataFun.LogRecording_OP(OpName, "INFO", "MOM", $"电芯{EngineID}结果写入1 合格");
                                MIS_BASE.Write_TagValueByTagTypeID(114 + i, OpName, 1); 
                            }
                            else if (code == "13921") // 质量判定不合格 走不合格放行
                            {
                                string msg = "电芯数据存在质量不合格：" + EngineID + "，code：" + code + "，message：" + message;
                                MisDataFun.LogRecording_OP(OpName, "ERROR", "MOM", msg);
                                MisDataFun.LogRecording_OP(OpName, "INFO", "MOM", $"电芯{EngineID}结果写入5 质量不合格");
                                MIS_BASE.Write_TagValueByTagTypeID(114 + i, OpName, 5);
                            }
                            else
                            {
                                string msg = "电芯数据保存失败：" + EngineID + "，code：" + code + "，message：" + message;
                                MisDataFun.LogRecording_OP(OpName, "ERROR", "MOM", msg);
                                MisDataFun.LogRecording_OP(OpName, "INFO", "MOM", $"电芯{EngineID}结果写入6 上传失败");
                                MIS_BASE.Write_TagValueByTagTypeID(114 + i, OpName, 6); // 还有一种是上传失败的！
                            }
                        }
                    }

                    // 判断是否存在首件标志
                    if (MIS_BASE.Read_TagValueByTagTypeID(62,OpName) == "1") MisDataFun.Save_FristWorkData(OpName, cell_code[0]); // 第一个产品 存首件数据

                    // 写入加工合格还是不合格 根据工位合格标志写
                    MIS_BASE.Write_OpNameIsOk(OpName, qualityMask);

                    // 4.通知PLC保存完成
                    MIS_BASE.Write_TagValueByTagTypeID(20, OpName, true);

                    // 工作完成
                    MIS_BASE.Write_MesWorkFinish(OpName, true);
                }
                else
                {
                    // 写入加工合格还是不合格 根据工位合格标志写
                    MIS_BASE.Write_OpNameIsOk(OpName, -99);
                    // 保存完成（MES）
                    MIS_BASE.Write_TagValueByTagTypeID(20, OpName, false);
                    // 工作完成
                    MIS_BASE.Write_MesWorkFinish(OpName, false);
                    MIS_BASE.Write_TagValueByTagTypeID(114, OpName, 0);
                    MIS_BASE.Write_TagValueByTagTypeID(115, OpName, 0);
                    MIS_BASE.Write_TagValueByTagTypeID(116, OpName, 0);
                    MIS_BASE.Write_TagValueByTagTypeID(117, OpName, 0);
                    MIS_BASE.Write_TagValueByTagTypeID(118, OpName, 0);
                    MIS_BASE.Write_TagValueByTagTypeID(119, OpName, 0);
                    MIS_BASE.Write_TagValueByTagTypeID(120, OpName, 0);
                    MIS_BASE.Write_TagValueByTagTypeID(121, OpName, 0);
                }

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


        }

    }
}
