Merge.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package test
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "gorm.io/driver/mysql"
  6. "gorm.io/gorm"
  7. "strings"
  8. "time"
  9. )
  10. var db *gorm.DB
  11. var sqlDB *sql.DB
  12. type WarningDB struct {
  13. Id int64 `gorm:"column:ID;type:bigint;primaryKey"`
  14. T_pid int32 `gorm:"column:t_pid;index:merge"` // Account.Company 绑定公司
  15. T_tp int `gorm:"column:t_tp;index:merge"` // 报警类型 ->WarningList
  16. T_sn string `gorm:"column:t_sn;index:tsn;size:256;"` // 设备序列号
  17. T_D_name string `gorm:"column:t__d_name;size:256;"` // 设备名称
  18. T_id int32 `gorm:"column:t_id;"` // 传感器 ID
  19. T_DS_name string `gorm:"column:t__d_s_name;size:256;"` // 传感器名称
  20. T_Remark string `gorm:"column:t__remark;type:longtext;"` // 采集内容
  21. T_Ut time.Time `gorm:"column:t__ut;type:datetime;"` // 采集时间
  22. T_fUt time.Time `gorm:"column:t_f_ut;type:datetime;"` // 首次采集时间
  23. T_Text string `gorm:"column:t__text;type:longtext;"` // 处理备注
  24. T_Log string `gorm:"column:t__log;type:longtext;"` // 通知日志
  25. T_Msid int64 `gorm:"column:t__msid;"` // 消息ID
  26. T_State int `gorm:"column:t__state;size:2;default:2;index:merge"` // 0 删除 1 不处理 2 已处理 3 未处理
  27. CreateTime time.Time `gorm:"column:create_time;type:datetime;autoUpdateTime"` //auto_now_add 第一次保存时才设置时间
  28. UpdateTime time.Time `gorm:"column:update_time;type:datetime;autoCreateTime"` //auto_now 每次 model 保存时都会对时间自动更新
  29. }
  30. var MysqlServer_UrlPort = "127.0.0.1:40306"
  31. var MysqlServer_Database = "cold"
  32. var MysqlServer_Username = "cold"
  33. var MysqlServer_Password = "yjwyEckZS7rE5H!"
  34. func init() {
  35. // 连接数据库
  36. var err error
  37. dsn := MysqlServer_Username + ":" + MysqlServer_Password + "@tcp(" + MysqlServer_UrlPort + ")/" + MysqlServer_Database + "?charset=utf8mb4&loc=Local&parseTime=True"
  38. db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
  39. if err != nil {
  40. fmt.Println(err)
  41. }
  42. sqlDB, err = db.DB()
  43. // SetMaxIdleConns 设置空闲连接池中的最大连接数。
  44. sqlDB.SetMaxIdleConns(1)
  45. // SetMaxOpenConns 设置数据库连接最大打开数。
  46. sqlDB.SetMaxOpenConns(10)
  47. // SetConnMaxLifetime 设置可重用连接的最长时间
  48. sqlDB.SetConnMaxLifetime(time.Hour)
  49. //db.AutoMigrate(&DeviceWarning{})
  50. //db.SetLogger(log.New(logs.LogxOrm, "\r\nWarningBack:", 0))
  51. fmt.Println("db init")
  52. }
  53. /*
  54. time_a : 开始时间 "2024-01-24 08:21:40"
  55. time_b : 结束时间 "2024-03-24 08:21:40"
  56. WHERE_t_pid : 公司ID > 0
  57. WHERE_t_tp : 报警类型 > 0
  58. WHERE_t__state : 状态 -1 无限制 0 删除 1 不处理 2 已处理 3 未处理
  59. WHERE_t_sn : 设备序列号
  60. PageIndex : 页码值
  61. PageSize : 每页多少
  62. */
  63. 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) {
  64. var offset int // 数据起点
  65. if PageIndex <= 1 {
  66. offset = 0
  67. } else {
  68. offset = (PageIndex - 1) * PageSize
  69. }
  70. var TABLE = "warning_"
  71. monthsArray := generateMonthArray(time_a, time_b)
  72. var Warning_List []WarningDB
  73. var Count_List []int
  74. var Count_Num int // 累积数量
  75. for _, v := range monthsArray {
  76. var count int64
  77. var countoffset int // 当前数据起点
  78. db.Table(TABLE+v).Where("t_tp = ?", 110).Count(&count)
  79. Count_List = append(Count_List, int(count))
  80. fmt.Println("TABLE:", TABLE+v, count)
  81. countoffset = offset - Count_Num // 当前数据起点
  82. if countoffset < 0 {
  83. countoffset = 0
  84. }
  85. Count_Num += int(count)
  86. Limit := PageSize - len(Warning_List) // 还缺多少数量
  87. if offset <= Count_Num && Limit > 0 {
  88. var warninglist []WarningDB
  89. dbW := db.Table(TABLE + v)
  90. if WHERE_t_pid > 0 {
  91. dbW.Where("t_tp = ? ", WHERE_t_pid)
  92. }
  93. if WHERE_t_tp > 0 {
  94. dbW.Where("t_pid = ? ", WHERE_t_pid)
  95. }
  96. if WHERE_t__state != -1 {
  97. dbW.Where("t__state = ? ", WHERE_t__state)
  98. }
  99. if len(WHERE_t_sn) > 0 {
  100. dbW.Where("t__state LIKE ? ", WHERE_t_sn)
  101. }
  102. dbW.Order("t__ut").Offset(countoffset).Limit(Limit).Find(&warninglist)
  103. Warning_List = append(Warning_List, warninglist...)
  104. }
  105. }
  106. return Warning_List, Count_Num
  107. }
  108. func (t *WarningDB) Create(TableName string) {
  109. CreateSQL:
  110. result := db.Table(TableName).Create(t) // 插入数据
  111. if result.Error != nil {
  112. if strings.Compare(result.Error.Error(), "doesn't exist") != 0 {
  113. db.Table(TableName).AutoMigrate(&WarningDB{}) // 建表
  114. goto CreateSQL // 返回 重新插入
  115. }
  116. }
  117. }
  118. func generateMonthArray(startDateStr string, endDateStr string) []string {
  119. startDate, _ := time.Parse("2006-01-02 15:04:05", startDateStr)
  120. endDate, _ := time.Parse("2006-01-02 15:04:05", endDateStr)
  121. monthsArray := []string{}
  122. for startDate.Before(endDate) || startDate.Equal(endDate) {
  123. monthsArray = append(monthsArray, fmt.Sprintf("%d_%02d", startDate.Year(), startDate.Month()))
  124. startDate = startDate.AddDate(0, 1, 0) // Add one month
  125. }
  126. return monthsArray
  127. }