123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- package Warning
- import (
- "Cold_mqtt/conf"
- "Cold_mqtt/logs"
- "github.com/jinzhu/gorm"
- _ "github.com/jinzhu/gorm/dialects/mysql"
- "log"
- "strings"
- "sync"
- "time"
- )
- var Db *gorm.DB
- var SysSeekCREATE sync.Map
- func init() {
- // 连接数据库
- var err error
- Db, err = gorm.Open("mysql", conf.MysqlServer_Username+":"+conf.MysqlServer_Password+"@tcp("+conf.MysqlServer_UrlPort+")/"+conf.MysqlServer_Database+"?charset=utf8mb4&loc=Local&parseTime=True")
- if err != nil {
- log.Fatal(err)
- }
- // SetMaxIdleConns 设置空闲连接池中的最大连接数。
- Db.DB().SetMaxIdleConns(10)
- // SetMaxOpenConns 设置数据库连接最大打开数。
- Db.DB().SetMaxOpenConns(600)
- // SetConnMaxLifetime 设置可重用连接的最长时间
- Db.DB().SetConnMaxLifetime(time.Hour)
- //Db.AutoMigrate(&DeviceWarning{})
- // 开启 Logger, 以展示详细的日志
- Db.LogMode(true)
- Db.SetLogger(log.New(logs.LogxOrm, "\r\nWarningBack:", 0))
- }
- //func Close() {
- // Db.Close()
- //}
- //type JsonTime struct {
- // time.Time
- //}
- //
- //func (t JsonTime) MarshalJSON() ([]byte, error) {
- // str := fmt.Sprintf("\"%s\"", t.Format("2006-01-02 15:04:05"))
- // return []byte(str), nil
- //}
- //
- //func (t JsonTime) Value() (driver.Value, error) {
- // var zeroTime time.Time
- // if t.Time.UnixNano() == zeroTime.UnixNano() {
- // return nil, nil
- // }
- // return t.Time, nil
- //}
- //
- //func (t *JsonTime) Scan(v interface{}) error {
- // value, ok := v.(time.Time)
- // if ok {
- // *t = JsonTime{Time: value}
- // return nil
- // }
- // return fmt.Errorf("error %v", v)
- //}
- // 创建备份报警表
- func SeekCREATE(name string) {
- _, ok := SysSeekCREATE.Load(name)
- if ok {
- return
- }
- rows, _ := Db.Raw("select * from information_schema.TABLES where TABLE_NAME = ? ;", name).Rows() // (*sql.Rows, error)
- defer rows.Close()
- if !rows.Next() {
- logs.Println("没有表:", name, " 现在新建!")
- sql := "CREATE TABLE `warning` (\n `ID` bigint NOT NULL AUTO_INCREMENT,\n `t_pid` int NULL DEFAULT NULL,\n `t_tp` int NULL DEFAULT NULL,\n `t_sn` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,\n `t__d_name` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,\n `t_id` int NULL DEFAULT NULL,\n `t__d_s_name` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,\n `t__remark` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL,\n `t__ut` datetime NULL DEFAULT NULL,\n `t_f_ut` datetime NULL DEFAULT NULL,\n `t__text` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL,\n `t__log` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL,\n `t__msid` bigint NULL DEFAULT NULL,\n `t__state` int NOT NULL DEFAULT 2,\n `create_time` datetime NULL DEFAULT NULL,\n `update_time` datetime NULL DEFAULT NULL,\n PRIMARY KEY (`ID`) USING BTREE,\n INDEX `warning_t__ut`(`t__ut`) USING BTREE,\n INDEX `warning_t_pid_t_tp_t__state`(`t_pid`, `t_tp`, `t__state`) USING BTREE,\n INDEX `warning_t_sn`(`t_sn`) USING BTREE\n) ENGINE = InnoDB AUTO_INCREMENT = 7522219 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;"
- sql = strings.Replace(sql, "warning", name, -1)
- Db.Exec(sql)
- time.Sleep(time.Second)
- }
- SysSeekCREATE.Store(name, "")
- return
- }
- // 插入 备份报警表
- func INSERT(name string, ID int64) {
- SeekCREATE(name) // 创建备份报警表
- logs.Println("导入数据:", name, " ID:", ID)
- Db.Exec("INSERT INTO "+name+" SELECT * FROM warning WHERE ID = ? ;", ID)
- }
- 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 DELETE(name string, currentTime string) {
- // fmt.Println("删除数据:", name, currentTime)
- // Db.Exec("DELETE FROM "+name+" WHERE t__ut <= ?;", currentTime+" 23:59:59")
- //
- //}
|