qixiaoning
2025-08-22 0f97177f258c67397b206b70e5aea2b24a4868c1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package models
 
import (
    "basic.com/valib/logger.git"
    "gorm.io/driver/sqlite"
    "gorm.io/gorm"
    "strings"
    "vamicro/config"
)
 
var (
    db           *gorm.DB
    DeviceInfo   Device
    ReportConfig Report
)
 
func Init(dbPath string) {
    var err error
 
    db, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
 
    if err != nil {
        logger.Debug("db open error ", err)
        return
    }
    _ = db.AutoMigrate(Device{}, Report{})
 
    // 添加时间间隔默认数据
    var sqls []string
    sqls = append(sqls, "INSERT INTO t_report(`id`,`serverAddr`,`interval`) SELECT 0,'',10 where not exists (select 1 from t_report);")
    sqls = append(sqls, "INSERT INTO t_device(`id`,`devId`) SELECT 0,'"+config.Server.AnalyServerId+"' where not exists (select 1 from t_device);")
    db.Exec(strings.Join(sqls, ""))
 
    DeviceInfo.Read()
    ReportConfig.Read()
 
    logger.Debug("DeviceInfo:", DeviceInfo, " ReportConfig", ReportConfig)
}
 
func GetDB() *gorm.DB {
    return db
}
 
func CloseDB() {
 
}