using RGD.Common;
using RGD.DataService;
using RGD.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;

namespace RGD.MdsAPI
{
    public class HouseBase : Base
    {
        [Serializable]
        public class LOCATION
        {
            private string _loc_no;
            private string _pallet_code;
            private string _need_back;

            public string LOC_NO
            {
                get { return this._loc_no; }
                set { this._loc_no = getWCS_LOC_NO(value); }
            }

            public string PALLET_CODE
            {
                get { return this._pallet_code; }
                set { this._pallet_code = value; }
            }

            public string NEED_BACK
            {
                get { return this._need_back; }
                set { this._need_back = value; }
            }
        }

        public class LOC_BoxInvTask
        {
            private string _loc_no;
            private string _pallet_code;
            private string _box_type;

            public string LOC_NO
            {
                get { return this._loc_no; }
                set { this._loc_no = getWCS_LOC_NO(value); }
            }

            public string PALLET_CODE
            {
                get { return this._pallet_code; }
                set { this._pallet_code = value; }
            }

            public string BOX_TYPE
            {
                get { return this._box_type; }
                set { this._box_type = value; }
            }
        }

        /// <summary>调用表库
        /// 调用表库
        /// </summary>
        /// <param name="sMethodName">方法名</param>
        /// <param name="sXmlIn">入参</param>
        /// <param name="sXmlOut">出参</param>
        /// <returns></returns>
        public bool InvokeHouse(String sMethodName, String sXmlIn, out string sXmlOut)
        {
            bool bResult = true;
            sXmlOut = string.Empty;
            DateTime dtBegin = System.DateTime.Now;
            DateTime dtEnd;
            try
            {
                string sClassFullName = string.Format("RGDWCSServices.WCS.{0}", sMethodName);

                bResult = this.Invoke(sClassFullName, "Notify", sXmlIn, out sXmlOut);
            }
            catch (Exception ex)
            {
                bResult = false;

                sXmlOut = ex.Message;
            }
            finally
            {
                dtEnd = System.DateTime.Now;
                string str = string.Format("平台调用表库接口开始时间 InvokeHouse：{3}\n\r方法：{0}\n\r入参：{1}\n\r出参：{2}\n\r", sMethodName, sXmlIn, sXmlOut, dtBegin.ToString("yyyyMMdd HH:mm:ss"));
                //this._log.Debug(str);
                LogUtil.WriteLog("D:\\调度与平台接口日志\\log", "WCS调用WMS", str);
                System.Diagnostics.Debug.WriteLine(str);
            }

            return bResult;
        }

        /// <summary>反射调用
        /// 反射调用
        /// </summary>
        /// <param name="sClassFullName">类名（全）</param>
        /// <param name="sMethodName">方法</param>
        /// <param name="sXmlIn">入参</param>
        /// <param name="sXmlOut">出参</param>
        /// <returns>返回结果</returns>
        public bool Invoke(string sClassFullName, string sMethodName, object sXmlIn, out string sXmlOut)
        {
            bool bResult = true;

            sXmlOut = string.Empty;

            Type t = this.GetType();

            Assembly complierAssembly = t.Assembly;

            object complierInstance = complierAssembly.CreateInstance(sClassFullName);

            Type type = complierInstance.GetType();

            MethodInfo Method = type.GetMethod(sMethodName);

            object[] obj = new object[] { sXmlIn, sXmlOut };

            object oResult = Method.Invoke(complierInstance, obj);

            bResult = Convert.ToBoolean(oResult);

            sXmlOut = obj[1].ToString();

            return bResult;
        }

