package com.chushang.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.chushang.common.core.util.StringUtils;
import com.chushang.common.mybatis.enums.Operator;
import com.chushang.system.entity.dto.ListMenuDTO;
import com.chushang.system.entity.po.SysMenu;
import com.chushang.system.entity.po.SysUser;
import com.chushang.system.entity.vo.RouterVo;
import com.chushang.system.entity.vo.TreeSelect;

import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author author
 * @since 2022-08-18
 */
public interface ISysMenuService extends IService<SysMenu> {

    Set<String> selectMenuPermsByUserId(Long userId);

    List<SysMenu> selectMenuList(ListMenuDTO menu, Long userId);

    default List<SysMenu> selectMenuList(Long userId){
        return selectMenuList(new ListMenuDTO(), userId);
    }

    default SysMenu selectMenuById(Long menuId){
        return getById(menuId);
    }

    List<TreeSelect> buildMenuTreeSelect(List<SysMenu> menus);

    List<Long> selectMenuListByRoleId(Long roleId);

    default boolean checkMenuNameUnique(SysMenu menu){
        long menuId = StringUtils.isNull(menu.getMenuId()) ? -1 : menu.getMenuId();
        SysMenu info = getOne(new LambdaQueryWrapper<SysMenu>()
                .eq(SysMenu::getMenuName, menu.getMenuName())
                .eq(SysMenu::getParentId, menu.getParentId())
                .last(Operator.LIMIT_ONE.getCharacter()));
        return StringUtils.isNotNull(info) && info.getMenuId() != menuId;
    }

    default boolean hasChildByMenuId(Long menuId){
        return count(new LambdaQueryWrapper<SysMenu>()
                .eq(SysMenu::getParentId, menuId)) > 0;
    }

    boolean checkMenuExistRole(Long menuId);

    List<SysMenu> selectMenuTreeByUserId(SysUser sysUser);

    List<RouterVo> buildMenus(List<SysMenu> menus);

    Set<String> selectMenuPermsByRoleId(Long roleId);
}
