package Task

import (
	"ColdVerify_local/conf"
	"ColdVerify_local/lib"
	"ColdVerify_local/logs"
	"encoding/json"
	"fmt"
	"github.com/astaxie/beego/cache"
	_ "github.com/astaxie/beego/cache/redis"
	"github.com/beego/beego/v2/adapter/orm"
	orm2 "github.com/beego/beego/v2/client/orm"
	"github.com/go-sql-driver/mysql"
	_ "github.com/go-sql-driver/mysql"
	"math"
	"sort"
	"strconv"
	"strings"
	"time"
)

const (
	Temperature = "Temperature"
	Humidity    = "Humidity"
)

// 模板  func TestBeego(t *testing.T) {
type TaskData struct {
	Id int `orm:"column(ID);size(11);auto;pk"`

	T_id   string    `orm:"size(256);null"`        // 标题
	T_t    float32   `orm:"size(10);null"`         // 温度
	T_rh   float32   `orm:"size(10);null"`         // 湿度
	T_time time.Time `orm:"type(timestamp);null;"` // 采集时间

	CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间 UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"`   //auto_now 每次 model 保存时都会对时间自动更新
}
type TaskData_Time_Min_Max_GROUP struct {
	T_max    float32 // 温度
	T_max_id string  // 温度
	T_min    float32 // 湿度
	T_min_id string  // 湿度
	T_times  string  // 采集时间

}

func (t *TaskData) TableName() string {
	return "task_data" // 数据库名称   // ************** 替换 FormulaList **************
}

var redisCache_TaskData cache.Cache

func init() {
	//orm2.Debug = true
	config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
		"redis_"+"TaskData", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
	logs.Println(config)
	var err error
	redisCache_TaskData, err = cache.NewCache("redis", config)
	if err != nil || redisCache_TaskData == nil {
		errMsg := "failed to init redis"
		logs.Println(errMsg, err)
	}
}

type TaskDataJPG struct {
	State int    `json:"state"` //1:生成中 2:已完成 3:失败
	Msg   string `json:"msg"`
	Url   string `json:"url"` //url
}

// ---------------- Redis -------------------
// Redis_Set(m.T_sn,m) // Redis 更新缓存
func Redis_TaskDataJPG_Set(key string, r TaskDataJPG) (err error) {
	//json序列化
	str, err := json.Marshal(r)
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return
	}
	err = redisCache_TaskData.Put(key, str, 5*time.Minute)
	if err != nil {
		logs.Println("set key:", key, ",value:", str, err)
	}
	return
}

// if r,is :=Redis_Get(T_sn);is{
// return r,nil
// }
func Redis_TaskDataJPG_Get(key string) (r TaskDataJPG, is bool) {
	if redisCache_TaskData.IsExist(key) {
		logs.Println("找到key:", key)
		v := redisCache_TaskData.Get(key)
		json.Unmarshal(v.([]byte), &r)
		return r, true
	}
	logs.Println("没有 找到key:", key)
	return TaskDataJPG{}, false
}
func Redis_TaskDataJPG_Del(key string) (err error) {
	err = redisCache_TaskData.Delete(key)
	return
}

// 创建数据库  Device.CREATE_TaskData("")
func CREATE_TaskData(alias_name, T_task_id string) bool {
	o := orm2.NewOrmUsingDB(alias_name)

	sql := "DROP TABLE IF EXISTS `z_task_data_" + T_task_id + "`"
	o.Raw(sql).Exec()

	sql = "CREATE TABLE IF NOT EXISTS `z_task_data_" + T_task_id + "` ( " +
		"  `ID` int(11) NOT NULL AUTO_INCREMENT," +
		"	`t_sn` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL," +
		"	`t_id` varchar(256) DEFAULT NULL," +
		"	`t_t` float(6, 1) NULL DEFAULT NULL," +
		"	`t_rh` float(6, 1) NULL DEFAULT NULL," +
		"	`t_time` datetime(0) NULL DEFAULT NULL," +
		"	PRIMARY KEY (`ID`) USING BTREE," +
		"   KEY `t_sn` (`t_sn`)," +
		"   KEY `t_id` (`t_id`)," +
		"   KEY `t_time` (`t_time`)" +
		") ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;"

	_, err := o.Raw(sql).Exec()
	if err != nil {
		return false
	}

	//sqlIndex := "ALTER TABLE `z_task_data_" + T_task_id + "` add unique index(`t_sn`,`t_id`,`t_time`);"
	//logs.Debug(sqlIndex)
	//o.Raw(sqlIndex).Exec()
	return true
}

// ---------------- 特殊方法 -------------------
// 清空
func Truncate_TaskData(alias_name, T_task_id string) bool {

	o := orm2.NewOrmUsingDB(alias_name)

	sql := "truncate table z_task_data_" + T_task_id
	logs.Println(sql)
	_, err := o.Raw(sql).Exec()
	if err != nil {
		// 数据库表不存在，则创建数据库
		if err.(*mysql.MySQLError).Number == 1146 {
			CREATE_TaskData(alias_name, T_task_id)
			return true
		}
		logs.Error(lib.FuncName(), err)
		return false
	}
	return true
}

type TaskData_ struct {
	ID               int     `orm:"column(ID);size(100);null"`   // ID
	T_sn             string  `orm:"column(t_sn);size(256);null"` // sn
	T_id             string  `orm:"column(t_id);size(256);null"` // 标题
	T_t              float32 `orm:"column(t_t);size(10);null"`   // 温度
	T_rh             float32 `orm:"column(t_rh);size(10);null"`  // 湿度
	T_time           string  `orm:"column(t_times);null;"`       // 采集时间
	T_Certificate_sn string  `orm:"size(256);null"`              // 证书编号
}
type TaskDataClass_ struct {
	T_sn       string `orm:"column(t_sn);size(256);null"` // 标题
	T_id       string `orm:"column(t_id);size(256);null"` // 名称
	T_terminal int
}
type TaskData_AVG struct {
	T_t    float32 `orm:"column(t_t);size(10);null"` // 温度
	T_time string  `orm:"column(t_times);null;"`     // 采集时间
}

type TaskData_Total struct {
	T_sn  string
	T_id  string
	Total int64
}

type TaskData_Average struct {
	T_sn    string
	T_id    string
	Average float64
}

func Read_TaskData_ById_List(T_task_id string, SN string, T_id string, Time_start_ string, Time_end_ string, page int, page_z int) ([]TaskData_, int64) {
	o := orm.NewOrm()
	var maps []TaskData_
	var maps_z []orm2.ParamsList
	pagez := page_z

	var offset int
	if page <= 1 {
		offset = 0
	} else {
		page -= 1
		offset = page * pagez
	}

	sql_condition := ""
	if len(Time_start_) > 1 {
		sql_condition += " AND t_time >= '" + Time_start_ + "'"
	}

	if len(Time_end_) > 1 {
		sql_condition += " AND t_time <= '" + Time_end_ + "'"
	}

	if len(T_id) > 0 {
		sql_condition += " AND t_id = '" + T_id + "'"
	}
	if len(SN) > 0 {
		sql_condition += " AND t_sn = '" + SN + "'"
	}
	if len(sql_condition) > 0 {
		sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
	}

	sql := "SELECT COUNT(ID) FROM z_task_data_" + T_task_id + sql_condition

	fmt.Println(sql)
	_, err := o.Raw(sql).ValuesList(&maps_z)
	if err != nil {
		return maps, 0
	}
	if len(maps_z) == 0 {
		return maps, 0
	}

	//fmt.Println("maps_z；",maps_z[0][0])
	sql = "SELECT ID,t_sn,t_id,t_t,t_rh,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i') AS t_times,t_time FROM z_task_data_" + T_task_id + sql_condition + " ORDER BY t_time DESC"
	if page_z != 9999 {
		sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(pagez)
	}

	fmt.Println(sql)
	_, err = o.Raw(sql).QueryRows(&maps)
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}

	//value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
	key, _ := strconv.Atoi(maps_z[0][0].(string))
	return maps, int64(key)
}

func Read_TaskData_ById_List_AES(T_task_id string, SN string, T_id string, Time_start_ string, Time_end_ string, page int, page_z int) ([]TaskData_, int64) {
	o := orm.NewOrm()
	var maps []TaskData_
	var maps_z []orm2.ParamsList
	pagez := page_z

	var offset int
	if page <= 1 {
		offset = 0
	} else {
		page -= 1
		offset = page * pagez
	}

	sql_condition := ""
	if len(Time_start_) > 1 {
		sql_condition += " AND t_time >= '" + Time_start_ + "'"
	}

	if len(Time_end_) > 1 {
		sql_condition += " AND t_time <= '" + Time_end_ + "'"
	}

	if len(T_id) > 0 {
		sql_condition += " AND t_id = '" + T_id + "'"
	}
	if len(SN) > 0 {
		if strings.Contains(SN, "|") {
			// 将字符串按 | 分割
			ids := strings.Split(strings.TrimSuffix(SN, "|"), "|")

			// 构建 SQL 查询
			query := " AND t_sn IN ("
			// 动态添加每个 id
			for i, id := range ids {
				query += "'" + id + "'"
				if i < len(ids)-1 {
					query += ", " // 添加逗号分隔
				}
			}
			query += ") " // 结束 SQL 查询
			sql_condition += query

		} else {
			sql_condition += " AND t_sn = '" + SN + "'"
		}
	}
	if len(sql_condition) > 0 {
		sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
	}

	sql := "SELECT COUNT(ID) FROM z_task_data_" + T_task_id + sql_condition

	fmt.Println(sql)
	_, err := o.Raw(sql).ValuesList(&maps_z)
	if err != nil {
		return maps, 0
	}
	if len(maps_z) == 0 {
		return maps, 0
	}

	//fmt.Println("maps_z；",maps_z[0][0])
	sql = "SELECT ID,t_sn,t_id,t_t,t_rh,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i') AS t_times,t_time FROM z_task_data_" + T_task_id + sql_condition + " ORDER BY t_time"
	if page_z != 9999 {
		sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(pagez)
	}

	fmt.Println(sql)
	_, err = o.Raw(sql).QueryRows(&maps)
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}

	//value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
	key, _ := strconv.Atoi(maps_z[0][0].(string))
	return maps, int64(key)
}

