82 lines
2.9 KiB
Go
82 lines
2.9 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: "course_difficulty", Status: &True, Desc: "课程难度等级字典"},
|
|
{Name: "课程状态", Type: "course_status", Status: &True, Desc: "课程状态字典"},
|
|
{Name: "章节状态", Type: "chapter_status", Status: &True, Desc: "章节状态字典"},
|
|
{Name: "知识点状态", Type: "knowledge_status", Status: &True, Desc: "知识点状态字典"},
|
|
{Name: "题目类型", Type: "question_type", Status: &True, Desc: "题目类型字典"},
|
|
{Name: "题目难度", Type: "question_difficulty", Status: &True, Desc: "题目难度等级字典"},
|
|
{Name: "学习状态", Type: "learning_status", Status: &True, Desc: "用户学习状态字典"},
|
|
{Name: "考试状态", Type: "exam_status", 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
|
|
}
|