fix top menu
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
package model
|
||||
|
||||
type B2BTopMenu struct {
|
||||
MenuID int `gorm:"column:menu_id;primaryKey;autoIncrement"`
|
||||
Label string `gorm:"column:label;type:longtext;not null;default:'{}'"`
|
||||
ParentID *int `gorm:"column:parent_id;index:FK_b2b_top_menu_parent_id"`
|
||||
Params string `gorm:"column:params;type:longtext;not null;default:'{}'"`
|
||||
Active int8 `gorm:"column:active;type:tinyint;not null;default:1"`
|
||||
Position int `gorm:"column:position;not null;default:1"`
|
||||
MenuID int `gorm:"column:menu_id;primaryKey;autoIncrement" json:"menu_id"`
|
||||
Label string `gorm:"column:label;type:longtext;not null;default:'{}'" json:"label"`
|
||||
ParentID *int `gorm:"column:parent_id;index:FK_b2b_top_menu_parent_id" json:"parent_id,omitempty"`
|
||||
Params string `gorm:"column:params;type:longtext;not null;default:'{}'" json:"params"`
|
||||
Active int8 `gorm:"column:active;type:tinyint;not null;default:1" json:"active"`
|
||||
Position int `gorm:"column:position;not null;default:1" json:"position"`
|
||||
|
||||
Parent *B2BTopMenu `gorm:"foreignKey:ParentID;references:MenuID;constraint:OnDelete:RESTRICT,OnUpdate:RESTRICT"`
|
||||
Children []B2BTopMenu `gorm:"foreignKey:ParentID"`
|
||||
Parent *B2BTopMenu `gorm:"foreignKey:ParentID;references:MenuID;constraint:OnDelete:RESTRICT,OnUpdate:RESTRICT" json:"parent,omitempty"`
|
||||
Children []B2BTopMenu `gorm:"foreignKey:ParentID" json:"children,omitempty"`
|
||||
}
|
||||
|
||||
func (B2BTopMenu) TableName() string {
|
||||
|
||||
@@ -99,32 +99,40 @@ func (a ByPosition) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a ByPosition) Less(i, j int) bool { return a[i].Position < a[j].Position }
|
||||
|
||||
func (s *MenuService) GetTopMenu(id uint) ([]model.B2BTopMenu, error) {
|
||||
menuMap := make(map[int]*model.B2BTopMenu)
|
||||
var roots []model.B2BTopMenu
|
||||
|
||||
items, err := s.routesRepo.GetTopMenu(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Build the map first
|
||||
for i := range items {
|
||||
items[i].Children = []model.B2BTopMenu{}
|
||||
menuMap[items[i].MenuID] = &items[i]
|
||||
if len(items) == 0 {
|
||||
return []model.B2BTopMenu{}, nil
|
||||
}
|
||||
|
||||
// Then link children to parents
|
||||
for _, item := range menuMap {
|
||||
if item.ParentID != nil && *item.ParentID != 0 {
|
||||
parent, exists := menuMap[*item.ParentID]
|
||||
if exists {
|
||||
parent.Children = append(parent.Children, *item)
|
||||
}
|
||||
// Build a map with empty children slices
|
||||
itemMap := make(map[int]model.B2BTopMenu, len(items))
|
||||
for i := range items {
|
||||
items[i].Children = []model.B2BTopMenu{}
|
||||
itemMap[items[i].MenuID] = items[i]
|
||||
}
|
||||
|
||||
// Build the tree
|
||||
var roots []model.B2BTopMenu
|
||||
for _, item := range itemMap {
|
||||
if item.ParentID == nil || *item.ParentID == 0 {
|
||||
roots = append(roots, itemMap[item.MenuID])
|
||||
} else {
|
||||
// This is a root item
|
||||
roots = append(roots, *item)
|
||||
parentID := *item.ParentID
|
||||
if parent, exists := itemMap[parentID]; exists {
|
||||
parent.Children = append(parent.Children, item)
|
||||
itemMap[parentID] = parent
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update roots with children
|
||||
for i := range roots {
|
||||
roots[i] = itemMap[roots[i].MenuID]
|
||||
}
|
||||
|
||||
return roots, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user