func Read_TaskData_ById_List_SQL(T_task_id string, SQL, SN string, T_id string, Time_start_ string, Time_end_ string, page int, page_z int) ([]TaskData_, int64) {
	o := orm.NewOrm()
	var maps []TaskData_
	var maps_z []orm2.ParamsList
	pagez := page_z

	var offset int
	if page <= 1 {
		offset = 0
	} else {
		page -= 1
		offset = page * pagez
	}

	sql_condition := ""
	if len(Time_start_) > 1 {
		sql_condition += " AND t_time >= '" + Time_start_ + "'"
	}

	if len(Time_end_) > 1 {
		sql_condition += " AND t_time <= '" + Time_end_ + "'"
	}

	if len(T_id) > 0 {
		sql_condition += " AND t_id = '" + T_id + "'"
	}
	if len(SQL) > 0 {
		sql_condition += SQL
	}
	if len(SN) > 0 {
		if strings.Contains(SN, "|") {
			// 将字符串按 | 分割
			ids := strings.Split(strings.TrimSuffix(SN, "|"), "|")

			// 构建 SQL 查询
			query := " AND t_sn IN ("
			// 动态添加每个 id
			for i, id := range ids {
				query += "'" + id + "'"
				if i < len(ids)-1 {
					query += ", " // 添加逗号分隔
				}
			}
			query += ") " // 结束 SQL 查询
			sql_condition += query

		} else {
			sql_condition += " AND t_sn = '" + SN + "'"
		}
	}
	if len(sql_condition) > 0 {
		sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
	}

	sql := "SELECT COUNT(ID) FROM z_task_data_" + T_task_id + sql_condition

	fmt.Println(sql)
	_, err := o.Raw(sql).ValuesList(&maps_z)
	if err != nil {
		return maps, 0
	}
	if len(maps_z) == 0 {
		return maps, 0
	}

	//fmt.Println("maps_z；",maps_z[0][0])
	sql = "SELECT ID,t_sn,t_id,t_t,t_rh,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i') AS t_times,t_time FROM z_task_data_" + T_task_id + sql_condition + " ORDER BY t_time"
	if page_z != 9999 {
		sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(pagez)
	}

	fmt.Println(sql)
	_, err = o.Raw(sql).QueryRows(&maps)
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}

	//value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
	key, _ := strconv.Atoi(maps_z[0][0].(string))
	return maps, int64(key)
}

func Read_TaskData_ById_List_(T_task_id string, SN string) []TaskData_ {
	o := orm.NewOrm()
	var maps []TaskData_

	sql_condition := ""

	sql_condition += " t_sn ='" + SN + "'"

	//fmt.Println("maps_z；",maps_z[0][0])
	sql := "SELECT ID,t_sn,t_id,t_t,t_rh,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i') AS t_times,t_time  FROM z_task_data_" + T_task_id + " WHERE " + sql_condition + " ORDER BY t_time "

	fmt.Println(sql)
	_, err := o.Raw(sql).QueryRows(&maps)
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return maps
	}

	return maps
}

func Read_TaskData_ById_AVG(T_task_id string, SN string, Time_start_ string, Time_end_ string) []TaskData_AVG {
	o := orm.NewOrm()
	var maps []TaskData_AVG

	sql_condition := ""
	if len(Time_start_) > 1 {
		sql_condition += " AND t_time >= '" + Time_start_ + "'"
	}

	if len(Time_end_) > 1 {
		sql_condition += " AND t_time <= '" + Time_end_ + "'"
	}

	if len(SN) > 0 {
		if strings.Contains(SN, "|") {
			// 将字符串按 | 分割
			ids := strings.Split(strings.TrimSuffix(SN, "|"), "|")

			// 构建 SQL 查询
			query := " AND t_sn IN ("
			// 动态添加每个 id
			for i, id := range ids {
				query += "'" + id + "'"
				if i < len(ids)-1 {
					query += ", " // 添加逗号分隔
				}
			}
			query += ") " // 结束 SQL 查询
			sql_condition += query
		} else {
			sql_condition += " AND t_sn = '" + SN + "'"
		}
	}
	if len(sql_condition) > 0 {
		sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
	}

	sql := "SELECT DATE_FORMAT(t_time,'%Y-%m-%d %H:%i') AS t_times, AVG(t_t) AS t_t FROM z_task_data_" + T_task_id + sql_condition + "  GROUP BY t_time  ORDER BY t_time"

	fmt.Println(sql)
	o.Raw(sql).QueryRows(&maps)

	return maps
}
func Read_TaskData_ById_AVG_DESC(T_task_id string, SN string, Time_start_ string, Time_end_ string) []TaskData_AVG {
	o := orm.NewOrm()
	var maps []TaskData_AVG

	sql_condition := ""
	if len(Time_start_) > 1 {
		sql_condition += " AND t_time >= '" + Time_start_ + "'"
	}

	if len(Time_end_) > 1 {
		sql_condition += " AND t_time <= '" + Time_end_ + "'"
	}

	if len(SN) > 0 {
		if strings.Contains(SN, "|") {
			// 将字符串按 | 分割
			ids := strings.Split(strings.TrimSuffix(SN, "|"), "|")

			// 构建 SQL 查询
			query := " AND t_sn IN ("
			// 动态添加每个 id
			for i, id := range ids {
				query += "'" + id + "'"
				if i < len(ids)-1 {
					query += ", " // 添加逗号分隔
				}
			}
			query += ") " // 结束 SQL 查询
			sql_condition += query
		} else {
			sql_condition += " AND t_sn = '" + SN + "'"
		}
	}
	if len(sql_condition) > 0 {
		sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
	}

	sql := "SELECT DATE_FORMAT(t_time,'%Y-%m-%d %H:%i') AS t_times, AVG(t_t) AS t_t FROM z_task_data_" + T_task_id + sql_condition + "  GROUP BY t_time  ORDER BY t_time DESC"

	fmt.Println(sql)
	o.Raw(sql).QueryRows(&maps)

	return maps
}
func Read_TaskData_ById_ClassList(T_task_id string) []TaskDataClass_ {
	o := orm.NewOrm()
	var maps []TaskDataClass_

	//fmt.Println("maps_z；",maps_z[0][0])
	//sql := "SELECT DISTINCT t_sn,t_id FROM z_task_data_" + T_task_id + " ORDER BY t_id "
	//sql := "SELECT DISTINCT t_sn FROM z_task_data_" + T_task_id + " ORDER BY t_id "
	sql := "SELECT t_sn,t_id FROM z_task_data_" + T_task_id + "  GROUP BY t_sn,t_id ORDER BY t_id+0 "

	fmt.Println(sql)
	_, err := o.Raw(sql).QueryRows(&maps)

	if err != nil {
		// 数据库表不存在，则创建数据库
		if err.(*mysql.MySQLError).Number == 1146 {
			CREATE_TaskData(conf.Local_AliasName, T_task_id)
		}
		logs.Error(lib.FuncName(), err)
		return maps
	}

	return maps
}

func Read_TaskData_sn(T_task_id string) []TaskDataClass_ {
	o := orm.NewOrm()
	var maps []TaskDataClass_

	sql := "SELECT DISTINCT t_sn FROM z_task_data_" + T_task_id + " ORDER BY t_sn "

	fmt.Println(sql)
	_, err := o.Raw(sql).QueryRows(&maps)

	if err != nil {
		logs.Error(lib.FuncName(), err)
		return maps
	}

	return maps
}

func Update_TaskData_ByT_sn(T_task_id, T_sn, T_id string) error {
	o := orm.NewOrm()
	// 修改id
	sql := "UPDATE z_task_data_" + T_task_id + " SET `t_id` = '" + T_id + "' WHERE `t_sn` = '" + T_sn + "'"
	logs.Println(sql)
	_, err := o.Raw(sql).Exec()
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return err
	}

	return nil
}

func Delete_TaskData_ByT_sn(T_task_id, T_sn string) error {
	o := orm.NewOrm()
	// 修改id
	sql := "DELETE FROM z_task_data_" + T_task_id + " WHERE `t_sn` = '" + T_sn + "'"
	logs.Println(sql)
	_, err := o.Raw(sql).Exec()
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return err
	}

	return nil
}

// 添加
func Add_TaskData(T_task_id string, T_sn string, T_id string, T_t string, T_rh string, T_time string) bool {

	o := orm2.NewOrm()
	tx, err := o.Begin()
	if err != nil {
		logs.Error("start the transaction failed")
		return false
	}
	// 开始插入数据
	//
	//sql := "INSERT INTO z_task_data_" + T_task_id + " (`t_sn`, `t_id`, `t_time`) " +
	//	"VALUES ('" + T_sn + "', " + T_id + ", '" + T_time + "') " +
	//	"ON DUPLICATE KEY UPDATE t_t=" + T_t + ", t_rh="+ T_rh +";"

	// 去重复数据
	sql := "DELETE FROM z_task_data_" + T_task_id + "  WHERE " + " t_id = '" + T_id + "' AND " + " t_sn = '" + T_sn + "' " + "AND t_time = '" + T_time + "'  "
	//  这里有时间优化  用于一次 prepare 多次 exec，以提高批量执行的速度
	//fmt.Println(sql)
	res, err := tx.Raw(sql).Exec()
	if err != nil {
		tx.Rollback()
		logs.Error(lib.FuncName(), err)
		return false
	}
	res.RowsAffected()

	sql = "INSERT INTO z_task_data_" + T_task_id + " (`t_sn`, `t_id`, `t_t`, `t_rh`, `t_time`) " +
		"VALUES ('" + T_sn + "', '" + T_id + "', " + T_t + "," + T_rh + ", '" + T_time + "')"

	// 更新数据
	//sql := "INSERT  INTO z_task_data_" + T_task_id + " (`t_sn`, `t_id`, `t_t`, `t_rh`, `t_time`) " +
	//	"VALUES ('" + T_sn + "', " + T_id + ", " + T_t + "," + T_rh + ", '" + T_time + "')" +
	//	"on duplicate key update `t_t`=" + T_t + ",`t_rh`=" + T_rh

	//fmt.Println(sql)
	res, err = tx.Raw(sql).Exec()
	if err != nil {
		tx.Rollback()
		logs.Error(lib.FuncName(), err)
		return false
	}
	tx.Commit()

	//fmt.Println("mysql row affected nums: ", num)
	return true
}

