using bizFacade;
using MesServerWork;
using System;
using System.Collections.Generic;
using System.Data;
//using SystemFramework;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        // 触发J07PA010流程
        private void J07PA010WorkStart(object e)
        {
            {
                try
                {
                    int EngineTypeID;
                    string EngineType;
                    string EngineID;
                    int PartPallet_Code;
                    string OrderForm;
                    
                    DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
                    string OpName = Convert.ToString(msgEvent.OpName);
                    int tagValue = Convert.ToInt32(msgEvent.TagValue);
                    int tagTypeID = Convert.ToInt32(msgEvent.TagTypeID);  // 1. 计数1   2.计数2
                    int TagTypeCodeID = Convert.ToInt32(msgEvent.TagTypeCodeID);  // 报警点位置

                    //25 ~34  10个地址
                    //25  26  27  为6s 触发
                    //28  29  为5s触发
                    //30  31  32  为6s报警
                    //33  34  5s报警

                    // 35 5S 3 触发
                    // 36 5S 3 报警
                    if (tagValue == 1)
                    {
                        List<int> errorPositonList = new List<int> {30,31,32,33,34,36};

                        for (int i = 0;i < errorPositonList.Count; i++) {
                            int itemErrorPositon = errorPositonList[i];
                            MIS_BASE.WritePLC(itemErrorPositon, OpName, false); // 清空报警信号
                        }

                        // 只用于PA010工位的逻辑循环
                        // 1.将报警变量置位为0
                        // 2.判断当前有没有选择订单号 若没有选择订单号则不工作，提示选择订单号
                        // 只需要 订单号，总成号，机型号 托盘编号为0
                        //北汽福田工位状态监控MOBY_查询_订单下达用
                        DataTable dt;
                        dt = BaseDataSystemMES.GetNowWorkOrderData(OpName, tagTypeID);
                        if(dt.Rows.Count > 0)
                        {
                            EngineTypeID = Convert.ToInt32(dt.Rows[0]["机型代码"]);
                            EngineType = dt.Rows[0]["零件号"].ToString();
                            EngineID = dt.Rows[0]["变速箱总成号"].ToString();
                            OrderForm = dt.Rows[0]["订单号"].ToString();

                            // 如果有订单但按钮按的位置与机型对不上则退出方法
                            if(EngineTypeID == 1 || EngineTypeID == 2)
                            {
                                if(TagTypeCodeID != 33 && TagTypeCodeID != 34 && TagTypeCodeID != 36)
                                {
                                    SendMessage.AsyncSendMessage_Notice(OpName, "请选择与机型相对应的按钮进行操作！");
                                    return;
                                }
                            }

                            // 如果有订单但按钮按的位置与机型对不上则退出方法
                            if (EngineTypeID == 4 || EngineTypeID == 8)
                            {
                                if (TagTypeCodeID != 30 && TagTypeCodeID != 31 && TagTypeCodeID != 32)
                                {
                                    SendMessage.AsyncSendMessage_Notice(OpName, "请选择与机型相对应的按钮进行操作！");
                                    return;
                                }
                            }

                            ////将产品信息写入PLC
                            MIS_BASE.Write_EngineTypeID_MES(OpName, EngineTypeID);
                            MIS_BASE.Write_EngineType_MES(OpName, EngineType);
                            MIS_BASE.Write_EngineID_MES(OpName, EngineID);
                            MIS_BASE.Write_OrderForm_MES(OpName, OrderForm);

                            BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.WorkStartIn, DateTime.Now.ToString());
                            // 将数据保存到监控MOBY中 
                            BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineType, EngineType);
                            BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineID, EngineID);
                            BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.OrderForm, OrderForm);
                            BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.PartPallet_Code, tagTypeID.ToString());
                            BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineID_MES, EngineID);
                            BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineType_MES, EngineType);
                            BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.OrderForm_MES, OrderForm);

                            //电子看板_装配零件_临时表_增加_北汽
                            //第二步，将零件信息转移到零件_临时表中
                            BaseDataSystemMES.MesServer_Process_EngineTypeOverBQ(OpName);

                            // 4.将这些数据推送到前端进行显示
                            //显示当前订单信息
                            SendMessage.ShowMoby(OpName);
                            SendMessageFun.SendMessage("12345", "11111", OpName);
                            // 5.保存过站数据
                            //测量数据发动机在线状态
                            BaseDataSystemMES.WorkStartInsert(OpName, 1);
                            // 6.该订单计数加一
                            BaseDataSystemMES.newAddOrderCount(OpName, EngineID, OrderForm, tagTypeID);
                            // 7.发送保存完成信号，结束本次循环
                            BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.WorkStartOut, DateTime.Now.ToString());
                            SendMessageFun.SendInfomation("FlushOverWorkList", "1", OpName);
                            MIS_BASE.Write_MaterialVerifyOver(OpName, true);
                        }
                        else
                        {
                            MIS_BASE.WritePLC(TagTypeCodeID, OpName, true);  // 计数12是否超过条码数 超过发1退出循环给PLC信号
                            SendMessage.AsyncSendMessage_Notice(OpName, "该订单已生产完毕或无选中的生产订单，请选择其他订单进行生产！");
                            return;
                        }
                    }
                    else
                    {
                        MIS_BASE.Write_MaterialVerifyOver(OpName, false);
                    }
                }
                catch (Exception err)
                {
                    ////ApplicationLog.WriteLog(err, "Function08:MesReadOver");
                }
            }

        }
    }
}
