You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.7 KiB

  1. package datas
  2. import (
  3. "gin-vue-admin/global"
  4. "github.com/gookit/color"
  5. "os"
  6. "time"
  7. "gin-vue-admin/model"
  8. "gorm.io/gorm"
  9. )
  10. func InitSysDictionary(db *gorm.DB) {
  11. var status = new(bool)
  12. *status = true
  13. Dictionaries := []model.SysDictionary{
  14. {GVA_MODEL: global.GVA_MODEL{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "性别", Type: "sex", Status: status, Desc: "性别字典"},
  15. {GVA_MODEL: global.GVA_MODEL{ID: 2, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库int类型", Type: "int", Status: status, Desc: "int类型对应的数据库类型"},
  16. {GVA_MODEL: global.GVA_MODEL{ID: 3, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库时间日期类型", Type: "time.Time", Status: status, Desc: "数据库时间日期类型"},
  17. {GVA_MODEL: global.GVA_MODEL{ID: 4, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库浮点型", Type: "float64", Status: status, Desc: "数据库浮点型"},
  18. {GVA_MODEL: global.GVA_MODEL{ID: 5, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库字符串", Type: "string", Status: status, Desc: "数据库字符串"},
  19. {GVA_MODEL: global.GVA_MODEL{ID: 6, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库bool类型", Type: "bool", Status: status, Desc: "数据库bool类型"},
  20. }
  21. if err := db.Transaction(func(tx *gorm.DB) error {
  22. if tx.Where("id IN ?", []int{1, 6}).Find(&[]model.SysDictionary{}).RowsAffected == 2 {
  23. color.Danger.Println("sys_dictionaries表的初始数据已存在!")
  24. return nil
  25. }
  26. if err := tx.Create(&Dictionaries).Error; err != nil { // 遇到错误时回滚事务
  27. return err
  28. }
  29. return nil
  30. }); err != nil {
  31. color.Warn.Printf("[Mysql]--> sys_dictionaries 表的初始数据失败,err: %v\n", err)
  32. os.Exit(0)
  33. }
  34. }