// 创建唯一索引
func Create_Unique_Index(T_task_id string) error {
	// 创建索引
	o := orm.NewOrm()
	createIndexSql := "ALTER TABLE z_task_data_" + T_task_id + " ADD CONSTRAINT unique_index_t_sn_t_id_t_time UNIQUE (t_sn, t_id, t_time);"
	//fmt.Println(sql)
	_, err := o.Raw(createIndexSql).Exec()
	if err != nil && !strings.Contains(err.Error(), "Duplicate key name 'unique_index_t_sn_t_id_t_time'") {
		logs.Error(lib.FuncName(), err)
		return err
	}
	return nil
}

// 删除唯一索引
func Delete_Unique_Index(T_task_id string) error {
	// 创建索引
	o := orm.NewOrm()
	// 删除索引
	deleteIndexSql := "ALTER TABLE z_task_data_" + T_task_id + " DROP INDEX unique_index_t_sn_t_id_t_time;"
	//fmt.Println(sql)
	_, err := o.Raw(deleteIndexSql).Exec()
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return err
	}
	return nil
}

func Adds_TaskData(T_task_id string, valueStrings []string) error {

	o := orm.NewOrm()

	// 插入数据
	sql := "INSERT INTO z_task_data_" + T_task_id + " (`t_sn`, `t_id`, `t_t`, `t_rh`, `t_time`) VALUES"
	sql += strings.Join(valueStrings, ",")
	sql += " ON DUPLICATE KEY UPDATE t_t = VALUES(t_t), t_rh = VALUES(t_rh)"
	//fmt.Println(sql)
	_, err := o.Raw(sql).Exec()
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return err
	}

	//fmt.Println("mysql row affected nums: ", num)
	return nil
}

// 批量添加数据
func Batch_Adds_TaskData(T_task_id string, valueStrings []string) error {

	pageSize := 10000                          // 每页的条数
	totalPages := len(valueStrings) / pageSize // 总页数

	// 遍历每一页
	for page := 1; page <= totalPages+1; page++ {

		// 计算当前页的起始索引和结束索引
		startIndex := (page - 1) * pageSize
		if startIndex > len(valueStrings) {
			startIndex = len(valueStrings)
		}
		endIndex := page * pageSize

		// 边界判断，如果结束索引超过列表长度，则将结束索引设置为列表最后一个元素的索引加一
		if endIndex > len(valueStrings) {
			endIndex = len(valueStrings)
		}

		// 获取当前页的数据
		currentList := valueStrings[startIndex:endIndex]
		err := Adds_TaskData(T_task_id, currentList)
		if err != nil {
			return err
		}
		logs.Info(fmt.Sprintf("第%d-%d条数据插入成功", startIndex+1, endIndex))

	}
	return nil
}

// 修改
func Up_TaskData(T_task_id string, Id string, T_t string, T_rh string, T_time string) bool {

	o := orm.NewOrm()
	SET_str := " SET "
	if len(T_t) > 0 {
		SET_str = SET_str + " t_t = " + T_t + " ,"
	}
	if len(T_rh) > 0 {
		SET_str = SET_str + " t_rh = " + T_rh + " ,"
	}
	if len(T_time) > 0 {
		SET_str = SET_str + " t_time = '" + T_time + "' ,"
	}

	if len(SET_str) > 7 {
		SET_str = SET_str[:(len(SET_str) - 1)]
	}

	// 开始插入数据 UPDATE `cold_verify`.`Z_TaskData_d8qMyeXLzIxn` SET `t_t` = 20.2 WHERE `ID` = 69
	sql := "UPDATE z_task_data_" + T_task_id + " " + SET_str + " WHERE ID = " + Id
	//  这里有时间优化  用于一次 prepare 多次 exec，以提高批量执行的速度
	//fmt.Println(sql)
	res, err := o.Raw(sql).Exec()
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return false
	}
	res.RowsAffected()

	//fmt.Println("mysql row affected nums: ", num)
	return true
}

// 删除
func Del_TaskData(T_task_id string, Id string) bool {

	o := orm.NewOrm()

	// 开始插入数据 UPDATE `cold_verify`.`Z_TaskData_d8qMyeXLzIxn` SET `t_t` = 20.2 WHERE `ID` = 69
	sql := "DELETE FROM z_task_data_" + T_task_id + " WHERE ID = " + Id
	//  这里有时间优化  用于一次 prepare 多次 exec，以提高批量执行的速度
	logs.Println(sql)
	res, err := o.Raw(sql).Exec()
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return false
	}
	res.RowsAffected()

	//fmt.Println("mysql row affected nums: ", num)
	return true
}

func Read_TaskData_ByT_id(T_task_id string, Id string) (t_sn string, err error) {

	o := orm.NewOrm()

	// 开始插入数据 UPDATE `cold_verify`.`Z_TaskData_d8qMyeXLzIxn` SET `t_t` = 20.2 WHERE `ID` = 69
	sql := "SELECT t_sn FROM z_task_data_" + T_task_id + " WHERE t_id = '" + Id + "'"
	//  这里有时间优化  用于一次 prepare 多次 exec，以提高批量执行的速度
	logs.Println(sql)

	err = o.Raw(sql).QueryRow(&t_sn)
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return t_sn, err
	}
	//fmt.Println("mysql row affected nums: ", num)
	return t_sn, err
}
func Read_TaskData_ByT_sn(T_task_id string, sn string) (t_id string, err error) {

	o := orm.NewOrm()

	// 开始插入数据 UPDATE `cold_verify`.`Z_TaskData_d8qMyeXLzIxn` SET `t_t` = 20.2 WHERE `ID` = 69
	sql := "SELECT t_id FROM z_task_data_" + T_task_id + " WHERE t_sn = '" + sn + "'"
	//  这里有时间优化  用于一次 prepare 多次 exec，以提高批量执行的速度
	logs.Println(sql)

	err = o.Raw(sql).QueryRow(&t_id)
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return t_id, err
	}
	//fmt.Println("mysql row affected nums: ", num)
	return t_id, err
}

// 添加
func Del_TaskData_t_id(T_task_id string, Id string) bool {

	o := orm.NewOrm()

	// 开始插入数据 UPDATE `cold_verify`.`Z_TaskData_d8qMyeXLzIxn` SET `t_t` = 20.2 WHERE `ID` = 69
	sql := "DELETE FROM z_task_data_" + T_task_id + " WHERE t_id = '" + Id + "'"
	//  这里有时间优化  用于一次 prepare 多次 exec，以提高批量执行的速度
	logs.Println(sql)
	res, err := o.Raw(sql).Exec()
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return false
	}
	res.RowsAffected()

	//fmt.Println("mysql row affected nums: ", num)
	return true
}

// 检查导出表总数
func Check_TaskData_Num(T_task_id string) bool {
	o1 := orm2.NewOrmUsingDB(conf.Local_AliasName)
	o2 := orm2.NewOrmUsingDB(conf.Server_AliasName)

	var maps_z1, maps_z2 []orm2.ParamsList
	sql := "SELECT COUNT(ID) FROM z_task_data_" + T_task_id
	fmt.Println(sql)
	_, err := o1.Raw(sql).ValuesList(&maps_z1)
	if err != nil {
		return false
	}
	_, err = o2.Raw(sql).ValuesList(&maps_z2)
	if err != nil {
		return false
	}
	if len(maps_z1) != len(maps_z2) {
		return false
	}

	return true
}

// 检查导出表总数
func Check_TaskData_Num_Verify1(T_task_id string) bool {
	o1 := orm2.NewOrmUsingDB(conf.Local_AliasName)
	o2 := orm2.NewOrmUsingDB(conf.Verify1_AliasName)

	var maps_z1, maps_z2 []orm2.ParamsList
	sql := "SELECT COUNT(ID) FROM z_task_data_" + strings.ToLower(T_task_id)
	fmt.Println(sql)
	_, err := o1.Raw(sql).ValuesList(&maps_z1)
	if err != nil {
		return false
	}
	sql2 := "SELECT COUNT(ID) FROM Z_TaskData_" + T_task_id
	_, err = o2.Raw(sql2).ValuesList(&maps_z2)
	if err != nil {
		return false
	}
	if len(maps_z1) != len(maps_z2) {
		return false
	}

	return true
}

func Read_TaskData_Num(T_task_id string) int {
	o1 := orm2.NewOrmUsingDB(conf.Local_AliasName)

	var maps_z []orm2.ParamsList
	sql := "SELECT COUNT(ID) FROM z_task_data_" + T_task_id
	fmt.Println(sql)
	_, err := o1.Raw(sql).ValuesList(&maps_z)
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return 0
	}
	key, _ := strconv.Atoi(maps_z[0][0].(string))
	return key
}

