using Dapper;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 全自动水压检测仪;

namespace 材料热传导系数
{
    public class ConductivityTestData
    {
        public int Id { get; set; }

        public int row { get; set; }

        /// <summary>
        /// 条码（保留用于兼容性）
        /// </summary>
        public string barcode { get; set; }

        /// <summary>
        /// 温度
        /// </summary>
        public double temperature { get; set; }

        /// <summary>
        /// 初始压力
        /// </summary>
        public double startpressure { get; set; }

        /// <summary>
        /// 保压时间
        /// </summary>
        public double dwelltime { get; set; }

        /// <summary>
        /// 压差
        /// </summary>
        public double diffpressure { get; set; }

        /// <summary>
        /// 结束压力
        /// </summary>
        public double endpressure { get; set; }

        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 普通0高温1
        /// </summary>
        public int Type { get; set; }




        public string kzh { get; set; }
        public DateTime? starttime { get; set; }
        public DateTime? endtime { get; set; }
        public string lldh { get; set; }
        public string jh { get; set; }
        public int quantity { get; set; }
        public decimal? standarderror { get; set; }
        public string testresult { get; set; }



    }

    public class ScanData
    {
        public int Id { get; set; }

        /// <summary>
        /// 条码（保留用于兼容性）
        /// </summary>
        public string barcode { get; set; }

        ///// <summary>
        ///// 数量
        ///// </summary>
        //public int Quantity { get; set; }

        /// <summary>
        /// 压力
        /// </summary>
        public float diffpressure { get; set; }

        /// <summary>
        /// 出口温度
        /// </summary>
        public float exit_temperature { get; set; }

        /// <summary>
        /// 温度
        /// </summary>
        public float temperature { get; set; }

        /// <summary>
        /// 保压时间
        /// </summary>
        public float dwelltime { get; set; }

        /// <summary>
        /// 压差设置
        /// </summary>
        public float PressureDifference { get; set; }

        ///// <summary>
        ///// 标准误差
        ///// </summary>
        //public float StandardError { get; set; }

        public DateTime CreateTime { get; set; }

        /// <summary>
        ///高温模式 1是0否   
        /// </summary>
        public bool IsHighMode { get; set; }


        /// <summary>
        ///模式选择
        /// </summary>
        public string TemperatureMode { get; set; }





        public string kzh { get; set; }

        public string lldh { get; set; }
        public string jh { get; set; }
        public int? quantity { get; set; }
        public float? pressuresetting { get; set; }
        public decimal? standarderror { get; set; }

    }


    public class ConductivityRepository
    {

        public readonly string _connectionString;

        public ConductivityRepository()
        {
            _connectionString = "Server=localhost;Database=fullautowaterpressure;User=root;Password=123456;port=3306;charset=utf8;";
        }

        public void InsertReportItems(ConductivityTestData data)
        {
            using (var connection = new MySqlConnection(_connectionString))
            {
                connection.Open();
                var sql = @"INSERT INTO normaltemperature (
	barcode,
	temperature,
	startpressure,
	dwelltime,
	diffpressure,
	endpressure,
	CreateTime,
	type,
	kzh,
	starttime,
	endtime,
	lldh,
	jh,
	quantity,
	standarderror,
	testresult 
)
VALUES
	(
		@barcode,
		@temperature,
		@startpressure,
		@dwelltime,
		@diffpressure,
		@endpressure,
	CURRENT_TIMESTAMP,
	@type,	@kzh,
	@starttime,
	@endtime,
	@lldh,
	@jh,
	@quantity,
	@standarderror,
	@testresult )";
                connection.Execute(sql, data);
            }
        }
        public void InsertScanItems(ScanData data)
        {
            using (var connection = new MySqlConnection(_connectionString))
            {
                connection.Open();
                var sql = @"INSERT INTO scandata 
                   (barcode,  diffpressure, exit_temperature, temperature, dwelltime, CreateTime, TemperatureMode,
kzh,
lldh,
jh,
quantity	,
pressuresetting	,
standarderror) 
                   VALUES 
                   (@barcode,  @diffpressure, @exit_temperature, @temperature, @dwelltime, CURRENT_TIMESTAMP, @TemperatureMode,
@kzh,
@lldh,
@jh,
@quantity,
@pressuresetting,
@standarderror
)";
                connection.Execute(sql, data);
            }
        }

        public void DeleteScanItems(int id)
        {
            using (var connection = new MySqlConnection(_connectionString))
            {
                connection.Open();
                var sql = @"delete from scandata where id=@id
                   ";
                connection.Execute(sql, new { id });
            }
        }

