using System.Data;
using System.Dynamic;
using BLW_Log.Models;
using BLW_Log.services;
using Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MySql.Data.MySqlClient;

namespace BLW_Log.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProductionlineController : ControllerBase
    {

        [HttpPost()]
        public ReturnInfo ProductionlineSelect([FromBody] QueryDataForm data)
        {
            ReturnInfo info = new ReturnInfo();
            try
            {

                string constr = ReadConfig.Instance.ProductionlineSQLConnectionString;
                var Q = new MySqlConnection(constr);
                if (Q.State != ConnectionState.Open)
                {
                    Q.Open();
                }
                string h22 = string.Join(",", data.testinfo.Select(name => $"'{name.Replace("'", "''")}'"));
                string sql = @$"SELECT `ID`,`测试时间`
                    ,`测试结果`
                    ,`测试信息`
                    ,`ADC_LEV_10`
                    ,`ADC_LEV_0`
                    ,`软件版本号`
                    ,`硬件版本号`
                    ,`UUID`
                    ,`状态机`
                    ,`实时值`
                    ,`背景值`
                    ,`基底值`
                    ,`LEL百分比`
                    ,`稳定状态`
                    ,`温度` FROM `calibrationtest`
                  where `测试时间`>=@testtime_start and `测试时间`<=@testtime_end
                  and `测试信息` in ({h22})
                  ";
                MySqlCommand cmd = new MySqlCommand(sql, Q);

                MySqlParameter pm3 = new("@testtime_start", data.testrange_start);
                MySqlParameter pm4 = new("@testtime_end", data.testrange_end);
                cmd.Parameters.Add(pm3);
                cmd.Parameters.Add(pm4);

                if (data.testresult != "")
                {
                    sql += " and `测试结果`=@testresult ";
                    MySqlParameter pm1 = new MySqlParameter("@testresult", data.testresult);
                    cmd.Parameters.Add(pm1);
                }

                cmd.CommandText = sql;
                MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet);

                DataTable dt = dataSet.Tables[0];

                List<dynamic> list = new List<dynamic>();
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow item in dt.Rows)
                    {
                        dynamic ddd = new ExpandoObject();
                        ddd.ID = item["ID"].ToString();
                        ddd.TestTime = item["测试时间"].ToString();
                        ddd.TestResult = item["测试结果"].ToString();
                        ddd.TestInfo = item["测试信息"].ToString();
                        ddd.ADC_LEV_10 = item["ADC_LEV_10"].ToString();
                        ddd.ADC_LEV_0 = item["ADC_LEV_0"].ToString();
                        ddd.SoftwareVer = item["软件版本号"].ToString();
                        ddd.HardwareVer = item["硬件版本号"].ToString();
                        ddd.UUID = item["UUID"].ToString();
                        ddd.StateMachine = item["状态机"]?.ToString();
                        ddd.RealtimeVal = item["实时值"]?.ToString();
                        ddd.BackgroundVal = item["背景值"]?.ToString();
                        ddd.BaseVal = item["基底值"]?.ToString();
                        ddd.LelPer = item["LEL百分比"].ToString();
                        ddd.Stabilise = item["稳定状态"].ToString();
                        ddd.Temp = item["温度"].ToString();
                        list.Add(ddd);
                    }
                }
                info.isok = true;
                info.response = list;
            }
            catch (Exception ex)
            {
                info.isok = false;
                info.message = ex.Message;
            }
            return info;
        }

        [HttpPost()]
        public ReturnInfo ProductionlineTestInfo()
        {
            ReturnInfo info = new ReturnInfo();
            try
            {

                string constr = ReadConfig.Instance.ProductionlineSQLConnectionString;
                var Q = new MySqlConnection(constr);
                if (Q.State != ConnectionState.Open)
                {
                    Q.Open();
                }
                string sql = @"SELECT `测试信息` from calibrationtest GROUP BY `测试信息`;";
                MySqlCommand cmd = new MySqlCommand(sql, Q);

                MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet);
                DataTable dt = dataSet.Tables[0];
                List<dynamic> list = new List<dynamic>();
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow item in dt.Rows)
                    {
                        dynamic ddd = new ExpandoObject();
                        ddd.value = item["测试信息"].ToString();
                        list.Add(ddd);
                    }
                }
                info.isok = true;
                info.response = list;
            }
            catch (Exception ex)
            {
                info.isok = false;
                info.message = ex.Message;
            }
            return info;
        }


        [HttpPost()]
        public ReturnInfo  Productionline(Dictionary<string, string> dic)
        {
            ReturnInfo info = new ReturnInfo();
            if (dic["key"].Equals("i_love_blw^_^"))
            {
                try
                {
                    MyExcel.TempNewMethod(true);
                    info.isok = true;
                }
                catch (Exception ex)
                {
                    info.message = ex.Message;
                    info.isok = false;
                }
            }
            else
            {
                info.isok = false;
                info.message = "你无法这样子操作";
            }
            return info;
        }
    }
    public class QueryDataForm
    {
        public string testresult { get; set; }
        public List<string> testinfo { get; set; }
        public string testrange_start { get; set; }
        public string testrange_end { get; set; }
    }
}