func Read_TaskData_Total_GroupBySnId(T_task_id string, SN string, Time_start_ string, Time_end_ string) []TaskData_Total {
	o := orm.NewOrm()

	var maps []TaskData_Total
	sql_condition := ""
	if len(Time_start_) > 1 {
		sql_condition += " AND t_time >= '" + Time_start_ + "'"
	}

	if len(Time_end_) > 1 {
		sql_condition += " AND t_time <= '" + Time_end_ + "'"
	}

	if len(SN) > 0 {
		if strings.Contains(SN, "|") {
			// 将字符串按 | 分割
			ids := strings.Split(strings.TrimSuffix(SN, "|"), "|")

			// 构建 SQL 查询
			query := " AND t_sn IN ("
			// 动态添加每个 id
			for i, id := range ids {
				query += "'" + id + "'"
				if i < len(ids)-1 {
					query += ", " // 添加逗号分隔
				}
			}
			query += ") " // 结束 SQL 查询
			sql_condition += query

		} else {
			sql_condition += " AND t_sn = '" + SN + "'"
		}
	}
	if len(sql_condition) > 0 {
		sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
	}

	sql := "SELECT t_sn,t_id, count(*) AS total FROM z_task_data_" + T_task_id + sql_condition + " GROUP BY T_sn,T_id ORDER BY total DESC"
	fmt.Println(sql)
	_, err := o.Raw(sql).QueryRows(&maps)
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return maps
	}

	return maps
}
func Read_TaskData_Average_GroupBySnId(T_task_id string, SN string, Time_start_ string, Time_end_ string) []TaskData_Average {
	o := orm.NewOrm()

	var maps []TaskData_Average
	sql_condition := ""
	if len(Time_start_) > 1 {
		sql_condition += " AND t_time >= '" + Time_start_ + "'"
	}

	if len(Time_end_) > 1 {
		sql_condition += " AND t_time <= '" + Time_end_ + "'"
	}

	if len(SN) > 0 {
		if strings.Contains(SN, "|") {
			// 将字符串按 | 分割
			ids := strings.Split(strings.TrimSuffix(SN, "|"), "|")

			// 构建 SQL 查询
			query := " AND t_sn IN ("
			// 动态添加每个 id
			for i, id := range ids {
				query += "'" + id + "'"
				if i < len(ids)-1 {
					query += ", " // 添加逗号分隔
				}
			}
			query += ") " // 结束 SQL 查询
			sql_condition += query

		} else {
			sql_condition += " AND t_sn = '" + SN + "'"
		}
	}
	if len(sql_condition) > 0 {
		sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
	}

	sql := "SELECT t_sn,t_id, AVG(t_t) AS average FROM z_task_data_" + T_task_id + sql_condition + " GROUP BY T_sn,T_id ORDER BY average DESC"
	fmt.Println(sql)
	_, err := o.Raw(sql).QueryRows(&maps)
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return maps
	}

	return maps
}
func Read_TaskData_Average(T_task_id string, SN string, Time_start_ string, Time_end_ string) float64 {
	o := orm.NewOrm()

	sql_condition := ""
	if len(Time_start_) > 1 {
		sql_condition += " AND t_time >= '" + Time_start_ + "'"
	}

	if len(Time_end_) > 1 {
		sql_condition += " AND t_time <= '" + Time_end_ + "'"
	}

	if len(SN) > 0 {
		if strings.Contains(SN, "|") {
			// 将字符串按 | 分割
			ids := strings.Split(strings.TrimSuffix(SN, "|"), "|")

			// 构建 SQL 查询
			query := " AND t_sn IN ("
			// 动态添加每个 id
			for i, id := range ids {
				query += "'" + id + "'"
				if i < len(ids)-1 {
					query += ", " // 添加逗号分隔
				}
			}
			query += ") " // 结束 SQL 查询
			sql_condition += query

		} else {
			sql_condition += " AND t_sn = '" + SN + "'"
		}
	}
	if len(sql_condition) > 0 {
		sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
	}
	var v float64
	sql := "SELECT  AVG(t_t)  FROM z_task_data_" + T_task_id + sql_condition
	fmt.Println(sql)
	err := o.Raw(sql).QueryRow(&v)
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return v
	}

	return v
}

func Dump_TaskData(T_task_id, root, password, url_port, database, sql_file string) (string, error) {
	// url_port 127.0.0.1:3306
	// mysql8.0 以上加 --column-statistics=0
	// mysqldump --column-statistics=0 -uroot -proot -h127.0.0.1 -P3306 cold_verify Z_TaskData_ixEfo5zk2Oeb > /Data/Z_TaskData_ixEfo5zk2Oeb.sql
	v := Read_Local_Mysql_Version()
	host_port := strings.Split(url_port, ":")
	table_name := "z_task_data_" + T_task_id
	org := "mysqldump "
	if v >= 8 {
		org += "--column-statistics=0 "
	}
	//--no-create-info 只导出数据，而不添加 CREATE TABLE 语句。
	//--single-transaction 在备份库的时候并不锁定数据表
	//--add-locks：在每个表导出之前增加LOCK TABLES并且之后UNLOCK TABLE。(默认为打开状态，使用--skip-add-locks取消选项)
	//--compact：压缩模式，产生更少的输出；

	org = org + fmt.Sprintf("-u%s -p%s -h%s -P%s --no-create-info --set-gtid-purged=OFF --skip-add-locks --compact --quick %s %s > %s ",
		root, password, host_port[0], host_port[1], database, table_name, sql_file)

	logs.Println(org)
	_, err := lib.Command(org)
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}
	return org, err
}

func Dump_TaskData_Verify(T_task_id, root, password, url_port, database, sql_file string) (string, error) {
	// url_port 127.0.0.1:3306
	// mysql8.0 以上加 --column-statistics=0
	// mysqldump --column-statistics=0 -uroot -proot -h127.0.0.1 -P3306 cold_verify Z_TaskData_ixEfo5zk2Oeb > /Data/Z_TaskData_ixEfo5zk2Oeb.sql
	v := Read_Local_Mysql_Version()
	host_port := strings.Split(url_port, ":")
	table_name := "Z_TaskData_" + T_task_id
	org := "mysqldump "
	if v >= 8 {
		org += "--column-statistics=0 "
	}
	//--no-create-info 只导出数据，而不添加 CREATE TABLE 语句。
	//--single-transaction 在备份库的时候并不锁定数据表
	//--add-locks：在每个表导出之前增加LOCK TABLES并且之后UNLOCK TABLE。(默认为打开状态，使用--skip-add-locks取消选项)
	//--compact：压缩模式，产生更少的输出；

	org = org + fmt.Sprintf("-u%s -p%s -h%s -P%s --no-create-info --set-gtid-purged=OFF --skip-add-locks --compact --quick %s %s > %s ",
		root, password, host_port[0], host_port[1], database, table_name, sql_file)

	logs.Println(org)
	_, err := lib.Command(org)
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}
	return org, err
}

func Insert_TaskData(root, password, url_port, database, sql_file string) (string, error) {
	// url_port 127.0.0.1:3306
	// mysql -u root -p root -h127.0.0.1 -P3306 cold_verify_local < /data/Z_TaskData_ixEfo5zk2Oeb.sql
	host_port := strings.Split(url_port, ":")
	org := fmt.Sprintf("mysql -u%s -p%s -h%s -P%s %s < %s",
		root, password, host_port[0], host_port[1], database, sql_file)
	logs.Println(org)
	_, err := lib.Command(org)
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}
	return org, err
}

func Read_Local_Mysql_Version() int {
	o := orm2.NewOrmUsingDB(conf.Local_AliasName)
	var params []orm2.Params
	o.Raw("select version();").Values(&params)
	version, _ := strconv.Atoi(params[0]["version()"].(string)[0:1])
	return version
}

func Import_TaskData(T_task_id string, offset, pagez int) bool {
	var maps []TaskData_
	serverOrm := orm2.NewOrmUsingDB(conf.Server_AliasName)
	localOrm := orm2.NewOrmUsingDB(conf.Local_AliasName)
	tb_name := "z_task_data_" + T_task_id

	// 开从先上版查询数据
	sql := "select `ID`, `t_sn`, `t_id`, `t_t`, `t_rh`, DATE_FORMAT(t_time,'%Y-%m-%d %H:%i') AS t_times from " + tb_name + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(pagez)
	logs.Println(sql)
	_, err := serverOrm.Raw(sql).QueryRows(&maps)
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return false
	}

	values := []string{}
	for _, row := range maps {
		// 处理数据
		values = append(values, fmt.Sprintf("('%s',%d,%v,%v,'%s')", row.T_sn, row.T_id, row.T_t, row.T_rh, row.T_time))
	}
	// 向本地版插入数据
	sql = "INSERT IGNORE INTO " + tb_name + "( `t_sn`, `t_id`, `t_t`, `t_rh`, `t_time`) VALUES " + strings.Join(values, ",")

	//logs.Println(sql)
	_, err = localOrm.Raw(sql).Exec()
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return false
	}

	return true
}

// InsertTaskData 添加设备数据
func InsertTaskData(T_task_id string, d TaskData_) {

	o := orm.NewOrm()
	var maps TaskData_
	sql := fmt.Sprintf("SELECT * FROM z_task_data_%s where t_time='%s' and t_id = '%s' and t_sn = '%s'", T_task_id, d.T_time, d.T_id, d.T_sn)
	err := o.Raw(sql).QueryRow(&maps)
	if err != nil && err.Error() != orm.ErrNoRows.Error() {
		logs.Error(lib.FuncName(), err)
	}
	// 添加
	if maps.ID == 0 {
		sqlStatement := fmt.Sprintf("insert into z_task_data_%s(t_sn, t_id, t_t, t_rh, t_time) value('%s','%s',%f,%f,'%s')", T_task_id, d.T_sn, d.T_id, d.T_t, d.T_rh, d.T_time)
		_, err = o.Raw(sqlStatement).Exec()
		if err != nil {
			logs.Error(lib.FuncName(), err)
		}
		return
	}

	//已经存在该条目，更新
	sqlStatement := fmt.Sprintf("update z_task_data_%s set t_t = %f,t_rh = %f where t_time= '%s' and t_id = '%s' and t_sn = '%s'", T_task_id, d.T_t, d.T_rh, d.T_time, d.T_id, d.T_sn)
	_, err = o.Raw(sqlStatement).Exec()
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}

}