        public List<ScanData> GetScanData()
        {
            using (var connection = new MySqlConnection(_connectionString))
            {
                connection.Open();
                var sql = @"SELECT * FROM scandata 
                           
                           ORDER BY id asc ";
                return connection.Query<ScanData>(sql).ToList();
            }
        }

        public List<ConductivityTestData> GetTestData()
        {
            using (var connection = new MySqlConnection(_connectionString))
            {
                connection.Open();
                var sql = @"SELECT * FROM normaltemperature 
                           
                           ORDER BY id asc ";
                return connection.Query<ConductivityTestData>(sql).ToList();
            }
        }


        public void DeleteTestAllItems()
        {
            using (var connection = new MySqlConnection(_connectionString))
            {
                connection.Open();
                var sql = @"delete from normaltemperature
                   ";
                connection.Execute(sql);
            }
        }

        public List<ScanData> GetScanDataBylldh_jh(string jh)
        {
            using (var connection = new MySqlConnection(_connectionString))
            {
                connection.Open();
                var sql = @"SELECT * FROM scandata 
                           where jh =  @jh
                           ORDER BY id asc ";
                return connection.Query<ScanData>(sql, new { jh }).ToList();
            }
        }

        /// <summary>
        /// 更新扫描数据
        /// </summary>
        /// <param name="scanData">要更新的扫描数据</param>
        public void UpdateScanItem(ScanData scanData)
        {
            using (var connection = new MySqlConnection(_connectionString))
            {
                connection.Open();
                var sql = @"UPDATE scandata SET 
                    barcode = @barcode,
                    diffpressure = @diffpressure,
                    exit_temperature = @exit_temperature,
                    temperature = @temperature,
                    dwelltime = @dwelltime,
                    TemperatureMode = @TemperatureMode,
                    kzh = @kzh,
                    lldh = @lldh,
                    jh = @jh,
                    quantity = @quantity,
                    pressuresetting = @pressuresetting,
                    standarderror = @standarderror
                    WHERE id = @Id";

                connection.Execute(sql, new
                {
                    scanData.barcode,
                    scanData.diffpressure,
                    scanData.exit_temperature,
                    scanData.temperature,
                    scanData.dwelltime,
                    scanData.TemperatureMode,
                    scanData.kzh,
                    scanData.lldh,
                    scanData.jh,
                    scanData.quantity,
                    scanData.pressuresetting,
                    scanData.standarderror,
                    scanData.Id
                });
            }
        }

        /// <summary>
        /// 根据ID获取扫描数据
        /// </summary>
        /// <param name="id">数据ID</param>
        /// <returns>扫描数据对象</returns>
        public ScanData GetScanDataById(int id)
        {
            using (var connection = new MySqlConnection(_connectionString))
            {
                connection.Open();
                var sql = @"SELECT * FROM scandata WHERE id = @id";
                return connection.QueryFirstOrDefault<ScanData>(sql, new { id });
            }
        }

        public bool TestConnection()
        {
            try
            {
                using (var connection = new MySqlConnection(_connectionString))
                {
                    connection.Open();
                    return connection.State == ConnectionState.Open;
                }
            }
            catch
            {
                return false;
            }
        }



        /// <summary>
        /// 根据ID获取测试数据
        /// </summary>
        public ConductivityTestData GetTestDataById(int id)
        {
            using (var connection = new MySqlConnection(_connectionString))
            {
                connection.Open();
                var sql = @"SELECT * FROM normaltemperature WHERE id = @id";
                return connection.QueryFirstOrDefault<ConductivityTestData>(sql, new { id });
            }
        }

        /// <summary>
        /// 更新测试数据
        /// </summary>
        public void UpdateTestData(ConductivityTestData testData)
        {
            using (var connection = new MySqlConnection(_connectionString))
            {
                connection.Open();
                var sql = @"UPDATE normaltemperature SET 
                    barcode = @barcode,
                    temperature = @temperature,
                    startpressure = @startpressure,
                    dwelltime = @dwelltime,
                    diffpressure = @diffpressure,
                    endpressure = @endpressure,
                    type = @type,
                    kzh = @kzh,
                    starttime = @starttime,
                    endtime = @endtime,
                    lldh = @lldh,
                    jh = @jh,
                    quantity = @quantity,
                    standarderror = @standarderror,
                    testresult = @testresult
                    WHERE id = @Id";

                connection.Execute(sql, testData);
            }
        }



    }
}
