using ExternalDataSync;
using ExternalDataSync.MOM;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using PLMTEST;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WebApi;
using static NPOI.HSSF.Record.UnicodeString;
using static System.Configuration.ConfigurationManager;

/// <summary>
/// 
/// </summary>
namespace SlMesDbIterface
{
    public partial class Form_MoveSqlTable : Form
    {
        public static Action<double, String, String> Delegate_UpdateValue = null;
        public int count = 0;
        public static int upload_Interval = Convert.ToInt32(AppSettings["upload_Interval"]);
        public static int upload_Interval_error = Convert.ToInt32(AppSettings["upload_Interval_error"]);
        InitServer initServer = null;
        UpLoadMessage uploadMessage = new UpLoadMessage();
        /// <summary>
        /// 
        /// </summary>
        public Form_MoveSqlTable()
        {
            InitializeComponent();
            initServer = new WebApi.InitServer(Program.WebSvrPort);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var a = new msg();
           // a.insertOrderData = new InsertOrderData();
            a.msgHeader = new msgHeader();
            //string bodyData = JsonConvert.SerializeObject(a);
            string bodyData = "{\r\n  \"msgHeader\": {\r\n    \"messageID\": \"865f0c4ed27f45718189a6cb0e83c1a4\",\r\n    \"interfaceID\": \"MOM-中控-011\",\r\n    \"transID\": \"865f0c4ed27f45718189a6cb0e83c1a4\",\r\n    \"sender\": \"mom\",\r\n    \"receiver\": \"中控\"\r\n  },\r\n  \"msgBody\": {\r\n    \"batchNo\": null,\r\n    \"flag\": \"1\",\r\n    \"HasProduSerial\": \"0\",\r\n    \"isOiling\": \"0\",\r\n    \"isPainting\": \"0\",\r\n    \"markDate\": null,\r\n    \"markTime\": null,\r\n    \"nature\": \"0\",\r\n    \"numCode\": null,\r\n    \"orderCode\": null,\r\n    \"planDate\": \"2023-06-08\",\r\n    \"planEndDate\": \"2023-06-27\",\r\n    \"planStartDate\": \"2023-06-27\",\r\n    \"priority\": \"0\",\r\n    \"proCode\": \"2401016-B4C/B\",\r\n    \"proName\": \"后桥半壳\",\r\n    \"proShift\": \"7\",\r\n    \"produceType\": \"0\",\r\n    \"productState\": \"生产\",\r\n    \"qty\": \"1\",\r\n    \"routeCode\": \"2401016-B4C/B\",\r\n    \"routeVer\": \"2401016-B4C/B_0\",\r\n    \"seqNo\": \"1\",\r\n    \"serialOrlor\": \"1\",\r\n    \"site\": \"W13\",\r\n    \"sn\": \"0\",\r\n    \"workCenter\": \"中重型桥壳冲压一线\",\r\n    \"workOrderCode\": \"10BK10|PO-202306027-001_002\",\r\n    \"msgTaskList\": [\r\n      {\r\n        \"equiCode\": null,\r\n        \"opCode\": \"10BK1001\",\r\n        \"planEndDate\": \"2023-06-27\",\r\n        \"planStartDate\": \"2023-06-27\",\r\n        \"proCode\": \"2401016-B4C/B\",\r\n        \"proName\": \"后桥半壳\",\r\n        \"qty\": \"1\",\r\n        \"taskOrderCode\": \"TO-20230627-2588\",\r\n        \"workCell\": null\r\n      },\r\n      {\r\n        \"equiCode\": null,\r\n        \"opCode\": \"10BK1002\",\r\n        \"planEndDate\": \"2023-06-27\",\r\n        \"planStartDate\": \"2023-06-27\",\r\n        \"proCode\": \"2401016-B4C/B\",\r\n        \"proName\": \"后桥半壳\",\r\n        \"qty\": \"1\",\r\n        \"taskOrderCode\": \"TO-20230627-2589\",\r\n        \"workCell\": null\r\n      },\r\n      {\r\n        \"equiCode\": null,\r\n        \"opCode\": \"10BK1003\",\r\n        \"planEndDate\": \"2023-06-27\",\r\n        \"planStartDate\": \"2023-06-27\",\r\n        \"proCode\": \"2401016-B4C/B\",\r\n        \"proName\": \"后桥半壳\",\r\n        \"qty\": \"1\",\r\n        \"taskOrderCode\": \"TO-20230627-2590\",\r\n        \"workCell\": null\r\n      },\r\n      {\r\n        \"equiCode\": null,\r\n        \"opCode\": \"10BK1004\",\r\n        \"planEndDate\": \"2023-06-27\",\r\n        \"planStartDate\": \"2023-06-27\",\r\n        \"proCode\": \"2401016-B4C/B\",\r\n        \"proName\": \"后桥半壳\",\r\n        \"qty\": \"1\",\r\n        \"taskOrderCode\": \"TO-20230627-2591\",\r\n        \"workCell\": null\r\n      },\r\n      {\r\n        \"equiCode\": null,\r\n        \"opCode\": \"10BK1005\",\r\n        \"planEndDate\": \"2023-06-27\",\r\n        \"planStartDate\": \"2023-06-27\",\r\n        \"proCode\": \"2401016-B4C/B\",\r\n        \"proName\": \"后桥半壳\",\r\n        \"qty\": \"1\",\r\n        \"taskOrderCode\": \"TO-20230627-2592\",\r\n        \"workCell\": null\r\n      },\r\n      {\r\n        \"equiCode\": null,\r\n        \"opCode\": \"10BK1006\",\r\n        \"planEndDate\": \"2023-06-27\",\r\n        \"planStartDate\": \"2023-06-27\",\r\n        \"proCode\": \"2401016-B4C/B\",\r\n        \"proName\": \"后桥半壳\",\r\n        \"qty\": \"1\",\r\n        \"taskOrderCode\": \"TO-20230627-2593\",\r\n        \"workCell\": null\r\n      },\r\n      {\r\n        \"equiCode\": null,\r\n        \"opCode\": \"10BK1007\",\r\n        \"planEndDate\": \"2023-06-27\",\r\n        \"planStartDate\": \"2023-06-27\",\r\n        \"proCode\": \"2401016-B4C/B\",\r\n        \"proName\": \"后桥半壳\",\r\n        \"qty\": \"1\",\r\n        \"taskOrderCode\": \"TO-20230627-2594\",\r\n        \"workCell\": null\r\n      },\r\n      {\r\n        \"equiCode\": null,\r\n        \"opCode\": \"10BK1008\",\r\n        \"planEndDate\": \"2023-06-27\",\r\n        \"planStartDate\": \"2023-06-27\",\r\n        \"proCode\": \"2401016-B4C/B\",\r\n        \"proName\": \"后桥半壳\",\r\n        \"qty\": \"1\",\r\n        \"taskOrderCode\": \"TO-20230627-2595\",\r\n        \"workCell\": null\r\n      },\r\n      {\r\n        \"equiCode\": null,\r\n        \"opCode\": \"10BK1009\",\r\n        \"planEndDate\": \"2023-06-27\",\r\n        \"planStartDate\": \"2023-06-27\",\r\n        \"proCode\": \"2401016-B4C/B\",\r\n        \"proName\": \"后桥半壳\",\r\n        \"qty\": \"1\",\r\n        \"taskOrderCode\": \"TO-20230627-2596\",\r\n        \"workCell\": null\r\n      }\r\n    ]\r\n  }\r\n}";
            string urlStr = "http://127.0.0.1:9981/api/IOrder/person";
            string retString;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlStr);
            request.Method = "POST";
            byte[] bytes = Encoding.UTF8.GetBytes(bodyData);
            request.Accept = "*/*";
            request.ContentType = "application/json;charset=UTF-8";