// InsertTaskData 添加设备数据
func InsertTaskData_TH(T_task_id string, T_switch_t, T_switch_h bool, d TaskData_) {

	o := orm.NewOrm()
	var maps TaskData_
	sql := fmt.Sprintf("SELECT * FROM z_task_data_%s where t_time='%s' and t_id = '%s' and t_sn = '%s'", T_task_id, d.T_time, d.T_id, d.T_sn)
	err := o.Raw(sql).QueryRow(&maps)
	if err != nil && err.Error() != orm.ErrNoRows.Error() {
		logs.Error(lib.FuncName(), err)
	}
	// 添加
	if maps.ID == 0 {
		sqlStatement := fmt.Sprintf("insert into z_task_data_%s(t_sn, t_id, t_t, t_rh, t_time) value('%s','%s',%f,%f,'%s')", T_task_id, d.T_sn, d.T_id, d.T_t, d.T_rh, d.T_time)
		_, err = o.Raw(sqlStatement).Exec()
		if err != nil {
			logs.Error(lib.FuncName(), err)
		}
		return
	}

	//已经存在该条目，更新
	sqlStatement := fmt.Sprintf("update z_task_data_%s set t_t = %f,t_rh = %f where t_time= '%s' and t_id = '%s' and t_sn = '%s'", T_task_id, d.T_t, d.T_rh, d.T_time, d.T_id, d.T_sn)
	if T_switch_t && !T_switch_h {
		sqlStatement = fmt.Sprintf("update z_task_data_%s set t_t = %f where t_time= '%s' and t_id = '%s' and t_sn = '%s'", T_task_id, d.T_t, d.T_time, d.T_id, d.T_sn)
	}
	if !T_switch_t && T_switch_h {
		sqlStatement = fmt.Sprintf("update z_task_data_%s set t_rh = %f where t_time= '%s' and t_id = '%s' and t_sn = '%s'", T_task_id, d.T_rh, d.T_time, d.T_id, d.T_sn)
	}
	_, err = o.Raw(sqlStatement).Exec()
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}

}

// DeleteTaskDataByTimeRange 删除时间范围内的数据
func DeleteTaskDataByTimeRange(T_task_id, sn string, id string, startTime, endTime string) {
	sqlStatement := fmt.Sprintf("delete from z_task_data_%s where t_id = '%s' and t_sn = '%s'", T_task_id, id, sn)
	if len(startTime) > 0 {
		sqlStatement += " AND t_time >='" + startTime + "'"
	}
	if len(endTime) > 0 {
		et, ok := lib.TimeStrToTime(endTime)
		if ok {
			ets := et.Format("2006-01-02 15:04") + ":59"
			sqlStatement += " AND t_time <= '" + ets + "'"
		}

	}
	o := orm.NewOrm()
	exec, err := o.Raw(sqlStatement).Exec()
	if err != nil {
		fmt.Println(err.Error())
	}
	affected, _ := exec.RowsAffected()
	logs.Println(fmt.Sprintf("从 %s - %s 时间段删除了%d条数据", startTime, endTime, affected))
}

func DeleteTaskAllDataByTimeRange(T_task_id, SN string, startTime, endTime string) {
	sqlStatement := fmt.Sprintf("delete from z_task_data_%s where ", T_task_id)

	sql_condition := ""
	if len(SN) > 0 {
		if strings.Contains(SN, "|") {
			// 将字符串按 | 分割
			ids := strings.Split(strings.TrimSuffix(SN, "|"), "|")

			// 构建 SQL 查询
			query := "t_sn IN ("
			// 动态添加每个 id
			for i, id := range ids {
				query += "'" + id + "'"
				if i < len(ids)-1 {
					query += ", " // 添加逗号分隔
				}
			}
			query += ") " // 结束 SQL 查询
			sql_condition += query

		} else {
			sql_condition += "t_sn = '" + SN + "'"
		}
	}
	if len(sql_condition) == 0 {
		logs.Println(fmt.Sprintf("SN 为空，禁止删除"))
		return
	}
	sqlStatement += sql_condition

	if len(startTime) > 0 {
		sqlStatement += " AND t_time >='" + startTime + "'"
	}
	if len(endTime) > 0 {
		sqlStatement += " AND t_time <= '" + endTime + "'"
	}
	o := orm.NewOrm()
	exec, err := o.Raw(sqlStatement).Exec()
	if err != nil {
		fmt.Println(err.Error())
	}
	affected, _ := exec.RowsAffected()
	logs.Println(fmt.Sprintf("从 %s - %s 时间段删除了%d条数据", startTime, endTime, affected))
}

// UpdateTaskDataTemperatureAndHumidityRandom 随机更新
func UpdateTaskDataTemperatureAndHumidityRandom(T_task_id, sn, id, startTime, endTime string, ttMax, ttMin, trhMax, trhMin int) {

	//sql语句
	sqlStatement := fmt.Sprintf("update z_task_data_%s set t_t = t_t + FLOOR(%d + RAND() * (%d - %d + 1)) /100.0, t_rh = t_rh + FLOOR(%d + RAND() * (%d - %d + 1)) /100.0 where t_time BETWEEN '%s' AND '%s' and t_id = '%s' and t_sn = '%s'", T_task_id, ttMin, ttMax, ttMin, trhMin, trhMax, trhMin, startTime, endTime, id, sn)
	o := orm.NewOrm()
	exec, err := o.Raw(sqlStatement).Exec()
	if err != nil {
		fmt.Println("执行错误：", sqlStatement)
	}
	affected, _ := exec.RowsAffected()
	fmt.Println("执行更新行数：", affected)
}

// UpdateTaskDataTemperatureAndHumidity 更新设备探头数据温湿度 固定偏移
func UpdateTaskDataTemperatureAndHumidity(T_task_id, sn, id, startTime, endTime string, temperature, humidity float64) {
	sqlStatement := fmt.Sprintf("update z_task_data_%s set t_t = t_t + %f , t_rh = t_rh + %f where t_id = '%s' and  t_sn = '%s' ",
		T_task_id, temperature, humidity, id, sn)
	if len(startTime) > 0 && len(endTime) > 0 {
		sqlStatement += " AND t_time BETWEEN '" + startTime + "' AND '" + endTime + "'"
	}

	o := orm.NewOrm()

	exec, err := o.Raw(sqlStatement).Exec()
	if err != nil {
		fmt.Println(err.Error())
	}
	affected, err := exec.RowsAffected()
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Printf("影响了%d行\n", affected)
}

// UpdateTaskData 更新设备探头数据
func UpdateTaskData(T_task_id, sn, id string, old, newO TaskData_) {
	sqlStatement := fmt.Sprintf("update z_task_data_%s set t_t = %f , t_rh = %f where t_sn = '%s' and t_id = '%s' and t_t = %f and t_rh = %f and t_time= '%s'", T_task_id, newO.T_t, newO.T_rh, sn, id, old.T_t, old.T_rh, old.T_time)
	fmt.Println("执行SQL：", sqlStatement)
	o := orm.NewOrm()
	exec, err := o.Raw(sqlStatement).Exec()
	if err != nil {
		fmt.Println("执行错误：", sqlStatement, err.Error())
	}
	affected, _ := exec.RowsAffected()
	fmt.Println("执行更新行数：", affected)
}

// 查询温度最小值 最大值
func Read_TaskData_T_Min_Max(T_task_id string, SN string, T_id string, Time_start_ string, Time_end_ string) (minT, maxT float64) {
	o := orm.NewOrm()

	sql_condition := ""
	if len(Time_start_) > 1 {
		sql_condition += " AND t_time >= '" + Time_start_ + "'"
	}

	if len(Time_end_) > 1 {
		sql_condition += " AND t_time <= '" + Time_end_ + "'"
	}

	if len(T_id) > 0 {
		sql_condition += " AND t_id = '" + T_id + "'"
	}
	if len(SN) > 0 {
		sql_condition += " AND t_sn = '" + SN + "'"
	}
	if len(sql_condition) > 0 {
		sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
	}
	//fmt.Println("maps_z；",maps_z[0][0])
	sql := "SELECT MIN(t_t)AS min_t, MAX(t_t)AS max_t FROM z_task_data_" + T_task_id + sql_condition

	fmt.Println(sql)
	err := o.Raw(sql).QueryRow(&minT, &maxT)
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}
	return

}

// 查询时间最小值 最大值
func Read_TaskData_T_time_T_Min_Max(T_task_id string, SN string, T_id string, Time_start_ string, Time_end_ string) (minT, maxT string) {
	o := orm.NewOrm()

	sql_condition := ""
	if len(Time_start_) > 1 {
		sql_condition += " AND t_time >= '" + Time_start_ + "'"
	}

	if len(Time_end_) > 1 {
		sql_condition += " AND t_time <= '" + Time_end_ + "'"
	}

	if len(T_id) > 0 {
		sql_condition += " AND t_id = '" + T_id + "'"
	}
	if len(SN) > 0 {
		sql_condition += " AND t_sn = '" + SN + "'"
	}
	if len(sql_condition) > 0 {
		sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
	}
	//fmt.Println("maps_z；",maps_z[0][0])
	sql := "SELECT DATE_FORMAT(MIN(t_time), '%Y-%m-%d %H:%i') AS min_t, DATE_FORMAT(MAX(t_time), '%Y-%m-%d %H:%i') AS max_t FROM z_task_data_" + T_task_id + sql_condition

	fmt.Println(sql)
	err := o.Raw(sql).QueryRow(&minT, &maxT)
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}
	return

}

