using CsharpHttpHelper;
using DevExpress.XtraEditors;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace Intend
{
    public partial class EntityToMssql : XtraForm
    {
        public static readonly string ServiceUrl = $"http://localhost/DeviceRepairAPI/";
        public EntityToMssql()
        {
            InitializeComponent();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //Ver1_1();
            try
            {
                HttpHelper http = new HttpHelper();
                HttpItem item = new HttpItem
                {
                    URL = $"{ServiceUrl}api/plan/GetPlanItems",
                    Method = "Get",
                    ContentType = "application/json; charset=utf-8"
                };

                item.Header.Add("auth", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJBdXRvSUQiOjEsIkdVSUQiOiJlYmVhMzBmZi0yODhhLTRjNGQtYWVlMS1mNTljODQ0OWQwM2EiLCJMb2dpbkNvZGUiOiJhZG1pbiIsIlBhc3NXb3JkIjoiRUI1OTMxMjhCRTYyRTdGMDVERjFEODIwQ0I1OEJFMTMiLCJSZWFsTmFtZSI6IueuoeeQhuWRmCIsIkdlbmRlciI6MCwiQmlydGhkYXkiOiJcL0RhdGUoLTY4NDc4MzM2MDAwMDApXC8iLCJQaG9uZSI6IjEzODAwMDAwMDAwIiwiRW1haWwiOiIiLCJTdGF0dXMiOnRydWUsIkxhc3RMb2dpblRpbWUiOiJcL0RhdGUoMTcxODI2NzMxMzU2MClcLyIsIkRlc2NyaXB0aW9uIjoiNDQxMjMzNDUifQ.jvti7nqTfTnOS1Ep96uiP4MHP8QXNsieW1BwMuUZ3kI");
                item.Header.Add("Operator","1");
                item.Header.Add("OperatorCode", "admin");
                item.Header.Add("OperatorName", "adminName");
                item.Header.Add("ClientMac", "38-D5-7A-E5-42-0D");
                item.Header.Add("IPAddress", "192.168.1.8");
                item.Header.Add("ClientName", "HONOR");

                item.Timeout = 30000;
                item.PostEncoding = Encoding.UTF8;

                HttpResult result = http.GetHtml(item);

                if (result.StatusCode != HttpStatusCode.OK)
                    throw new Exception(result.Html);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void Ver1_1()
        {

            try
            {
                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = "Data Source=www.clovejunti.cn,11433;Initial Catalog=DriveMaintenance;Persist Security Info=True;User ID=sa;Password=P@ssw0rd;",
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true,
                    InitKeyType = InitKeyType.Attribute
                }))
                {
                    List<MainModel> lst = db.Queryable<MainModel>().ToList();
                    memoEdit1.EditValue = JsonConvert.SerializeObject(lst);
                }
            }
            catch (SqlSugarException e)
            {
                MessageBox.Show(e.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}