WarningBack.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package Warning
  2. import (
  3. "Cold_mqtt/conf"
  4. "Cold_mqtt/logs"
  5. "github.com/jinzhu/gorm"
  6. _ "github.com/jinzhu/gorm/dialects/mysql"
  7. "log"
  8. "strings"
  9. "sync"
  10. "time"
  11. )
  12. var Db *gorm.DB
  13. var SysSeekCREATE sync.Map
  14. func init() {
  15. // 连接数据库
  16. var err error
  17. Db, err = gorm.Open("mysql", conf.MysqlServer_Username+":"+conf.MysqlServer_Password+"@tcp("+conf.MysqlServer_UrlPort+")/"+conf.MysqlServer_Database+"?charset=utf8mb4&loc=Local&parseTime=True")
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. // SetMaxIdleConns 设置空闲连接池中的最大连接数。
  22. Db.DB().SetMaxIdleConns(10)
  23. // SetMaxOpenConns 设置数据库连接最大打开数。
  24. Db.DB().SetMaxOpenConns(600)
  25. // SetConnMaxLifetime 设置可重用连接的最长时间
  26. Db.DB().SetConnMaxLifetime(time.Hour)
  27. //Db.AutoMigrate(&DeviceWarning{})
  28. // 开启 Logger, 以展示详细的日志
  29. Db.LogMode(true)
  30. Db.SetLogger(log.New(logs.LogxOrm, "\r\nWarningBack:", 0))
  31. }
  32. //func Close() {
  33. // Db.Close()
  34. //}
  35. //type JsonTime struct {
  36. // time.Time
  37. //}
  38. //
  39. //func (t JsonTime) MarshalJSON() ([]byte, error) {
  40. // str := fmt.Sprintf("\"%s\"", t.Format("2006-01-02 15:04:05"))
  41. // return []byte(str), nil
  42. //}
  43. //
  44. //func (t JsonTime) Value() (driver.Value, error) {
  45. // var zeroTime time.Time
  46. // if t.Time.UnixNano() == zeroTime.UnixNano() {
  47. // return nil, nil
  48. // }
  49. // return t.Time, nil
  50. //}
  51. //
  52. //func (t *JsonTime) Scan(v interface{}) error {
  53. // value, ok := v.(time.Time)
  54. // if ok {
  55. // *t = JsonTime{Time: value}
  56. // return nil
  57. // }
  58. // return fmt.Errorf("error %v", v)
  59. //}
  60. // 创建备份报警表
  61. func SeekCREATE(name string) {
  62. _, ok := SysSeekCREATE.Load(name)
  63. if ok {
  64. return
  65. }
  66. rows, _ := Db.Raw("select * from information_schema.TABLES where TABLE_NAME = ? ;", name).Rows() // (*sql.Rows, error)
  67. defer rows.Close()
  68. if !rows.Next() {
  69. logs.Println("没有表:", name, " 现在新建!")
  70. 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;"
  71. sql = strings.Replace(sql, "warning", name, -1)
  72. Db.Exec(sql)
  73. time.Sleep(time.Second)
  74. }
  75. SysSeekCREATE.Store(name, "")
  76. return
  77. }
  78. // 插入 备份报警表
  79. func INSERT(name string, ID int64) {
  80. SeekCREATE(name) // 创建备份报警表
  81. logs.Println("导入数据:", name, " ID:", ID)
  82. Db.Exec("INSERT INTO "+name+" SELECT * FROM warning WHERE ID = ? ;", ID)
  83. }
  84. func UPDATE(name string, ID int64, t__log string) {
  85. logs.Println("导入数据:", name, " ID:", ID)
  86. Db.Exec("UPDATE "+name+" SET t__log = ? WHERE ID = ? ;", t__log, ID)
  87. }
  88. //func DELETE(name string, currentTime string) {
  89. // fmt.Println("删除数据:", name, currentTime)
  90. // Db.Exec("DELETE FROM "+name+" WHERE t__ut <= ?;", currentTime+" 23:59:59")
  91. //
  92. //}