package logic

import (
	"context"
	"fmt"

	sysModel "sundynix-micro-go/app/system/model"
	"sundynix-micro-go/app/system/rpc/internal/svc"
	"sundynix-micro-go/app/system/rpc/system"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

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

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

func (l *DeleteMenuLogic) DeleteMenu(in *system.IdsReq) (*system.CommonResp, error) {
	err := l.svcCtx.DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Where("id IN ?", in.Ids).Delete(&sysModel.SundynixMenu{}).Error; err != nil {
			return fmt.Errorf("删除菜单失败: %w", err)
		}
		if err := tx.Where("menu_id IN ?", in.Ids).Delete(&sysModel.SundynixRoleMenu{}).Error; err != nil {
			return fmt.Errorf("清除角色关联失败: %w", err)
		}
		return nil
	})
	if err != nil {
		return nil, err
	}
	return &system.CommonResp{Code: 200, Msg: "success"}, nil
}
