package com.hzs.system.config.controller.api;

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.enums.EMenu;
import com.hzs.common.core.enums.EMenuDetail;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.system.config.BdMenuDetail;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.system.config.service.IBdMenuDetailService;
import com.hzs.system.config.vo.ApiMenuVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 前端菜单权限控制器
 * Author: jiang chao
 * Time: 2022/10/10 14:39
 * Classname: ApiBdMenuController
 * PackageName: com.hzs.system.config.controller.api
 */
@RestController
@RequestMapping("/api/menu")
public class ApiBdMenuController {

    @Autowired
    private IBdMenuDetailService iBdMenuDetailService;

    @DubboReference
    private IMemberServiceApi iMemberServiceApi;

    /**
     * 获取菜单权限列表
     *
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list() {
        // 当前用户ID
        Long userId = SecurityUtils.getUserId();

        // 返回菜单显示
        List<ApiMenuVO> resultList = new ArrayList<>();

        CuMember cuMember = iMemberServiceApi.getMember(userId).getData();
        if (null == cuMember) {
            // 不在正常用户树上，此处不做作任何返回
            return AjaxResult.success(resultList);
        }
        // 结算等级
        int pkGrade = cuMember.getPkSettleGrade();
        // 真实奖衔
        int pkAwards = cuMember.getPkAwards();
        // 注册权限
        int authority = cuMember.getRegisterAuthority();

        // 查询配置菜单列表
        List<BdMenuDetail> list = iBdMenuDetailService.showMenuDetail(pkGrade, pkAwards, authority, SecurityUtils.getPkCountry());

        if (CollectionUtil.isNotEmpty(list)) {
            // 如果有配置数据，则按配置显示
            for (BdMenuDetail bdMenuDetail : list) {
                EMenu eMenu = EMenu.getEnumByValue(bdMenuDetail.getDetailValue());
                if (null != eMenu) {
                    resultList.add(ApiMenuVO.builder()
                            .menuKey(eMenu.getMenuKey())
                            .menu(eMenu.getValue())
                            .menuParent(0)
                            .menuVal(eMenu.getLabel())
                            .build());
                    continue;
                }
                EMenuDetail eMenuDetail = EMenuDetail.getEnumByValue(bdMenuDetail.getDetailValue());
                if (null != eMenuDetail) {
                    handleMenuDetail(resultList, eMenuDetail);
                }
            }
        } else {
            // 如果没有配置数据，则显示全部
            for (EMenu eMenu : EMenu.values()) {
                resultList.add(ApiMenuVO.builder()
                        .menuKey(eMenu.getMenuKey())
                        .menu(eMenu.getValue())
                        .menuParent(0)
                        .menuVal(eMenu.getLabel())
                        .build());
            }
            for (EMenuDetail eMenuDetail : EMenuDetail.values()) {
                handleMenuDetail(resultList, eMenuDetail);
            }
        }

        return AjaxResult.success(resultList);
    }

    /**
     * 处理菜单详情
     *
     * @param resultList  返回菜单显示数据
     * @param eMenuDetail 菜单明细
     */
    private void handleMenuDetail(List<ApiMenuVO> resultList, EMenuDetail eMenuDetail) {
        resultList.add(ApiMenuVO.builder()
                .menuKey(eMenuDetail.getMenuKey())
                .menu(eMenuDetail.getValue())
                .menuParent(eMenuDetail.getParent())
                .menuVal(eMenuDetail.getLabel())
                .build());
    }

}
