|
@@ -1,153 +1,154 @@
|
|
|
package Warning
|
|
|
|
|
|
-import (
|
|
|
- "database/sql"
|
|
|
- "fmt"
|
|
|
- "gorm.io/driver/mysql"
|
|
|
- "gorm.io/gorm"
|
|
|
- "strings"
|
|
|
-
|
|
|
- "time"
|
|
|
-)
|
|
|
-
|
|
|
-var db *gorm.DB
|
|
|
-var sqlDB *sql.DB
|
|
|
-
|
|
|
-type WarningDB struct {
|
|
|
- Id int64 `gorm:"column:ID;type:bigint;primaryKey"`
|
|
|
- T_pid int32 `gorm:"column:t_pid;index:merge"` // Account.Company 绑定公司
|
|
|
- T_tp int `gorm:"column:t_tp;index:merge"` // 报警类型 ->WarningList
|
|
|
- T_sn string `gorm:"column:t_sn;index:tsn;size:256;"` // 设备序列号
|
|
|
- T_D_name string `gorm:"column:t__d_name;size:256;"` // 设备名称
|
|
|
- T_id int32 `gorm:"column:t_id;"` // 传感器 ID
|
|
|
- T_DS_name string `gorm:"column:t__d_s_name;size:256;"` // 传感器名称
|
|
|
- T_Remark string `gorm:"column:t__remark;type:longtext;"` // 采集内容
|
|
|
- T_Ut time.Time `gorm:"column:t__ut;type:datetime;"` // 采集时间
|
|
|
- T_fUt time.Time `gorm:"column:t_f_ut;type:datetime;"` // 首次采集时间
|
|
|
- T_Text string `gorm:"column:t__text;type:longtext;"` // 处理备注
|
|
|
- T_Log string `gorm:"column:t__log;type:longtext;"` // 通知日志
|
|
|
- T_Msid int64 `gorm:"column:t__msid;"` // 消息ID
|
|
|
- T_State int `gorm:"column:t__state;size:2;default:2;index:merge"` // 0 删除 1 不处理 2 已处理 3 未处理
|
|
|
- CreateTime time.Time `gorm:"column:create_time;type:datetime;autoUpdateTime"` //auto_now_add 第一次保存时才设置时间
|
|
|
- UpdateTime time.Time `gorm:"column:update_time;type:datetime;autoCreateTime"` //auto_now 每次 model 保存时都会对时间自动更新
|
|
|
-}
|
|
|
-
|
|
|
-var MysqlServer_UrlPort = "127.0.0.1:40306"
|
|
|
-var MysqlServer_Database = "cold"
|
|
|
-var MysqlServer_Username = "cold"
|
|
|
-var MysqlServer_Password = "yjwyEckZS7rE5H!"
|
|
|
-
|
|
|
-func init() {
|
|
|
- // 连接数据库
|
|
|
- var err error
|
|
|
-
|
|
|
- dsn := MysqlServer_Username + ":" + MysqlServer_Password + "@tcp(" + MysqlServer_UrlPort + ")/" + MysqlServer_Database + "?charset=utf8mb4&loc=Local&parseTime=True"
|
|
|
- db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
|
|
- if err != nil {
|
|
|
-
|
|
|
- fmt.Println(err)
|
|
|
-
|
|
|
- }
|
|
|
- sqlDB, err = db.DB()
|
|
|
- // SetMaxIdleConns 设置空闲连接池中的最大连接数。
|
|
|
- sqlDB.SetMaxIdleConns(1)
|
|
|
-
|
|
|
- // SetMaxOpenConns 设置数据库连接最大打开数。
|
|
|
- sqlDB.SetMaxOpenConns(10)
|
|
|
-
|
|
|
- // SetConnMaxLifetime 设置可重用连接的最长时间
|
|
|
- sqlDB.SetConnMaxLifetime(time.Hour)
|
|
|
- //db.AutoMigrate(&DeviceWarning{})
|
|
|
-
|
|
|
- //db.SetLogger(log.New(logs.LogxOrm, "\r\nWarningBack:", 0))
|
|
|
- fmt.Println("db init")
|
|
|
-}
|
|
|
-
|
|
|
-/*
|
|
|
-time_a : 开始时间 "2024-01-24 08:21:40"
|
|
|
-time_b : 结束时间 "2024-03-24 08:21:40"
|
|
|
-WHERE_t_pid : 公司ID > 0
|
|
|
-WHERE_t_tp : 报警类型 > 0
|
|
|
-WHERE_t__state : 状态 -1 无限制 0 删除 1 不处理 2 已处理 3 未处理
|
|
|
-WHERE_t_sn : 设备序列号
|
|
|
-PageIndex : 页码值
|
|
|
-PageSize : 每页多少
|
|
|
-*/
|
|
|
-
|
|
|
-func Handy(time_a, time_b string, WHERE_t_pid int, WHERE_t_tp int, WHERE_t__state int, WHERE_t_sn string, PageIndex int, PageSize int) ([]WarningDB, int) {
|
|
|
- var offset int // 数据起点
|
|
|
- if PageIndex <= 1 {
|
|
|
- offset = 0
|
|
|
- } else {
|
|
|
- offset = (PageIndex - 1) * PageSize
|
|
|
- }
|
|
|
- var TABLE = "warning_"
|
|
|
- monthsArray := generateMonthArray(time_a, time_b)
|
|
|
-
|
|
|
- var Warning_List []WarningDB
|
|
|
- var Count_List []int
|
|
|
- var Count_Num int // 累积数量
|
|
|
- for _, v := range monthsArray {
|
|
|
- var count int64
|
|
|
- var countoffset int // 当前数据起点
|
|
|
-
|
|
|
- db.Table(TABLE+v).Where("t_tp = ?", 110).Count(&count)
|
|
|
- Count_List = append(Count_List, int(count))
|
|
|
- fmt.Println("TABLE:", TABLE+v, count)
|
|
|
- countoffset = offset - Count_Num // 当前数据起点
|
|
|
- if countoffset < 0 {
|
|
|
- countoffset = 0
|
|
|
- }
|
|
|
- Count_Num += int(count)
|
|
|
- Limit := PageSize - len(Warning_List) // 还缺多少数量
|
|
|
- if offset <= Count_Num && Limit > 0 {
|
|
|
- var warninglist []WarningDB
|
|
|
- dbW := db.Table(TABLE + v)
|
|
|
- if WHERE_t_pid > 0 {
|
|
|
- dbW.Where("t_tp = ? ", WHERE_t_pid)
|
|
|
- }
|
|
|
- if WHERE_t_tp > 0 {
|
|
|
- dbW.Where("t_pid = ? ", WHERE_t_pid)
|
|
|
- }
|
|
|
- if WHERE_t__state != -1 {
|
|
|
- dbW.Where("t__state = ? ", WHERE_t__state)
|
|
|
- }
|
|
|
- if len(WHERE_t_sn) > 0 {
|
|
|
- dbW.Where("t__state LIKE ? ", WHERE_t_sn)
|
|
|
- }
|
|
|
- dbW.Order("t__ut").Offset(countoffset).Limit(Limit).Find(&warninglist)
|
|
|
- Warning_List = append(Warning_List, warninglist...)
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- return Warning_List, Count_Num
|
|
|
-}
|
|
|
-
|
|
|
-func (t *WarningDB) Create(TableName string) {
|
|
|
-CreateSQL:
|
|
|
- result := db.Table(TableName).Create(t) // 插入数据
|
|
|
- if result.Error != nil {
|
|
|
- if strings.Compare(result.Error.Error(), "doesn't exist") != 0 {
|
|
|
- db.Table(TableName).AutoMigrate(&WarningDB{}) // 建表
|
|
|
- goto CreateSQL // 返回 重新插入
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-//func UPDATE(name string, ID int64, t__log string) {
|
|
|
-// logs.Println("修改数据:", name, " ID:", ID)
|
|
|
-// db.Exec("UPDATE "+name+" SET t__log = ? WHERE ID = ? ;", t__log, ID)
|
|
|
+//
|
|
|
+//import (
|
|
|
+// "database/sql"
|
|
|
+// "fmt"
|
|
|
+// "gorm.io/driver/mysql"
|
|
|
+// "gorm.io/gorm"
|
|
|
+// "strings"
|
|
|
+//
|
|
|
+// "time"
|
|
|
+//)
|
|
|
+//
|
|
|
+//var db *gorm.DB
|
|
|
+//var sqlDB *sql.DB
|
|
|
+//
|
|
|
+//type WarningDB struct {
|
|
|
+// Id int64 `gorm:"column:ID;type:bigint;primaryKey"`
|
|
|
+// T_pid int32 `gorm:"column:t_pid;index:merge"` // Account.Company 绑定公司
|
|
|
+// T_tp int `gorm:"column:t_tp;index:merge"` // 报警类型 ->WarningList
|
|
|
+// T_sn string `gorm:"column:t_sn;index:tsn;size:256;"` // 设备序列号
|
|
|
+// T_D_name string `gorm:"column:t__d_name;size:256;"` // 设备名称
|
|
|
+// T_id int32 `gorm:"column:t_id;"` // 传感器 ID
|
|
|
+// T_DS_name string `gorm:"column:t__d_s_name;size:256;"` // 传感器名称
|
|
|
+// T_Remark string `gorm:"column:t__remark;type:longtext;"` // 采集内容
|
|
|
+// T_Ut time.Time `gorm:"column:t__ut;type:datetime;"` // 采集时间
|
|
|
+// T_fUt time.Time `gorm:"column:t_f_ut;type:datetime;"` // 首次采集时间
|
|
|
+// T_Text string `gorm:"column:t__text;type:longtext;"` // 处理备注
|
|
|
+// T_Log string `gorm:"column:t__log;type:longtext;"` // 通知日志
|
|
|
+// T_Msid int64 `gorm:"column:t__msid;"` // 消息ID
|
|
|
+// T_State int `gorm:"column:t__state;size:2;default:2;index:merge"` // 0 删除 1 不处理 2 已处理 3 未处理
|
|
|
+// CreateTime time.Time `gorm:"column:create_time;type:datetime;autoUpdateTime"` //auto_now_add 第一次保存时才设置时间
|
|
|
+// UpdateTime time.Time `gorm:"column:update_time;type:datetime;autoCreateTime"` //auto_now 每次 model 保存时都会对时间自动更新
|
|
|
+//}
|
|
|
+//
|
|
|
+//var MysqlServer_UrlPort = "127.0.0.1:40306"
|
|
|
+//var MysqlServer_Database = "cold"
|
|
|
+//var MysqlServer_Username = "cold"
|
|
|
+//var MysqlServer_Password = "yjwyEckZS7rE5H!"
|
|
|
+//
|
|
|
+//func init() {
|
|
|
+// // 连接数据库
|
|
|
+// var err error
|
|
|
+//
|
|
|
+// dsn := MysqlServer_Username + ":" + MysqlServer_Password + "@tcp(" + MysqlServer_UrlPort + ")/" + MysqlServer_Database + "?charset=utf8mb4&loc=Local&parseTime=True"
|
|
|
+// db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
|
|
+// if err != nil {
|
|
|
+//
|
|
|
+// fmt.Println(err)
|
|
|
+//
|
|
|
+// }
|
|
|
+// sqlDB, err = db.DB()
|
|
|
+// // SetMaxIdleConns 设置空闲连接池中的最大连接数。
|
|
|
+// sqlDB.SetMaxIdleConns(1)
|
|
|
+//
|
|
|
+// // SetMaxOpenConns 设置数据库连接最大打开数。
|
|
|
+// sqlDB.SetMaxOpenConns(10)
|
|
|
+//
|
|
|
+// // SetConnMaxLifetime 设置可重用连接的最长时间
|
|
|
+// sqlDB.SetConnMaxLifetime(time.Hour)
|
|
|
+// //db.AutoMigrate(&DeviceWarning{})
|
|
|
+//
|
|
|
+// //db.SetLogger(log.New(logs.LogxOrm, "\r\nWarningBack:", 0))
|
|
|
+// fmt.Println("db init")
|
|
|
+//}
|
|
|
+//
|
|
|
+///*
|
|
|
+//time_a : 开始时间 "2024-01-24 08:21:40"
|
|
|
+//time_b : 结束时间 "2024-03-24 08:21:40"
|
|
|
+//WHERE_t_pid : 公司ID > 0
|
|
|
+//WHERE_t_tp : 报警类型 > 0
|
|
|
+//WHERE_t__state : 状态 -1 无限制 0 删除 1 不处理 2 已处理 3 未处理
|
|
|
+//WHERE_t_sn : 设备序列号
|
|
|
+//PageIndex : 页码值
|
|
|
+//PageSize : 每页多少
|
|
|
+//*/
|
|
|
+//
|
|
|
+//func Handy(time_a, time_b string, WHERE_t_pid int, WHERE_t_tp int, WHERE_t__state int, WHERE_t_sn string, PageIndex int, PageSize int) ([]WarningDB, int) {
|
|
|
+// var offset int // 数据起点
|
|
|
+// if PageIndex <= 1 {
|
|
|
+// offset = 0
|
|
|
+// } else {
|
|
|
+// offset = (PageIndex - 1) * PageSize
|
|
|
+// }
|
|
|
+// var TABLE = "warning_"
|
|
|
+// monthsArray := generateMonthArray(time_a, time_b)
|
|
|
+//
|
|
|
+// var Warning_List []WarningDB
|
|
|
+// var Count_List []int
|
|
|
+// var Count_Num int // 累积数量
|
|
|
+// for _, v := range monthsArray {
|
|
|
+// var count int64
|
|
|
+// var countoffset int // 当前数据起点
|
|
|
+//
|
|
|
+// db.Table(TABLE+v).Where("t_tp = ?", 110).Count(&count)
|
|
|
+// Count_List = append(Count_List, int(count))
|
|
|
+// fmt.Println("TABLE:", TABLE+v, count)
|
|
|
+// countoffset = offset - Count_Num // 当前数据起点
|
|
|
+// if countoffset < 0 {
|
|
|
+// countoffset = 0
|
|
|
+// }
|
|
|
+// Count_Num += int(count)
|
|
|
+// Limit := PageSize - len(Warning_List) // 还缺多少数量
|
|
|
+// if offset <= Count_Num && Limit > 0 {
|
|
|
+// var warninglist []WarningDB
|
|
|
+// dbW := db.Table(TABLE + v)
|
|
|
+// if WHERE_t_pid > 0 {
|
|
|
+// dbW.Where("t_tp = ? ", WHERE_t_pid)
|
|
|
+// }
|
|
|
+// if WHERE_t_tp > 0 {
|
|
|
+// dbW.Where("t_pid = ? ", WHERE_t_pid)
|
|
|
+// }
|
|
|
+// if WHERE_t__state != -1 {
|
|
|
+// dbW.Where("t__state = ? ", WHERE_t__state)
|
|
|
+// }
|
|
|
+// if len(WHERE_t_sn) > 0 {
|
|
|
+// dbW.Where("t__state LIKE ? ", WHERE_t_sn)
|
|
|
+// }
|
|
|
+// dbW.Order("t__ut").Offset(countoffset).Limit(Limit).Find(&warninglist)
|
|
|
+// Warning_List = append(Warning_List, warninglist...)
|
|
|
+// }
|
|
|
+//
|
|
|
+// }
|
|
|
+// return Warning_List, Count_Num
|
|
|
+//}
|
|
|
+//
|
|
|
+//func (t *WarningDB) Create(TableName string) {
|
|
|
+//CreateSQL:
|
|
|
+// result := db.Table(TableName).Create(t) // 插入数据
|
|
|
+// if result.Error != nil {
|
|
|
+// if strings.Compare(result.Error.Error(), "doesn't exist") != 0 {
|
|
|
+// db.Table(TableName).AutoMigrate(&WarningDB{}) // 建表
|
|
|
+// goto CreateSQL // 返回 重新插入
|
|
|
+// }
|
|
|
+// }
|
|
|
+//}
|
|
|
+//
|
|
|
+////func UPDATE(name string, ID int64, t__log string) {
|
|
|
+//// logs.Println("修改数据:", name, " ID:", ID)
|
|
|
+//// db.Exec("UPDATE "+name+" SET t__log = ? WHERE ID = ? ;", t__log, ID)
|
|
|
+////}
|
|
|
+//
|
|
|
+//func generateMonthArray(startDateStr string, endDateStr string) []string {
|
|
|
+// startDate, _ := time.Parse("2006-01-02 15:04:05", startDateStr)
|
|
|
+// endDate, _ := time.Parse("2006-01-02 15:04:05", endDateStr)
|
|
|
+//
|
|
|
+// monthsArray := []string{}
|
|
|
+// for startDate.Before(endDate) || startDate.Equal(endDate) {
|
|
|
+// monthsArray = append(monthsArray, fmt.Sprintf("%d_%02d", startDate.Year(), startDate.Month()))
|
|
|
+// startDate = startDate.AddDate(0, 1, 0) // Add one month
|
|
|
+// }
|
|
|
+//
|
|
|
+// return monthsArray
|
|
|
//}
|
|
|
-
|
|
|
-func generateMonthArray(startDateStr string, endDateStr string) []string {
|
|
|
- startDate, _ := time.Parse("2006-01-02 15:04:05", startDateStr)
|
|
|
- endDate, _ := time.Parse("2006-01-02 15:04:05", endDateStr)
|
|
|
-
|
|
|
- monthsArray := []string{}
|
|
|
- for startDate.Before(endDate) || startDate.Equal(endDate) {
|
|
|
- monthsArray = append(monthsArray, fmt.Sprintf("%d_%02d", startDate.Year(), startDate.Month()))
|
|
|
- startDate = startDate.AddDate(0, 1, 0) // Add one month
|
|
|
- }
|
|
|
-
|
|
|
- return monthsArray
|
|
|
-}
|