dictionary.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package source
  2. import (
  3. "time"
  4. "github.com/flipped-aurora/gin-vue-admin/server/global"
  5. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  6. "github.com/gookit/color"
  7. "gorm.io/gorm"
  8. )
  9. var Dictionary = new(dictionary)
  10. type dictionary struct{}
  11. var status = new(bool)
  12. //@author: [SliverHorn](https://github.com/SliverHorn)
  13. //@description: sys_dictionaries 表数据初始化
  14. func (d *dictionary) Init() error {
  15. *status = true
  16. var dictionaries = []system.SysDictionary{
  17. {GVA_MODEL: global.GVA_MODEL{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "性别", Type: "sex", Status: status, Desc: "性别字典"},
  18. {GVA_MODEL: global.GVA_MODEL{ID: 2, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库int类型", Type: "int", Status: status, Desc: "int类型对应的数据库类型"},
  19. {GVA_MODEL: global.GVA_MODEL{ID: 3, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库时间日期类型", Type: "time.Time", Status: status, Desc: "数据库时间日期类型"},
  20. {GVA_MODEL: global.GVA_MODEL{ID: 4, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库浮点型", Type: "float64", Status: status, Desc: "数据库浮点型"},
  21. {GVA_MODEL: global.GVA_MODEL{ID: 5, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库字符串", Type: "string", Status: status, Desc: "数据库字符串"},
  22. {GVA_MODEL: global.GVA_MODEL{ID: 6, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库bool类型", Type: "bool", Status: status, Desc: "数据库bool类型"},
  23. }
  24. return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  25. if tx.Where("id IN ?", []int{1, 6}).Find(&[]system.SysDictionary{}).RowsAffected == 2 {
  26. color.Danger.Println("\n[Mysql] --> sys_dictionaries 表初始数据已存在!")
  27. return nil
  28. }
  29. if err := tx.Create(&dictionaries).Error; err != nil { // 遇到错误时回滚事务
  30. return err
  31. }
  32. color.Info.Println("\n[Mysql] --> sys_dictionaries 表初始数据成功!")
  33. return nil
  34. })
  35. }