package Task

import (
	"ColdVerify_local/conf"
	"ColdVerify_local/lib"
	"ColdVerify_local/logs"
	"fmt"
	_ "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"
	"strconv"
	"strings"
	"time"
)

// 模板
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 保存时都会对时间自动更新
}

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

func init() {
	orm2.Debug = true
}

// 创建数据库  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 {
		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;"`       // 采集时间
}
type TaskDataClass_ struct {
	T_sn string `orm:"column(t_sn);size(256);null"` // 标题
	T_id string `orm:"column(t_id);size(256);null"` // 名称
}

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:%s') 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 {
		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:%s') 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-%c-%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_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 ORDER BY t_id+0 "

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

	if err != nil {
		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 := orm.NewOrm()

	// 开始插入数据
	//
	//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 := o.Raw(sql).Exec()
	//if err != nil {
	//	fmt.Println(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 := 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 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 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 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:%s') 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)
	}

}

// DeleteTaskDataByTimeRange 删除时间范围内的数据
func DeleteTaskDataByTimeRange(T_task_id, sn string, id string, start, end string) {
	sqlStatement := fmt.Sprintf("delete from z_task_data_%s where t_time between '%s' and '%s' and t_id = '%s' and t_sn = '%s'", T_task_id, start, end, id, sn)
	o := orm.NewOrm()
	exec, err := o.Raw(sqlStatement).Exec()
	if err != nil {
		fmt.Println(err.Error())
	}
	affected, _ := exec.RowsAffected()
	fmt.Printf("从%s～%s时间段删除了%d条数据", start, end, 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' and t_time BETWEEN '%s' and '%s'", T_task_id, temperature, humidity, id, sn, startTime, 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)
}