// UpdateTaskDataTemperatureAndHumidityByGeometric 更新设备探头数据温湿度 等比缩放
func UpdateTaskDataTemperatureAndHumidityByGeometric(T_task_id, sn, id, startTime, endTime string, temperature, humidity float64) {

	sqlStatement := fmt.Sprintf("update z_task_data_%s set t_t = t_t * %f , t_rh = t_rh * %f where t_id = '%s' and  t_sn = '%s' ", T_task_id, temperature, humidity, id, sn)
	if len(startTime) > 0 && len(endTime) > 0 {
		sqlStatement += " AND t_time BETWEEN '" + startTime + "' AND '" + endTime + "'"
	}
	o := orm.NewOrm()

	exec, err := o.Raw(sqlStatement).Exec()
	if err != nil {
		fmt.Println(err.Error())
	}
	affected, err := exec.RowsAffected()
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Printf("影响了%d行\n", affected)
}
func UpdateTaskDataTemperatureAndHumidityByGeometric_id(T_task_id, id, startTime, endTime string, compress float64) {

	sqlStatement := fmt.Sprintf("update z_task_data_%s set t_t = t_t * %f where t_id = '%s' ", T_task_id, compress, id)
	if len(startTime) > 0 && len(endTime) > 0 {
		sqlStatement += " AND t_time BETWEEN '" + startTime + "' AND '" + endTime + "'"
	}
	o := orm.NewOrm()

	exec, err := o.Raw(sqlStatement).Exec()
	if err != nil {
		fmt.Println(err.Error())
	}
	affected, err := exec.RowsAffected()
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Printf("影响了%d行\n", affected)
}

// UpdateTaskDataTemperatureAndHumidityByGeometric 更新设备探头数据温湿度 偏移
func UpdateTaskDataTemperatureAndHumidityByGeometricAVG(T_task_id, id, startTime, endTime string, temperature float64) {

	sqlStatement := ""
	if temperature > 0 {
		sqlStatement = fmt.Sprintf("update z_task_data_%s set t_t = t_t + %f  where t_id = '%s' ", T_task_id, temperature, id)
	} else {
		sqlStatement = fmt.Sprintf("update z_task_data_%s set t_t = t_t %f  where t_id = '%s' ", T_task_id, temperature, id)
	}

	if len(startTime) > 0 && len(endTime) > 0 {
		sqlStatement += " AND t_time BETWEEN '" + startTime + "' AND '" + endTime + "'"
	}
	o := orm.NewOrm()

	exec, err := o.Raw(sqlStatement).Exec()
	if err != nil {
		fmt.Println(err.Error())
	}
	affected, err := exec.RowsAffected()
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Printf("影响了%d行\n", affected)
}

func Read_TaskData_T_Min_Max_Time_Min_Max(T_task_id string, SN []string, T_id []string, Time_start_ string, Time_end_ string) (minT, maxT float64, minTime, maxTime time.Time) {
	o := orm.NewOrm()

	sql_condition := ""

	if len(Time_start_) > 1 {
		sql_condition += " AND t_time >= '" + Time_start_ + "'"
	}

	if len(Time_end_) > 1 {
		sql_condition += " AND t_time <= '" + Time_end_ + "'"
	}

	if len(T_id) > 0 || len(SN) > 0 {
		sql_condition += " AND t_id in (" + strings.Join(T_id, ",") + ") OR t_sn in (" + strings.Join(SN, ",") + ")"
	}

	if len(sql_condition) > 0 {
		sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
	}
	//fmt.Println("maps_z；",maps_z[0][0])
	sql := "SELECT MIN(t_t) AS min_t, MAX(t_t) AS max_t,MIN(t_time) AS min_time, MAX(t_time) AS max_time FROM z_task_data_" + T_task_id + sql_condition

	fmt.Println(sql)
	err := o.Raw(sql).QueryRow(&minT, &maxT, &minTime, &maxTime)
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}
	return

}

// 获取线上设备数据
func Read_DeviceData_T_Min_Max_Time_Min_Max(T_sn, T_id string, Time_start_ string, Time_end_ string) (minT, maxT float64, minTime, maxTime time.Time) {
	o := orm2.NewOrmUsingDB(conf.Server_AliasName)

	sql_condition := ""

	if len(Time_start_) > 1 {
		sql_condition += " AND t_time >= '" + Time_start_ + "'"
	}

	if len(Time_end_) > 1 {
		sql_condition += " AND t_time <= '" + Time_end_ + "'"
	}

	sql_condition += " AND t_id = '" + T_id + "'"

	if len(sql_condition) > 0 {
		sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
	}
	//fmt.Println("maps_z；",maps_z[0][0])
	sql := "SELECT MIN(t_t) AS min_t, MAX(t_t) AS max_t,MIN(t_time) AS min_time, MAX(t_time) AS max_time FROM z_devicedata_" + T_sn + sql_condition

	fmt.Println(sql)
	err := o.Raw(sql).QueryRow(&minT, &maxT, &minTime, &maxTime)
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}
	return

}

func Read_TaskData_ByIds_List(T_task_id string, SN []string, T_id []string, Time_start_ string, Time_end_ string) []TaskData {
	o := orm.NewOrm()
	var maps []TaskData

	sql_condition := ""
	if len(Time_start_) > 1 {
		sql_condition += " AND t_time >= '" + Time_start_ + "'"
	}

	if len(Time_end_) > 1 {
		sql_condition += " AND t_time <= '" + Time_end_ + "'"
	}

	if len(T_id) > 0 || len(SN) > 0 {
		sql_condition += " AND （t_id in (" + strings.Join(T_id, ",") + ") OR t_sn in (" + strings.Join(SN, ",") + "))"
	}
	if len(sql_condition) > 0 {
		sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
	}

	//fmt.Println("maps_z；",maps_z[0][0])
	sql := "SELECT ID,t_sn,t_id,t_t,t_rh,t_time FROM z_task_data_" + T_task_id + sql_condition + " ORDER BY t_time"

	fmt.Println(sql)
	_, err := o.Raw(sql).QueryRows(&maps)
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}

	return maps
}

func Read_TaskData_T_Min_Max_Time_Min_Max_ListGROUP(T_task_id string, SN string, Time_start_ string, Time_end_ string) []TaskData_Time_Min_Max_GROUP {
	o := orm.NewOrm()
	var maps_Time_Min_Max_GROUP []TaskData_Time_Min_Max_GROUP

	时间_a, _ := time.Parse("2006-01-02 15:04", Time_start_)
	时间_b, _ := time.Parse("2006-01-02 15:04", Time_end_)

	for 时间_a.Unix() <= 时间_b.Unix() {

		var maps []TaskData_
		maps_Time_Min_Max_GROUP_ := TaskData_Time_Min_Max_GROUP{
			T_max:   -100,
			T_min:   100,
			T_times: 时间_a.Format("2006-01-02 15:04"),
		}
		sql_condition := ""
		Time_start_ = 时间_a.Format("2006-01-02 15:04")
		Time_end_ = 时间_b.Format("2006-01-02 15:04")
		if len(Time_start_) > 1 {
			sql_condition += " AND t_time >= '" + Time_start_ + "'"
		}
		if len(Time_end_) > 1 {
			sql_condition += " AND t_time <= '" + Time_end_ + "'"
		}

		if len(SN) > 0 {
			if strings.Contains(SN, "|") {
				// 将字符串按 | 分割
				ids := strings.Split(strings.TrimSuffix(SN, "|"), "|")

				// 构建 SQL 查询
				query := " AND t_sn IN ("
				// 动态添加每个 id
				for i, id := range ids {
					query += "'" + id + "'"
					if i < len(ids)-1 {
						query += ", " // 添加逗号分隔
					}
				}
				query += ") " // 结束 SQL 查询
				sql_condition += query

			} else {
				sql_condition += " AND t_sn = '" + SN + "'"
			}
		}
		if len(sql_condition) > 0 {
			sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
		}

		//fmt.Println("maps_z；",maps_z[0][0])
		sql := "SELECT ID,t_sn,t_id,t_t,t_rh,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i') AS t_times,t_time FROM z_task_data_" + T_task_id + sql_condition + " ORDER BY t_time DESC"

		fmt.Println(sql)
		_, err := o.Raw(sql).QueryRows(&maps)
		if err != nil {
			logs.Error(lib.FuncName(), err)
		}

		for _, data := range maps {
			if data.T_t > maps_Time_Min_Max_GROUP_.T_max {
				maps_Time_Min_Max_GROUP_.T_max = data.T_t
				maps_Time_Min_Max_GROUP_.T_max_id = data.T_id
			}
			if data.T_t < maps_Time_Min_Max_GROUP_.T_max {
				maps_Time_Min_Max_GROUP_.T_min = data.T_t
				maps_Time_Min_Max_GROUP_.T_min_id = data.T_id
			}
		}
		maps_Time_Min_Max_GROUP = append(maps_Time_Min_Max_GROUP, maps_Time_Min_Max_GROUP_)

		时间_a = 时间_a.Add(time.Minute * 1)

	}

	return maps_Time_Min_Max_GROUP
}

// 按照 t_id 分组数据
func GroupDataByTID(data []TaskData_) map[string][]TaskData_ {
	groupedData := make(map[string][]TaskData_)
	for _, d := range data {
		groupedData[d.T_id] = append(groupedData[d.T_id], d)
	}
	return groupedData
}

// 删除的重复数据
func DeleteDeduplicate(T_task_id string) (int64, error) {
	localOrm := orm2.NewOrmUsingDB(conf.Local_AliasName)
	tb_name := "z_task_data_" + T_task_id
	var cnt int64
	// 创建临时表
	sqlCreate := "CREATE TABLE `tmp_table` AS (SELECT MIN(`ID`) AS `min_id` FROM " + tb_name + " GROUP BY `t_sn`,`t_id`,`t_time`);"
	_, err := localOrm.Raw(sqlCreate).Exec()
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return cnt, err
	}
	sqlDelete := "DELETE FROM " + tb_name + "  WHERE `ID` NOT IN (SELECT `min_id` FROM `tmp_table`);"
	res, err := localOrm.Raw(sqlDelete).Exec()
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return cnt, err
	}
	cnt, _ = res.RowsAffected()
	sqlDrop := "DROP TABLE `tmp_table`;"
	_, err = localOrm.Raw(sqlDrop).Exec()
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return cnt, err
	}
	return cnt, nil
}

