public int AllotSpaceId(string taskid, TransportStr si)
        {
            int n = 0;

            #region 向中间表插入数据,向WMS发起请求
            //发起前先判断中间表是否已存在该条申请,状态1=下发  2=已处理状态
            DataSet dsisexist = DataTrans.D_GetIDX_ASRS_SEND(taskid, "15", "1','2");
            if (dsisexist != null && dsisexist.Tables[0].Rows.Count == 0)
            {
                DataSet ds = DataTrans.D_GetAlleyIdBytaskid(si.Taskid);
                if (ds == null)
                {
                    return(0);
                }
                string alleyid = ds.Tables[0].Rows[0]["TCARGO_ALLEY_ID"].ToString();
                //15=货位申请  1=下方
                int k = DataTrans.D_InsertIDX_ASRS_SEND("15", "1", taskid, si.TRAYCODE, "", alleyid);
                if (k > 0)
                {
                    logWrite.WriteLog($"输送机{si.SSJID}调度指令{taskid}入库已向WMS申请货位托盘条码为{si.TRAYCODE}");
                    NotifyEvent?.Invoke($"输送机{si.SSJID}调度指令{taskid}入库已向WMS申请货位托盘条码为{si.TRAYCODE}");
                }
                else
                {
                    logWrite.WriteLog($"输送机{si.SSJID}调度指令{taskid}入库向WMS未申请到货位");
                    NotifyEvent?.Invoke($"输送机{si.SSJID}调度指令{taskid}入库向WMS未申请到货位");
                }
            }
            #endregion

            //获取WMS重新分配的货位,2代表WMS已下发
            DataSet dsidx = DataTrans.D_GetIDX_ASRS_SEND(taskid, "15", "2");
            //若是查询到数据说明获取到货位
            if (dsidx != null && dsidx.Tables[0].Rows.Count > 0)
            {
                logWrite.WriteLog($"输送机{si.SSJID}调度指令{taskid}申请的货位WMS已处理");
                NotifyEvent?.Invoke($"输送机{si.SSJID}调度指令{taskid}申请的货位WMS已处理");
                //临时库位字段
                string spaceid = dsidx.Tables[0].Rows[0]["LOCATION"].ToString();
                //根据货位id获取货位信息
                DataSet ds = DataTrans.P_getSpaceInfo(spaceid);
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    //货位名称
                    string spacename = ds.Tables[0].Rows[0]["CARGO_SPACE_NAME"].ToString();
                    //货位排
                    string tCSPLATOON = ds.Tables[0].Rows[0]["CSPLATOON"].ToString();
                    //货位列
                    string tCSCOLUMN = ds.Tables[0].Rows[0]["CSCOLUMN"].ToString();
                    //货位层
                    string tCSFLOOR = ds.Tables[0].Rows[0]["CSFLOOR"].ToString();

                    n = DataTrans.P_UpSchSpaceInfo(spaceid, spacename, tCSPLATOON, tCSCOLUMN, tCSFLOOR, si.Taskid);
                }
            }
            return(n);
        }
        /// <summary>
        /// 给堆垛机下达任务
        /// </summary>
        /// <param name="craneid"></param>
        /// <param name="buffer"></param>
        public void IssuedCraneTask(string craneid, byte[] buffer)
        {
            CraneStr cs = CRLBase.CraneStrList.Find(c => c.Btid == craneid);

            //任务结束,删除指令,放货完成
            if (cs.Jdbz == "4")
            {
                cs.Zyfs = "4";
                LogWrite.WriteLog($"放货完成开始删除堆垛机{cs.Btid}任务{cs.Zxrwh}");
                NotifyShowEvent?.Invoke("R", $"防火完成开始删除堆垛机{cs.Btid}任务{cs.Zxrwh}");
                if (WriteToCrane(cs))
                {
                    DataSet dssch = DataTrans.D_GetSchByTaskno(cs.Zxrwh.ToString());
                    if (dssch == null)
                    {
                        return;
                    }
                    if (dssch.Tables[0].Rows.Count > 0)
                    {
                        //入库或者移库
                        if (dssch.Tables[0].Rows[0]["TASKTYPE"].ToString() == "1" || dssch.Tables[0].Rows[0]["TASKTYPE"].ToString() == "3")
                        {
                            DataTrans.D_CraneCommandTrans(cs.Btid, dssch.Tables[0].Rows[0]["TASKID"].ToString(), "4", "4", "4");
                            LogWrite.WriteLog($"放货完成删除堆垛机{cs.Btid}任务{cs.Zxrwh}完成 托盘条码:{dssch.Tables[0].Rows[0]["traycode"]}");
                            NotifyShowEvent?.Invoke("R", $"放货完成删除堆垛机{cs.Btid}任务{cs.Zxrwh}完成 托盘条码:{dssch.Tables[0].Rows[0]["traycode"]}");
                        }
                        //出库
                        else if (dssch.Tables[0].Rows[0]["TASKTYPE"].ToString() == "2")
                        {
                            DataTrans.D_CraneCommandTrans(cs.Btid, dssch.Tables[0].Rows[0]["TASKID"].ToString(), "3", "1", "3");
                            LogWrite.WriteLog($"放货完成删除堆垛机{cs.Btid}任务{cs.Zxrwh}完成 托盘条码:{dssch.Tables[0].Rows[0]["traycode"]}");
                            NotifyShowEvent?.Invoke("R", $"放货完成删除堆垛机{cs.Btid}任务{cs.Zxrwh}完成 托盘条码:{dssch.Tables[0].Rows[0]["traycode"]}");
                        }
                    }
                }
                //入库目标有货
                else if (cs.Rkyh == "1" && cs.Alarm != "0")
                {
                    //根据任务号获取调度指令
                    DataSet dsoldsh = DataTrans.D_GetSchByTaskno(cs.Zxrwh.ToString());

                    //判断是否入库或者移库
                    if ((dsoldsh != null && dsoldsh.Tables[0].Rows.Count > 0 && (dsoldsh.Tables[0].Rows[0]["tasktype"].ToString() == "1")) || dsoldsh.Tables[0].Rows[0]["tasktype"].ToString() == "3")
                    {
                        #region 向中间表插入数据,向WMS发起请求
                        //发起前先判断中间表是否已存在该条申请,状态为 1=已下发,2=已处理
                        DataSet dsisexist = DataTrans.D_GetIDX_ASRS_SEND(dsoldsh.Tables[0].Rows[0]["TASKID"].ToString(), "10", $"1','2");
                        if (dsisexist != null && dsisexist.Tables[0].Rows.Count == 0)
                        {
                            LogWrite.WriteLog($"堆垛机{cs.Btid}调度指令{dsoldsh.Tables[0].Rows[0]["taskid"]}入库货位有货,开始向WMS申请新货位...");
                            NotifyShowEvent?.Invoke("R", $"堆垛机{cs.Btid}调度指令{dsoldsh.Tables[0].Rows[0]["taskid"]}入库货位有货,开始向WMS申请新货位...");
                            string alleyid = AlleyIdRelation.GetAlleyId(cs.Btid);
                            //10代表存货占位,1代表下发
                            int    n   = DataTrans.D_InsertIDX_ASRS_SEND("10", "1", dsoldsh.Tables[0].Rows[0]["taskid"].ToString(), dsoldsh.Tables[0].Rows[0]["TRAYCODE"].ToString(), dsoldsh.Tables[0].Rows[0]["TCARGO_SPACE_ID"].ToString(), alleyid);
                            string res = n > 0 ? "成功" : "失败";
                            LogWrite.WriteLog($"堆垛机{cs.Btid}调度指令{dsoldsh.Tables[0].Rows[0]["taskid"]}入库货位有货,向WMS申请新货位{res}");
                            NotifyShowEvent?.Invoke("R", $"堆垛机{cs.Btid}调度指令{dsoldsh.Tables[0].Rows[0]["taskid"]}入库货位有货,向WMS申请新货位{res}");

                            //等待wms处理2s
                            Thread.Sleep(2000);
                        }
                        #endregion

                        //获取WMS重新分配的货位
                        DataSet dsidx = DataTrans.D_GetIDX_ASRS_SEND(dsoldsh.Tables[0].Rows[0]["TASKID"].ToString(), "10", "2");
                        //查询到数据说明获取到货位
                        if (dsidx != null && dsidx.Tables[0].Rows.Count > 0)
                        {
                            LogWrite.WriteLog($"堆垛机{cs.Btid}货位有货重新分配货位");
                            NotifyShowEvent?.Invoke("R", $"堆垛机{cs.Btid}货位有货重新分配货位");

                            string taskid = dsoldsh.Tables[0].Rows[0]["TASKID"].ToString();
                            string sendid = dsidx.Tables[0].Rows[0]["SENDID"].ToString();
                            //重分的货位号
                            string spaceid = dsidx.Tables[0].Rows[0]["LOCATION"].ToString();
                            //根据货位号获取货位信息
                            DataSet dsspace = DataTrans.P_getSpaceInfo(spaceid);
                            if (dsspace == null)
                            {
                                return;
                            }
                            //目标排号
                            string desrow = dsspace.Tables[0].Rows[0]["CSPLATOON"].ToString();
                            //目标列号
                            string descolnum = dsspace.Tables[0].Rows[0]["CSCOLUMN"].ToString();
                            //目标层号
                            string desfloor = dsspace.Tables[0].Rows[0]["CSFLOOR"].ToString();
                            //目标名称
                            string spacename = dsspace.Tables[0].Rows[0]["CARGO_SPACE_NAME"].ToString();

                            int remainder = Convert.ToInt32(desrow) % 2;
                            cs.Mbph = remainder == 0 ? "2" : "1";

                            cs.Mblh = descolnum;
                            cs.Mbch = desfloor;
                            AlleyIdRelation.GetRKDesSpace(cs.Btid, ref cs);
                            cs.Zyfs = "5";
                            if (WriteToCrane(cs))
                            {
                                LogWrite.WriteLog($"{cs.Btid}满入解警成功");
                                NotifyShowEvent?.Invoke("R", $"{cs.Btid}满入解警成功");
                                //更新相关表货位信息
                                DataTrans.D_GetSpaceUpInfo(taskid, cs.Btid, "3", sendid, spaceid, desrow, descolnum, desfloor, spacename);
                                cs.Zyfs = "1";

                                string res = WriteToCrane(cs) ? "成功" : "失败";

                                LogWrite.WriteLog($"给堆垛机{cs.Btid}重新分货位{spaceid}{res}");
                                NotifyShowEvent?.Invoke("R", $"给堆垛机{cs.Btid}重新分货位{spaceid}{res}");
                            }
                            else
                            {
                                LogWrite.WriteLog($"{cs.Btid}满入解警失败");
                                NotifyShowEvent?.Invoke("R", $"{cs.Btid}满入解警失败");
                            }
                        }
                    }
                }
                //出库无货
                else if (cs.Ckwh == "1" && cs.Alarm != "0")
                {
                    //根据任务号获取调度指令
                    DataSet dsoldsch = DataTrans.D_GetSchByTaskno(cs.Zxrwh.ToString());

                    //判断是否出库
                    //出库将旧指令删除即可,调度指令作废
                    if (dsoldsch != null && dsoldsch.Tables[0].Rows.Count > 0 && dsoldsch.Tables[0].Rows[0]["tasktype"].ToString() == "2")
                    {
                        #region 向中间表插入数据,向WMS发起请求
                        //发起前先判断中间表是否已存在该条申请
                        //状态  1=下发,2=已处理状态
                        DataSet dsisexist = DataTrans.D_GetIDX_ASRS_SEND(dsoldsch.Tables[0].Rows[0]["TASKID"].ToString(), "11", $"1','3',4','2");
                        if (dsisexist.Tables[0].Rows.Count == 0)
                        {
                            //判断出库目标无货无申请,则插入一条申请
                            string alleyid = AlleyIdRelation.GetAlleyId(cs.Btid);
                            //11=出库目标无货  1=下发
                            int n = DataTrans.D_InsertIDX_ASRS_SEND("11", "1", dsoldsch.Tables[0].Rows[0]["taskid"].ToString(), dsoldsch.Tables[0].Rows[0]["TRAYCODE"].ToString(), dsoldsch.Tables[0].Rows[0]["TCAGRO_SPACE_ID"].ToString(), alleyid);
                            if (n > 0)
                            {
                                //插入成功,将调度指令更新为作废,将中间表更新为已完成
                                int k = DataTrans.D_RKYHDel(cs.Btid, dsoldsch.Tables[0].Rows[0]["taskid"].ToString());
                                if (k > 0)
                                {
                                    string res = WriteToCrane(cs) ? "成功" : "失败";
                                    LogWrite.WriteLog($"{cs.Btid}空出解警成功");
                                    NotifyShowEvent?.Invoke("R", $"{cs.Btid}空出解警成功");
                                }
                            }
                        }
                        #endregion
                    }
                    else
                    {
                        cs.Zyfs = "5";
                        string res = WriteToCrane(cs) ? "成功" : "失败";
                        LogWrite.WriteLog($"{cs.Btid}空出解警{res}");
                        NotifyShowEvent?.Invoke("R", $"{cs.Btid}空出解警{res}");
                    }
                }
                //待机
                else if (cs.Jdbz == "0")
                {
                    #region 一楼出库
                    //一楼出库
                    if (runorder == 1 && n == 0)
                    {
                        DataTable dt = new DataTable();
                        //查询一楼出库口输送机信息,设备类型为103
                        string       dttype = "103";
                        TransportStr ts     = lsTransport.Find(t => t.DTYPE == dttype && t.BTID == cs.Btid && t.KXBZ == "1");

                        if (ts == null)
                        {
                            return;
                        }
                        crl.HsWcsReadSSJ(ref ts);
                        if (ts.KXBZ != "1")
                        {
                            return;
                        }
                        //查询调度任务,
                        //任务类型2(TASKTYPE出库),任务类型描述(DESCRIPTION),状态为2(STATUS待执行)
                        string alleyid = AlleyIdRelation.GetAlleyId(cs.Btid);
                        //查询出库调度任务
                        //任务类型为2(TASKTYPE出库),层号(FLOOR)为1,状态为2(STATUS待执行)
                        DataSet dstask = DataTrans.D_GetOutSchTaskByCrane("2", "1", "2", alleyid, string.Empty, cs);
                        if (dstask != null && dstask.Tables[0].Rows.Count > 0)
                        {
                            dt = dstask.Tables[0].Clone();
                            dt.ImportRow(dstask.Tables[0].Rows[0]);
                            bool flag1 = true;
                            //调度指令表中任务号不存在,分配新的任务号
                            if (dt.Rows[0]["task"].ToString() == "0" || string.IsNullOrEmpty(dt.Rows[0]["taskno"].ToString()))
                            {
                                //任务号分配
                                string taskno = DataTrans.D_AllotTaskno().ToString();
                                if (!string.IsNullOrEmpty(taskno) && taskno != "0")
                                {
                                    dt.Rows[0]["taskno"] = taskno;
                                    flag1 = DataTrans.D_UpSchTask(taskno, dt.Rows[0]["taskid"].ToString());
                                }
                                else
                                {
                                    flag1 = false;
                                }
                            }
                            if (flag1)
                            {
                                LogWrite.WriteLog($"巷道{cs.Btid}开始一楼出库任务,任务号为{dt.Rows[0]["taskno"]}托盘条码:{dt.Rows[0]["traycode"]}");
                                NotifyShowEvent?.Invoke("R", $"巷道{cs.Btid}开始一楼出库任务,任务号为{dt.Rows[0]["taskno"]}托盘条码:{dt.Rows[0]["traycode"]}");
                                bool flag = DDJCommand(ref cs, dt);

                                if (flag)
                                {
                                    //根据巷道获取出库目标
                                    AlleyIdRelation.GetCKOneDesSpace(cs.Btid, ref cs);

                                    //出库
                                    cs.Zyfs = "2";

                                    if (WriteToCrane(cs))
                                    {
                                        LogWrite.WriteLog($"给堆垛机{cs.Btid}下发出库任务{dt.Rows[0]["taskno"]}成功,托盘条码:{dt.Rows[0]["traycode"]},目标地址:{cs.Mbph}排{cs.Mblh}列{cs.Mbch}层");
                                        NotifyShowEvent?.Invoke("R", $"给堆垛机{cs.Btid}下发出库任务{dt.Rows[0]["taskno"]}成功,托盘条码:{dt.Rows[0]["traycode"]},目标地址:{cs.Mbph}排{cs.Mblh}列{cs.Mbch}层");
                                        DataTrans.D_CraneCommandTrans(cs.Btid, dt.Rows[0]["TASKID"].ToString(), "2", "2", "3");
                                        Thread.Sleep(1000);
                                    }
                                    else
                                    {
                                        LogWrite.WriteLog($"给堆垛机{cs.Btid}下发出库任务{dt.Rows[0]["taskno"]}失败");
                                        NotifyShowEvent?.Invoke("R", $"给堆垛机{cs.Btid}下发出库任务{dt.Rows[0]["taskno"]}失败");
                                    }
                                }
                            }
                        }
                    }
                    #endregion

                    #region 一楼入库
                    else if (runorder == 2)
                    {
                        DataTable dt = new DataTable();
                        //查询二楼入库口输送机信息,设备类型为207
                        string       dttype = "102";
                        TransportStr ts     = lsTransport.Find(s => s.DTYPE == dttype && s.BTID == cs.Btid && s.DWXH == "1");
                        if (ts == null)
                        {
                            return;
                        }
                        else
                        {
                            crl.HsWcsReadSSJ(ref ts);
                            if (ts.DWXH != "1")
                            {
                                return;
                            }
                            //根据输送机任务号获取调度信息
                            DataSet   dstask = DataTrans.D_GetSchByTaskno(ts.ZXRWH);
                            DataRow[] drs    = dstask.Tables[0].Select("TCARGO_SPACE_ID is not null", "");

                            if (drs.Length > 0)
                            {
                                dt = dstask.Tables[0].Clone();
                                dt.ImportRow(drs[0]);
                                LogWrite.WriteLog($"开始入库任务,任务号为:{dt.Rows[0]["taskno"]},托盘条码:{dt.Rows[0]["traycode"]}");
                                NotifyShowEvent?.Invoke("R", $"开始入库任务,任务号为:{dt.Rows[0]["taskno"]},托盘条码:{dt.Rows[0]["traycode"]}");
                                bool flag = DDJCommand(ref cs, dt);
                                if (flag)
                                {
                                    //如果入库,目标地址为巷道
                                    AlleyIdRelation.GetRKDesSpace(cs.Btid, ref cs);
                                    cs.Zyfs = "1";
                                    //如果插入指令表成功,将指令写入到堆垛机中
                                    if (WriteToCrane(cs))
                                    {
                                        LogWrite.WriteLog($"给堆垛机{cs.Btid}下发任务{dt.Rows[0]["taskno"]}成功,托盘条码:{dt.Rows[0]["traycode"]},目标地址{cs.Mbph}排-{cs.Mblh}列-{cs.Mbch}层");
                                        NotifyShowEvent?.Invoke("R", $"给堆垛机{cs.Btid}下发任务{dt.Rows[0]["taskno"]}成功,托盘条码:{dt.Rows[0]["traycode"]},目标地址{cs.Mbph}排-{cs.Mblh}列-{cs.Mbch}层");
                                        DataTrans.D_CraneCommandTrans(cs.Btid, dt.Rows[0]["TASKID"].ToString(), "3", "3", "3");
                                        Thread.Sleep(1000);
                                    }
                                    else
                                    {
                                        LogWrite.WriteLog($"给堆垛机{cs.Btid}下发入库任务{dt.Rows[0]["taskno"]}失败");
                                        NotifyShowEvent?.Invoke("R", $"给堆垛机{cs.Btid}下发入库任务{dt.Rows[0]["taskno"]}失败");
                                    }
                                }
                                return;
                            }
                        }
                    }
                    #endregion
                }
            }
        }