59 lines
1.8 KiB
Go
59 lines
1.8 KiB
Go
package peak_valley
|
||
|
||
import (
|
||
"energy-management-system/global"
|
||
"fmt"
|
||
"gorm.io/gorm"
|
||
"time"
|
||
)
|
||
|
||
const MinutesInADay = 24 * 60
|
||
|
||
// PeakValleyTimeBlock 峰谷时间区块
|
||
type PeakValleyTimeBlock struct {
|
||
BlockIndex uint `gorm:"column:block_index;primaryKey;comment:区块编号" json:"block_index"`
|
||
StartTime uint `gorm:"column:start_time;comment:开始时间" json:"start_time"`
|
||
EndTime uint `gorm:"column:end_time;comment:结束时间" json:"end_time"`
|
||
Created time.Time `gorm:"column:created;autoCreateTime;comment:创建时间" json:"created"`
|
||
Updated time.Time `gorm:"column:updated;autoUpdateTime;comment:修改时间" json:"updated"`
|
||
DeletedAt gorm.DeletedAt `gorm:"index;comment:删除时间" json:"-"`
|
||
}
|
||
|
||
func (r *PeakValleyTimeBlock) TableName() string {
|
||
return global.AppConf.Db.TablePrefix + "peak_valley_time_blocks"
|
||
}
|
||
|
||
func Init() {
|
||
// 一天有24小时,即1440分钟
|
||
totalMinutes := 24 * 60
|
||
// 时间块的持续时间:10分钟
|
||
blockDuration := 10
|
||
|
||
// 遍历一天中的所有时间块
|
||
for i := 0; i < totalMinutes; i += blockDuration {
|
||
startTime := i
|
||
endTime := i + blockDuration
|
||
|
||
fmt.Println("start", startTime)
|
||
fmt.Println("start", endTime)
|
||
|
||
global.Db.Create(&PeakValleyTimeBlock{
|
||
StartTime: uint(startTime),
|
||
EndTime: uint(endTime),
|
||
})
|
||
|
||
// 打印开始时间和结束时间,格式为 HH:MM
|
||
//fmt.Printf("时间块 %d: 开始时间: %02d:%02d, 结束时间: %02d:%02d\n",
|
||
// i/blockDuration+1,
|
||
// startTime/60, startTime%60,
|
||
// endTime/60, endTime%60)
|
||
|
||
// 使用整数存储时间块的开始和结束时间(以分钟为单位)
|
||
//startTime := 0 // 00:00
|
||
//endTime := 10 // 00:10
|
||
//
|
||
//fmt.Printf("开始时间: %02d:%02d\n", startTime/60, startTime%60)
|
||
//fmt.Printf("结束时间: %02d:%02d\n", endTime/60, endTime%60)
|
||
}
|
||
}
|