using RGD.DBUtility;
using System;
using System.Data;

namespace RGD.DataService
{
    public static class CGeneralFunction
    {
        private static Model.MDevice devinfo;

        /// <summary>
        /// 双叉关联任务是否能同步运行
        /// </summary>
        /// <param name="monitorIndex">设备指令索引</param>
        /// <returns></returns>
        public static bool DoubleForkIfSync(int monitorIndex, int devIndex, int devKind)
        {//查找关联的调度任务（主任务或者辅助任务）；输送机与堆垛机区别对待
            //如果只能找到关联任务未被拆分，那么认为能同步运行20100323
            int mankind = GetManageTaskKindIndexFromMonitor(monitorIndex);
            int FID = GetManageTaskIndexfromMonitor(monitorIndex);
            int devOrder = GetDeviceOrderFromMonitor(monitorIndex);
            int dfx = GetDoubleForkX(mankind, FID, devIndex, devOrder);
            int mx = GetXCoorFromMonitor(monitorIndex, devIndex, devOrder);
            int dfy = GetDoubleForkY(mankind, FID, devIndex, devOrder);
            int my = GetYCoorFromMonitor(monitorIndex, devIndex, devOrder);
            int dfz = GetDoubleForkZ(mankind, FID, devIndex, devOrder);
            int mz = GetZCoorFromMonitor(monitorIndex, devIndex, devOrder);
            if (GetDoubleForkUnIntoStep(monitorIndex) == true) return true;

            switch (devKind)
            {
                case 1:
                    //如果主任务和副任务的列数都是1列，则是1121、1122顶升机构，可以同步
                    if (1 == GetDoubleForkX(mankind, FID, devIndex, devOrder) && (GetXCoorFromMonitor(monitorIndex, devIndex, devOrder) == 1))
                    {
                        return true;
                    }

                    //堆垛机的同排，同层，临列可以同步并且ST_CELL的FDoubleFork值匹配

                    if ((Math.Abs(dfx - mx) == 1) && (dfy == my) && (mz == dfz))
                    {
                        if (EqualMonitorDoubleFork(mankind, FID, devIndex, devOrder) == true)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }

                case 2:
                    //输送机同步
                    return true;

                case 6:
                    //AGV临列可以同步并且ST_CELL的F_UseAwayFork值匹配

                    if (Math.Abs(dfx - mx) == 1)
                    {
                        if (EqualAGVGateDoubleFork(mankind, FID, devIndex, devOrder) == true)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }

                default:
                    return false;
            }
        }

        /// <summary>
        /// 获得双叉控制的堆垛机的送货任务（已经分配远货叉或者近货叉的任务）的值
        /// </summary>
        /// <param name="monitorIndex">设备指令索引</param>
        /// <returns></returns>
        public static string GetUseAwayFork(int monitorIndex)
        {
            DataView dv = DbHelperSQL.Query("SELECT F_UseAwayFork FROM T_Monitor_Task WHERE (F_MonitorIndex = " + monitorIndex + ")").Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                return dv[0][0].ToString();
            }
            else
            {
                return "-";
            }
        }

