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

namespace RGD.WCS
{
    /// <summary>
    /// Creator:RGD
    /// 通用函数类库
    /// </summary>
    public class CCommonFunction
    {
        private string _DisassembleTaskError = "";

        public string DisassembleTaskError
        {
            get { return _DisassembleTaskError; }
            set { _DisassembleTaskError = value; }
        }

        public CCommonFunction()
        {
        }

        /// <summary>
        /// //获得将要生成的设备指令任务 索引号
        /// </summary>
        /// <param name="managerTaskIndex">没用到</param>
        /// <param name="managerTaskKindIndex">没用到</param>
        /// <returns>设备指令索引值１－－32000</returns>
        public int GetMonitorIndex(int managerTaskIndex, int managerTaskKindIndex)
        {
            DataView dvMt;
            try
            {
                int maxIdx = 0;
                string sql = "SELECT F_MonitorIndex FROM T_Base_Monitor_Task_Index";
                dvMt = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                if (dvMt.Count > 0 && dvMt[0]["F_MonitorIndex"] != DBNull.Value)
                {
                    maxIdx = (Convert.ToInt32(dvMt[0]["F_MonitorIndex"]) + 1);
                    if (maxIdx >= 32000)//预留2000个给接近上限的拆分任务
                    {
                        maxIdx = 1;
                    }
                }
                else//现在没有该管理的设备指令记录
                {
                    maxIdx = 1;
                }
                //判断调度任务表是否有重复值
                if (IFExitMonitorIndex(maxIdx) == true)
                {
                    UpdateMonitorIndex(maxIdx);
                    maxIdx = GetMonitorIndex(managerTaskIndex, managerTaskKindIndex);
                }
                UpdateMonitorIndex(maxIdx);
                return maxIdx;
            }
            catch (Exception ex)
            {
                _DisassembleTaskError = "获得指定调度任务的设备指令索引失败！" + ex.Message;
                throw ex;
            }
            finally
            {
                dvMt = null;
            }
        }

        /// <summary>
        /// 判断调度任务号在队列中是否存在，true已经存在；false不存在
        /// </summary>
        /// <param name="Monitorindex"></param>
        /// <returns></returns>
        private bool IFExitMonitorIndex(int Monitorindex)
        {
            DataView dv;
            try
            {
                dv = DbHelperSQL.Query("SELECT F_MonitorIndex FROM T_Monitor_Task WHERE (F_MonitorIndex = " + Monitorindex + ")").Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
            }
        }

        /// <summary>
        /// 更新设备指令索引
        /// </summary>
        /// <param name="MonitorIndex"></param>
        public void UpdateMonitorIndex(int MonitorIndex)
        {
            DbHelperSQL.ExecuteSql("update T_Base_Monitor_Task_Index set F_MonitorIndex=" + MonitorIndex);
        }

        //---------------------------------------------------------------------------------------------------------
        /// <summary>
        /// 获取调度生成的调度任务的索引
        /// </summary>
        /// <returns></returns>
        public int GetTempManageIdx()
        {
            DataSet ds;
            try
            {
                int maxIdx = 10001;
                ds = DbHelperSQL.Query("SELECT F_ManageTaskIndex FROM T_Base_Manage_Task_Index_Temp_Task");
                if (ds.Tables[0].DefaultView.Count > 0)
                {
                    if ((Convert.ToInt32(ds.Tables[0].DefaultView[0]["F_ManageTaskIndex"]) + 1) >= 19998)
                    {
                        maxIdx = 10001;
                    }
                    else
                    {
                        maxIdx = (Convert.ToInt32(ds.Tables[0].DefaultView[0]["F_ManageTaskIndex"]) + 1);
                    }
                }
                else
                {
                    maxIdx = 10001;
                }
                //判断临时调度任务表是否有重复值
                if (IFExitTempManageIndex(maxIdx) == true)
                {
                    RecordMaxTempManageTaskFID(maxIdx);
                    maxIdx = GetTempManageIdx();
                    return maxIdx;
                }
                RecordMaxTempManageTaskFID(maxIdx);
                return maxIdx;
            }
            catch (Exception ex)
            {
                _DisassembleTaskError = "获得指定调度任务的临时任务索引失败，" + ex.Message;
                throw ex;
            }
            finally
            {
                ds = null;
            }
        }

