using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using MesServerWork;
using MisDataFunDll;
using MisDataSaveDate;

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)
        {
            // 0711Add 先给允许工作 然后进行电芯判断
            MIS_BASE.Write_TagValueByTagTypeID(66, OpName, true);
            string operationTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");

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

            List<string> inventoryArray = new List<string>();
            inventoryArray.Add(cell_code1);
            inventoryArray.Add(cell_code2);
            inventoryArray.Add(cell_code3);
            inventoryArray.Add(cell_code4);
            inventoryArray.Add(cell_code5);
            inventoryArray.Add(cell_code6);
            inventoryArray.Add(cell_code7);
            inventoryArray.Add(cell_code8);

            Dictionary<string, string> checkResult = new Dictionary<string, string>();

            for (int i = 0; i < inventoryArray.Count; i++)
            {
                string inventory = inventoryArray[i];
                // 2.OP030需要向CMaaS校验 接口校验
                CallWebService.Call_miCheckInventoryAttributes(OpName, inventory, out string code, out string message);
                string resCode = code == "0" ? "1" : "999";
                checkResult.Add(inventory, resCode);
            }

            int cell_code1_result = Convert.ToInt32(checkResult[cell_code1]);
            int cell_code2_result = Convert.ToInt32(checkResult[cell_code2]);
            int cell_code3_result = Convert.ToInt32(checkResult[cell_code3]);
            int cell_code4_result = Convert.ToInt32(checkResult[cell_code4]);
            int cell_code5_result = Convert.ToInt32(checkResult[cell_code5]);
            int cell_code6_result = Convert.ToInt32(checkResult[cell_code6]);
            int cell_code7_result = Convert.ToInt32(checkResult[cell_code7]);
            int cell_code8_result = Convert.ToInt32(checkResult[cell_code8]);

            // 3.校验结果储存数据库同时写给PLC
            MisDataFun.Cell_CheckDataAdd(OpName, cell_code1, operationTime, cell_code1_result);
            MisDataFun.Cell_CheckDataAdd(OpName, cell_code2, operationTime, cell_code2_result);
            MisDataFun.Cell_CheckDataAdd(OpName, cell_code3, operationTime, cell_code3_result);
            MisDataFun.Cell_CheckDataAdd(OpName, cell_code4, operationTime, cell_code4_result);
            MisDataFun.Cell_CheckDataAdd(OpName, cell_code5, operationTime, cell_code5_result);
            MisDataFun.Cell_CheckDataAdd(OpName, cell_code6, operationTime, cell_code6_result);
            MisDataFun.Cell_CheckDataAdd(OpName, cell_code7, operationTime, cell_code7_result);
            MisDataFun.Cell_CheckDataAdd(OpName, cell_code8, operationTime, cell_code8_result);

            // 4.过站
            MisDataFun.Cell_PassStation(OpName, cell_code1, operationTime, cell_code1_result);
            MisDataFun.Cell_PassStation(OpName, cell_code2, operationTime, cell_code2_result);
            MisDataFun.Cell_PassStation(OpName, cell_code3, operationTime, cell_code3_result);
            MisDataFun.Cell_PassStation(OpName, cell_code4, operationTime, cell_code4_result);
            MisDataFun.Cell_PassStation(OpName, cell_code5, operationTime, cell_code5_result);
            MisDataFun.Cell_PassStation(OpName, cell_code6, operationTime, cell_code6_result);
            MisDataFun.Cell_PassStation(OpName, cell_code7, operationTime, cell_code7_result);
            MisDataFun.Cell_PassStation(OpName, cell_code8, operationTime, cell_code8_result);
            // 回写PLC
            MIS_BASE.Write_TagValueByTagTypeID(114, OpName, cell_code1_result);
            MIS_BASE.Write_TagValueByTagTypeID(115, OpName, cell_code2_result);
            MIS_BASE.Write_TagValueByTagTypeID(116, OpName, cell_code3_result);
            MIS_BASE.Write_TagValueByTagTypeID(117, OpName, cell_code4_result);
            MIS_BASE.Write_TagValueByTagTypeID(118, OpName, cell_code5_result);
            MIS_BASE.Write_TagValueByTagTypeID(119, OpName, cell_code6_result);
            MIS_BASE.Write_TagValueByTagTypeID(120, OpName, cell_code7_result);
            MIS_BASE.Write_TagValueByTagTypeID(121, OpName, cell_code8_result);

            // 4.MES工作完成
            MIS_BASE.Write_TagValueByTagTypeID(7, OpName, 1);
            MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "MES工作完成！");

        }

        /// <summary>
        /// 二次工作
        /// </summary>
        /// <param name="OpName"></param>
        private void EngineTypeGetOver_PLC_Work_two(string OpName)
        {
            int EngineTypeID;//机型代码
            int PartPallet_Code;//托盘编号

            string EngineType;//机型
            string EngineID;//工件编号
            string OrderForm;
            MIS_BASE.GetEngineTypeFromPLC(OpName, out EngineTypeID, out EngineType, out EngineID, out PartPallet_Code, out OrderForm);
            //EngineTypeGetOver_PLC_Work(OpName, EngineTypeID, EngineType, EngineID, Convert.ToInt32(PartPallet_Code), OrderForm);

        }
        /// <summary>
        /// 跳过二次工作
        /// </summary>
        /// <param name="OpName"></param>
        private void EngineTypeGetOver_PLC_Work_Skip(string OpName)
        {
            //MIS_BASE.Write_MesReadOver(OpName, true);
            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);
        }

        /// <summary>
        /// 更新待上料数量
        /// </summary>
        /// <param name="OpName"></param>
        private void UpdateWait_NUM_PLC(object e)
        {
            var msgEvent = (DeviceDriver_BasicData.CustomeEvetnArgs)e;

            string OpName = Convert.ToString(msgEvent.OpName);
            int tagValue = Convert.ToInt32(msgEvent.TagValue);
            // 更新待上料数量到数据库
            Db.UpdateWait_NUM_PLC(OpName, tagValue);
        }



        /// <summary>
        /// 空料握手plc
        /// </summary>
        /// <param name="OpName"></param>
        private void Handshake_Fun(object e)
        {

           
        }
    }
}