        /// <summary>
        /// 获取双叉控制的堆垛机的取货任务（尚未指定应该使用哪个货叉）是否使用远货叉（主货叉）：0，近货叉；1远货叉
        /// </summary>
        /// <param name="z">排</param>
        /// <param name="x">列</param>
        /// <param name="y">层</param>
        /// <param name="FWAREHOUSE">库房索引</param>
        /// <param name="Mankind">任务类型</param>
        /// <param name="ManFID">调度任务索引</param>
        /// <param name="devIndex">设备索引</param>
        /// <param name="devOrder">指令</param>
        /// <returns></returns>
        public static int GetUseAwayFork(int monitorIndex, int devIndex, int devOrder)
        {
            int z = GetZCoorFromMonitor(monitorIndex, devIndex, devOrder);
            int x = GetXCoorFromMonitor(monitorIndex, devIndex, devOrder);
            int y = GetYCoorFromMonitor(monitorIndex, devIndex, devOrder);

            string FWAREHOUSE = GetWAREHOUSEFromSTCELL(devIndex);
            int Mankind = GetManageTaskKindIndexFromMonitor(monitorIndex);
            int ManFID = GetManageTaskIndexfromMonitor(monitorIndex);

            DataView dv; int UseAwayFork = 1;
            try
            {
                dv = DbHelperSQL.Query("SELECT FDoubleFork FROM ST_CELL WHERE (FWAREHOUSE='" + FWAREHOUSE
                + "') and (F_Z = " + z + ") AND (F_X = " + x + ") AND (F_Y = " + y + ")").Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    UseAwayFork = Convert.ToInt32(dv[0][0]);
                }
                else
                {
                    UseAwayFork = 0;
                }
                switch (UseAwayFork)
                {
                    case 0://可用任何一个货叉:
                        int xcoor = GetDoubleForkX(Mankind, ManFID, devIndex, devOrder);
                        if (xcoor != -1)
                        {
                            //根据关联任务的货位判断使用近货叉（列坐标小）或者远货叉（列坐标大）
                            if (xcoor != x)
                            {
                                if (xcoor < x)
                                {
                                    UseAwayFork = 1;
                                }
                                else
                                {
                                    UseAwayFork = 0;
                                }
                            }
                            else
                            {
                                //列坐标相同的，判断层坐标，近货叉（层坐标小）或者远货叉（层坐标大）
                                int ycoor = GetDoubleForkY(Mankind, ManFID, devIndex, devOrder);
                                if (ycoor != -1)
                                {
                                    if (ycoor != y)
                                    {
                                        if (ycoor < y)
                                        {
                                            UseAwayFork = 1;
                                        }
                                        else
                                        {
                                            UseAwayFork = 0;
                                        }
                                    }
                                    else
                                    {
                                        //列和层都相同的，判断排坐标，近货叉（排坐标小）或者远货叉（排坐标大）
                                        int zcoor = GetDoubleForkZ(Mankind, ManFID, devIndex, devOrder);
                                        if (zcoor != -1)
                                        {
                                            if (zcoor != z)
                                            {
                                                if (zcoor < z)
                                                {
                                                    UseAwayFork = 1;
                                                }
                                                else
                                                {
                                                    UseAwayFork = 0;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            UseAwayFork = 1;
                        }
                        break;

                    case 1://只能用近叉
                        UseAwayFork = 0;
                        break;

                    case 2://只能用远叉
                        UseAwayFork = 1;
                        break;
                }
                return UseAwayFork;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
            }
        }

        public static int GetDoubleForkFromST_CELL(int z, int x, int y, string FWAREHOUSE)
        {
            DataView dv; int UseAwayFork = 1;
            try
            {
                dv = DbHelperSQL.Query("SELECT FDoubleFork FROM ST_CELL WHERE (FWAREHOUSE='" + FWAREHOUSE
                + "') and (F_Z = " + z + ") AND (F_X = " + x + ") AND (F_Y = " + y + ")").Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    UseAwayFork = Convert.ToInt32(dv[0][0]);
                }
                else
                {
                    UseAwayFork = 0;
                }

                return UseAwayFork;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
            }
        }

        public static string GetWAREHOUSEFromSTCELL(int stackIndex)
        {
            DataView dv;
            try
            {
                dv = DbHelperSQL.Query("SELECT FWAREHOUSE FROM ST_CELL WHERE  (FStack = " + stackIndex + ")").Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return dv[0][0].ToString();
                }
                else
                {
                    return "";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally { dv = null; }
        }

        /// <summary>
        /// 查找双叉关联任务的列坐标
        /// </summary>
        /// <param name="mankind">任务类型</param>
        /// <param name="fid">调度任务索引</param>
        /// <param name="devIdx">设备索引</param>
        /// <param name="devOrder">设备指令</param>
        /// <returns></returns>
        public static int GetDoubleForkX(int mankind, int fid, int devIdx, int devOrder)
        {
            DataView dv;
            string xc = "F_NumParam2";
            if (GetDeviceKindIdx(devIdx) == 1)
            {
                if (devOrder == 4)
                {
                    xc = "F_NumParam2";
                }
                else if (devOrder == 5)
                {
                    xc = "F_NumParam5";
                }
            }
            try
            {
                //查找被关联辅助设备指令索引的列坐标
                DataView dv0 = DbHelperSQL.Query("select F_RELATIVECONTORLID from T_Manage_Task where (FID = " +
                    fid + ") AND  (F_ManageTaskKindIndex = " + mankind + ")").Tables[0].DefaultView;
                if (dv0.Count == 0) return -1;
                string sql = "SELECT " + xc + " FROM T_Monitor_Task Where ( T_Monitor_Task.F_ManageTaskKindIndex = " + mankind
                    + ")  and (F_DeviceIndex=" + devIdx + " and F_DeviceCommandIndex=" + devOrder + ")  AND (T_Monitor_Task.F_ManageTaskIndex=" + dv0[0]["F_RELATIVECONTORLID"] + ")";
                dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return Convert.ToInt32(dv[0][xc]);
                }
                dv0 = DbHelperSQL.Query("select T_Manage_Task.FID  from T_Manage_Task where (T_Manage_Task.F_ManageTaskKindIndex = " + mankind + ") AND (F_RELATIVECONTORLID = " +
                    fid + ")").Tables[0].DefaultView;
                if (dv0.Count == 0) return -1;
                //查找关联自己的主设备指令索引的列坐标
                sql = "SELECT " + xc + " FROM T_Monitor_Task Where ( T_Monitor_Task.F_ManageTaskKindIndex = " + mankind + ") and (F_DeviceIndex=" + devIdx + " and F_DeviceCommandIndex=" + devOrder
                    + ") and  (T_Monitor_Task.F_ManageTaskIndex= " + dv0[0]["FID"] + ")";
                dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return Convert.ToInt32(dv[0][xc]);
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
            }
        }

        /// <summary>
        /// 查找双叉关联任务的层坐标
        /// </summary>
        /// <param name="mankind">任务类型</param>
        /// <param name="fid">调度任务索引</param>
        /// <param name="devIdx">设备索引</param>
        /// <param name="devOrder">设备指令</param>
        /// <returns></returns>
        public static int GetDoubleForkY(int mankind, int fid, int devIdx, int devOrder)
        {
            DataView dv;
            string yc = "F_NumParam3";
            if (GetDeviceKindIdx(devIdx) == 1)
            {
                if (devOrder == 4)
                {
                    yc = "F_NumParam3";
                }
                else if (devOrder == 5)
                {
                    yc = "F_NumParam6";
                }
            }
            try
            {
                //string aa22 = DateTime.Now.Second.ToString() + "-" + DateTime.Now.Millisecond.ToString();
                DataView dv0 = DbHelperSQL.Query("select F_RELATIVECONTORLID from T_Manage_Task where FID = " +
                    fid + " and F_ManageTaskKindIndex=" + mankind + "").Tables[0].DefaultView;
                if (dv0.Count == 0) return -1;
                //查找被关联辅助设备指令索引的层坐标
                string sql = "SELECT " + yc + " FROM T_Monitor_Task Where (T_Monitor_Task.F_ManageTaskIndex=" + dv0[0]["F_RELATIVECONTORLID"] + " " +
                    " ) and ( T_Monitor_Task.F_ManageTaskKindIndex = " + mankind + ")  and (F_DeviceIndex=" + devIdx + " and F_DeviceCommandIndex=" + devOrder + ")";
                dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return Convert.ToInt32(dv[0][yc]);
                }
                dv0 = DbHelperSQL.Query("select FID from T_Manage_Task where F_RELATIVECONTORLID = " +
                    fid + " and F_ManageTaskKindIndex=" + mankind + "").Tables[0].DefaultView;
                if (dv0.Count == 0) return -1;
                //string aa212 = DateTime.Now.Second.ToString() + "-" + DateTime.Now.Millisecond.ToString();
                //查找关联自己的主设备指令索引的层坐标
                sql = "SELECT " + yc + " FROM T_Monitor_Task Where (T_Monitor_Task.F_ManageTaskIndex=" + dv0[0]["FID"] + " " +
                    ") and ( T_Monitor_Task.F_ManageTaskKindIndex = " + mankind + ")  and (F_DeviceIndex=" + devIdx + " and F_DeviceCommandIndex=" + devOrder + ")";
                dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                //string aa2qq2 = DateTime.Now.Second.ToString() + "-" + DateTime.Now.Millisecond.ToString();
                if (dv.Count > 0)
                {
                    return Convert.ToInt32(dv[0][yc]);
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
            }
        }

        /// <summary>
        /// 查找双叉关联任务的排坐标
        /// </summary>
        /// <param name="mankind">任务类型</param>
        /// <param name="fid">调度任务索引</param>
        /// <param name="devIdx">设备索引</param>
        /// <param name="devOrder">设备指令</param>
        /// <returns></returns>
        public static int GetDoubleForkZ(int mankind, int fid, int devIdx, int devOrder)
        {
            DataView dv;
            string zc = "F_NumParam1";
            if (GetDeviceKindIdx(devIdx) == 1)
            {
                if (devOrder == 4)
                {
                    zc = "F_NumParam1";
                }
                else if (devOrder == 5)
                {
                    zc = "F_NumParam4";
                }
            }
            try
            {
                //查找被关联辅助设备指令索引的排坐标
                string sql = "SELECT " + zc + " FROM T_Manage_Task,T_Monitor_Task Where (T_Manage_Task.F_RELATIVECONTORLID = T_Monitor_Task.F_ManageTaskIndex " +
                    " AND T_Manage_Task.F_ManageTaskKindIndex = T_Monitor_Task.F_ManageTASKKINDINDEX) and ( T_Monitor_Task.F_ManageTaskKindIndex = " + mankind + ") AND (FID = " +
                    fid + ") and (F_DeviceIndex=" + devIdx + " and F_DeviceCommandIndex=" + devOrder + ")";
                dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return Convert.ToInt32(dv[0][zc]);
                }

                //查找关联自己的主设备指令索引的排坐标
                sql = "SELECT " + zc + " FROM T_Manage_Task,T_Monitor_Task Where (T_Manage_Task.FID = T_Monitor_Task.F_ManageTaskIndex " +
                    " AND T_Manage_Task.F_ManageTaskKindIndex = T_Monitor_Task.F_ManageTASKKINDINDEX) and ( T_Monitor_Task.F_ManageTaskKindIndex = " + mankind + ") AND (F_RELATIVECONTORLID = " +
                    fid + ") and (F_DeviceIndex=" + devIdx + " and F_DeviceCommandIndex=" + devOrder + ")";
                dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return Convert.ToInt32(dv[0][zc]);
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
            }
        }

        /// <summary>
        /// 查找双叉关联任务的ST_CELL表的使用哪个货叉属性是否相同
        /// </summary>
        /// <param name="mankind">任务类型</param>
        /// <param name="fid">调度任务索引</param>
        /// <param name="devIdx">设备索引</param>
        /// <param name="devOrder">设备指令</param>
        /// <returns></returns>
        public static bool EqualMonitorDoubleFork(int mankind, int fid, int devIdx, int devOrder)
        {
            DataView dv; char DoubleFork = '0', DoubleFork1 = '0';
            string xc = "F_NumParam2"; string yc = "F_NumParam3"; string zc = "F_NumParam1";
            if (GetDeviceKindIdx(devIdx) == 1)
            {
                if ((devOrder == 4) || (devOrder == 2))
                {
                    xc = "F_NumParam2";
                    yc = "F_NumParam3";
                    zc = "F_NumParam1";
                }
                else if ((devOrder == 5) || (devOrder == 3))
                {
                    xc = "F_NumParam5";
                    yc = "F_NumParam6";
                    zc = "F_NumParam4";
                }
            }

            try
            {
                int z = 0, x = 0, y = 0, z1 = 0, x1 = 0, y1 = 0;
                //查找被关联辅助设备指令索引的列坐标
                string sql = "SELECT " + zc + "," + xc + "," + yc + ",F_UseAwayFork FROM T_Manage_Task,T_Monitor_Task Where (T_Manage_Task.F_RELATIVECONTORLID = T_Monitor_Task.F_ManageTaskIndex " +
                    " AND T_Manage_Task.F_ManageTaskKindIndex = T_Monitor_Task.F_ManageTASKKINDINDEX) and ( T_Monitor_Task.F_ManageTaskKindIndex = " + mankind + ") AND (FID = " +
                    fid + ") and (F_DeviceIndex=" + devIdx + " and F_DeviceCommandIndex=" + devOrder + ")";
                dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    z = Convert.ToInt32(dv[0][zc]);
                    x = Convert.ToInt32(dv[0][xc]);
                    y = Convert.ToInt32(dv[0][yc]);
                    DoubleFork = Convert.ToChar(dv[0]["F_UseAwayFork"]);
                }
                else
                {
                    //查找关联自己的主设备指令索引的列坐标
                    sql = "SELECT " + zc + "," + xc + "," + yc + ",F_UseAwayFork FROM T_Manage_Task,T_Monitor_Task Where (T_Manage_Task.FID = T_Monitor_Task.F_ManageTaskIndex " +
                        " AND T_Manage_Task.F_ManageTaskKindIndex = T_Monitor_Task.F_ManageTASKKINDINDEX) and ( T_Monitor_Task.F_ManageTaskKindIndex = " + mankind + ") AND (F_RELATIVECONTORLID = " +
                        fid + ") and (F_DeviceIndex=" + devIdx + " and F_DeviceCommandIndex=" + devOrder + ")";
                    dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                    if (dv.Count > 0)
                    {
                        z = Convert.ToInt32(dv[0][zc]);
                        x = Convert.ToInt32(dv[0][xc]);
                        y = Convert.ToInt32(dv[0][yc]);
                        DoubleFork = Convert.ToChar(dv[0]["F_UseAwayFork"]);
                    }
                }
                if ((z == 0) && (x == 0) && (y == 0))
                {
                    return false;
                }
                else
                {
                    sql = "SELECT " + zc + "," + xc + "," + yc + ",F_UseAwayFork FROM T_Monitor_Task Where ( F_ManageTASKKINDINDEX = " + mankind + ") AND (F_ManageTaskIndex = " +
                    fid + ") and (F_DeviceIndex=" + devIdx + " and F_DeviceCommandIndex=" + devOrder + ")";
                    dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                    if (dv.Count > 0)
                    {
                        z1 = Convert.ToInt32(dv[0][zc]);
                        x1 = Convert.ToInt32(dv[0][xc]);
                        y1 = Convert.ToInt32(dv[0][yc]);
                        DoubleFork1 = Convert.ToChar(dv[0]["F_UseAwayFork"]);
                        if (((x1 > x) && (DoubleFork1 > DoubleFork)) || ((x > x1) && (DoubleFork > DoubleFork1)))
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
            }
        }

        /// <summary>
        /// 查找双叉关联任务的T_Base_AGV_Gate表的货叉属性是否匹配
        /// </summary>
        /// <param name="mankind">任务类型</param>
        /// <param name="fid">调度任务索引</param>
        /// <param name="devIdx">设备索引</param>
        /// <param name="devOrder">设备指令</param>
        /// <returns></returns>
        public static bool EqualAGVGateDoubleFork(int mankind, int fid, int devIdx, int devOrder)
        {
            DataView dv;
            string xc = "F_NumParam2";
            if (GetDeviceKindIdx(devIdx) == 6)
            {
                if (devOrder == 1)
                {
                    xc = "F_NumParam2";
                }
                else if (devOrder == 2)
                {
                    xc = "F_NumParam5";
                }
                else if (devOrder == 4)
                {
                    xc = "F_NumParam5";
                }
            }

            try
            {
                int x = 0, x1 = 0;
                int channelleft = 0, channelleft1 = 0, xd = 0, xd1 = 0;
                //查找被关联辅助设备指令索引的列坐标
                string sql = "SELECT " + xc + " FROM T_Manage_Task,T_Monitor_Task Where (T_Manage_Task.F_RELATIVECONTORLID = T_Monitor_Task.F_ManageTaskIndex " +
                    " AND T_Manage_Task.F_ManageTaskKindIndex = T_Monitor_Task.F_ManageTASKKINDINDEX) and ( T_Monitor_Task.F_ManageTaskKindIndex = " + mankind + ") AND (FID = " +
                    fid + ") and (F_DeviceIndex=" + devIdx + " and F_DeviceCommandIndex=" + devOrder + ")";
                dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    xd = Convert.ToInt32(dv[0][xc]);
                    dv = DbHelperSQL.Query("SELECT F_Sequence FROM T_Base_AGV_Gate WHERE (F_AGVGateDeviceIndex = " + xd + ")").Tables[0].DefaultView;
                    if (dv.Count > 0)
                    {
                        x = Convert.ToInt32(dv[0]["F_Sequence"]);
                    }
                    else
                    {
                        x = 0;
                    }
                }
                else
                {
                    //查找关联自己的主设备指令索引的列坐标
                    sql = "SELECT " + xc + " FROM T_Manage_Task,T_Monitor_Task Where (T_Manage_Task.FID = T_Monitor_Task.F_ManageTaskIndex " +
                        " AND T_Manage_Task.F_ManageTaskKindIndex = T_Monitor_Task.F_ManageTASKKINDINDEX) and ( T_Monitor_Task.F_ManageTaskKindIndex = " + mankind + ") AND (F_RELATIVECONTORLID = " +
                        fid + ") and (F_DeviceIndex=" + devIdx + " and F_DeviceCommandIndex=" + devOrder + ")";
                    dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                    if (dv.Count > 0)
                    {
                        xd = Convert.ToInt32(dv[0][xc]);
                        dv = DbHelperSQL.Query("SELECT F_Sequence FROM T_Base_AGV_Gate WHERE (F_AGVGateDeviceIndex = " + xd + ")").Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            x = Convert.ToInt32(dv[0]["F_Sequence"]);
                        }
                        else
                        {
                            x = 0;
                        }
                    }
                }
                if (x == 0)
                {
                    return false;
                }
                else
                {
                    sql = "SELECT " + xc + " FROM T_Monitor_Task Where ( F_ManageTASKKINDINDEX = " + mankind + ") AND (F_ManageTaskIndex = " +
                    fid + ") and (F_DeviceIndex=" + devIdx + " and F_DeviceCommandIndex=" + devOrder + ")";
                    dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                    if (dv.Count > 0)
                    {
                        xd1 = Convert.ToInt32(dv[0][xc]);
                        dv = DbHelperSQL.Query("SELECT F_Sequence FROM T_Base_AGV_Gate WHERE (F_AGVGateDeviceIndex = " + xd1 + ")").Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            x1 = Convert.ToInt32(dv[0]["F_Sequence"]);
                        }
                        else
                        {
                            x1 = 0;
                        }
                        int DoubleFork = 0, DoubleFork1 = 0;
                        dv = DbHelperSQL.Query("SELECT F_UseAwayFork,F_IfChannelLeft FROM T_Base_AGV_Gate WHERE F_AGVGateDeviceIndex =" + xd).Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            DoubleFork = Convert.ToInt32(dv[0]["F_UseAwayFork"]);
                            channelleft = Convert.ToInt32(dv[0]["F_IfChannelLeft"]);
                        }
                        else
                        {
                            return false;
                        }
                        dv = DbHelperSQL.Query("SELECT F_UseAwayFork,F_IfChannelLeft FROM T_Base_AGV_Gate WHERE F_AGVGateDeviceIndex =" + xd1).Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            DoubleFork1 = Convert.ToInt32(dv[0]["F_UseAwayFork"]);
                            channelleft1 = Convert.ToInt32(dv[0]["F_IfChannelLeft"]);
                        }
                        else
                        {
                            return false;
                        }
                        if ((((x1 > x) && (DoubleFork1 > DoubleFork)) || ((x > x1) && (DoubleFork > DoubleFork1))) && (channelleft == channelleft1))
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
            }
        }

        /// <summary>
        /// 在设备指令队列表获取Z 排坐标
        /// </summary>
        /// <param name="monitorIndex">设备指令索引</param>
        /// <returns></returns>
        public static int GetZCoorFromMonitor(int monitorIndex, int devIdx, int devOrder)
        {
            DataView dv;
            string zc = "F_NumParam1";
            if (GetDeviceKindIdx(devIdx) == 1)
            {
                if (devOrder == 2 || devOrder == 4)
                {
                    zc = "F_NumParam1";
                }
                else if (devOrder == 3 || devOrder == 5)
                {
                    zc = "F_NumParam4";
                }
            }
            try
            {
                dv = DbHelperSQL.Query("SELECT " + zc + " FROM T_Monitor_Task WHERE (F_MonitorIndex = " + monitorIndex + ")").Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return Convert.ToInt32(dv[0][0]);
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
            }
        }

        /// <summary>
        /// 在设备指令队列表获取X 列坐标
        /// </summary>
        /// <param name="monitorIndex">设备指令索引</param>
        /// <returns></returns>
        public static int GetXCoorFromMonitor(int monitorIndex, int devIdx, int devOrder)
        {
            DataView dv;
            string xc = "F_NumParam2";
            if (GetDeviceKindIdx(devIdx) == 1)
            {
                if (devOrder == 4)
                {
                    xc = "F_NumParam2";
                }
                else if (devOrder == 5)
                {
                    xc = "F_NumParam5";
                }
            }
            try
            {
                dv = DbHelperSQL.Query("SELECT " + xc + " FROM T_Monitor_Task WHERE (F_MonitorIndex = " + monitorIndex + ")").Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return Convert.ToInt32(dv[0][0]);
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
            }
        }

        /// <summary>
        /// 在设备指令队列表获取Y 层坐标
        /// </summary>
        /// <param name="monitorIndex">设备指令索引</param>
        /// <returns></returns>
        public static int GetYCoorFromMonitor(int monitorIndex, int devIdx, int devOrder)
        {
            DataView dv;
            string yc = "F_NumParam3";
            if (GetDeviceKindIdx(devIdx) == 1)
            {
                if (devOrder == 4)
                {
                    yc = "F_NumParam3";
                }
                else if (devOrder == 5)
                {
                    yc = "F_NumParam6";
                }
            }
            try
            {
                dv = DbHelperSQL.Query("SELECT " + yc + " FROM T_Monitor_Task WHERE (F_MonitorIndex = " + monitorIndex + ")").Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return Convert.ToInt32(dv[0][0]);
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
            }
        }

        /// <summary>
        /// 返回双叉关联设备指令信息：【0】设备指令索引【1】提前检测【2】设备索引【3】路径
        /// </summary>
        /// <param name="monitorIndex">设备指令索引</param>
        /// <param name="devIdx">设备索引</param>
        /// <returns></returns>
        public static string[] GetDoubleForkMonitorInfo(int monitorIndex, int devIdx)
        {
            DataView dv;

            string[] rr = null;
            try
            {
                int mankind = GetManageTaskKindIndexFromMonitor(monitorIndex);
                int fid = GetManageTaskIndexfromMonitor(monitorIndex);
                int devOrder = GetDeviceOrderFromMonitor(monitorIndex);
                //查找被关联辅助设备指令索引的列坐标
                string sql = "SELECT F_MonitorIndex,F_AheadDetect,F_DeviceIndex,F_RouteID FROM T_Manage_Task,T_Monitor_Task Where (T_Manage_Task.F_RELATIVECONTORLID = T_Monitor_Task.F_ManageTaskIndex " +
                    " AND T_Manage_Task.F_ManageTaskKindIndex = T_Monitor_Task.F_ManageTASKKINDINDEX) and ( T_Monitor_Task.F_ManageTaskKindIndex = " + mankind + ") AND (FID = " +
                    fid + ") and (F_DeviceIndex=" + devIdx + " and F_DeviceCommandIndex=" + devOrder + ")";
                dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    rr = new string[4];
                    rr[0] = dv[0]["F_MonitorIndex"].ToString();
                    rr[1] = dv[0]["F_AheadDetect"].ToString();
                    rr[2] = dv[0]["F_DeviceIndex"].ToString();
                    rr[3] = dv[0]["F_RouteID"].ToString();
                }
                else
                {
                    //查找关联自己的主设备指令索引的列坐标
                    sql = "SELECT F_MonitorIndex,F_AheadDetect,F_DeviceIndex,F_RouteID FROM T_Manage_Task,T_Monitor_Task Where (T_Manage_Task.FID = T_Monitor_Task.F_ManageTaskIndex " +
                        " AND T_Manage_Task.F_ManageTaskKindIndex = T_Monitor_Task.F_ManageTASKKINDINDEX) and ( T_Monitor_Task.F_ManageTaskKindIndex = " + mankind + ") AND (F_RELATIVECONTORLID = " +
                        fid + ") and (F_DeviceIndex=" + devIdx + " and F_DeviceCommandIndex=" + devOrder + ")";
                    dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                    if (dv.Count > 0)
                    {
                        rr = new string[4];
                        rr[0] = dv[0]["F_MonitorIndex"].ToString();
                        rr[1] = dv[0]["F_AheadDetect"].ToString();
                        rr[2] = dv[0]["F_DeviceIndex"].ToString();
                        rr[3] = dv[0]["F_RouteID"].ToString();
                    }
                    else
                    {
                        return null;
                    }
                }
                return rr;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
                rr = null;
            }
        }