        public bool START_INTASK(string WH_ID, string EQUIP_TYPE, string EQUIP_COUNT, string TASK_NO, string PLATFORM_NO, string IO_WH_SORT_CODE, List<LOCATION> LOC_LIST, string xmlin, out string sResult)
        {
            bool bResult = true;
            sResult = string.Empty;

            DataSet ds_task = DbHelperSQL.Query("select * from T_warehouse");
            if (ds_task.Tables[0].Rows[0]["F_TASK_NO"] != DBNull.Value && ds_task.Tables[0].Rows[0]["F_TASK_NO"].ToString() != "0" && ds_task.Tables[0].Rows[0]["F_TASK_NO"].ToString() != "")
            {
                if (ds_task.Tables[0].Rows[0]["F_TASK_NO"].ToString() == TASK_NO)
                {
                    return true;
                }
                DbHelperSQL.ExecuteSql("insert into T_TASK_LIST TASK_NO,METHOD_NAME,XMLIN,TASK_STATUS,TASK_START_TIME values ('" + TASK_NO + "','START_INTASK','" + xmlin + "','Waiting',getdate())");
                sResult = "有正在执行中的任务。任务号：" + ds_task.Tables[0].Rows[0]["F_TASK_NO"] + ",任务类型：" + ds_task.Tables[0].Rows[0]["F_TASK_TYPE"];
                return true;
            }

            if (LOC_LIST.Count == 0)
            {
                DataSet ds = DbHelperSQL.Query("select * from IO_RFID where STATION_CODE='" + PLATFORM_NO + "'");
                if (ds.Tables[0].Rows.Count == 0)
                {
                    sResult = "未找到入库站台";
                    return false;
                }
            }
            else
            {
                new ManageTaskService().CreateMove(TASK_NO, Enum.CONTROL_TYPE.Up.ToString("d"), PLATFORM_NO, LOC_LIST[0].LOC_NO, LOC_LIST[0].PALLET_CODE, out sResult);
            }

            DbHelperSQL.ExecuteSql("update T_WareHouse set F_WarehouseIndex='" + WH_ID + "',F_TASK_NO='" + TASK_NO + "',F_TASK_TYPE='In',F_TASK_STATUS='RUN',F_EQUIP_TYPE='" + EQUIP_TYPE + "',F_EQUIP_LEFT='" + EQUIP_COUNT + "',F_IO_WH_SORT_CODE='" + IO_WH_SORT_CODE + "'");

            return bResult;
        }