            msgHeader mHeader = new msgHeader();
            //mHeader.messageID = "API_InsertOrder_002";
            //mHeader.interfaceID = "API_InsertOrder_002";
            //mHeader.transID = "API_InsertOrder_002";
            //mHeader.sender = "JF_MTD00002";
            //mHeader.receiver = "JF_D00002";

            ////请求头
            //request.Headers.Add("messageID", mHeader.messageID);
            //request.Headers.Add("interfaceID", mHeader.interfaceID);
            //request.Headers.Add("transID", mHeader.transID);
            //request.Headers.Add("sender", mHeader.sender);
            //request.Headers.Add("receiver", mHeader.receiver);


            request.ContentLength = bytes.Length;
            try
            {
                Stream myResponseStream = request.GetRequestStream();
                myResponseStream.Write(bytes, 0, bytes.Length);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                retString = myStreamReader.ReadToEnd();
                msgResHeader mrh = JsonConvert.DeserializeObject<msgResHeader>(retString);
                int statusCode = (int)response.StatusCode;
                if (statusCode == 200)
                {
                    myStreamReader.Close();
                    myResponseStream.Close();
                    if (response != null)
                    {
                        response.Close();
                    }
                    if (request != null)
                    {
                        request.Abort();
                    }
                }
                MyLog4Net.MyLogHelper.Info("res:", retString);
            }
            catch (Exception err)
            {

            }
        }
        /// <summary>
        /// 过点信息上传测试
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click1111(object sender, EventArgs e)
        {
            //uploadMessage.GetUpLoadMessageButton("MES_产品过点信息", 1);
            new Thread(new ThreadStart(delegate () {
                while (true)
                {
                    try
                    {
                        uploadMessage.GetUpLoadMessageButton("接口_上传_质量数据");
                    }
                    catch (Exception err)
                    {
                    }
                    Thread.Sleep(5000);
                }
            }))
            { IsBackground = true }.Start();
        }
        /// <summary>
        /// 报警信息上传测试
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click2222(object sender, EventArgs e)
        {
            new Thread(new ThreadStart(delegate () {
                while (true)
                {
                    try
                    {
                        //uploadMessage.GetUpLoadMessageButton("上传MES_设备报警信息");
                    }
                    catch (Exception err)
                    {
                    }
                    Thread.Sleep(5000);
                }
            }))
            { IsBackground = true }.Start();
        }
      
        private void Form_MoveSqlTable_Load(object sender, EventArgs e)
        {
            MyLog4Net.MyLogHelper.LogCommit += MyLogHelper_LogCommit;
        }

        private void MyLogHelper_LogCommit(object sender, MyLog4Net.MyLogEventArgs e)
        {
            var module = e.Module;
            var message = e.Message;

            var log = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + " " + module + " " + message + "\r\n";

            count++;


            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() =>
                {
                    if (count > 100)
                    {
                        this.richTextBoxLogBox.Text = "";
                    }
                    this.richTextBoxLogBox.AppendText(log);
                    // this.richTextBoxLogBox.Text += log;
                    richTextBoxLogBox.Focus();
                }));
            }
            else
            {
                if (count > 100)
                {
                    this.richTextBoxLogBox.Text = "";
                }
                this.richTextBoxLogBox.AppendText(log);
                // this.richTextBoxLogBox.Text += log;
                richTextBoxLogBox.Focus();
            }
            
        }

        private void Form_MoveSqlTable_FormClosing_1(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("确认关闭？", "是否关闭", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                //initServer.Close();
                Process.GetCurrentProcess().Kill();
            }
            else
            {
                e.Cancel = true;
            }
        }
    }
}
