package com.cool.store.vo.menu;

import com.cool.store.entity.SysMenuDO;
import com.cool.store.enums.MenuTypeEnum;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * describe:
 *
 * @author zhouyiping
 * @date 2020/09/22
 */
@Data
public class MenuTreeVO {

    @ApiModelProperty("请求地址(前端路由)")
    private String path;

    @ApiModelProperty("菜单编号")
    private String code;

    @ApiModelProperty("菜单名称")
    private String name;

    @ApiModelProperty("菜单id")
    private Long id;

    @ApiModelProperty("父级菜单")
    private Long parentId;

    @ApiModelProperty("图标'")
    private String icon;

    @ApiModelProperty("菜单类型 1菜单 2权限")
    private Integer menuType;

    @ApiModelProperty("权限列表")
    private List<String> authorityList;

    @ApiModelProperty("子菜单")
    private List<MenuTreeVO> children;

    public static List<MenuTreeVO> dealMenuTree(Long rootId, List<SysMenuDO> menuList) {
        if(CollectionUtils.isEmpty(menuList)){
            return Lists.newArrayList();
        }
        Map<Long, List<SysMenuDO>> parentGroup = ListUtils.emptyIfNull(menuList).stream().collect(Collectors.groupingBy(SysMenuDO::getParentId));
        List<SysMenuDO> menuDOList = parentGroup.get(rootId);
        if (CollectionUtils.isEmpty(menuDOList)) {
            return Collections.emptyList();
        }
        List<MenuTreeVO> voList = convertVO(menuDOList);
        List<MenuTreeVO> treeVOList = new LinkedList<>(voList);
        for (MenuTreeVO data : treeVOList) {
            getChild(data, parentGroup);
        }
        return treeVOList;
    }

    public static List<MenuTreeVO> convertVO(List<SysMenuDO> menuList) {
        menuList = menuList.stream().sorted(Comparator.comparing(SysMenuDO::getSort)).collect(Collectors.toList());
        List<MenuTreeVO> resultList = new ArrayList<>();
        for (SysMenuDO menu : menuList) {
            MenuTreeVO vo = new MenuTreeVO();
            vo.setId(menu.getId());
            vo.setParentId(menu.getParentId());
            vo.setName(menu.getName());
            vo.setCode(menu.getCode());
            vo.setPath(menu.getPath());
            vo.setIcon(menu.getIcon());
            vo.setMenuType(menu.getMenuType());
            resultList.add(vo);
        }
        return resultList;
    }

    public static void getChild(MenuTreeVO data, Map<Long, List<SysMenuDO>> parentGroup) {
        List<SysMenuDO> sysMenuDOList = parentGroup.get(data.getId());
        List<SysMenuDO> parentMenuList = ListUtils.emptyIfNull(sysMenuDOList).stream().filter(menu -> MenuTypeEnum.MENU.getCode().equals(menu.getMenuType())).collect(Collectors.toList());
        List<SysMenuDO> parentAuthList = ListUtils.emptyIfNull(sysMenuDOList).stream().filter(menu -> MenuTypeEnum.AUTH.getCode().equals(menu.getMenuType())).collect(Collectors.toList());
        //属于菜单下时候
        if (CollectionUtils.isNotEmpty(parentMenuList)) {
            List<MenuTreeVO> voList = convertVO(parentMenuList);
            List<MenuTreeVO> menuList = voList.stream().filter(vo -> MenuTypeEnum.MENU.getCode().equals(vo.getMenuType())).collect(Collectors.toList());
            data.setChildren(menuList);
            voList.forEach(child -> {
                getChild(child, parentGroup);
            });
        }
        //数据是权限的的时候
        if (CollectionUtils.isNotEmpty(parentAuthList)) {
            List<String> authList = parentAuthList.stream().map(SysMenuDO::getCode).filter(Objects::nonNull).collect(Collectors.toList());
            data.setAuthorityList(authList);
        }
    }

}