        public bool START_OUTTASK(string WH_ID, string EQUIP_TYPE, string TASK_NO, string PLATFORM_NO, string IO_WH_SORT_CODE, List<LOCATION> LOC_LIST, string xmlin, out string sResult)
        {
            bool bResult = true;
            sResult = string.Empty;
            DataSet ds_task = DbHelperSQL.Query("select * from T_warehouse");
            if (ds_task.Tables[0].Rows[0]["F_TASK_NO"] != DBNull.Value && ds_task.Tables[0].Rows[0]["F_TASK_NO"].ToString() != "0" && ds_task.Tables[0].Rows[0]["F_TASK_NO"].ToString() != "")
            {
                if (ds_task.Tables[0].Rows[0]["F_TASK_NO"].ToString() == TASK_NO)
                {
                    return true;
                }
                DbHelperSQL.ExecuteSql("insert into T_TASK_LIST TASK_NO,METHOD_NAME,XMLIN,TASK_STATUS,TASK_START_TIME values ('" + TASK_NO + "','START_INTASK','" + xmlin + "','Waiting',getdate())");
                sResult = "有正在执行中的任务。任务号：" + ds_task.Tables[0].Rows[0]["F_TASK_NO"] + ",任务类型：" + ds_task.Tables[0].Rows[0]["F_TASK_TYPE"];
                return true;
            }

            if (LOC_LIST.Count() <= 0)
            {
                sResult = "出库下发的货位号为空！";
                return false;
            }
            //dbo.TransBegin();

            if (EQUIP_TYPE == "05" && PLATFORM_NO == "12032")
            {
                PLATFORM_NO = "12046";
            }
            try
            {
                //切换回库叠盘机
                DataView dv = DbHelperSQL.Query("select * from T_Base_Stock_Station where equip_type='" + EQUIP_TYPE + " '").Tables[0].DefaultView;
                if (dv.Count == 0)
                {
                    DbHelperSQL.ExecuteSql("Insert into T_Base_Stock_Station values('" + EQUIP_TYPE + "',0)");
                }
                else
                {
                    DbHelperSQL.ExecuteSql("update T_Base_Stock_Station set change_station= change_station^1 where equip_type='" + EQUIP_TYPE + "'");
                }

                foreach (LOCATION LOC in LOC_LIST)
                {
                    string Sql, _tasktype, _startposition, _startsite = "-", _endposition, _endsite = "-";

                    DataSet ds;
                    _tasktype = "2";
                    _endposition = PLATFORM_NO;
                    int flaneway = 0;
                    int stackno = 0;

                    _startsite = LOC.LOC_NO;
                    ds = DbHelperSQL.Query("SELECT * FROM ST_CELL where FCELLSTATUS<> '-1' and FCELLCODE='" + _startsite + "'");
                    if (ds.Tables[0].Rows.Count == 0)
                    {
                        sResult = "起始位置和起始货位编码在数据库中没有记录或货位不可用！";
                        return false;
                    }
                    _startposition = ds.Tables[0].Rows[0]["FLaneWay"].ToString();
                    flaneway = Convert.ToInt32(_startposition);

                    //20100304
                    Sql = "SELECT F_RouteID FROM T_Base_Route WHERE (F_StartDevice = " + _startposition + ") AND (F_EndDevice = " + _endposition + ")";
                    ds = DbHelperSQL.Query(Sql).Tables[0].DataSet;
                    if (ds.Tables[0].DefaultView.Count <= 0)
                    {
                        sResult = "起始位置和结束位置不在有效路径的范围内！";
                        return false;
                    }

                    string FSTARTCELL = _startsite, FENDCELL = _endsite, UseAwayFork = "-";
                    int FSTARTDEVICE = Convert.ToInt32(_startposition); int FENDDEVICE = Convert.ToInt32(_endposition);

                    stackno = Convert.ToInt32(DbHelperSQL.GetSingle("SELECT F_StackIndex FROM T_Base_LaneInfo WHERE (F_LaneDeviceIndex = " + flaneway + ")"));
                    string dtime = DateTime.Now.ToString("u");
                    dtime = dtime.Substring(0, dtime.Length - 1);
                    int AutoManageIdx = GetAutoManageIdx();

                    int startcol = 0, startlayer = 0;

                    startcol = Convert.ToInt32(_startsite.Substring(3, 2));
                    startlayer = Convert.ToInt32(_startsite.Substring(6, 2));

                    int SENDOUT_TYPE = 1;
                    if (EQUIP_TYPE == "03")
                    {
                        SENDOUT_TYPE = 2;
                    }

                    Sql = "insert into T_Manage_Task(FID,FPALLETBARCODE,FCONTROLTASKTYPE,F_ManageTaskKindIndex,FSTARTWAREHOUSE,FSTARTDEVICE,FSTARTCELL," +
                    "FENDWAREHOUSE,FENDDEVICE,FENDCELL,FLANEWAY,FSTACK,FUseAwayFork,FBEGTIME,BOX_QUANTITY,UNPACK_QUANTITY,FStartCol,FStartLayer,FMANAGEID,SENDOUT_TYPE) " +
                    "values(" + AutoManageIdx + ",'" + LOC.PALLET_CODE + "','" + _tasktype + "',1,'" + GetWarehouseIndex() + "','" + _startposition + "','" + _startsite + "','" + GetWarehouseIndex() + "','"
                    + _endposition + "','" + _endsite + "'," + flaneway + "," + stackno + ",'" + UseAwayFork + "','" + dtime + "',1,1," + startcol + "," + startlayer + ",'" + TASK_NO + "'," + SENDOUT_TYPE + ")";
                    DbHelperSQL.ExecuteSql(Sql);
                }
                DbHelperSQL.ExecuteSql("update T_WareHouse set F_WarehouseIndex='" + WH_ID + "',F_TASK_NO='" + TASK_NO + "',F_TASK_TYPE='Out',F_TASK_STATUS='RUN',F_EQUIP_TYPE='" + EQUIP_TYPE + "',F_IO_WH_SORT_CODE='" + IO_WH_SORT_CODE + "'");
                DbHelperSQL.ExecuteSql("delete IO_VISION_DETAIL");
            }
            catch (Exception ex)
            {
                bResult = false;
                sResult = "下达出库任务时出错：" + ex.Message;
            }
            return bResult;
        }

