using RGD.DataService;
using RGD.DBUtility;
using RGD.ZhiQianAPI;
using RGD.ZhiQianAPI.ZhiQianModel;
using RGD.MdsAPI;
using RGD.Model;
using System;
using System.Data;
using System.Windows.Forms;
using System.Collections.Generic;

namespace RGD.WCS
{
    public partial class TEST1 : Form
    {
        private static TEST1 _formInstance;
        public bool flag = true;
        private string start = "01-01-01";
        private int fid = 1;
        public TEST1()
        {
            InitializeComponent();
            _formInstance = this;
        }
        private static int taskid = 0;
        public static TEST1 FormInstance
        {
            get
            {
                if (_formInstance != null)
                {
                    _formInstance.Dispose();
                    _formInstance = null;
                }
                if (_formInstance == null)
                {
                    _formInstance = new TEST1();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //CreateOrder testInTaskOrder=new CreateOrder();
            //testInTaskOrder.zoneid = 0;
            //testInTaskOrder.poolid = 1;
            //testInTaskOrder.appendflag = true;
            //testInTaskOrder.orderno= DateTime.Now.ToString("yyyyMMddHHmmssfff");
            //List<BoxList> testLists = new List<BoxList>();
            //BoxList testList = new BoxList();
            //testList.src = "12004";
            //testList.dst = "01-01-01";
            //testList.box = "340030001234";
            //testList.rgsTaskId = 123;
            //testLists.Add(testList);
            //testInTaskOrder.boxlist = testLists;
            //CreateOrderResponse createTestOrderResponse= SendDataToAgv.SendFirstInTaskToAgv(testInTaskOrder);
            //taskid =createTestOrderResponse.result.taskid;
            //Console.WriteLine(createTestOrderResponse.ToString());

            CreateOrder testInTaskOrder = new CreateOrder();
            testInTaskOrder.zoneid = 0;
            testInTaskOrder.poolid = 1;
            testInTaskOrder.appendflag = false;
            testInTaskOrder.orderno = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            List<BoxList> testLists = new List<BoxList>();
            BoxList testList = new BoxList();
            testList.src = "01-03-06";
            testList.dst = "12004";
            //testList.src = "12004";
            //testList.dst = "01-03-06";
            testList.box = "340030001235";
            testList.rgsTaskId = 123;
            testLists.Add(testList);
            testInTaskOrder.boxlist = testLists;
            CreateOrderResponse createTestOrderResponse = SendDataToAgv.SendFirstInTaskToAgv(testInTaskOrder);
            Console.WriteLine(createTestOrderResponse.ToString());
            FrmMain.taskId = createTestOrderResponse.result.taskid;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            AppendOrder appendOrder = new AppendOrder();
            appendOrder.taskid = FrmMain.taskId;
            List<BoxList> list = new List<BoxList>();
            BoxList boxList = new BoxList();
            boxList.src = "01-01-02";
            boxList.dst = "12004";
            //boxList.src = "12004";
            //boxList.dst = "01-01-02";
            boxList.box = "3400300012352";
            boxList.rgsTaskId = 123;
            list.Add(boxList);
            appendOrder.boxlist = list;
            AppendCompleteCancelResponse appendResponse = SendDataToAgv.SendSuperaddTaskToAgv(appendOrder);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            CompleteOrder testOrder = new CompleteOrder();
            testOrder.taskid = FrmMain.taskId;
            AppendCompleteCancelResponse appendCompleteCancelResponse = SendDataToAgv.SendStopAppendTaskToAgv(testOrder);
            Console.WriteLine();
        }

        
        private void button6_Click(object sender, EventArgs e)
        {
            AppendOrder testOutTaskOrder = new AppendOrder();
            testOutTaskOrder.taskid = 639;
            List<BoxList> testLists = new List<BoxList>();
            BoxList testList = new BoxList();
            testList.src = start;
            DataView dv = DbHelperSQL.Query("select top 1 * from st_cell where fid>" + fid + " and fcellcode='" + start + "'").Tables[0].DefaultView;
            start = dv[0]["fcellcode"].ToString();
            testList.dst = dv[0]["fcellcode"].ToString();
            testList.box = "340030001235";
            testList.rgsTaskId = 123;
            testLists.Add(testList);
            testOutTaskOrder.boxlist = testLists;
            AppendCompleteCancelResponse createTestOrderResponse = SendDataToAgv.SendSuperaddTaskToAgv(testOutTaskOrder);
            Console.WriteLine(createTestOrderResponse.ToString());
        }

       

        private void timer1_Tick(object sender, EventArgs e)
        {
            cellTest();
        }

        private void cellTest()
        {
            #region 货位测试
            try
            {
                MDevice deviceInfo = BaseDeviceService.GetDeviceInfo(12001);
                DataView task = DbHelperSQL.Query("select * from T_manage_task").Tables[0].DefaultView;
                if (deviceInfo.SplitByte_0.ToString() == "1" && task.Count == 0)
                {
                    DataView dv = DbHelperSQL.Query("select * from st_cell where inout='0' and not ((F_Z=1 and F_X IN (5,6)) or (F_Z=4 and F_X IN (5,6)))").Tables[0].DefaultView;
                    if (dv.Count > 0)
                    {
                        string 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) values(12820,'100','1',2,'3422101800000002','12001','-','3422101800000002','18001','" + dv[0]["FCELLCODE"].ToString() + "',18001,11001,'-','2022-10-20 10:25:04',1,1,0,0)";
                        DbHelperSQL.ExecuteSql(sql);
                        DbHelperSQL.ExecuteSql("update st_cell set inout = '1' where FCELLCODE='" + dv[0]["FCELLCODE"] + "'");
                    }
                }
                else if (task.Count == 0 && deviceInfo.SplitByte_0.ToString() == "0")
                {
                    Object a = DbHelperSQL.GetSingle("select * from st_cell where FPALLETBARCODE='100'");
                    if (a != null)
                    {
                        DataView dv = DbHelperSQL.Query("select * from st_cell where FPALLETBARCODE='100'").Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            string 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) values(12821,'100','2',2,'3422101800000002','18001','" + dv[0]["FCELLCODE"].ToString() + "','3422101800000002','12001','-',18001,11001,'-','2022-10-20 10:40:14',1,1,8,7)";
                            DbHelperSQL.ExecuteSql(sql);
                        }
                    }
                }
            }
            catch (Exception ex)
            {

                throw;
            }
            #endregion
        }

