//using DBFactory;
using Matrix.Utility;
using RGD.DBUtility;
using RGDWCSServices.WCS;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RGDWCSServices
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        ServiceHost serviceHost;

        private void button1_Click(object sender, EventArgs e)
        {
         
             serviceHost = new ServiceHost(typeof(RGDWCSService));
             //serviceHost.BaseAddresses[0].Host;
            //打开服务创建监听，并开始监听消息

            serviceHost.Open();
            this.Text = "WCS服务开启中";
            MessageBox.Show("服务启动完成");
            label1.Text= "WCS服务已开启";
            button1.Enabled = false;
            LogUtil.WriteLog("webService", "MainForm_开启服务");
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.TopMost = true;
            this.Visible = true;
            this.TopMost = false;
            //20091223
            this.WindowState = FormWindowState.Normal;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("Explorer.exe", "F:\\调度与平台接口日志\\log");
        }
        bool close = false;
        private void button3_Click(object sender, EventArgs e)
        {
            close = true;
            this.Close();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(!close||!checkBox1.Checked)
            {
                WindowState = FormWindowState.Minimized;
                e.Cancel = true;
            }
        }
        //DbHelperSQLperator DbHelperSQL = new DbHelperSQLperator();
        //DbHelperSQLperator DbHelperSQLh = new DbHelperSQLperator("ConnString");

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!button1.Enabled)
            {
                {
                    DataView dv_task = DbHelperSQL.Query("select * from T_Warehouse").Tables[0].DefaultView;
                    if (dv_task[0]["F_TASK_STATUS"].ToString() == "FREE")
                    {
                        DataView dv_task_list = DbHelperSQL.Query("select top 1 * from t_task_list where TASK_STATUS ='Waiting'").Tables[0].DefaultView;
                        if (dv_task_list.Count > 0)
                        {
                            string sResult = string.Empty;

                            string sClassFullName = string.Format("RGDWCSServices.WCS.{0}", dv_task_list[0]["METHOD_NAME"].ToString());

                            new HouseBase().Invoke(sClassFullName, "Notify", dv_task_list[0]["XMLIN"].ToString(), out sResult);
                        }
                    }
                }
                //{
                //    DataView dv_task = DbHelperSQLh.ExceSQL("select * from T_Warehouse").Tables[0].DefaultView;
                //    if (dv_task[0]["F_TASK_STATUS"].ToString() == "FREE")
                //    {
                //        DataView dv_task_list = DbHelperSQLh.ExceSQL("select top 1 * from t_task_list where TASK_STATUS ='Waiting'").Tables[0].DefaultView;
                //        if (dv_task_list.Count > 0)
                //        {
                //            string sResult = string.Empty;

                //            string sClassFullName = string.Format("RGDWCSServices.WCS.{0}", dv_task_list[0]["METHOD_NAME"].ToString());

                //            new HouseBase().Invoke(sClassFullName, "Notify", dv_task_list[0]["XMLIN"].ToString(), out sResult);
                //        }
                //    }
                //}
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //string sss = RGDWCSServices.Base.getWCS_LOC_NO("00975");
            //string sXmlIn = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
            //       sXmlIn +="<PARA><WH_ID>6000001378</WH_ID><EQUIP_TYPE></EQUIP_TYPE><TASK_NO>12810</TASK_NO><PLATFORM_NO>12004</PLATFORM_NO><IS_PAST>0</IS_PAST><LOC_LIST><LOC><LOC_NO>00001</LOC_NO><PALLET_CODE>345830000217,345830000701</PALLET_CODE><NEED_BACK>0</NEED_BACK></LOC><LOC><LOC_NO>00012</LOC_NO><PALLET_CODE>345810000535,345810001223</PALLET_CODE><NEED_BACK>0</NEED_BACK></LOC><LOC><LOC_NO>00023</LOC_NO><PALLET_CODE>345810000850,345810000405</PALLET_CODE><NEED_BACK>0</NEED_BACK></LOC></LOC_LIST></PARA>";
            //string sResult = "";
            //startInventoryTask s = new startInventoryTask();
            //startInventoryTask.PARA page = s.DeSerialize<startInventoryTask.PARA>(sXmlIn);
            //s.START_INVENTORYTASK(page.WH_ID, page.EQUIP_TYPE, page.TASK_NO, page.LOC_LIST, sXmlIn, out sResult);
            //int i = 1;

            //string text = "";
            //for (; i <= 840; i++)
            //{

            //    text += i.ToString().PadLeft(4, '0') + ",";
            //    if (i % 6 == 0)
            //    {
            //        LogUtil.WriteLog("chuwei", text.Substring(0, text.Length - 1));
            //        text = "";
            //    }
            //}

        }
    }
}