func CheckTableExist(alias_name, T_task_id string) bool {
	o := orm2.NewOrmUsingDB(alias_name)

	sql := "select count(ID) from z_task_data_" + T_task_id
	_, err := o.Raw(sql).Exec()
	if err != nil {
		// 数据库表不存在，则创建数据库
		logs.Error(lib.FuncName(), err)
		if err.(*mysql.MySQLError).Number == 1146 {
			return false
		}
	}
	return true
}

// 修改auto_increment的初始值
func UpdateAUTO_INCREMENT(alias_name, T_task_id string) error {
	o := orm2.NewOrmUsingDB(alias_name)
	var max_id int
	sql := "select max(ID) from z_task_data_" + T_task_id
	err := o.Raw(sql).QueryRow(&max_id)
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return err
	}
	// ALTER TABLE z_task_data_lg9v2zpfem7b AUTO_INCREMENT = 39905;
	sql = "ALTER TABLE z_task_data_" + T_task_id + " AUTO_INCREMENT = " + strconv.Itoa(max_id+1)
	_, err = o.Raw(sql).Exec()
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return err
	}
	return nil
}

// 修改auto_increment的初始值
func Read_TaskData_AVG(T_task_id, T_sn, T_id, startTime, endTime string) float64 {
	o := orm.NewOrm()
	var v float64
	sql := fmt.Sprintf("select AVG(t_t) FROM z_task_data_%s where t_id = '%s' and t_time BETWEEN '%s' and '%s'", T_task_id, T_id, startTime, endTime)

	err := o.Raw(sql).QueryRow(&v)
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return 0
	}

	return v
}

// 修改auto_increment的初始值
func Read_TaskData_max(T_task_id, T_sn, T_id, startTime, endTime string) float64 {
	o := orm.NewOrm()
	var v float64
	sql := fmt.Sprintf("select MAX(t_t) FROM z_task_data_%s where t_id = '%s' ", T_task_id, T_id)
	if len(startTime) > 0 && len(endTime) > 0 {
		sql += " AND t_time BETWEEN '" + startTime + "' AND '" + endTime + "'"
	}
	err := o.Raw(sql).QueryRow(&v)
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return 0
	}

	return v
}

// 修改auto_increment的初始值
func Read_TaskData_min(T_task_id, T_sn, T_id, startTime, endTime string) float64 {
	o := orm.NewOrm()
	var v float64
	sql := fmt.Sprintf("select MIN(t_t) FROM z_task_data_%s where t_id = '%s' ", T_task_id, T_id)
	if len(startTime) > 0 && len(endTime) > 0 {
		sql += " AND t_time BETWEEN '" + startTime + "' AND '" + endTime + "'"
	}
	err := o.Raw(sql).QueryRow(&v)
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return 0
	}

	return v
}

// 获取第一条数据
func Read_TaskData_first(T_task_id, startTime, endTime string) TaskData_ {
	o := orm.NewOrm()

	sql := "select ID,t_sn,t_id,t_t,t_rh,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i') AS t_times,t_time  FROM z_task_data_" + T_task_id
	if len(startTime) > 0 && len(endTime) > 0 {
		sql += " where t_time BETWEEN '" + startTime + "' AND '" + endTime + "'"
	}
	sql = sql + " LIMIT 0,1"
	fmt.Println(sql)
	var maps TaskData_
	err := o.Raw(sql).QueryRow(&maps)
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return maps
	}

	return maps
}

// 重置 SN数据

func Import_TaskData_Back(Sn string, T_id string, T_task_id string, Time_start string, Time_end string) error {
	o := orm.NewOrm()
	// 开始插入数据
	sql := "insert into z_task_data_" + T_task_id + "(t_sn,t_id,t_t,t_rh,t_time) select '" + Sn + "','" + T_id + "',t_t,t_rh,t_time from z_devicedata_" + Sn
	if len(Time_start) > 0 && len(Time_end) > 0 {
		sql = sql + " WHERE t_time >= '" + Time_start + "' AND t_time <= '" + Time_end + "'"
	}
	logs.Println(sql)
	_, err := o.Raw(sql).Exec()
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return err
	}

	return nil
}

func Import_Task_Back(Sn string, T_id string, T_task_id, To_T_task_id string, Time_start string, Time_end string) error {
	o := orm.NewOrm()
	// 开始插入数据
	sql := "insert into z_task_data_" + To_T_task_id + "(t_sn,t_id,t_t,t_rh,t_time) select t_sn,t_id,t_t,t_rh,t_time from z_task_data_" + T_task_id
	if len(Time_start) > 0 && len(Time_end) > 0 {
		sql = sql + " WHERE t_sn = '" + Sn + "' AND t_id  = '" + T_id + "' AND t_time >= '" + Time_start + "' AND t_time <= '" + Time_end + "'"
	}
	logs.Println(sql)
	_, err := o.Raw(sql).Exec()
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return err
	}

	return nil
}

/* ==============================================================================================================
	                 V型数据，下降持续5分钟，持续上升 5 分钟，取上升点的第一条数据
==============================================================================================================
*/
// isDescending checks if the given data points are strictly descending.
func isDescending(data []TaskData_) bool {
	if len(data) < 2 {
		return false // Not enough data to determine a trend.
	}
	for i := 1; i < len(data); i++ {
		if data[i].T_t >= data[i-1].T_t {
			return false // Not strictly descending.
		}
	}
	return true
}

// isAscending checks if the given data points are strictly ascending.
func isAscending(data []TaskData_) bool {
	if len(data) < 2 {
		return false // Not enough data to determine a trend.
	}
	for i := 1; i < len(data); i++ {
		if data[i].T_t <= data[i-1].T_t {
			return false // Not strictly ascending.
		}
	}
	return true
}

// findVTrend identifies a V-shaped trend in the data and returns the time of the lowest point.
// It looks for a sequence of at least 5 descending points followed by at least 5 ascending points.
func FindVTrend(data []TaskData_) (int, bool) {
	if len(data) < 10 {
		return -1, false // Not enough data to find a V-trend.
	}

	for i := 5; i < len(data)-5; i++ { // Adjust loop for index out of bounds error
		descendingData := data[i-5 : i] // Get 5 descending point
		ascendingData := data[i : i+5]  // get 5 ascending point

		if isDescending(descendingData) && isAscending(ascendingData) {
			// Found a V-trend.  Return the time of the lowest point (data[i]).
			return i, true
		}
	}

	return -1, false // No V-trend found.
}

type CalculateHumps_R struct {
	Start TaskData_AVG
	Peak  TaskData_AVG
	End   TaskData_AVG
}

// 计算驼峰趋势
func CalculateHumps(data []TaskData_AVG) []CalculateHumps_R {
	var humps []CalculateHumps_R
	n := len(data)
	if n < 3 {
		return humps
	}

	i := 0
	for i < n-5 {
		// 寻找开始的最低点
		for !(data[i].T_t <= data[i+1].T_t && data[i+1].T_t <= data[i+2].T_t && data[i+2].T_t <= data[i+3].T_t) {
			i++
			if i > n-5 {
				break
			}
		}
		start := i

		// 寻找最高点
		for !(data[i].T_t >= data[i+1].T_t && data[i+1].T_t > data[i+2].T_t && data[i+2].T_t > data[i+3].T_t) {
			i++
			if i > n-5 {
				break
			}
		}
		peak := i
		Abs_peak := math.Abs(float64(data[start].T_t - data[peak].T_t))
		if Abs_peak < 0.2 {
			continue
		}

		// 寻找结束的最低点
		for !(data[i].T_t <= data[i+1].T_t && data[i+1].T_t < data[i+2].T_t && data[i+2].T_t < data[i+3].T_t) {
			i++
			if i > n-5 {
				break
			}
		}
		end := i
		Abs_end := math.Abs(float64(data[end].T_t - data[peak].T_t))
		if Abs_end < 0.3 {
			continue
		}
		// 如果找到了一个完整的驼峰
		if start < peak && peak < end {
			CalculateHumps_ := CalculateHumps_R{
				Start: data[start],
				Peak:  data[peak],
				End:   data[end],
			}
			humps = append(humps, CalculateHumps_)
		}
	}

	if len(humps) == 0 {
		i = 0
		for i < n-4 {
			// 寻找开始的最低点
			for !(data[i].T_t <= data[i+1].T_t && data[i+1].T_t <= data[i+2].T_t) {
				i++
				if i > n-4 {
					break
				}
			}
			start := i

			// 寻找最高点
			for !(data[i].T_t >= data[i+1].T_t && data[i+1].T_t > data[i+2].T_t) {
				i++
				if i > n-4 {
					break
				}
			}
			peak := i
			Abs_peak := math.Abs(float64(data[start].T_t - data[peak].T_t))
			if Abs_peak < 0.2 {
				continue
			}

			// 寻找结束的最低点
			for !(data[i].T_t <= data[i+1].T_t && data[i+1].T_t < data[i+2].T_t) {
				i++
				if i > n-4 {
					break
				}
			}
			end := i
			Abs_end := math.Abs(float64(data[end].T_t - data[peak].T_t))
			if Abs_end < 0.3 {
				continue
			}
			// 如果找到了一个完整的驼峰
			if start < peak && peak < end {
				CalculateHumps_ := CalculateHumps_R{
					Start: data[start],
					Peak:  data[peak],
					End:   data[end],
				}
				humps = append(humps, CalculateHumps_)
			}
		}
	}

	return humps
}
func CalculateHumpsByThreeDots(data []TaskData_AVG) []CalculateHumps_R {
	var humps []CalculateHumps_R
	n := len(data)
	if n < 3 {
		return humps
	}

	i := 0

	for i < n-4 {
		// 寻找开始的最低点
		for !(data[i].T_t <= data[i+1].T_t && data[i+1].T_t <= data[i+2].T_t) {
			i++
			if i > n-4 {
				break
			}
		}
		start := i

		// 寻找最高点
		for !(data[i].T_t >= data[i+1].T_t && data[i+1].T_t > data[i+2].T_t) {
			i++
			if i > n-4 {
				break
			}
		}
		peak := i
		Abs_peak := math.Abs(float64(data[start].T_t - data[peak].T_t))
		if Abs_peak < 0.2 {
			continue
		}

		// 寻找结束的最低点
		for !(data[i].T_t <= data[i+1].T_t && data[i+1].T_t < data[i+2].T_t) {
			i++
			if i > n-4 {
				break
			}
		}
		end := i
		Abs_end := math.Abs(float64(data[end].T_t - data[peak].T_t))
		if Abs_end < 0.3 {
			continue
		}
		// 如果找到了一个完整的驼峰
		if start < peak && peak < end {
			CalculateHumps_ := CalculateHumps_R{
				Start: data[start],
				Peak:  data[peak],
				End:   data[end],
			}
			humps = append(humps, CalculateHumps_)
		}
	}

	return humps
}