        /// <summary>
        /// 是否有任务状态大于0的双叉关联任务的存在
        /// </summary>
        /// <param name="monitorIndex"></param>
        /// <param name="devIdx"></param>
        /// <returns></returns>
        public static bool GetDoubleForkMonitorInfo(int monitorIndex, int devIdx, int status)
        {
            DataView dv;

            try
            {
                int mankind = GetManageTaskKindIndexFromMonitor(monitorIndex);
                int fid = GetManageTaskIndexfromMonitor(monitorIndex);
                //查找被关联辅助设备指令索引的列坐标
                string sql = "SELECT F_MonitorIndex,F_AheadDetect,F_DeviceIndex,F_RouteID FROM T_Manage_Task,T_Monitor_Task Where (T_Manage_Task.F_RELATIVECONTORLID = T_Monitor_Task.F_ManageTaskIndex " +
                    " AND T_Manage_Task.F_ManageTaskKindIndex = T_Monitor_Task.F_ManageTASKKINDINDEX) and ( T_Monitor_Task.F_ManageTaskKindIndex = " + mankind + ") AND (FID = " +
                    fid + ") and (F_DeviceIndex=" + devIdx + " and f_status>" + status + ")";
                dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return true;
                }
                else
                {
                    //查找关联自己的主设备指令索引的列坐标
                    sql = "SELECT F_MonitorIndex,F_AheadDetect,F_DeviceIndex,F_RouteID FROM T_Manage_Task,T_Monitor_Task Where (T_Manage_Task.FID = T_Monitor_Task.F_ManageTaskIndex " +
                        " AND T_Manage_Task.F_ManageTaskKindIndex = T_Monitor_Task.F_ManageTASKKINDINDEX) and ( T_Monitor_Task.F_ManageTaskKindIndex = " + mankind + ") AND (F_RELATIVECONTORLID = " +
                        fid + ") and (F_DeviceIndex=" + devIdx + " and f_status>" + status + ")";
                    dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                    if (dv.Count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
            }
        }

        /// <summary>
        /// 返回未拆分的双叉关联设备指令,true 代表有未拆分的双叉关联设备指令
        /// </summary>
        /// <param name="monitorIndex">设备指令索引</param>
        /// <returns></returns>
        public static bool GetDoubleForkUnIntoStep(int monitorIndex)
        {
            DataView dv;
            try
            {
                int mankind = GetManageTaskKindIndexFromMonitor(monitorIndex);
                int fid = GetManageTaskIndexfromMonitor(monitorIndex);
                //查找被关联辅助设备指令索引的列坐标
                string sql = "SELECT F_RELATIVECONTORLID FROM T_Manage_Task Where (T_Manage_Task.F_RELATIVECONTORLID <>-1 ) and ( F_ManageTaskKindIndex = " + mankind + ") AND (FID = " +
                    fid + ")";
                dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    sql = "SELECT FID FROM T_Manage_Task Where ( F_ManageTaskKindIndex = " + mankind + ") AND (FID = " +
                    dv[0][0] + " and FIntoStepOK<>'1')";
                    dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                    if (dv.Count > 0)
                    {
                        return true;
                    }
                }
                else
                {
                    //查找关联自己的主设备指令索引的列坐标
                    sql = "SELECT FID FROM T_Manage_Task Where ( F_ManageTaskKindIndex = " + mankind + ") AND (F_RELATIVECONTORLID = " +
                        fid + ")";
                    dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                    if (dv.Count > 0)
                    {
                        sql = "SELECT FID FROM T_Manage_Task Where ( F_ManageTaskKindIndex = " + mankind + ") AND (FID = " +
                        dv[0][0] + " and FIntoStepOK<>'1')";
                        dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            return true;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
            }
        }

        public static int GetManageTaskIndexfromMonitor(int monitorIdx)
        {
            //20100108
            DataView dv;
            try
            {
                //20100108
                dv = DbHelperSQL.Query("SELECT F_ManageTaskIndex FROM T_Monitor_Task WHERE (F_MonitorIndex = " + monitorIdx + ")").Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return Convert.ToInt32(dv[0]["F_ManageTaskIndex"]);
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception ex)
            {//20100108
                throw ex;
            }
            finally
            {//20100108
                dv = null;
            }
        }

        public static int GetManageTaskKindIndexFromMonitor(int monitorIdx)
        {//20100108
            DataView dv;
            try
            {
                //20100108
                dv = DbHelperSQL.Query("SELECT F_ManageTaskKindIndex FROM T_Monitor_Task WHERE (F_MonitorIndex = " + monitorIdx + ")").Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return Convert.ToInt32(dv[0]["F_ManageTaskKindIndex"]);
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception ex)
            {//20100108
                throw ex;
            }
            finally
            {//20100108
                dv = null;
            }
        }

        /// <summary>
        /// 在调度队列中找到设备命令
        /// </summary>
        /// <param name="MonitorIndex">调度所引</param>
        /// <returns></returns>
        public static int GetDeviceOrderFromMonitor(int MonitorIndex)
        {
            DataView dv;
            try
            {
                string sql = "select F_DeviceCommandIndex from T_Monitor_Task where (F_DeviceCommandIndex IS NOT NULL) and F_MonitorIndex=" + MonitorIndex;
                //20100108
                dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return Convert.ToInt32(dv[0]["F_DeviceCommandIndex"]);
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
            }
        }

        public static int GetDeviceKindIdx(int devIdx)
        {
            try
            {
                devinfo = BaseDeviceService.GetDeviceInfo(devIdx);
                return devinfo.DeviceKind;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static int GetDeviceIndexFromMonitor(int MonitorIndex)
        {
            //20100108
            DataView dv;
            try
            {
                string sql = "select F_DeviceIndex from T_Monitor_Task where (F_DeviceIndex IS NOT NULL) and F_MonitorIndex=" + MonitorIndex;
                //20100108
                dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return Convert.ToInt32(dv[0]["F_DeviceIndex"]);
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
            }
        }

        /// <summary>
        /// 获得表型层数
        /// </summary>
        /// <param name="MonitorIndex"></param>
        /// <returns></returns>
        public static int GetBoxQuantityFromMonitor(int MonitorIndex)
        {
            //20100108
            DataView dv;   //NUMParam5 表示表箱层数
            try
            {
                string sql = "select F_NumParam5 from T_Monitor_Task where (F_NumParam5 IS NOT NULL) and F_MonitorIndex=" + MonitorIndex;
                //20100108
                dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return Convert.ToInt32(dv[0]["F_NumParam5"]);
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
            }
        }

        /// <summary>
        /// 获得管理下任务12029输送机处的类型
        /// </summary>
        /// <param name="MonitorIndex"></param>
        /// <returns></returns>
        public static int GetTypeFromMonitor(int MonitorIndex)
        {
            //20120212
            DataView dv;
            try
            {
                string sql = "select * from T_Manage_Task,T_Monitor_Task where T_Manage_Task.fid= T_Monitor_Task.F_ManageTaskIndex and F_MonitorIndex=" + MonitorIndex;
                //20100108
                dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return Convert.ToInt32(dv[0]["SENDOUT_TYPE"]);
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
            }
        }

        /// <summary>
        /// 双叉堆垛机在一个输送机取送盘时需要额外检测的探物开关（隔壁输送机是否有探物）
        /// </summary>
        /// <param name="devinx"></param>
        /// <returns></returns>
        public static int GetDoubleForkDetect(int devinx)
        {
            try
            {
                devinfo = BaseDeviceService.GetDeviceInfo(devinx);
                return Convert.ToInt32(devinfo.DoubleForkDetect);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                devinfo = null;
            }
        }

        #region 与管理任务交互的调度任务状态

        /// <summary>
        /// 任务等待，默认0
        /// </summary>
        public static readonly int TASKWAIT = IOControlService.GetIOControlStatus("TASKWAIT").StatusID;

        /// <summary>
        /// 任务开始运行，默认10
        /// </summary>
        public static readonly int TASKRUN = IOControlService.GetIOControlStatus("TASKRUN").StatusID;

        /// <summary>
        /// 调度申请改道，默认30
        /// </summary>
        public static readonly int TASKALTERROUTEAPPLY = IOControlService.GetIOControlStatus("TASKALTERROUTEAPPLY").StatusID;

        /// <summary>
        /// 管理答复改道申请，默认40
        /// </summary>
        public static readonly int TASKALTERROUTEREPLY = IOControlService.GetIOControlStatus("TASKALTERROUTEREPLY").StatusID;

        /// <summary>
        /// 管理任务被调度删除，默认900
        /// </summary>
        public static readonly int TASKDELETE = IOControlService.GetIOControlStatus("TASKDELETE").StatusID;

        /// <summary>
        /// 人工暂停出库，默认940
        /// </summary>
        public static readonly int TASKSTOPOUTPUT = IOControlService.GetIOControlStatus("TASKSTOPOUTPUT").StatusID;

        /// <summary>
        /// 拆盘任务完成，默认950
        /// </summary>
        public static readonly int TASKUNPACKFINISH = IOControlService.GetIOControlStatus("TASKUNPACKFINISH").StatusID;

        /// <summary>
        /// 任务类型错，送到拆盘机的不是空托盘组，默认955
        /// </summary>
        public static readonly int TASKTYPEERROR = IOControlService.GetIOControlStatus("TASKTYPEERROR").StatusID;

        /// <summary>
        /// 配送出库到叠盘输送机，默认960
        /// </summary>
        public static readonly int TASKPACKFINISH = IOControlService.GetIOControlStatus("TASKPACKFINISH").StatusID;

        /// <summary>
        /// 任务被取消，搬运到异常出库站台，默认966
        /// </summary>
        public static readonly int TASKCANCEL = IOControlService.GetIOControlStatus("TASKCANCEL").StatusID;

        /// <summary>
        /// 堆垛机的送货重需要改路径处理，默认970
        /// </summary>
        public static readonly int TASKREPEATINPUT = IOControlService.GetIOControlStatus("TASKREPEATINPUT").StatusID;

        /// <summary>
        /// 堆垛机的取空处理，调度取消执行管理任务，默认980
        /// </summary>
        public static readonly int TASKEMPTYOUTPUT = IOControlService.GetIOControlStatus("TASKEMPTYOUTPUT").StatusID;

        /// <summary>
        /// 任务异常完成，默认990
        /// </summary>
        public static readonly int TASKABEND = IOControlService.GetIOControlStatus("TASKABEND").StatusID;

        /// <summary>
        /// 任务搬运完成，默认999
        /// </summary>
        public static readonly int TASKFINISH = IOControlService.GetIOControlStatus("TASKFINISH").StatusID;

        /// <summary>
        /// 入库任务RFID比对错误，默认值930
        /// </summary>
        public static readonly int TASKINPUTRFIDERROR = IOControlService.GetIOControlStatus("TASKINPUTRFIDERROR").StatusID;

        /// <summary>
        /// 20100617管理任务对出库托盘确认接收，默认值50
        /// </summary>
        public static readonly int TASKOUTCONFIRM = IOControlService.GetIOControlStatus("TASKOUTCONFIRM").StatusID;

        /// <summary>
        /// 20101011双叉极限货位需要更换货叉，默认值800
        /// </summary>
        public static readonly int TASKCHANGEFORK = IOControlService.GetIOControlStatus("TASKCHANGEFORK").StatusID;

        /// <summary>
        /// 20101011调度已经获取任务，默认值7
        /// </summary>
        public static readonly int TASKCONTROLREADED = IOControlService.GetIOControlStatus("TASKCONTROLREADED").StatusID;

        /// <summary>
        /// 20101028堆垛机单叉入库任务立即执行：默认值，25
        /// </summary>
        public static readonly int TASKSINGLEFORKRUN = IOControlService.GetIOControlStatus("TASKSINGLEFORKRUN").StatusID;

        #endregion 与管理任务交互的调度任务状态

        #region 堆垛机取空和送重的故障码

        /// <summary>
        /// 堆垛机的送重故障码，默认48（双叉中的近叉）
        /// </summary>
        public static readonly int STACKREPEATINPUT = IOControlService.GetIOControlStatus("STACKREPEATINPUT").StatusID;

        /// <summary>
        /// 堆垛机的取空故障码，默认49（双叉中的近叉）
        /// </summary>
        public static readonly int STACKEMPTYOUTPUT = IOControlService.GetIOControlStatus("STACKEMPTYOUTPUT").StatusID;

        /// <summary>
        /// 堆垛机的送重故障码，默认60（双叉中的远叉）
        /// </summary>
        public static readonly int STACKREPEATINPUTAWAY = IOControlService.GetIOControlStatus("STACKREPEATINPUTAWAY").StatusID;

        /// <summary>
        /// 堆垛机的取空故障码，默认62（双叉中的远叉）
        /// </summary>
        public static readonly int STACKEMPTYOUTPUTAWAY = IOControlService.GetIOControlStatus("STACKEMPTYOUTPUTAWAY").StatusID;

        /// <summary>
        /// 堆垛机的送重故障码，默认61（双叉）
        /// </summary>
        public static readonly int STACKREPEATINPUTDOUBLE = IOControlService.GetIOControlStatus("STACKREPEATINPUTDOUBLE").StatusID;

        /// <summary>
        /// 堆垛机的取空故障码，默认63（双叉）
        /// </summary>
        public static readonly int STACKEMPTYOUTPUTDOUBLE = IOControlService.GetIOControlStatus("STACKEMPTYOUTPUTDOUBLE").StatusID;

        #endregion 堆垛机取空和送重的故障码
    }
}