package com.ningdatech.pmapi.sys.manage;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.pmapi.common.util.TreeUtil;
import com.ningdatech.pmapi.sys.model.entity.Menu;
import com.ningdatech.pmapi.sys.model.entity.RoleMenu;
import com.ningdatech.pmapi.sys.model.vo.MenuRoleVO;
import com.ningdatech.pmapi.sys.service.IRoleMenuService;
import com.ningdatech.pmapi.user.security.auth.model.UserInfoDetails;
import com.ningdatech.pmapi.user.util.LoginUserUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 * MenuManage - 菜单管理业务层
 * </p>
 *
 * @author WendyYang
 * @since 14:23 2023/1/28
 */
@Component
@RequiredArgsConstructor
public class MenuManage {

    private final IRoleMenuService roleMenuService;

    public List<MenuRoleVO> buildUserMenu(List<Menu> list, UserInfoDetails loginUser) {
        /*if (Objects.isNull(loginUser) || CollUtil.isEmpty(loginUser.getRoleIdList())) {
            return Collections.emptyList();
        }*/
        List<RoleMenu> roleMenus = roleMenuService.list(Wrappers.lambdaQuery(RoleMenu.class)
                .in(RoleMenu::getRoleId, LoginUserUtil.getRoleIdList()));
        List<MenuRoleVO> menuRoles = CollUtils.convert(list, w -> BeanUtil.copyProperties(w, MenuRoleVO.class));
        return TreeUtil.buildUserTree(menuRoles, roleMenus);
    }

}
