package logic

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	sysModel "sundynix-micro-go/app/system/model"
	"sundynix-micro-go/app/system/rpc/internal/svc"
	"sundynix-micro-go/app/system/rpc/system"
)

type UpdateMenuLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateMenuLogic {
	return &UpdateMenuLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
}

func (l *UpdateMenuLogic) UpdateMenu(in *system.MenuUpdateReq) (*system.CommonResp, error) {
	if err := l.svcCtx.DB.Model(&sysModel.SundynixMenu{}).Where("id = ?", in.Id).Updates(map[string]interface{}{
		"parent_id": in.ParentId, "category": in.Category, "name": in.Name, "title": in.Title,
		"code": in.Code, "path": in.Path, "permission": in.Permission, "locale": in.Locale,
		"icon": in.Icon, "sort": in.Sort,
	}).Error; err != nil {
		return nil, fmt.Errorf("更新菜单失败")
	}
	return &system.CommonResp{Code: 200, Msg: "success"}, nil
}