        /// <summary>
        /// 自动任务索引是否存在
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        private bool IFExitTempManageIndex(int fid)
        {
            DataView dv;
            try
            {
                dv = DbHelperSQL.Query("SELECT fid FROM T_Manage_Task WHERE (F_ManageTaskKindIndex=2 and fid = " + fid + ")").Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
            }
        }

        /// <summary>
        /// 记录调度生成的调度任务的索引的最大值
        /// </summary>
        /// <param name="fid">调度生成的调度任务的索引</param>
        private void RecordMaxTempManageTaskFID(int fid)
        {//20100108
            DataView dv;
            try
            {//20100108
                dv = DbHelperSQL.Query("select F_ManageTaskIndex from T_Base_Manage_Task_Index_Temp_Task").Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    //if (fid == 19998)
                    //{
                    //    dbo.ExceSQL("UPDATE T_Base_Manage_Task_Index_Temp_Task SET F_ManageTaskIndex =10001");
                    //    return;
                    //}
                    //if (fid > Convert.ToInt32(dv[0]["F_ManageTaskIndex"]))
                    //{
                    DbHelperSQL.ExecuteSql("UPDATE T_Base_Manage_Task_Index_Temp_Task SET F_ManageTaskIndex =" + fid);
                    return;
                    //}
                }
                else
                {
                    DbHelperSQL.ExecuteSql("INSERT INTO T_Base_Manage_Task_Index_Temp_Task (F_ManageTaskIndex)VALUES (" + fid + ")");
                    return;
                }
            }
            catch (Exception ex)
            {//20100108
                _DisassembleTaskError = "获得记录调度任务最大任务索引失败，" + ex.Message;
                throw ex;
                //return 0;
            }
            finally
            {//20100108
                dv = null;
            }
        }

        //---------------------------------------------------------------------------------------------------------

