package com.bohuikeji.frame.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.frame.module.admin.base.ServiceImplBase;
import com.bohuikeji.frame.module.admin.entity.sys.SysMenu;
import com.bohuikeji.frame.module.admin.entity.sys.SysMenuRole;
import com.bohuikeji.frame.module.admin.mapper.SysMenuMapper;
import com.bohuikeji.frame.module.admin.service.ISysMenuRoleService;
import com.bohuikeji.frame.module.admin.service.ISysMenuService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统菜单 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-08-09
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class SysMenuServiceImpl extends ServiceImplBase<SysMenuMapper, SysMenu> implements ISysMenuService {

    final ISysMenuRoleService menuRoleService;


    @Override
    public boolean AuthCheck(Integer roleId, String menuUrl) {
        if(roleId==1){
            return true;
        }
        menuUrl=menuUrl.replaceFirst("/delivery","");
        log.info("蚕食：{}",menuUrl);
        SysMenu menu=getOne(new QueryWrapper<SysMenu>().eq("url",menuUrl),false);
        if (menu==null){
            return false;
        }
        SysMenuRole menuRole=menuRoleService.getOne(new QueryWrapper<SysMenuRole>()
                .eq("menu_id",menu.getId()).eq("role_id",roleId),false);
        //log.info("哈哈哈哈：{}",menuRoleLisy);
        if(menuRole!=null){
            return true;
        }
        //配置了上级菜单访问权限
        menuRole=menuRoleService.getOne(new QueryWrapper<SysMenuRole>()
                .eq("menu_id",menu.getSuperId()).eq("role_id",roleId),false);
        if(menuRole!=null){
            int sons=menuRoleService.count(new QueryWrapper<SysMenuRole>()
                    .apply(" menu_id in(select id from sys_menu where super_id={0})",menu.getSuperId()));
            if(sons==0){
                return true;
            }
        }
        return false;
    }
}
