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

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// 正常逻辑工作(传递机型)
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="EngineTypeID"></param>
        /// <param name="EngineType"></param>
        /// <param name="EngineID"></param>
        /// <param name="PartPallet_Code"></param>
        private void EngineTypeGetOver_PLC_Work(string OpName)
        {
            // 1.读取所有的电芯码
            string cell_code1 = "";
            string cell_code2 = "";
            string cell_code3 = "";
            string cell_code4 = "";
            string cell_code5 = "";
            string cell_code6 = "";
            string cell_code7 = "";
            string cell_code8 = "";

            cell_code1 = MIS_BASE.Read_TagValueByTagTypeID(200, OpName).ToString();
            cell_code2 = MIS_BASE.Read_TagValueByTagTypeID(201, OpName).ToString();
            cell_code3 = MIS_BASE.Read_TagValueByTagTypeID(202, OpName).ToString();
            cell_code4 = MIS_BASE.Read_TagValueByTagTypeID(203, OpName).ToString();
            cell_code5 = MIS_BASE.Read_TagValueByTagTypeID(204, OpName).ToString();
            cell_code6 = MIS_BASE.Read_TagValueByTagTypeID(205, OpName).ToString();
            cell_code7 = MIS_BASE.Read_TagValueByTagTypeID(206, OpName).ToString();
            cell_code8 = MIS_BASE.Read_TagValueByTagTypeID(207, OpName).ToString();
            string operationTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");

            // 2.记录进站数据到数据库
            if (cell_code1 != "" & cell_code2 != "" && cell_code3 != "" &&  cell_code4 != "" && cell_code5 != "" && cell_code6 != "" && cell_code7 != "" && cell_code8 != "")
            {
                MisDataFun.Cell_PassStation(OpName, cell_code1, operationTime, 1);
                MisDataFun.Cell_PassStation(OpName, cell_code2, operationTime, 1);
                MisDataFun.Cell_PassStation(OpName, cell_code3, operationTime, 1);
                MisDataFun.Cell_PassStation(OpName, cell_code4, operationTime, 1);
                MisDataFun.Cell_PassStation(OpName, cell_code5, operationTime, 1);
                MisDataFun.Cell_PassStation(OpName, cell_code6, operationTime, 1);
                MisDataFun.Cell_PassStation(OpName, cell_code7, operationTime, 1);
                MisDataFun.Cell_PassStation(OpName, cell_code8, operationTime, 1);

                // 3.通知PLC允许工作
                MIS_BASE.Write_TagValueByTagTypeID(66, OpName, true);
            }
            else
            {
                string msg = $"电芯码读取失败：【1】{cell_code1}【2】{cell_code2}【3】{cell_code3}【4】{cell_code4}【5】{cell_code5}【6】{cell_code6}【7】{cell_code7}【8】{cell_code8}";
                MisDataFun.LogRecording_OP(OpName, "ERROR", "MIS", msg);
                MyLog4Net.MyLogHelper.Error("EngineTypeGetOver", msg);
                MIS_BASE.Write_MesErrorType(OpName, 1004);  // 写入MES报警类型 电芯码读取失败 存在空电芯码！
            }
        }
        /// <summary>
        /// 二次工作
        /// </summary>
        /// <param name="OpName"></param>
        private void EngineTypeGetOver_PLC_Work_two(string OpName)
        {
            MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "PLC传递机型！");
            MIS_BASE.Write_TagValueByTagTypeID(34, OpName, false); // 复位报警（MES）

            int EngineTypeID;//机型代码
            string EngineType;//机型
            string EngineID;//工件编号
            string OrderForm; // 工单号
            int PartPallet_Code;//托盘编号

            MIS_BASE.GetEngineTypeFromPLC(OpName, out EngineTypeID, out EngineType, out EngineID, out PartPallet_Code, out OrderForm);

            // 判断PLC机型是否写入
            if (EngineTypeID <= 0 || EngineID == "")
            {
                MIS_BASE.Write_TagValueByTagTypeID(34, OpName, true);// MES报警
                string msg = "【" + OpName + "】" + "PLC机型写入为空，EngineTypeID：" + EngineTypeID + "，EngineID：" + EngineID;
                SendMessage.AsyncSendMessage_Notice(OpName, msg);
                MisDataFun.LogRecording_OP(OpName, "ERROR", "EngineTypeGetOver", msg);
                return;
            }

            // PDC产线plc只写了发动机号和机型代码，其余要从数据库中读取
            MisDataFun.Moby_Select_PDC(EngineID, out OrderForm, out EngineType, out EngineTypeID);

            MisDataFun.OpName_MIS_MOBY_Insert(OpName, EngineTypeID, EngineType, EngineID, PartPallet_Code, OrderForm, 0, 0, "");
            EngineTypeGetOver_PLC_Work(OpName);

        }
        /// <summary>
        /// 跳过二次工作
        /// </summary>
        /// <param name="OpName"></param>
        private void EngineTypeGetOver_PLC_Work_Skip(string OpName)
        {
            MIS_BASE.Write_MesWorkFinish(OpName, true);
        }

        /// <summary>
        /// 返修件需要工作
        /// </summary>
        /// <param name="OpName">工位号</param>
        private void IsNeedRepaire_OK(string OpName)
        {
            string orderForm = "";
            int engineTypeID;
            string engineType = "";
            string engineID = "";
            string palletCode = "";
            int isRepair;
            string repairCode = "";
            //从监控系统中查询出本工位信息
            //BaseDataSystemMES.OpName_MIS_Moby_Select(OpName,out orderForm, out engineTypeID, out engineType,out engineID, out palletCode, out isRepair, out repairCode);

            //EngineTypeGetOver_PLC_Work(OpName, engineTypeID, engineType, engineID, Convert.ToInt32(palletCode), orderForm);
        }




    }
}