        private int GetAutoManageIdx()
        {
            DataSet ds;
            try
            {
                int maxIdx = 1;
                ds = DbHelperSQL.Query("SELECT F_ManageTaskIndex FROM T_Base_Manage_Task_Index_Auto_Task");
                if (ds.Tables[0].DefaultView.Count > 0)
                {
                    if ((Convert.ToInt32(ds.Tables[0].DefaultView[0]["F_ManageTaskIndex"]) + 1) >= 9998)
                    {
                        maxIdx = 1;
                    }
                    else
                    {
                        maxIdx = (Convert.ToInt32(ds.Tables[0].DefaultView[0]["F_ManageTaskIndex"]) + 1);
                    }
                }
                else
                {
                    maxIdx = 1;
                }

                DataView dv = DbHelperSQL.Query("select F_ManageTaskIndex from T_Base_Manage_Task_Index_Auto_Task").Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    if (maxIdx > Convert.ToInt32(dv[0]["F_ManageTaskIndex"]) || (Convert.ToInt32(ds.Tables[0].DefaultView[0]["F_ManageTaskIndex"]) + 1) >= 9998)
                    {
                        DbHelperSQL.Query("UPDATE T_Base_Manage_Task_Index_Auto_Task SET F_ManageTaskIndex =" + maxIdx);
                    }
                }
                else
                {
                    DbHelperSQL.Query("INSERT INTO T_Base_Manage_Task_Index_Auto_Task (F_ManageTaskIndex)VALUES (" + maxIdx + ")");
                }
                return maxIdx;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ds = null;
            }
        }

