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.

32 lines
1.0 KiB

  1. package datas
  2. import (
  3. "github.com/gookit/color"
  4. "os"
  5. "time"
  6. "gin-vue-admin/model"
  7. "gorm.io/gorm"
  8. )
  9. var Authorities = []model.SysAuthority{
  10. {CreatedAt: time.Now(), UpdatedAt: time.Now(), AuthorityId: "888", AuthorityName: "普通用户", ParentId: "0"},
  11. {CreatedAt: time.Now(), UpdatedAt: time.Now(), AuthorityId: "8881", AuthorityName: "普通用户子角色", ParentId: "888"},
  12. {CreatedAt: time.Now(), UpdatedAt: time.Now(), AuthorityId: "9528", AuthorityName: "测试角色", ParentId: "0"},
  13. }
  14. func InitSysAuthority(db *gorm.DB) {
  15. if err := db.Transaction(func(tx *gorm.DB) error {
  16. if tx.Where("authority_id IN ? ", []string{"888", "9528"}).Find(&[]model.SysAuthority{}).RowsAffected == 2 {
  17. color.Danger.Println("sys_authorities表的初始数据已存在!")
  18. return nil
  19. }
  20. if err := tx.Create(&Authorities).Error; err != nil { // 遇到错误时回滚事务
  21. return err
  22. }
  23. return nil
  24. }); err != nil {
  25. color.Warn.Printf("[Mysql]--> sys_authorities 表的初始数据失败,err: %v\n", err)
  26. os.Exit(0)
  27. }
  28. }