// 根据温度获取列表
func Read_TaskData_ByT_t_List_AES(T_task_id string, SN string, T_t float32, Time_start_ string, Time_end_ string) ([]TaskData_, int64) {
	o := orm.NewOrm()
	var maps []TaskData_
	var maps_z []orm2.ParamsList

	sql_condition := ""
	if len(Time_start_) > 1 {
		sql_condition += " AND t_time >= '" + Time_start_ + "'"
	}

	if len(Time_end_) > 1 {
		sql_condition += " AND t_time <= '" + Time_end_ + "'"
	}
	if T_t > 0 {
		sql_condition += fmt.Sprintf(" AND t_t = %f", T_t)
	}
	if len(SN) > 0 {
		if strings.Contains(SN, "|") {
			// 将字符串按 | 分割
			ids := strings.Split(strings.TrimSuffix(SN, "|"), "|")

			// 构建 SQL 查询
			query := " AND t_sn IN ("
			// 动态添加每个 id
			for i, id := range ids {
				query += "'" + id + "'"
				if i < len(ids)-1 {
					query += ", " // 添加逗号分隔
				}
			}
			query += ") " // 结束 SQL 查询
			sql_condition += query

		} else {
			sql_condition += " AND t_sn = '" + SN + "'"
		}
	}
	if len(sql_condition) > 0 {
		sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
	}

	sql := "SELECT COUNT(ID) FROM z_task_data_" + T_task_id + sql_condition

	fmt.Println(sql)
	_, err := o.Raw(sql).ValuesList(&maps_z)
	if err != nil {
		return maps, 0
	}
	if len(maps_z) == 0 {
		return maps, 0
	}

	//fmt.Println("maps_z；",maps_z[0][0])
	sql = "SELECT ID,t_sn,t_id,t_t,t_rh,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i') AS t_times,t_time FROM z_task_data_" + T_task_id + sql_condition + " ORDER BY t_time"

	fmt.Println(sql)
	_, err = o.Raw(sql).QueryRows(&maps)
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}

	//value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
	key, _ := strconv.Atoi(maps_z[0][0].(string))
	return maps, int64(key)
}

// 获取第一个时间点数据
func GetFirstTimeData(data []TaskData_) (list []TaskData_) {
	if len(data) == 0 {
		return list
	}
	for i := 0; i < len(data); i++ {
		if data[i].T_time == data[0].T_time {
			list = append(list, data[i])
		}
	}
	return list

}

// 温度下降到指定温度 或者下降到最低点+0.2℃
func FindFirstDecreasingToNumber(data []TaskData_, number float32) *TaskData_ {
	if len(data) < 2 {
		return nil
	}

	startIndex := -1
	for i := 0; i < len(data); i++ {
		if data[i].T_t > number {
			startIndex = i
			break
		}
	}
	if startIndex == -1 {
		return nil
	}

	inDecreasing := false
	record := data[startIndex:]
	prev := record[0]

	for i := 1; i < len(record); i++ {
		curr := record[i]
		if curr.T_t < prev.T_t {
			// 下降趋势
			if !inDecreasing {
				inDecreasing = true
			}
			// 判断是否到达 8
			if curr.T_t <= number {
				return &curr
			}
		} else {
			// 趋势中断，重置
			inDecreasing = false
		}
		prev = curr
	}

	var dropStartIndex = -1
	var dropEndIndex = -1

	// 阶段 1: 识别首次下降趋势
	for i := 1; i < len(record); i++ {
		if record[i].T_t < record[i-1].T_t {
			if dropStartIndex == -1 {
				dropStartIndex = i - 1
			}
			dropEndIndex = i
		} else if dropStartIndex != -1 {
			break
		}
	}

	// 如果没找到下降段
	if dropStartIndex == -1 || dropEndIndex == -1 {
		return nil
	}

	// 找最低点
	minRecord := record[dropStartIndex]
	for i := dropStartIndex + 1; i <= dropEndIndex; i++ {
		if record[i].T_t < minRecord.T_t {
			minRecord = record[i]
		}
	}

	// 阶段 2: 从最低点开始向后找 ≥ min + 0.2℃ 的记录
	minTemp := minRecord.T_t
	minIndex := -1
	for i, r := range record {
		if r.ID == minRecord.ID {
			minIndex = i
			break
		}
	}

	for i := minIndex + 1; i < len(record); i++ {
		if record[i].T_t >= minTemp+0.2 {
			return &record[i]
		}
	}

	return nil
}

// 第一个持续下降趋势 到指定温度
func FindFirstDecreasingTendency(data []TaskData_, number float32) *TaskData_ {
	if len(data) < 2 {
		return nil
	}

	startIndex := -1
	for i := 0; i < len(data); i++ {
		if data[i].T_t > number {
			startIndex = i
			break
		}
	}
	if startIndex == -1 {
		return nil
	}

	inDecreasing := false
	record := data[startIndex:]
	prev := record[0]

	for i := 1; i < len(record); i++ {
		curr := record[i]
		if curr.T_t < prev.T_t {
			// 下降趋势
			if !inDecreasing {
				inDecreasing = true
			}
			// 判断是否到达 8
			if curr.T_t <= number {
				return &curr
			}
		} else {
			// 趋势中断，重置
			inDecreasing = false
		}
		prev = curr
	}

	return nil
}

// 按照 t_id 分组数据
func GroupDataByTSN(data []TaskData_) map[string][]TaskData_ {
	groupedData := make(map[string][]TaskData_)
	for _, d := range data {
		groupedData[d.T_sn] = append(groupedData[d.T_sn], d)
	}
	return groupedData
}

//计算样本标准差（n-1分母）

func calculateStdDev(values []float64) float64 {
	n := len(values)
	if n < 2 {
		return 0 // 单点数据无波动
	}

	// 计算均值
	sum := 0.0
	for _, v := range values {
		sum += v
	}
	mean := sum / float64(n)

	// 计算方差
	variance := 0.0
	for _, v := range values {
		diff := v - mean
		variance += diff * diff
	}
	variance /= float64(n - 1) // 样本方差分母n-1

	return math.Sqrt(variance) // 标准差
}

// 获取波动度最大的三条数据集
func GetTheGreatestFluctuations(data []TaskData_, num int) string {
	// 存储波动度结果
	type Result struct {
		T_sn       string
		StdDev     float64
		DataPoints []TaskData_
	}
	dataSets := GroupDataByTSN(data)
	results := make([]Result, 0)

	// 计算每条数据集的波动度
	for T_sn, dataset := range dataSets {
		values := make([]float64, len(dataset))
		for j, dp := range dataset {
			values[j] = float64(dp.T_t)
		}
		results = append(results, Result{
			T_sn: T_sn,
			//StdDev:     calculateStdDev(values),
			StdDev:     standardDeviation(values),
			DataPoints: dataset,
		})
	}

	// 按波动度降序排序
	sort.Slice(results, func(i, j int) bool {
		return results[i].StdDev > results[j].StdDev
	})

	snList := make([]string, num)
	if len(results) >= num {
		for i := 0; i < num; i++ {
			fmt.Println(results[i].T_sn, results[i].StdDev)
			snList = append(snList, results[i].T_sn)
		}
	} else {
		for _, v := range results {
			snList = append(snList, v.T_sn)
		}
	}

	return strings.Join(snList, "|")
}

// 计算平均值
func mean(data []float64) float64 {
	sum := 0.0
	for _, value := range data {
		sum += value
	}
	return sum / float64(len(data))
}
func standardDeviation(data []float64) float64 {
	meanVal := mean(data)
	var sumSquares float64
	for _, value := range data {
		sumSquares += math.Pow(value-meanVal, 2)
	}
	return math.Sqrt(sumSquares / float64(len(data)))
}

func FindUnchangedInterval(data []TaskData_) (int, string, string) {
	if len(data) == 0 {
		return 0, "", ""
	}

	maxLen := 1       // 最大连续长度
	currentLen := 1   // 当前连续长度
	startIndex := 0   // 最大连续段的起始索引
	currentStart := 0 // 当前连续段的起始索引

	for i := 1; i < len(data); i++ {
		if data[i].T_t == data[i-1].T_t {
			currentLen++
			// 发现更长的连续段时更新记录
			if currentLen > maxLen {
				maxLen = currentLen
				startIndex = currentStart
			}
		} else {
			currentLen = 1
			currentStart = i
		}
	}

	// 计算时间区间
	endIndex := startIndex + maxLen - 1
	startTime := data[startIndex].T_time
	endTime := data[endIndex].T_time

	return maxLen, startTime, endTime
}
