package com.storlead.system.pojo.vo;

import com.storlead.system.pojo.entity.MenuEntity;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @program: sp-sales
 * @description:
 * @author: chenkq
 * @create: 2022-07-11 16:51
 */
@Data
public class ResTreeVo {

    private Long id;
    private String treeId;
    private Long parentId;
    private Long appId;
    private String title;
    private String code;
    private Object arg;
    /**
     * 10：应用，20：菜单，30：权限资源
     */
    private Integer type;
    private List<ResTreeVo> children;

    /**
     * List<menu> 转为 List<ResTreeVo>
     * @param menus
     * @return
     */
    public static List<ResTreeVo> menuListToResTreeVoList(List<MenuEntity> menus) {
        if (CollectionUtils.isEmpty(menus)) {
            return null;
        }
        List<ResTreeVo> menuVOS = menus.stream().map(ResTreeVo::menuListToResTreeVo).filter(obj -> !Objects.isNull(obj)).collect(Collectors.toList());
        return menuVOS;
    }

    public static ResTreeVo  menuListToResTreeVo(MenuEntity menu) {
        if (null == menu) {
            return null;
        }
        ResTreeVo menuVO = new ResTreeVo();
        menuVO.setId(menu.getId());
        menuVO.setTitle(menu.getMenuName());
        menuVO.setParentId(menu.getPid());
        menuVO.setArg(menu);
        menuVO.setType(20);
        return menuVO;
    }
}
