72 lines
1.9 KiB
Go
72 lines
1.9 KiB
Go
package system
|
|
|
|
import (
|
|
"context"
|
|
|
|
"management/internal/erpserver/model/system"
|
|
"management/internal/erpserver/repository"
|
|
)
|
|
|
|
type menuRepository struct {
|
|
repo *repository.Repository
|
|
}
|
|
|
|
func NewMenuRepository(repo *repository.Repository) system.MenuRepository {
|
|
return &menuRepository{
|
|
repo: repo,
|
|
}
|
|
}
|
|
|
|
func (r *menuRepository) Create(ctx context.Context, obj *system.Menu) error {
|
|
return r.repo.DB(ctx).Create(obj).Error
|
|
}
|
|
|
|
func (r *menuRepository) Update(ctx context.Context, obj *system.Menu) error {
|
|
return r.repo.DB(ctx).Save(obj).Error
|
|
}
|
|
|
|
func (r *menuRepository) Get(ctx context.Context, id int32) (*system.Menu, error) {
|
|
var menu system.Menu
|
|
err := r.repo.DB(ctx).Where("id = ?", id).First(&menu).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &menu, nil
|
|
}
|
|
|
|
func (r *menuRepository) GetByUrl(ctx context.Context, url string) (*system.Menu, error) {
|
|
var menu system.Menu
|
|
err := r.repo.DB(ctx).Where("url = ?", url).First(&menu).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &menu, nil
|
|
}
|
|
|
|
func (r *menuRepository) All(ctx context.Context) ([]*system.Menu, error) {
|
|
var menus []*system.Menu
|
|
err := r.repo.DB(ctx).Find(&menus).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return menus, nil
|
|
}
|
|
|
|
func (r *menuRepository) RebuildParentPath(ctx context.Context) error {
|
|
query := `UPDATE sys_menu AS tm
|
|
SET parent_path = (SELECT ',' || string_agg(cast(t.parent_id AS VARCHAR), ',') || ','
|
|
FROM (WITH RECURSIVE temp (id, parent_id) AS (SELECT id, tm.parent_id
|
|
FROM sys_menu
|
|
WHERE id = tm.id
|
|
UNION ALL
|
|
SELECT sys_menu.id, sys_menu.parent_id
|
|
FROM sys_menu,
|
|
temp
|
|
WHERE sys_menu.id = temp.parent_id)
|
|
SELECT id, parent_id
|
|
FROM temp
|
|
ORDER BY id) AS t)
|
|
WHERE tm.status = 0;`
|
|
return r.repo.DB(ctx).Exec(query).Error
|
|
}
|