        private void button8_Click(object sender, EventArgs e)
        {
            CreateOrder testOutTaskOrder = new CreateOrder();
            testOutTaskOrder.zoneid = 2;
            testOutTaskOrder.poolid = 1;
            testOutTaskOrder.appendflag = false;
            testOutTaskOrder.orderno = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            List<BoxList> testLists = new List<BoxList>();
            BoxList testList = new BoxList();
            testList.src = "01-01-01";
            testList.dst = "12004";
            testList.box = "340030001235";
            testList.rgsTaskId = 123;
            testLists.Add(testList);
            testList = new BoxList();

            testList = new BoxList();
            testList.src = "01-02-01";
            testList.dst = "12004";
            testList.box = "340030001233";
            testList.rgsTaskId = 123;
            testLists.Add(testList);


            testList = new BoxList();
            testList.src = "01-03-01";
            testList.dst = "12004";
            testList.box = "340030001231";
            testList.rgsTaskId = 123;

            testLists.Add(testList);
            
            testOutTaskOrder.boxlist = testLists;
            CreateOrderResponse createTestOrderResponse = SendDataToAgv.SendFirstInTaskToAgv(testOutTaskOrder);
            Console.WriteLine(createTestOrderResponse.ToString());
        }

        private void TEST1_FormClosed(object sender, FormClosedEventArgs e)
        {
            timer1.Enabled = false;
        }

        private void TEST1_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer1.Enabled = false;
        }


        private void button4_Click(object sender, EventArgs e)
        {
            CreateOrder testOutTaskOrder = new CreateOrder();
            testOutTaskOrder.zoneid = 0;
            testOutTaskOrder.poolid = 1;
            testOutTaskOrder.appendflag = false;
            testOutTaskOrder.orderno = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            List<BoxList> testLists = new List<BoxList>();
            BoxList testList = new BoxList();
            //testList.src = "04-13-04";
            //testList.dst = "01-01-01";
            //testList.box = "340030001235";
            //testList.rgsTaskId = 123;
            //testLists.Add(testList);
            //testList = new BoxList();
            testList.src = "04-14-02";
            testList.dst = "12004";
            testList.box = "340030001234";
            testList.rgsTaskId = 123;
            testLists.Add(testList);
            //testList = new BoxList();

            //testList.src = "01-02-01";
            //testList.dst = "12004";
            //testList.box = "340030001233";
            //testList.rgsTaskId = 123;
            //testLists.Add(testList);


            //testList = new BoxList();
            //testList.src = "01-03-01";
            //testList.dst = "12004";
            //testList.box = "340030001231";
            //testList.rgsTaskId = 123;
            //testLists.Add(testList);
            //testList = new BoxList();
            //testList.src = "01-04-01";
            //testList.dst = "12004";
            //testList.box = "34003000121";
            //testList.rgsTaskId = 123;
            //testLists.Add(testList);
            //testList = new BoxList();

            //testList.src = "01-06-01";
            //testList.dst = "12004";
            //testList.box = "340030123";
            //testList.rgsTaskId = 123;
            //testLists.Add(testList);

            testOutTaskOrder.boxlist = testLists;
            CreateOrderResponse createTestOrderResponse = SendDataToAgv.SendFirstInTaskToAgv(testOutTaskOrder);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void button6_Click_1(object sender, EventArgs e)
        {
            CreateOrder testInTaskOrder = new CreateOrder();
            testInTaskOrder.zoneid = 0;
            testInTaskOrder.poolid = 1;
            testInTaskOrder.appendflag = true;
            testInTaskOrder.orderno = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            List<BoxList> testLists = new List<BoxList>();
            BoxList testList = new BoxList();
            testList.src = "12004";
            testList.dst = "01-01-01";
            testList.box = "340030001235";
            testList.rgsTaskId = 123;
            testLists.Add(testList);
            testInTaskOrder.boxlist = testLists;
            CreateOrderResponse createTestOrderResponse = SendDataToAgv.SendFirstInTaskToAgv(testInTaskOrder);
            Console.WriteLine(createTestOrderResponse.ToString());

            for (int i = 0; i < 3; i++)
            {
                AppendOrder appendOrder = new AppendOrder();
                appendOrder.taskid = FrmMain.taskId;
                List<BoxList> list = new List<BoxList>();
                BoxList boxList = new BoxList();
                boxList.src = "12004";
                boxList.dst = "01-01-0"+i+2;
                boxList.box = "340030001235"+i;
                boxList.rgsTaskId = 123;
                list.Add(boxList);
                appendOrder.boxlist = list;
                AppendCompleteCancelResponse appendResponse = SendDataToAgv.SendSuperaddTaskToAgv(appendOrder);
            }

            CompleteOrder completeOrder = new CompleteOrder();
            completeOrder.taskid = 123;
            AppendCompleteCancelResponse completeResponse = SendDataToAgv.SendStopAppendTaskToAgv(completeOrder);
        }
    }
}