Files
energy-management-system/model/init-db-data/point_name.go
2024-09-03 15:52:38 +08:00

57 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package init_db_data
import (
"energy-management-system/global"
"energy-management-system/model"
"github.com/gookit/color"
"gorm.io/gorm"
)
type InitPointNameData struct{}
var initPointNameData = new(InitPointNameData)
// type api struct{}
//func buildData() (dataList []*peak_valley_model.PeakValleyTimeBlock) {
// // 一天有24小时即1440分钟
// totalMinutes := 24 * 60
// // 时间块的持续时间10分钟
// blockDuration := 10
// // 遍历一天中的所有时间块
// for i := 0; i < totalMinutes; i += blockDuration {
// startTime := i
// endTime := i + blockDuration
// dataList = append(dataList, &peak_valley_model.PeakValleyTimeBlock{
// StartTime: uint(startTime),
// EndTime: uint(endTime),
// })
// }
// return dataList
//}
var pointNames = []model.PointName{
{Name: "测点-用电量"},
{Name: "测点-用水量"},
}
// Init 初始化用户数据
func (i *InitPointNameData) Init() error {
return global.Db.Transaction(func(tx *gorm.DB) error {
m := &model.PointName{}
var count int64
err := tx.Model(&model.PointName{}).Count(&count).Error
if err != nil {
return err
}
if count > 0 {
color.Danger.Println("\n[PGSQL] --> " + m.TableName() + " 表的初始数据已存在!")
return nil
}
if err := tx.Create(&pointNames).Error; err != nil { // 遇到错误时回滚事务
return err
}
color.Info.Println("\n[PGSQL] --> " + m.TableName() + " 表初始数据成功!")
return nil
})
}