        /// <summary>
        /// Control成功发送命令后的处理信息
        /// </summary>
        /// <param name="taskKindIndex">调度任务类型873154021</param>
        /// <param name="Managefid">调度任务索引</param>
        /// <param name="MonirotIdx">设备指令索引</param>
        /// <param name="deviceidx">设备索引</param>
        /// <param name="detailidx">详细路径步骤索引</param>
        public void SendOrderSuccess(int taskKindIndex, int Managefid, int MonirotIdx, int deviceidx, int detailidx)
        {
            string AssociateDevice = "";//关联设备
            string LockedDeviceIndex = "";//被锁定的设备索引组，以“；”分隔
            char[] sep = new char[1];
            string[] split;
            DataView dvs;
            try
            {
                int devKind = BaseDeviceService.GetDeviceKindIdx(deviceidx);
                int order = GetDeviceOrderFromMonitor(MonirotIdx);
                //8：更改作业记录的状态F_Status为1,更改设备为占用状态（T_Base_device的F_LockedState=设备指令单）

                string dtime = DateTime.Now.ToString("u");
                dtime = dtime.Substring(0, dtime.Length - 1);
                DbHelperSQL.ExecuteSql("update T_Monitor_Task set F_StartTime='" + dtime + "',F_Status=1,F_SendCount=F_SendCount+1 where F_Status=0 and F_MonitorIndex=" + MonirotIdx);//20100905
                if (taskKindIndex != 4)//20100610
                {
                    //20101220AGV和输送机送出指令不需要锁定设备
                    if (devKind != 5 && devKind != 6 && (devKind != 2 && order != 6))
                    {
                        DbHelperSQL.ExecuteSql("update T_Base_device set F_LockedState=" + MonirotIdx + " where   F_DeviceIndex=" + deviceidx + "");
                    }
                }
                if (devKind == 1)
                {
                    //修改T_Manage_Task的FLANEWAY，FSTACK
                    dvs = DbHelperSQL.Query("SELECT F_LaneDeviceIndex,F_StackIndex  FROM T_Base_LaneInfo where F_StackIndex=" + deviceidx + "").Tables[0].DefaultView;
                    int laneway = 0;
                    if (dvs.Count > 0)
                    {
                        laneway = Convert.ToInt32(dvs[0]["F_LaneDeviceIndex"]);
                    }
                    DbHelperSQL.ExecuteSql("update T_Manage_Task set FLANEWAY=" + laneway + ",FSTACK=" + deviceidx + " where F_ManageTaskKindIndex=" + taskKindIndex + " and FID=" + Managefid);
                }
                if (taskKindIndex != 4)
                {
                    //如果是RGV、堆垛机的将取（运行）命令需要增加调度任务预约锁F_ManTaskReserve   && (order == 7))  && ((order == 2) || (order == 4))
                    if ((devKind == 4) || (devKind == 1))//20100610
                    {////RGV运到到输送机//堆垛机将取
                        DbHelperSQL.ExecuteSql("UPDATE T_Base_Device SET F_ManTaskReserve = " + (taskKindIndex.ToString() + Managefid.ToString()) + " WHERE (F_DeviceIndex = " + deviceidx + ")");
                    }
                }
                //9：给调度任务表回写调度任务IO_Control的正在执行状态FSTATUS=1;T_Manage_Task的正在执行状态FSTATUS=1
                DbHelperSQL.ExecuteSql("update T_Manage_Task set FSTATUS='1' where F_ManageTaskKindIndex=" + taskKindIndex + " and FID=" + Managefid);
                if (taskKindIndex == 1)//调度任务
                {//上报管理运行
                }
                //10:调度任务表里的F_RunningLock所有对应设备索引加锁，但是F_AssociateDeviceIndex关联设备不加锁
                AssociateDevice = GetAssociateDevice(MonirotIdx);
                if (AssociateDevice == "") AssociateDevice = null;
                if (AssociateDevice != null)
                {
                    sep[0] = '-';
                    split = AssociateDevice.Split(sep);
                    AssociateDevice = split[0];
                }
                sep[0] = ';';
                LockedDeviceIndex = GetLockedDeviceIndex(MonirotIdx);
                if (LockedDeviceIndex != "")
                {
                    split = LockedDeviceIndex.Split(sep);
                    for (int m = split.GetLowerBound(0); m <= split.GetUpperBound(0); m++)
                    {
                        DbHelperSQL.ExecuteSql("update T_Base_device set F_LockedState=" + MonirotIdx + " where F_DeviceIndex=" + Convert.ToInt32(split[m]) + "and F_DeviceIndex<>" + Convert.ToInt32(AssociateDevice));
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                sep = null;
                split = null;
                dvs = null;
            }
        }

        /// <summary>
        /// 根据前面得到的F_MonitorIndex值获得被锁定的设备索引
        /// </summary>
        /// <returns>以“；”隔开的设备索引组字符串</returns>
        private string GetLockedDeviceIndex(int MonitorIndex)
        {
            DataSet ds;
            try
            {
                string sql = "select F_RunningLock from T_Monitor_Task where F_MonitorIndex=" + MonitorIndex;
                ds = DbHelperSQL.Query(sql);
                if (ds.Tables[0].DefaultView.Count > 0)
                {
                    return ds.Tables[0].DefaultView[0]["F_RunningLock"].ToString();
                }
                else
                    return "";
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ds = null;
            }
        }

        /// <summary>
        /// 根据前面得到的MonitorIndex值取得"关联设备索引-命令"
        /// </summary>
        /// <returns>关联设备索引-命令，null表示没有</returns>
        private string GetAssociateDevice(int MonitorIndex)
        {
            DataView dv;
            DataView dvv;
            try
            {
                string sql = "select F_Associate from T_Monitor_Task where (F_Associate IS NOT NULL) and F_MonitorIndex=" + MonitorIndex;
                dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    string sql1 = "select F_DeviceIndex,F_DeviceCommandIndex from T_Monitor_Task where F_MonitorIndex=" + dv[0]["F_Associate"];
                    dvv = DbHelperSQL.Query(sql1).Tables[0].DefaultView;
                    if (dvv.Count > 0)
                    {
                        return dvv[0]["F_DeviceIndex"].ToString() + "-" + dvv[0]["F_DeviceCommandIndex"].ToString();
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
                dvv = null;
            }
        }

        //---------------------------------------------------------------------------------------------------------

        /// <summary>
        /// 在设备指令信息中获得堆垛机的
        /// 取货点Z坐标（站在堆垛机上，面向操作面板：左侧-1，右侧-2）
        /// 取货点X坐标（沿轨道方向）
        /// 取货点Y坐标（沿高度方向）
        /// 送货点Z坐标（站在堆垛机上，面向操作面板：左侧-1，右侧-2）
        /// 送货点X坐标（沿轨道方向）
        /// 送货点Y坐标（沿高度方向）
        /// </summary>
        /// <param name="TaskIdx">设备指令索引</param>
        /// <returns>0--5六个数组分别依次代表６个坐标</returns>
        public int[] GetCoordinatesFromMonitorTask(int TaskIdx)
        {
            DataView dv;
            int[] gc;
            try
            {
                dv = DbHelperSQL.Query("select * from T_Monitor_Task where F_MonitorIndex=" + TaskIdx).Tables[0].DefaultView;
                gc = new int[6];
                //F_NumParam1--z,x--2,y--3;F_NumParam4--z,5--x,6--y
                if (dv.Count > 0)
                {
                    gc[0] = Convert.ToInt32(dv[0]["F_NumParam1"]);
                    gc[1] = Convert.ToInt32(dv[0]["F_NumParam2"]);
                    gc[2] = Convert.ToInt32(dv[0]["F_NumParam3"]);
                    gc[3] = Convert.ToInt32(dv[0]["F_NumParam4"]);
                    gc[4] = Convert.ToInt32(dv[0]["F_NumParam5"]);
                    gc[5] = Convert.ToInt32(dv[0]["F_NumParam6"]);
                    return gc;
                }
                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
                gc = null;
            }
        }

        /// <summary>
        /// 在调度队列中找到设备命令
        /// </summary>
        /// <param name="MonitorIndex">调度所引</param>
        /// <returns></returns>
        public 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;
                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 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 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;
            }
        }

        public int GetFCONTROLTASKTYPEFromManageTask(int taskKindIndex, int Managefid)
        {
            //20100108
            DataView dv;
            try
            {
                //20100108
                dv = DbHelperSQL.Query("SELECT FCONTROLTASKTYPE FROM T_Manage_Task WHERE (FID = " + Managefid + ") AND (F_ManageTaskKindIndex = " + taskKindIndex + ")").Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    if (dv[0]["FCONTROLTASKTYPE"] == DBNull.Value) return -1;
                    return Convert.ToInt32(dv[0]["FCONTROLTASKTYPE"]);
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception ex)
            {//20100108
                throw ex;
            }
            finally
            {//20100108
                dv = null;
            }
        }

        /// <summary>
        /// 是否需要优化调度
        /// </summary>
        /// <param name="DeviceIdx"></param>
        /// <returns></returns>
        public bool NeedOptimize(int DeviceIdx)
        {
            DataView dv;
            try
            {
                dv = DbHelperSQL.Query("SELECT F_DeviceIndex, F_NeedOptimize FROM T_Base_Device where F_DeviceIndex=" + DeviceIdx + " and  F_NeedOptimize='1'").Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return true;
                }
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
            }
        }