        public bool START_INVENTORYTASK(string WH_ID, string EQUIP_TYPE, string TASK_NO, List<LOCATION> LOC_LIST, string xmlin, out string sResult)
        {
            bool bResult = true;
            sResult = string.Empty;

            DataSet ds_task = DbHelperSQL.Query("select * from T_warehouse");
            if (ds_task.Tables[0].Rows[0]["F_TASK_NO"] != DBNull.Value && ds_task.Tables[0].Rows[0]["F_TASK_NO"].ToString() != "0" && ds_task.Tables[0].Rows[0]["F_TASK_NO"].ToString() != "")
            {
                if (ds_task.Tables[0].Rows[0]["F_TASK_NO"].ToString() == TASK_NO)
                {
                    return true;
                }
                DbHelperSQL.ExecuteSql("insert into T_TASK_LIST TASK_NO,METHOD_NAME,XMLIN,TASK_STATUS,TASK_START_TIME values ('" + TASK_NO + "','START_INTASK','" + xmlin + "','Waiting',getdate())");
                sResult = "有正在执行中的任务。任务号：" + ds_task.Tables[0].Rows[0]["F_TASK_NO"] + ",任务类型：" + ds_task.Tables[0].Rows[0]["F_TASK_TYPE"];
                return true;
            }

            if (LOC_LIST.Count() <= 0)
            {
                sResult = "盘库下发的货位号为空！";
                return false;
            }
            DbHelperSQL.ExecuteSql("update T_WareHouse set F_WarehouseIndex='" + WH_ID + "',F_TASK_NO='" + TASK_NO + "',F_TASK_TYPE='Inventory',F_TASK_STATUS='RUN',F_EQUIP_TYPE='" + EQUIP_TYPE + "'");
            foreach (LOCATION LOC in LOC_LIST)
            {
                string Sql, _tasktype, _startposition, _startsite = "-";// _endposition, _endsite = "-";

                DataSet ds;
                _tasktype = "5";
                //_endposition = PLATFORM_NO;
                int flaneway = 0;
                int stackno = 0;

                _startsite = LOC.LOC_NO;
                ds = DbHelperSQL.Query("SELECT * FROM ST_CELL where FCELLSTATUS<> '-1' and FCELLCODE='" + _startsite + "'");
                if (ds.Tables[0].Rows.Count == 0)
                {
                    sResult = "起始位置和起始货位编码在数据库中没有记录或货位不可用！";
                    return false;
                }
                _startposition = ds.Tables[0].Rows[0]["FLaneWay"].ToString();
                flaneway = Convert.ToInt32(_startposition);

                try
                {
                    string FSTARTCELL = _startsite, UseAwayFork = "-";
                    int FSTARTDEVICE = Convert.ToInt32(_startposition);

                    stackno = Convert.ToInt32(DbHelperSQL.GetSingle("SELECT F_StackIndex FROM T_Base_LaneInfo WHERE (F_LaneDeviceIndex = " + flaneway + ")"));
                    string dtime = DateTime.Now.ToString("u");
                    dtime = dtime.Substring(0, dtime.Length - 1);
                    int AutoManageIdx = GetAutoManageIdx();

                    int startcol = 0, startlayer = 0;

                    startcol = Convert.ToInt32(_startsite.Substring(3, 2));
                    startlayer = Convert.ToInt32(_startsite.Substring(6, 2));

                    Sql = "insert into T_Manage_Task(FID,FPALLETBARCODE,FCONTROLTASKTYPE,F_ManageTaskKindIndex,FSTARTWAREHOUSE,FSTARTDEVICE,FSTARTCELL," +
                    "FENDWAREHOUSE,FENDDEVICE,FENDCELL,FLANEWAY,FSTACK,FUseAwayFork,FBEGTIME,BOX_QUANTITY,UNPACK_QUANTITY,FStartCol,FStartLayer,FMANAGEID) " +
                    "values(" + AutoManageIdx + ",'" + LOC.PALLET_CODE + "','" + _tasktype + "',1,'" + GetWarehouseIndex() + "','" + _startposition + "','" + _startsite + "','" + GetWarehouseIndex() + "','"
                    + "0" + "','" + "" + "'," + flaneway + "," + stackno + ",'" + UseAwayFork + "','" + dtime + "',1,1," + startcol + "," + startlayer + ",'" + TASK_NO + "')";
                    DbHelperSQL.ExecuteSql(Sql);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            return bResult;
        }

        public bool GETWHEQUIPSTATUS(string WH_ID, out string sResult, out List<RGD.MdsAPI.WCS.getWhEquipStatus.DETAIL> DETAIL_LIST)
        {
            bool bResult = true;
            sResult = string.Empty;
            DETAIL_LIST = new List<WCS.getWhEquipStatus.DETAIL>();

            DataSet ds = DbHelperSQL.Query("select F_DeviceIndex,F_DeviceName,F_RunState from t_base_device");

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                WCS.getWhEquipStatus.DETAIL detail = new WCS.getWhEquipStatus.DETAIL();
                detail.CUR_STATUS = dr["F_RunState"].ToString();
                detail.DEVICE_NO = dr["F_DeviceIndex"].ToString();
                detail.DEVICE_NAME = dr["F_DeviceName"].ToString();
                DETAIL_LIST.Add(detail);
            }

            return bResult;
        }

        /// <summary>
        /// 扫码上架，入库口到叠盘机任务
        /// </summary>
        /// <param name="TASK_NO"></param>
        /// <param name="BOX_BAR_CODE"></param>
        /// <param name="sResult"></param>
        /// <returns></returns>
        public bool PACKTASK(string TASK_NO, string BOX_BAR_CODE, out string sResult)
        {
            bool bResult = true;
            sResult = string.Empty;

            string Sql, _tasktype, _startposition, _startsite = "-", _endposition, _endsite = "-";

            _tasktype = "2";
            _startposition = "12001";
            _endposition = "12004";//区分叠盘机
            int flaneway = -1;
            int stackno = -1;

            try
            {
                string UseAwayFork = "-";
                string dtime = DateTime.Now.ToString("u");
                dtime = dtime.Substring(0, dtime.Length - 1);
                int AutoManageIdx = GetAutoManageIdx();

                int startcol = 0, startlayer = 0;

                Sql = "insert into T_Manage_Task(FID,FPALLETBARCODE,FCONTROLTASKTYPE,F_ManageTaskKindIndex,FSTARTWAREHOUSE,FSTARTDEVICE,FSTARTCELL," +
                "FENDWAREHOUSE,FENDDEVICE,FENDCELL,FLANEWAY,FSTACK,FUseAwayFork,FBEGTIME,BOX_QUANTITY,UNPACK_QUANTITY,FStartCol,FStartLayer,FMANAGEID) " +
                "values(" + AutoManageIdx + ",'" + BOX_BAR_CODE + "','" + _tasktype + "',1,'" + GetWarehouseIndex() + "','" + _startposition + "','" + _startsite + "','" + GetWarehouseIndex() + "','"
                + _endposition + "','" + _endsite + "'," + flaneway + "," + stackno + ",'" + UseAwayFork + "','" + dtime + "',1,1," + startcol + "," + startlayer + ",'" + TASK_NO + "')";
                DbHelperSQL.ExecuteSql(Sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return bResult;
        }

        public bool STARTARRANGETASK(string WH_ID, string EQUIP_TYPE, string TASK_NO, string ARRANGE_TYPE, List<LOCATION> LOC_LIST, string xmlin, out string sResult)
        {
            bool bResult = true;
            sResult = string.Empty;

            DataSet ds_task = DbHelperSQL.Query("select * from T_warehouse");
            if (ds_task.Tables[0].Rows[0]["F_TASK_NO"] != DBNull.Value && ds_task.Tables[0].Rows[0]["F_TASK_NO"].ToString() != "0" && ds_task.Tables[0].Rows[0]["F_TASK_NO"].ToString() != "")
            {
                if (ds_task.Tables[0].Rows[0]["F_TASK_NO"].ToString() == TASK_NO)
                {
                    return true;
                }
                DbHelperSQL.ExecuteSql("insert into T_TASK_LIST TASK_NO,METHOD_NAME,XMLIN,TASK_STATUS,TASK_START_TIME values ('" + TASK_NO + "','START_INTASK','" + xmlin + "','Waiting',getdate())");
                sResult = "有正在执行中的任务。任务号：" + ds_task.Tables[0].Rows[0]["F_TASK_NO"] + ",任务类型：" + ds_task.Tables[0].Rows[0]["F_TASK_TYPE"];
                return true;
            }

            if (LOC_LIST.Count() <= 0)
            {
                sResult = "整理下发的货位号为空！";
                return false;
            }
            DbHelperSQL.ExecuteSql("update T_WareHouse set F_WarehouseIndex='" + WH_ID + "',F_TASK_NO='" + TASK_NO + "',F_TASK_TYPE='Arrange',F_TASK_STATUS='RUN',F_EQUIP_TYPE='" + EQUIP_TYPE + "'");

            return bResult;
        }

        /// <summary>
        /// 删除指定任务号等待执行任务
        /// </summary>
        /// <param name="TASK_NO"></param>
        /// <param name="sResult"></param>
        /// <returns></returns>
        public bool STOPTASK(string TASK_NO, out string sResult)
        {
            bool bResult = true;
            sResult = string.Empty;

            DataView dv = DbHelperSQL.Query("select F_TASK_NO,F_TASK_TYPE,F_TASK_STATUS,F_EQUIP_TYPE from T_Warehouse").Tables[0].DefaultView;

            if (TASK_NO != dv[0]["F_TASK_NO"].ToString())
            {
                DataView dv_list = DbHelperSQL.Query("select * from t_task_list where task_type in ('Waiting','Runnning') and task_no='" + TASK_NO + "'").Tables[0].DefaultView;
                if (dv_list.Count > 0)
                {
                    DbHelperSQL.ExecuteSql("update t_task_list set task_type='stopped' where task_id=" + dv_list[0]["task_id"]);
                    return true;
                }
                else
                {
                    bResult = false;
                    sResult = "未找到对应任务。";
                    return bResult;
                }
            }
            string TASK_TYPE = dv[0]["F_TASK_TYPE"].ToString();

            switch (TASK_TYPE)
            {
                case "In":
                    bool TaskCleared = true;
                    //查找目标叠盘机的任务，标记last
                    //查找有箱无任务的叠盘机，放行
                    dv = DbHelperSQL.Query("select F_Bindingdevice,F_Barcode from t_base_plc_ask where F_askkind=2").Tables[0].DefaultView;
                    for (int i = 0; i < dv.Count; i++)
                    {
                        DataView dvt = DbHelperSQL.Query("select FID from t_manage_task where FENDDEVICE=" + dv[i]["F_bindingdevice"]).Tables[0].DefaultView;
                        if (dvt.Count != 0 || dv[i]["f_Barcode"].ToString() != "")
                        {
                            TaskCleared = false;
                            //dbo.ExecuteSql("update T_MANAGE_TASK set LAST_BOX_REMARK=" + dvt[0][0] + " where LAST_BOX_REMARK is null and FENDDEVICE = '" + dv[i]["F_bindingdevice"] + "'");
                        }
                    }
                    //停止任务接口：停止任务时总是报“正在停止，有未执行完的任务”
                    //标记入库任务
                    if (!TaskCleared)
                    {
                        DbHelperSQL.ExecuteSql("update T_Warehouse set f_task_status='STOPPING'");
                        //bResult = false;
                        sResult = "正在停止，有未执行完的任务。";
                    }
                    else
                    {
                        DataTable dt = DbHelperSQL.Query("select * from t_manage_task where fmanageid='" + TASK_NO + "'").Tables[0];
                        if (dt.Rows.Count == 0)
                        {
                            DbHelperSQL.ExecuteSql("update T_Warehouse set F_TASK_NO='0',f_TASK_TYPE='',f_task_status='FREE',F_IO_WH_SORT_CODE=''");
                        }
                        else
                        {
                            DbHelperSQL.ExecuteSql("update T_Warehouse set f_task_status='STOPPING'");
                            //bResult = false;
                            sResult = "正在停止，有未执行完的任务。";
                        }
                    }
                    break;

                case "Out":
                    DbHelperSQL.ExecuteSql("delete from t_manage_task where FIntoStepOK=0 and fmanageid='" + TASK_NO + "'");
                    DbHelperSQL.ExecuteSql("update t_warehouse set f_task_status='STOPPING'");
                    //dbo.ExecuteSql("update T_Warehouse set F_TASK_NO='0',f_TASK_TYPE='',f_task_status='FREE'");
                    DataView dv_manage = DbHelperSQL.Query("select FID from t_manage_task where fmanageid='" + TASK_NO + "'").Tables[0].DefaultView;
                    bool bcompleted = false;
                    if (dv_manage.Count == 0)
                    {//没有任务
                        DataView dv_diepan = DbHelperSQL.Query("select * from t_base_plc_ask where f_askkind=2 and not f_barcode=''").Tables[0].DefaultView;
                        if (dv_diepan.Count == 0)
                        {//叠盘机无货
                            DataView dv_chaipan = DbHelperSQL.Query("select * from t_base_plc_ask where f_bindingdevice=12032").Tables[0].DefaultView;
                            if (dv_chaipan[0]["f_HAVE_GOODS"] != DBNull.Value && !((bool)dv_chaipan[0]["F_HAVE_GOODS"]))
                            {//拆盘机无物
                                DataView dv_rfid = DbHelperSQL.Query("select SOURCE_CELL_CODE from IO_RFID where STATION_CODE='12004' and not SOURCE_CELL_CODE='' and SOURCE_CELL_CODE is not null").Tables[0].DefaultView;
                                if (dv_rfid.Count == 0)
                                {//射频无物
                                    DbHelperSQL.ExecuteSql("update T_Warehouse set F_TASK_NO='0',f_TASK_TYPE='',f_task_status='FREE',F_IO_WH_SORT_CODE=''");
                                    bcompleted = true;
                                }
                            }
                        }
                    }
                    if (!bcompleted)
                    {
                        //bResult = false;
                        sResult = "正在停止，有未执行完的任务。";
                    }
                    break;

                case "Inventory":
                    DbHelperSQL.ExecuteSql("delete from t_manage_task where FIntoStepOK=0 and FMANAGETASKTYPE='5' and fmanageid='" + TASK_NO + "'");
                    DataTable dtInventory = DbHelperSQL.Query("select * from t_manage_task where fmanageid='" + TASK_NO + "'").Tables[0];
                    if (dtInventory.Rows.Count == 0)
                    {
                        DataView dv_rfid = DbHelperSQL.Query("select SOURCE_CELL_CODE from IO_RFID where STATION_CODE='12004' and (not SOURCE_CELL_CODE='' or SOURCE_CELL_CODE is not null)").Tables[0].DefaultView;
                        if (dv_rfid.Count == 0)
                        {//射频无物
                            DbHelperSQL.ExecuteSql("update T_Warehouse set F_TASK_NO='0',f_TASK_TYPE='',f_task_status='FREE',F_IO_WH_SORT_CODE=''");
                        }
                        else
                        {
                            //bResult = false;
                            sResult = "正在停止，有未执行完的任务。";
                            DbHelperSQL.ExecuteSql("update T_Warehouse set f_task_status='STOPPING'");
                        }
                    }
                    else
                    {
                        //bResult = false;
                        sResult = "正在停止，有未执行完的任务。";
                        DbHelperSQL.ExecuteSql("update T_Warehouse set f_task_status='STOPPING'");
                    }
                    break;
            }
            return bResult;
        }
    }
}