hpds_jkw_web/model/index.go

158 lines
3.0 KiB
Go
Raw Permalink Normal View History

2023-01-06 10:09:23 +08:00
package model
import (
"fmt"
"git.hpds.cc/Component/logging"
2023-01-06 10:09:23 +08:00
"github.com/go-redis/redis"
_ "github.com/go-sql-driver/mysql"
"go.uber.org/zap"
"hpds-iot-web/config"
"os"
"time"
"xorm.io/xorm"
"xorm.io/xorm/dialects"
)
2023-03-24 08:42:28 +08:00
type Statistics struct {
Total int64
}
2023-01-06 10:09:23 +08:00
var (
DB *xorm.Engine
Redis *redis.Client
)
func New(driveName, dsn string, showSql bool, logger *logging.Logger) {
2023-03-23 18:03:09 +08:00
DB, _ = NewDbConnection(driveName, dsn)
DB.ShowSQL(showSql)
2023-01-06 10:09:23 +08:00
DB.Dialect().SetQuotePolicy(dialects.QuotePolicyReserved)
err := DB.Sync2(
&Brand{},
2023-01-13 11:26:39 +08:00
&Dataset{},
&DatasetDetail{},
2023-05-14 18:23:12 +08:00
&DatasetOperationLog{},
2023-01-06 10:09:23 +08:00
&DetectionTask{},
&Device{},
&DeviceType{},
&Disease{},
2023-01-12 10:21:40 +08:00
&DiseaseType{},
2023-01-11 18:05:29 +08:00
&FileManager{},
2023-06-17 09:38:26 +08:00
&Gnss{},
&LabelFile{},
2023-01-06 10:09:23 +08:00
&MatterAttribute{},
&MatterCategory{},
&MatterEvent{},
&MatterEventParams{},
&MatterModel{},
&MatterService{},
&MatterServiceParams{},
&MatterVersion{},
2023-01-06 16:10:18 +08:00
&Model{},
&ModelVersion{},
2023-06-17 09:38:26 +08:00
&MultiLevelPlatform{},
2023-01-06 10:09:23 +08:00
&Node{},
2023-03-23 18:03:09 +08:00
&NodeState{},
&IssueModel{},
2023-01-06 10:09:23 +08:00
&OriginalData{},
&Owner{},
&Project{},
2023-06-17 09:38:26 +08:00
&ProjectResult{},
2023-01-06 10:09:23 +08:00
&SystemMenu{},
&OperationLog{},
&SystemRoleMenu{},
&SystemRoleRoute{},
&SystemRole{},
&SystemUser{},
2023-01-10 10:01:42 +08:00
&SystemUserRole{},
2023-03-23 18:03:09 +08:00
&Task{},
&TaskLog{},
2023-03-23 18:03:09 +08:00
&TaskResult{},
2023-05-14 18:23:12 +08:00
&TrainingDataset{},
&TrainingDatasetDetail{},
&TrainTask{},
&TrainTaskLog{},
2023-06-17 09:38:26 +08:00
&TrainTaskResult{},
2023-01-06 10:09:23 +08:00
)
if err != nil {
logger.Error("同步数据库表结构", zap.Error(err))
2023-01-06 10:09:23 +08:00
os.Exit(1)
}
}
2023-03-23 18:03:09 +08:00
func NewDbConnection(driveName, dsn string) (db *xorm.Engine, err error) {
db, err = xorm.NewEngine(driveName, dsn)
2023-01-06 10:09:23 +08:00
if err != nil {
zap.L().Error("创建数据库连接", zap.Error(err))
os.Exit(-1)
}
db.SetMaxOpenConns(300)
return
}
func NewCache(c config.CacheConfig) {
Redis = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", c.Host, c.Port),
Password: c.Pass, // no password set
DB: c.DB, // use default DB
PoolSize: c.PoolSize, // Redis连接池大小
MaxRetries: 3, // 最大重试次数
IdleTimeout: 10 * time.Second, // 空闲链接超时时间
})
pong, err := Redis.Ping().Result()
if err == redis.Nil {
zap.L().Error("访问Redis异常", zap.Error(fmt.Errorf("redis.Nil")))
} else if err != nil {
zap.L().Error("访问Redis异常", zap.Error(err))
} else {
zap.L().Info("Redis连接成功", zap.String("pong", pong))
}
}
func GetTrainCategory(categoryId int) string {
switch categoryId {
case 1:
return "train"
case 2:
return "val"
case 3:
return "test"
}
return "other"
}
2023-06-17 09:38:26 +08:00
func GetBizType(categoryId int) string {
switch categoryId {
case 1:
return "道路"
case 2:
return "桥梁"
case 3:
return "隧道"
case 4:
return "边坡"
}
return "其他"
}
func GetFileType(categoryId int) string {
switch categoryId {
case 1:
return "图片"
case 2:
return "视频"
case 3:
return "雷达图谱"
}
return "其他"
}
func GetLabelType(typeId int) string {
switch typeId {
case 1:
return "有病害"
case 2:
return "无病害"
}
return "其他分类"
}