优化结构
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"kra/internal/adminsurface"
|
||||
"kra/internal/biz"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -12,7 +13,7 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func SeedSystem(ctx context.Context, db *gorm.DB, input *biz.DatabaseConfig) error {
|
||||
func SeedSystem(ctx context.Context, db *gorm.DB, input *biz.DatabaseConfig, surfaces ...adminsurface.Surface) error {
|
||||
return db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
rootParentID := uint(0)
|
||||
authority := authorityPO{AuthorityID: 888, AuthorityName: "超级管理员", ParentID: &rootParentID, DataScope: 1, DefaultRouter: "dashboard"}
|
||||
@@ -28,6 +29,11 @@ func SeedSystem(ctx context.Context, db *gorm.DB, input *biz.DatabaseConfig) err
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, surface := range surfaces {
|
||||
if err := seedAdminSurface(tx, surface); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
var persisted []menuPO
|
||||
if err := tx.Order("sort asc, id asc").Find(&persisted).Error; err != nil {
|
||||
return err
|
||||
@@ -140,6 +146,47 @@ func SeedSystem(ctx context.Context, db *gorm.DB, input *biz.DatabaseConfig) err
|
||||
})
|
||||
}
|
||||
|
||||
func seedAdminSurface(tx *gorm.DB, surface adminsurface.Surface) error {
|
||||
for _, item := range surface.Menus {
|
||||
parentID := uint(0)
|
||||
if item.ParentName != "" {
|
||||
var parent menuPO
|
||||
if err := tx.Where("name = ?", item.ParentName).First(&parent).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
parentID = parent.ID
|
||||
}
|
||||
menu := menuPO{
|
||||
MenuLevel: 1,
|
||||
ParentID: parentID,
|
||||
Path: item.Path,
|
||||
Name: item.Name,
|
||||
Component: item.Component,
|
||||
Title: item.Title,
|
||||
Icon: item.Icon,
|
||||
Sort: item.Sort,
|
||||
}
|
||||
if item.ParentName == "" {
|
||||
menu.MenuLevel = 0
|
||||
}
|
||||
if err := tx.Where("name = ?", item.Name).FirstOrCreate(&menu).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, item := range surface.APIs {
|
||||
api := apiPO{
|
||||
Path: item.Path,
|
||||
Method: strings.ToUpper(item.Method),
|
||||
APIGroup: item.APIGroup,
|
||||
Description: item.Description,
|
||||
}
|
||||
if err := tx.Where("path = ? AND method = ?", api.Path, api.Method).FirstOrCreate(&api).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func defaultMenus() []menuPO {
|
||||
root := func(path, name, title, icon string, sort int) menuPO {
|
||||
return menuPO{Path: path, Name: name, Component: "view/routerHolder.vue", Title: title, Icon: icon, Sort: sort}
|
||||
|
||||
Reference in New Issue
Block a user