|
|
|
@ -353,13 +353,6 @@ func seedRoleMenus(db *gorm.DB) { |
|
|
|
} |
|
|
|
|
|
|
|
for _, r := range roles { |
|
|
|
// 检查角色是否已有菜单关联
|
|
|
|
var count int64 |
|
|
|
db.Model(&model.RoleMenu{}).Where("role_id = ?", r.Id).Count(&count) |
|
|
|
if count > 0 { |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
|
var menuIds []int64 |
|
|
|
switch r.Code { |
|
|
|
case model.RoleSuperAdmin, model.RoleAdmin: |
|
|
|
@ -368,12 +361,23 @@ func seedRoleMenus(db *gorm.DB) { |
|
|
|
menuIds = defaultMenuIds |
|
|
|
} |
|
|
|
|
|
|
|
if len(menuIds) > 0 { |
|
|
|
records := make([]model.RoleMenu, 0, len(menuIds)) |
|
|
|
for _, menuId := range menuIds { |
|
|
|
records = append(records, model.RoleMenu{RoleId: r.Id, MenuId: menuId}) |
|
|
|
// 获取已有的菜单关联
|
|
|
|
var existingMenuIds []int64 |
|
|
|
db.Model(&model.RoleMenu{}).Where("role_id = ?", r.Id).Pluck("menu_id", &existingMenuIds) |
|
|
|
existingSet := make(map[int64]bool) |
|
|
|
for _, id := range existingMenuIds { |
|
|
|
existingSet[id] = true |
|
|
|
} |
|
|
|
|
|
|
|
// 添加缺失的菜单关联
|
|
|
|
var newRecords []model.RoleMenu |
|
|
|
for _, menuId := range menuIds { |
|
|
|
if !existingSet[menuId] { |
|
|
|
newRecords = append(newRecords, model.RoleMenu{RoleId: r.Id, MenuId: menuId}) |
|
|
|
} |
|
|
|
db.Create(&records) |
|
|
|
} |
|
|
|
if len(newRecords) > 0 { |
|
|
|
db.Create(&newRecords) |
|
|
|
} |
|
|
|
} |
|
|
|
log.Println("[Seed] RoleMenus seeded successfully") |
|
|
|
|