//using DBFactory;
using log4net;
using Microsoft.VisualBasic;
using RGD.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
namespace CommonClassLib
{
    public class StaticClass
    {
        public static ILog _log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public static string GetWarehouseIndex()
        {
            DataView dv;
            try
            {
                dv = DbHelperSQL.Query("select F_WarehouseIndex from   T_Warehouse").Tables[0].DefaultView;
                if (dv.Count > 0)
                {

                    return dv[0]["F_WarehouseIndex"].ToString();

                }
                else
                {
                    return "1";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
            }

        }

        //public static bool CreateMove(string TASK_NO, string CONTROL_TASK_TYPE, string START_DEVICE, string END_DEVICE, string STOCK_BARCODE,string BOX_LABLE, out string sResult)
        //{
        //    return CreateMove(TASK_NO, CONTROL_TASK_TYPE, START_DEVICE, END_DEVICE, STOCK_BARCODE, 0, "", false, false, out sResult);
        //}
        public static bool CreateMove(string TASK_NO, string CONTROL_TASK_TYPE, string START_DEVICE, string END_DEVICE, string STOCK_BARCODE, out string sResult)
        {
            //return CreateMove(TASK_NO, CONTROL_TASK_TYPE, START_DEVICE, END_DEVICE, STOCK_BARCODE, 0, "", false,false, false, out sResult);
            return CreateMove(TASK_NO, CONTROL_TASK_TYPE, START_DEVICE, END_DEVICE, STOCK_BARCODE, "", 0, "", false, false, false, 1, out sResult);
        }
        //用来拆盘送出
        public static bool CreateMove(string TASK_NO, string CONTROL_TASK_TYPE, string START_DEVICE, string END_DEVICE, string STOCK_BARCODE, bool needback, out string sResult)
        {
            
            return CreateMove(TASK_NO, CONTROL_TASK_TYPE, START_DEVICE, END_DEVICE, STOCK_BARCODE, "", 0, "", true, false, false, 1, out sResult);
        }
        public static bool CreateMove(string TASK_NO, string CONTROL_TASK_TYPE, string START_DEVICE, string END_DEVICE, string STOCK_BARCODE,string BOX_LABLE, out string sResult)
        {
            //return CreateMove(TASK_NO, CONTROL_TASK_TYPE, START_DEVICE, END_DEVICE, STOCK_BARCODE, 0, "", false, false, false, out sResult);
            return CreateMove(TASK_NO, CONTROL_TASK_TYPE, START_DEVICE, END_DEVICE, STOCK_BARCODE, BOX_LABLE, 0, "", false, false, false, 1, out sResult);
        }
        //public static bool CreateMove(string TASK_NO, string CONTROL_TASK_TYPE, string START_DEVICE, string END_DEVICE, string STOCK_BARCODE,int TASK_LEVEL, out string sResult)
        //{
        //    return CreateMove(TASK_NO, CONTROL_TASK_TYPE, START_DEVICE, END_DEVICE, STOCK_BARCODE,"", 0, "", false, false,false,TASK_LEVEL, out sResult);
        //}
        //public static bool CreateMove(string TASK_NO, string CONTROL_TASK_TYPE, string START_DEVICE, string END_DEVICE, string STOCK_BARCODE,bool IS_LASTBOX,out string sResult)
        //{
        //    return CreateMove(TASK_NO, CONTROL_TASK_TYPE, START_DEVICE, END_DEVICE, STOCK_BARCODE, 0, "", false, IS_LASTBOX, out sResult);
        //}
        //public static bool CreateMove(string TASK_NO, string CONTROL_TASK_TYPE, string START_DEVICE, string END_DEVICE, string STOCK_BARCODE, int PICKUP_LOCATION, string PICKUP_TYPE, bool IS_PICKUP, bool IS_LASTBOX, out string sResult)
        //{
        //    return CreateMove(TASK_NO, CONTROL_TASK_TYPE, START_DEVICE, END_DEVICE, STOCK_BARCODE,"", PICKUP_LOCATION, PICKUP_TYPE, IS_PICKUP,false, IS_LASTBOX, 1, out sResult);
        //}
        public static bool CreateMove(string TASK_NO, string CONTROL_TASK_TYPE, string START_DEVICE, string END_DEVICE, string STOCK_BARCODE, int PICKUP_LOCATION, string PICKUP_TYPE, bool IS_PICKUP, bool IS_OUT, bool IS_LASTBOX, out string sResult)
        {
            return CreateMove(TASK_NO, CONTROL_TASK_TYPE, START_DEVICE, END_DEVICE, STOCK_BARCODE,"", PICKUP_LOCATION, PICKUP_TYPE, IS_PICKUP, IS_OUT, IS_LASTBOX, 1, out sResult);
        }
        public static bool CreateMove(string TASK_NO, string CONTROL_TASK_TYPE, string START_DEVICE, string END_DEVICE, string STOCK_BARCODE,string BOX_LABLE, int PICKUP_LOCATION, string PICKUP_TYPE, bool IS_PICKUP,bool IS_OUT, bool IS_LASTBOX,int TASK_LEVEL, out string sResult)
        {
            bool bResult = true;
            sResult = "";
            
            try
            {
                //等待执行的；已批复的申请改道的

                string UseAwayFork = "-";
                if (STOCK_BARCODE != "")
                {
                    DataView dvt = DbHelperSQL.Query("select * from T_Manage_Task where FPALLETBARCODE = '" + STOCK_BARCODE + "'").Tables[0].DefaultView;//dzf20120822
                    if (dvt.Count > 0)
                    {
                        sResult = "此托盘任务已存在。";
                        return false;
                    }
                }

                DataView dvl;

                object obj = null;
                string FSTARTCELL = ""; string FENDCELL = ""; char[] cc = new char[1] { '-' };
                int FSTARTDEVICE = 0; int FENDDEVICE = 0;
                string flaneway = "-1";
                int stackno = -1;
                int FStartCol = 0, FStartLayer = 0, FEndCol = 0, FEndLayer = 0;


                if (Information.IsNumeric(START_DEVICE) == false)
                {
                    dvl = DbHelperSQL.Query("SELECT FLaneWay,FStack FROM ST_CELL where FCELLCODE='" + START_DEVICE + "'").Tables[0].DefaultView;
                    if (dvl.Count > 0)
                    {
                        flaneway = dvl[0]["FLaneWay"].ToString();
                        stackno = Convert.ToInt32(dvl[0]["FStack"]);//20101028
                        FStartCol = Convert.ToInt32(START_DEVICE.Substring(3, 2));
                        FStartLayer = Convert.ToInt32(START_DEVICE.Substring(6, 2));
                    }

                }
                else
                {
                    if (Information.IsNumeric(END_DEVICE) == false)//dzf理解 终点设备不是数字的情况
                    {
                        //20101028
                        dvl = DbHelperSQL.Query("SELECT FLaneWay,FStack FROM ST_CELL where FCELLCODE='" + END_DEVICE + "'").Tables[0].DefaultView;
                        if (dvl.Count > 0)
                        {
                            flaneway = dvl[0]["FLaneWay"].ToString();
                            stackno = Convert.ToInt32(dvl[0]["FStack"]);//20101028
                            FEndCol = Convert.ToInt32(END_DEVICE.Substring(3, 2));
                            FEndLayer = Convert.ToInt32(END_DEVICE.Substring(6, 2));
                        }
                    }
                }


                int AutoManageIdx = GetAutoManageIdx();

                if (Information.IsNumeric(START_DEVICE) == true)
                {
                    FSTARTDEVICE = Convert.ToInt32(START_DEVICE);
                    FSTARTCELL = "-";
                }
                else
                {//货位编码
                    obj = DbHelperSQL.GetSingle("SELECT FLaneWay FROM ST_CELL where FCELLCODE='" + START_DEVICE + "'");
                    if (obj != null)
                    {
                        FSTARTDEVICE = Convert.ToInt32(obj);
                    }
                    else
                    {
                        sResult = "获取任务时：没找到调度任务索引：" + START_DEVICE + ",的起点位置！";
                        return false;
                    }
                    FSTARTCELL = START_DEVICE;
                }


                if (Information.IsNumeric(END_DEVICE) == true)//dzf理解 终点设备是数字的情况
                {
                    FENDDEVICE = Convert.ToInt32(END_DEVICE);
                    FENDCELL = "-";
                }
                else if (END_DEVICE == "")
                { }
                else //dzf理解 终点设备是货位的情况，获得终点设备号FENDDEVICE是巷道
                {//货位编码
                    obj = DbHelperSQL.GetSingle("SELECT FLaneWay FROM ST_CELL where FCELLCODE='" + END_DEVICE + "'");
                    if (obj != null)
                    {
                        FENDDEVICE = Convert.ToInt32(obj);
                    }
                    else
                    {
                        sResult = "获取任务时：没找到调度任务索引：" + END_DEVICE + ",的终点位置！";
                        return false;
                    }
                    FENDCELL = END_DEVICE;
                }


                //20091014
                int FTASKLEVEL = 0;//空托盘组出库的优先级应该最大

                DataView dv = DbHelperSQL.Query("select F_EQUIP_TYPE from t_warehouse").Tables[0].DefaultView;

                string sendout_type = "1";

                if (dv[0][0].ToString()=="03")
                {
                    sendout_type = "2";
                }


                string sql = "INSERT INTO T_Manage_Task" +
                  "(FID, F_ManageTaskKindIndex,FMANAGEID,F_RELATIVECONTORLID,FPALLETBARCODE,FCONTROLTASKTYPE, FTASKLEVEL, FISRETURN," +
                  "FSTARTWAREHOUSE, FSTARTCELL, FSTARTDEVICE,FENDWAREHOUSE,FENDCELL,FENDDEVICE, " +
                  "FSTATUS,FIntoStepOK,FREMARK,FLANEWAY,FSTACK,FUseAwayFork,SENDOUT_TYPE,FStartCol,FStartLayer,FEndCol,FEndLayer,PICKUP_LOCATION,PICKUP_TYPE,IS_PICKUP,IS_OUT,BOX_LABLE,LAST_BOX_REMARK) " +
                  "VALUES (" + AutoManageIdx + ",1,'" + TASK_NO + "',0,'" + STOCK_BARCODE +
                   "','" + CONTROL_TASK_TYPE + "'," + FTASKLEVEL + ",'"
                  + 0 + "','1','" + FSTARTCELL + "','"
                  + FSTARTDEVICE + "','1','" + FENDCELL + "','" + FENDDEVICE + "',"
                  + "'0','" + 0 + "',''," + flaneway + "," +
                  stackno + ",'" + UseAwayFork + "'," + sendout_type + "," + FStartCol + "," + FStartLayer + "," + FEndCol + "," + FEndLayer + "," + PICKUP_LOCATION + "," + (PICKUP_TYPE == "" ? "0" : PICKUP_TYPE) + "," + (IS_PICKUP ? 1 : 0) + "," + (IS_OUT ? 1 : 0) + ",'" + BOX_LABLE + "',"+(IS_LASTBOX?1:0) +")";//20110318
               
                int rc = DbHelperSQL.ExecuteSql(sql);
                if (rc == 0)
                {
                    sResult = "写入失败";
                    return false;
                }
                if (IS_LASTBOX)
                {
                    DbHelperSQL.Query("update t_warehouse set F_LASTBOX_REACH_FLAG=1");
                }
            }
            catch (Exception ex)
            {
                
                sResult = ex.Message;
                bResult= false;
            }

            if(!bResult)
                _log.Debug(sResult);

            return bResult;
        }
        private static 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"])|| maxIdx==1)
                    {
                        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;
            }
        }
    }
}
