Files
xuanzhi-service/server/source/system/dictionary.go
wdh-home 1e33640629
Some checks failed
CI / init (pull_request) Has been cancelled
CI / Frontend node 18.16.0 (pull_request) Has been cancelled
CI / Backend go (1.22) (pull_request) Has been cancelled
CI / release-pr (pull_request) Has been cancelled
CI / devops-test (1.22, 18.16.0) (pull_request) Has been cancelled
CI / release-please (pull_request) Has been cancelled
CI / devops-prod (1.22, 18.x) (pull_request) Has been cancelled
CI / docker (pull_request) Has been cancelled
基础项目
2026-04-26 15:32:21 +08:00

78 lines
2.8 KiB
Go

package system
import (
"context"
sysModel "github.com/flipped-aurora/gin-vue-admin/server/model/system"
"github.com/flipped-aurora/gin-vue-admin/server/service/system"
"github.com/pkg/errors"
"gorm.io/gorm"
)
const initOrderDict = initOrderCasbin + 1
type initDict struct{}
// auto run
func init() {
system.RegisterInit(initOrderDict, &initDict{})
}
func (i *initDict) MigrateTable(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
return ctx, db.AutoMigrate(&sysModel.SysDictionary{})
}
func (i *initDict) TableCreated(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
return db.Migrator().HasTable(&sysModel.SysDictionary{})
}
func (i *initDict) InitializerName() string {
return sysModel.SysDictionary{}.TableName()
}
func (i *initDict) InitializeData(ctx context.Context) (next context.Context, err error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
True := true
entities := []sysModel.SysDictionary{
{Name: "性别", Type: "gender", Status: &True, Desc: "性别字典"},
{Name: "数据库int类型", Type: "int", Status: &True, Desc: "int类型对应的数据库类型"},
{Name: "数据库时间日期类型", Type: "time.Time", Status: &True, Desc: "数据库时间日期类型"},
{Name: "数据库浮点型", Type: "float64", Status: &True, Desc: "数据库浮点型"},
{Name: "数据库字符串", Type: "string", Status: &True, Desc: "数据库字符串"},
{Name: "数据库bool类型", Type: "bool", Status: &True, Desc: "数据库bool类型"},
{Name: "作者状态", Type: "book_author_status", Status: &True, Desc: "作者状态字典"},
{Name: "书籍评论状态", Type: "book_comment_status", Status: &True, Desc: "书籍评论状态字典"},
{Name: "书籍完结状态", Type: "book_completion_status", Status: &True, Desc: "书籍完结状态字典"},
{Name: "书籍时代标签", Type: "book_era_tag", Status: &True, Desc: "书籍时代标签字典"},
{Name: "书籍上下架状态", Type: "book_publish_status", Status: &True, Desc: "书籍上下架状态字典"},
{Name: "书籍类型", Type: "book_type", Status: &True, Desc: "书籍类型动态字典"},
}
if err = db.Create(&entities).Error; err != nil {
return ctx, errors.Wrap(err, sysModel.SysDictionary{}.TableName()+"表数据初始化失败!")
}
next = context.WithValue(ctx, i.InitializerName(), entities)
return next, nil
}
func (i *initDict) DataInserted(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
if errors.Is(db.Where("type = ?", "bool").First(&sysModel.SysDictionary{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据
return false
}
return true
}