package com.changfa.controller;

import com.alibaba.fastjson.JSONObject;
import com.changfa.base.BaseController;
import com.changfa.config.shiro.CustomRealm;
import com.changfa.constants.ApiResult;
import com.changfa.enums.StatusEnum;
import com.changfa.dto.MenuDto;
import com.changfa.exception.BuzEx;
import com.changfa.service.ITMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author wwz
 * @since 2022-06-24
 */
@RestController
@Api(tags = "菜单表")
@RequestMapping(value = "/menu",method = RequestMethod.POST)
public class MenuController extends BaseController {

    @Autowired
    private ITMenuService menuService;
    @Autowired
    private CustomRealm customRealm;

    @ApiOperation("查询菜单")
    @RequestMapping("/selectMenu")
    public ApiResult selectMenu(){
        try{
            // 根据当前用户查询菜单
            List<MenuDto> menuList = menuService.selectMenuListByRoleId(userBaseInfo().getRole().getId());
            if(!ObjectUtils.isEmpty(menuList)){
                JSONObject jsonObject = new JSONObject();
                // 菜单递归排列
                jsonObject.put("menuList",customRealm.recursionForMenu(-1,menuList));
                return ApiResult.success(jsonObject);
            }
        }catch (BuzEx buzEx){
            return ApiResult.error(buzEx.getStatusEnum());
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ApiResult.error(StatusEnum.FAIL);

    }

}