        /// <summary>
        /// 根据设备指令索引MonitorIndex检索出需要向T_Monitor_Task_Child插入的纪录，并且完成插入工作
        /// </summary>
        /// <param name="MonitorIndex"></param>
        public void InsertMonitorOptimizeChildTask(int MonitorIndex)
        {
            try
            {
                //起始x坐标，列-沿轨道方向
                // 起始y坐标，层-高度方向
                // 起始z坐标，排-面向堆垛机操作面板，1-左侧，2-右侧
                //F_NumParam1--z,x--2,y--3;F_NumParam4--z,5--x,6--y
                string sql;
                int[] childInfo = GetMonitorChildIndex(MonitorIndex);
                sql = "INSERT INTO T_Monitor_Task_Child(F_Child_Index, F_TargetX, F_TargetY, F_TargetZ, F_OriginX, F_OriginY, F_OriginZ, F_Order,F_DeviceKindIndex,F_DummyDeviceIndex,F_MonitorIndex, F_Status)" +
                    "VALUES (" + childInfo[0] + "," + childInfo[2] + "," + childInfo[3] + "," + childInfo[1] + "," + childInfo[5] + "," + childInfo[6] + "," + childInfo[4] + "," + childInfo[7] + "," + childInfo[8] + "," + childInfo[9] + "," + MonitorIndex + ",0,)";
                DbHelperSQL.ExecuteSql(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// //获得需要优化调度设备指令的子任务索引
        ///conInfo[9]=设备索引 ;
        ///conInfo[8]=设备种类索引 ;
        ///conInfo[7]=命令 ;
        ///conInfo[1] = ["F_NumParam1"];
        ///conInfo[2] = ["F_NumParam2"];
        ///conInfo[3] = ["F_NumParam3"];
        ///conInfo[4] = ["F_NumParam4"];
        ///conInfo[5] = ["F_NumParam5"];
        ///conInfo[6] = ["F_NumParam6"];
        /// </summary>
        /// <param name="monitorTaskIndex"></param>
        /// <returns>子任务索引值１－－６５５３５</returns>
        private int[] GetMonitorChildIndex(int monitorTaskIndex)
        {
            //20100108
            DataView dvMt;
            int[] conInfo = new int[10];
            DataView dv;

            try
            {
                int devKindIndex;

                //20100108
                dv = DbHelperSQL.Query("SELECT T_Base_Device.F_DeviceIndex,T_Base_Device.F_DeviceKindIndex,T_Monitor_Task.F_DeviceCommandIndex," +
                    "F_NumParam1,F_NumParam2,F_NumParam3,F_NumParam4,F_NumParam5,F_NumParam6 FROM T_Monitor_Task" +
                    " WHERE (T_Monitor_Task.F_MonitorIndex = " + monitorTaskIndex + ") and " +
                    " (T_Monitor_Task.F_DeviceIndex = T_Base_Device.F_DeviceIndex)").Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    devKindIndex = Convert.ToInt32(dv[0]["F_DeviceKindIndex"]);
                    conInfo[9] = Convert.ToInt32(dv[0]["F_DeviceIndex"]);
                    conInfo[8] = devKindIndex;
                    conInfo[7] = Convert.ToInt32(dv[0]["F_DeviceCommandIndex"]);
                    conInfo[1] = Convert.ToInt32(dv[0]["F_NumParam1"]);
                    conInfo[2] = Convert.ToInt32(dv[0]["F_NumParam2"]);
                    conInfo[3] = Convert.ToInt32(dv[0]["F_NumParam3"]);
                    conInfo[4] = Convert.ToInt32(dv[0]["F_NumParam4"]);
                    conInfo[5] = Convert.ToInt32(dv[0]["F_NumParam5"]);
                    conInfo[6] = Convert.ToInt32(dv[0]["F_NumParam6"]);
                }
                else
                {
                    return null;
                }
                string sql = "SELECT F_Child_Index FROM T_Monitor_Task_Child_Index";
                //20100108
                dvMt = DbHelperSQL.Query(sql).Tables[0].DefaultView;
                if (dvMt.Count > 0 && dvMt[0]["F_Child_Index"] != DBNull.Value)
                {
                    int maxIdx;
                    maxIdx = (Convert.ToInt32(dvMt[0]["F_Child_Index"]) + 1);
                    if (maxIdx >= 64000)
                    {
                        UpdateMonitorChildIndex(Convert.ToInt32(devKindIndex.ToString() + "0" + "1"));
                        conInfo[0] = Convert.ToInt32(devKindIndex.ToString() + "0" + "1");
                        return conInfo;
                    }
                    else
                    {
                        UpdateMonitorChildIndex(maxIdx);
                        conInfo[0] = maxIdx;
                        return conInfo;
                    }
                }
                else//现在没有该设备指令子任务记录
                {
                    UpdateMonitorIndex(Convert.ToInt32(devKindIndex.ToString() + "0" + "1"));
                    conInfo[0] = Convert.ToInt32(devKindIndex.ToString() + "0" + "1");
                    return conInfo;
                }
            }
            catch (Exception ex)
            {
                _DisassembleTaskError = "获得指定调度任务的设备指令索引失败！" + ex.Message;
                throw ex;
                //return 0;
            }
            finally
            {//20100108
                dv = null;
                dvMt = null;
                conInfo = null;
            }
        }

        private void UpdateMonitorChildIndex(int MonitorChildIndex)
        {
            DbHelperSQL.ExecuteSql("update T_Monitor_Task_Child_Index set F_Child_Index=" + MonitorChildIndex);
        }

        /// <summary>
        /// 获得设备指令任务的关联对等设备索引：堆垛机的取货指令任务的取货站台的双叉对等关联站台
        /// 返回值“-1”表示无关联站台；“0”表示自关联站台；其它值表示关联站台设备索引
        /// </summary>
        /// <param name="MonitorTask">设备指令索引</param>
        /// <param name="DeviceIndex">设备索引</param>
        /// <returns></returns>
        //public int GetCorrel_DeviceIndex(int MonitorTask)
        //{
        //    int[] zxy = GetCoordinatesFromMonitorTask(MonitorTask);
        //    string zxystr = "";
        //    if (zxy != null)
        //    {
        //        zxystr = (zxy[0].ToString().Length == 1 ? "0" + zxy[0].ToString() : zxy[0].ToString()) + "-" +
        //            ((zxy[1].ToString().Length == 1) ? ("0" + zxy[1].ToString()) : (zxy[1].ToString())) + "-" +
        //            ((zxy[2].ToString().Length == 1) ? ("0" + zxy[2].ToString()) : (zxy[2].ToString()));
        //        DataView dv = DbHelperSQL.Query("SELECT F_LaneGateDeviceIndex,F_CorrelDeviceIndex FROM T_Base_Lane_Gate where (T_Base_Lane_Gate.F_ZXY = '" + zxystr + "')").Tables[0].DefaultView;
        //        if (dv.Count > 0)
        //        {
        //            if (dv[0]["F_LaneGateDeviceIndex"].ToString() == dv[0]["F_CorrelDeviceIndex"].ToString())
        //            {
        //                return 0;
        //            }
        //            else
        //            {
        //                return int.Parse(dv[0]["F_CorrelDeviceIndex"].ToString());
        //            }
        //        }
        //    }
        //    return -1;
        //}
        /// <summary>
        /// 任务拆分 调用
        /// 根据设备索引获得关联设备信息：【0】有物检测【1】顶升高位检测【2】顶升低位检测
        /// 【3】设备索引【4】运行监测【5】近巷道2列有物检测【6】远巷道1列有物检测【7】是否使用远货叉【8】关联设备索引【9】顶升中位检测
        /// </summary>
        /// <param name="DeviceIndex">设备索引</param>
        /// <param name="ownCorrel">是否自关联</param>
        /// <returns></returns>
        public string[] GetCorrel_DeviceInfo(int DeviceIndex, bool ownCorrel)
        {
            string[] rr = new string[4];

            // 顶升设备是为普通的输送机处理  2011//11/16
            DataView dv = DbHelperSQL.Query("SELECT * FROM T_Base_Lane_Gate  where  F_LaneGateDeviceIndex = " + DeviceIndex).Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                //rr[0] = dv[0]["F_LaneGateDeviceIndex"].ToString();
                //rr[1] = dv[0]["F_HighDetect"].ToString();
                //rr[2] = dv[0]["F_LowDetect"].ToString();
                //rr[3] = dv[0]["F_HavingDetect"].ToString();
                //rr[4] = dv[0]["F_RunDetect"].ToString();
                //rr[5] = dv[0]["F_NearDetect"].ToString();
                //rr[6] = dv[0]["F_FarDetect"].ToString();
                //rr[7] = dv[0]["F_UseAwayFork"].ToString();
                //rr[8] = dv[0]["F_CorrelDeviceIndex"].ToString();
                //rr[9] = dv[0]["F_MiddleDetect"].ToString();//20101220 升降站台中位检测
                rr[0] = dv[0]["F_HavingDetect"].ToString();
                rr[1] = dv[0]["F_HighDetect"].ToString();
                rr[2] = dv[0]["F_LowDetect"].ToString();
                rr[3] = dv[0]["F_LaneGateDeviceIndex"].ToString();
                //rr[3] = dv[0]["F_HavingDetect"].ToString();
                // rr[4] = dv[0]["F_RunDetect"].ToString();
                if (ownCorrel == true)
                {
                    if (dv[0]["F_LaneGateDeviceIndex"].ToString() == DeviceIndex.ToString())
                    {
                        return rr;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return rr;
                }
            }
            return null;
        }


        Model.MDevice devinfo;
        /// <summary>
        /// 发送设备的送出指令时，需要检测的光电开关设备索引
        /// </summary>
        /// <param name="devinx">输送设备索引</param>
        /// <returns></returns>
        public string GetSendOutDetect(int devinx)
        {
            //20101118
            devinfo = BaseDeviceService.GetDeviceInfo(devinx);
            if (devinfo.SendOutDetect == null) return "";
            return devinfo.SendOutDetect;

        }
        public string GetBarCodeFromMonitor(int Monitorindex)
        {
            object ob = DbHelperSQL.GetSingle("SELECT  F_TxtParam  FROM T_Monitor_Task WHERE (F_MonitorIndex = " + Monitorindex + ")");
            if (ob != null)
            {
                return ob.ToString();
            }
            else
            {

                return "0";
            }
        }
    }
}