package system import ( "context" "fmt" 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 initOrderDictDetail = initOrderDict + 1 type initDictDetail struct{} // auto run func init() { system.RegisterInit(initOrderDictDetail, &initDictDetail{}) } func (i *initDictDetail) 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.SysDictionaryDetail{}) } func (i *initDictDetail) TableCreated(ctx context.Context) bool { db, ok := ctx.Value("db").(*gorm.DB) if !ok { return false } return db.Migrator().HasTable(&sysModel.SysDictionaryDetail{}) } func (i *initDictDetail) InitializerName() string { return sysModel.SysDictionaryDetail{}.TableName() } func (i *initDictDetail) InitializeData(ctx context.Context) (context.Context, error) { db, ok := ctx.Value("db").(*gorm.DB) if !ok { return ctx, system.ErrMissingDBContext } dicts, ok := ctx.Value(new(initDict).InitializerName()).([]sysModel.SysDictionary) if !ok { return ctx, errors.Wrap(system.ErrMissingDependentContext, fmt.Sprintf("未找到 %s 表初始化数据", sysModel.SysDictionary{}.TableName())) } True := true dicts[0].SysDictionaryDetails = []sysModel.SysDictionaryDetail{ {Label: "男", Value: "1", Status: &True, Sort: 1}, {Label: "女", Value: "2", Status: &True, Sort: 2}, } dicts[1].SysDictionaryDetails = []sysModel.SysDictionaryDetail{ {Label: "smallint", Value: "1", Status: &True, Extend: "mysql", Sort: 1}, {Label: "mediumint", Value: "2", Status: &True, Extend: "mysql", Sort: 2}, {Label: "int", Value: "3", Status: &True, Extend: "mysql", Sort: 3}, {Label: "bigint", Value: "4", Status: &True, Extend: "mysql", Sort: 4}, {Label: "int2", Value: "5", Status: &True, Extend: "pgsql", Sort: 5}, {Label: "int4", Value: "6", Status: &True, Extend: "pgsql", Sort: 6}, {Label: "int6", Value: "7", Status: &True, Extend: "pgsql", Sort: 7}, {Label: "int8", Value: "8", Status: &True, Extend: "pgsql", Sort: 8}, } dicts[2].SysDictionaryDetails = []sysModel.SysDictionaryDetail{ {Label: "date", Value: "0", Status: &True, Extend: "mysql", Sort: 0}, {Label: "time", Value: "1", Status: &True, Extend: "mysql", Sort: 1}, {Label: "year", Value: "2", Status: &True, Extend: "mysql", Sort: 2}, {Label: "datetime", Value: "3", Status: &True, Extend: "mysql", Sort: 3}, {Label: "timestamp", Value: "5", Status: &True, Extend: "mysql", Sort: 5}, {Label: "timestamptz", Value: "6", Status: &True, Extend: "pgsql", Sort: 5}, } dicts[3].SysDictionaryDetails = []sysModel.SysDictionaryDetail{ {Label: "float", Value: "0", Status: &True, Extend: "mysql", Sort: 0}, {Label: "double", Value: "1", Status: &True, Extend: "mysql", Sort: 1}, {Label: "decimal", Value: "2", Status: &True, Extend: "mysql", Sort: 2}, {Label: "numeric", Value: "3", Status: &True, Extend: "pgsql", Sort: 3}, {Label: "smallserial", Value: "4", Status: &True, Extend: "pgsql", Sort: 4}, } dicts[4].SysDictionaryDetails = []sysModel.SysDictionaryDetail{ {Label: "char", Value: "0", Status: &True, Extend: "mysql", Sort: 0}, {Label: "varchar", Value: "1", Status: &True, Extend: "mysql", Sort: 1}, {Label: "tinyblob", Value: "2", Status: &True, Extend: "mysql", Sort: 2}, {Label: "tinytext", Value: "3", Status: &True, Extend: "mysql", Sort: 3}, {Label: "text", Value: "4", Status: &True, Extend: "mysql", Sort: 4}, {Label: "blob", Value: "5", Status: &True, Extend: "mysql", Sort: 5}, {Label: "mediumblob", Value: "6", Status: &True, Extend: "mysql", Sort: 6}, {Label: "mediumtext", Value: "7", Status: &True, Extend: "mysql", Sort: 7}, {Label: "longblob", Value: "8", Status: &True, Extend: "mysql", Sort: 8}, {Label: "longtext", Value: "9", Status: &True, Extend: "mysql", Sort: 9}, } dicts[5].SysDictionaryDetails = []sysModel.SysDictionaryDetail{ {Label: "tinyint", Value: "1", Extend: "mysql", Status: &True}, {Label: "bool", Value: "2", Extend: "pgsql", Status: &True}, } // 课程难度字典详情 dicts[6].SysDictionaryDetails = []sysModel.SysDictionaryDetail{ {Label: "初级", Value: "beginner", Status: &True, Sort: 1}, {Label: "中级", Value: "intermediate", Status: &True, Sort: 2}, {Label: "高级", Value: "advanced", Status: &True, Sort: 3}, } // 课程状态字典详情 dicts[7].SysDictionaryDetails = []sysModel.SysDictionaryDetail{ {Label: "草稿", Value: "draft", Status: &True, Sort: 1}, {Label: "已发布", Value: "published", Status: &True, Sort: 2}, {Label: "已下架", Value: "archived", Status: &True, Sort: 3}, } // 章节状态字典详情 dicts[8].SysDictionaryDetails = []sysModel.SysDictionaryDetail{ {Label: "草稿", Value: "draft", Status: &True, Sort: 1}, {Label: "已发布", Value: "published", Status: &True, Sort: 2}, {Label: "隐藏", Value: "hidden", Status: &True, Sort: 3}, } // 知识点状态字典详情 dicts[9].SysDictionaryDetails = []sysModel.SysDictionaryDetail{ {Label: "草稿", Value: "draft", Status: &True, Sort: 1}, {Label: "已发布", Value: "published", Status: &True, Sort: 2}, {Label: "隐藏", Value: "hidden", Status: &True, Sort: 3}, } // 题目类型字典详情 dicts[10].SysDictionaryDetails = []sysModel.SysDictionaryDetail{ {Label: "单选题", Value: "single_choice", Status: &True, Sort: 1}, {Label: "多选题", Value: "multiple_choice", Status: &True, Sort: 2}, {Label: "判断题", Value: "true_false", Status: &True, Sort: 3}, {Label: "填空题", Value: "fill_blank", Status: &True, Sort: 4}, } // 题目难度字典详情 dicts[11].SysDictionaryDetails = []sysModel.SysDictionaryDetail{ {Label: "简单", Value: "easy", Status: &True, Sort: 1}, {Label: "中等", Value: "medium", Status: &True, Sort: 2}, {Label: "困难", Value: "hard", Status: &True, Sort: 3}, } // 学习状态字典详情 dicts[12].SysDictionaryDetails = []sysModel.SysDictionaryDetail{ {Label: "未开始", Value: "not_started", Status: &True, Sort: 1}, {Label: "学习中", Value: "learning", Status: &True, Sort: 2}, {Label: "已完成", Value: "completed", Status: &True, Sort: 3}, {Label: "已暂停", Value: "paused", Status: &True, Sort: 4}, } // 考试状态字典详情 dicts[13].SysDictionaryDetails = []sysModel.SysDictionaryDetail{ {Label: "草稿", Value: "draft", Status: &True, Sort: 1}, {Label: "已发布", Value: "published", Status: &True, Sort: 2}, {Label: "已结束", Value: "ended", Status: &True, Sort: 3}, {Label: "已取消", Value: "cancelled", Status: &True, Sort: 4}, } for _, dict := range dicts { if err := db.Model(&dict).Association("SysDictionaryDetails"). Replace(dict.SysDictionaryDetails); err != nil { return ctx, errors.Wrap(err, sysModel.SysDictionaryDetail{}.TableName()+"表数据初始化失败!") } } return ctx, nil } func (i *initDictDetail) DataInserted(ctx context.Context) bool { db, ok := ctx.Value("db").(*gorm.DB) if !ok { return false } var dict sysModel.SysDictionary if err := db.Preload("SysDictionaryDetails"). First(&dict, &sysModel.SysDictionary{Name: "数据库bool类型"}).Error; err != nil { return false } return len(dict.SysDictionaryDetails) > 0 && dict.SysDictionaryDetails[0].Label == "tinyint" }