package service

import (
	"database/sql"
	"fmt"
	"log"
	"strconv"
	"time"
	"ydMonitoring/databases"
)

type DeviceData struct {
	T_Id        string         `json:"t_id"`
	T_Sp        string         `json:"t_sp"`
	T_Time      string         `json:"t_time"`
	T_T         string         `json:"t_t"`
	T_Rh        string         `json:"t_rh"`
	T_Site      sql.NullString `json:"t_site"`
	Create_time string         `json:"create_time"`
}
type TimeData struct {
	T_Time time.Time
	Data   DeviceData
}

func TestService() {
	sn := "kf861693223539284"
	t_id := "2"
	startTime := "2024-05-14 00:00:00"
	endTime := "2024-09-11 23:59:59"
	sql := fmt.Sprintf("SELECT * FROM z_device_data_%s WHERE t_id=%s and  t_time  BETWEEN '%s' AND '%s';", sn, t_id, startTime, endTime)
	fmt.Println(sql)
	devices := make([]DeviceData, 0)
	rows, err := databases.Db.Query(sql)
	if err != nil {
		log.Fatalf("查询失败 %v", err)
	}
	defer rows.Close()
	// 遍历查询结果
	for rows.Next() {
		var device DeviceData
		err := rows.Scan(&device.T_Id, &device.T_Sp, &device.T_Time, &device.T_T, &device.T_Rh, &device.T_Site, &device.Create_time)
		if err != nil {
			log.Fatalf("扫描失败 %v", err)
		}
		devices = append(devices, device)
	}
	// 检查遍历过程中是否有错误发生
	if err := rows.Err(); err != nil {
		log.Fatalf("遍历错误: %v", err)
	}
	fmt.Println(devices)
	sqls := fmt.Sprintf("SELECT t_save_t FROM device_parameter WHERE t_sn='%s' ORDER BY update_time DESC LIMIT 1", sn)
	var t_save_t string //系统设置间隔时间
	databases.Db.QueryRow(sqls).Scan(&t_save_t)
	fmt.Println(t_save_t)
	adjustData(devices, t_save_t, sn)
}
func adjustData(devices []DeviceData, t_save_t, sn string) {
	timeFormat := "2006-01-02 15:04:05"
	for i := 0; i < len(devices)-1; i++ {
		t1, err := time.Parse(timeFormat, devices[i].T_Time)
		if err != nil {
			log.Println("解析时间失败:", err)
			continue
		}
		t2, err := time.Parse(timeFormat, devices[i+1].T_Time)
		if err != nil {
			log.Println("解析时间失败:", err)
			continue
		}
		timeDiff := t2.Sub(t1).Seconds()
		fmt.Println(timeDiff)
		float, _ := strconv.ParseFloat(t_save_t, 64) //获取系统设置时间
		if timeDiff > float {
			fmt.Printf("时间差大于系统设置时间%s\n %s", devices[i], devices[i+1])
			numInserts := int(timeDiff / float)
			for j := 1; j <= numInserts; j++ {
				newTime := t1.Add(time.Duration(j*int(float)) * time.Second)
				devi := DeviceData{
					T_Id:        devices[i].T_Id,
					T_Sp:        devices[i].T_Sp,
					T_Time:      newTime.Format(timeFormat),
					T_T:         devices[i].T_T,
					T_Rh:        devices[i].T_Rh,
					T_Site:      devices[i].T_Site,
					Create_time: devices[i].Create_time,
				}
				//fmt.Println("插入新时间点:", devi)
				log.Println(devi)
				insertSql := fmt.Sprintf("INSERT INTO z_device_data_%s (t_id, t_sp, t_time, t_t, t_rh, t_site, create_time) VALUES ('%v', '%v', '%v', '%v', '%v' , '%v', '%v')", sn, devi.T_Id, devi.T_Sp, devi.T_Time, devi.T_T, devi.T_Rh, devi.T_Site.String, devi.Create_time)
				_, err := databases.Db.Exec(insertSql)
				if err != nil {
					fmt.Println("插入新时间点失败:", err)
				}
				fmt.Println(insertSql)
			}
		}
	}
}
