package com.iailab.module.system.service.app;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
import com.iailab.framework.common.enums.CommonStatusEnum;
import com.iailab.framework.common.util.object.BeanUtils;
import com.iailab.module.system.controller.admin.permission.vo.menu.MenuListReqVO;
import com.iailab.module.system.controller.admin.permission.vo.menu.MenuSaveVO;
import com.iailab.module.system.dal.dataobject.app.AppMenuDO;
import com.iailab.module.system.dal.mysql.app.AppMenuMapper;
import com.iailab.module.system.dal.redis.RedisKeyConstants;
import com.iailab.module.system.enums.permission.MenuTypeEnum;
import com.iailab.module.system.service.tenant.TenantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.iailab.framework.common.util.collection.CollectionUtils.convertList;
import static com.iailab.framework.common.util.collection.CollectionUtils.convertMap;
import static com.iailab.module.system.dal.dataobject.app.AppMenuDO.ID_ROOT;
import static com.iailab.module.system.enums.ErrorCodeConstants.*;


/**
 * 菜单 Service 实现
 *
 * @author iailab
 */
@Service
@Slf4j
public class AppMenuServiceImpl implements AppMenuService {

    @Resource
    private AppMenuMapper appMenuMapper;
    @Resource
    @Lazy // 延迟,避免循环依赖报错
    private TenantService tenantService;

    @Override
    @CacheEvict(value = RedisKeyConstants.PERMISSION_MENU_ID_LIST, key = "#createReqVO.permission",
            condition = "#createReqVO.permission != null")
    public Long createMenu(MenuSaveVO createReqVO) {
        // 校验父菜单存在
        validateParentMenu(createReqVO.getParentId(), null);
        // 校验菜单(自己)
        validateMenu(createReqVO.getParentId(), createReqVO.getName(), null);

        // 插入数据库
        AppMenuDO menu = BeanUtils.toBean(createReqVO, AppMenuDO.class);
        initMenuProperty(menu);
        appMenuMapper.insert(menu);
        // 返回
        return menu.getId();
    }

    @Override
    @CacheEvict(value = RedisKeyConstants.PERMISSION_MENU_ID_LIST,
            allEntries = true) // allEntries 清空所有缓存,因为 permission 如果变更,涉及到新老两个 permission。直接清理,简单有效
    public void updateMenu(MenuSaveVO updateReqVO) {
        // 校验更新的菜单是否存在
        if (appMenuMapper.selectById(updateReqVO.getId()) == null) {
            throw exception(MENU_NOT_EXISTS);
        }
        // 校验父菜单存在
        validateParentMenu(updateReqVO.getParentId(), updateReqVO.getId());
        // 校验菜单(自己)
        validateMenu(updateReqVO.getParentId(), updateReqVO.getName(), updateReqVO.getId());

        // 更新到数据库
        AppMenuDO updateObj = BeanUtils.toBean(updateReqVO, AppMenuDO.class);
        initMenuProperty(updateObj);
        appMenuMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = RedisKeyConstants.PERMISSION_MENU_ID_LIST,
            allEntries = true) // allEntries 清空所有缓存,因为此时不知道 id 对应的 permission 是多少。直接清理,简单有效
    public void deleteMenu(Long id) {
        // 校验是否还有子菜单
        if (appMenuMapper.selectCountByParentId(id) > 0) {
            throw exception(MENU_EXISTS_CHILDREN);
        }
        // 校验删除的菜单是否存在
        if (appMenuMapper.selectById(id) == null) {
            throw exception(MENU_NOT_EXISTS);
        }
        // 标记删除
        appMenuMapper.deleteById(id);
    }

    @Override
    public List<AppMenuDO> getMenuList() {
        return appMenuMapper.selectList();
    }

    @Override
    public List<AppMenuDO> getMenuList(Integer type) {
        LambdaQueryWrapper<AppMenuDO> queryWrapper = new LambdaQueryWrapper<>();
        if (type == 1) {
            queryWrapper.eq(AppMenuDO::getType, type);
        }
        return appMenuMapper.selectList(queryWrapper);
    }

    @Override
    public List<AppMenuDO> getMenuListByTenant(Integer type) {
        // 查询所有菜单,并过滤掉关闭的节点
        List<AppMenuDO> menus = getMenuList(type);
        // 开启多租户的情况下,需要过滤掉未开通的菜单
        tenantService.handleTenantMenu(menuIds -> menus.removeIf(menu -> !CollUtil.contains(menuIds, menu.getId())));
        return menus;
    }

    @Override
    public List<AppMenuDO> selectListByParentId(Collection<Long> parentIds) {
        return appMenuMapper.selectListByParentId(parentIds);
    }

    @Override
    public List<AppMenuDO> getMenuListByTenant(MenuListReqVO reqVO) {
        // 查询所有菜单,并过滤掉关闭的节点
        List<AppMenuDO> menus = getMenuList(reqVO);
        // 开启多租户的情况下,需要过滤掉未开通的菜单
        tenantService.handleTenantMenu(menuIds -> menus.removeIf(menu -> !CollUtil.contains(menuIds, menu.getId())));
        return menus;
    }

    @Override
    public List<AppMenuDO> filterDisableMenus(List<AppMenuDO> menuList) {
        if (CollUtil.isEmpty(menuList)){
            return Collections.emptyList();
        }
        Map<Long, AppMenuDO> menuMap = convertMap(menuList, AppMenuDO::getId);

        // 遍历 menu 菜单,查找不是禁用的菜单,添加到 enabledMenus 结果
        List<AppMenuDO> enabledMenus = new ArrayList<>();
        Set<Long> disabledMenuCache = new HashSet<>(); // 存下递归搜索过被禁用的菜单,防止重复的搜索
        for (AppMenuDO menu : menuList) {
            if (isMenuDisabled(menu, menuMap, disabledMenuCache)) {
                continue;
            }
            enabledMenus.add(menu);
        }
        return enabledMenus;
    }

    private boolean isMenuDisabled(AppMenuDO node, Map<Long, AppMenuDO> menuMap, Set<Long> disabledMenuCache) {
        // 如果已经判定是禁用的节点,直接结束
        if (disabledMenuCache.contains(node.getId())) {
            return true;
        }

        // 1. 遍历到 parentId 为根节点,则无需判断
        Long parentId = node.getParentId();
        if (ObjUtil.equal(parentId, ID_ROOT)) {
            if (CommonStatusEnum.isDisable(node.getStatus())) {
                disabledMenuCache.add(node.getId());
                return true;
            }
            return false;
        }

        // 2. 继续遍历 parent 节点
        AppMenuDO parent = menuMap.get(parentId);
        if (parent == null || isMenuDisabled(parent, menuMap, disabledMenuCache)) {
            disabledMenuCache.add(node.getId());
            return true;
        }
        return false;
    }

    @Override
    public List<AppMenuDO> getMenuList(MenuListReqVO reqVO) {
        return appMenuMapper.selectList(reqVO);
    }

    @Override
    @Cacheable(value = RedisKeyConstants.PERMISSION_MENU_ID_LIST, key = "#permission")
    public List<Long> getMenuIdListByPermissionFromCache(String permission) {
        List<AppMenuDO> menus = appMenuMapper.selectListByPermission(permission);
        return convertList(menus, AppMenuDO::getId);
    }

    @Override
    public AppMenuDO getMenu(Long id) {
        return appMenuMapper.selectById(id);
    }

    @Override
    public List<AppMenuDO> getMenuList(Collection<Long> ids) {
        // 当 ids 为空时,返回一个空的实例对象
        if (CollUtil.isEmpty(ids)) {
            return Lists.newArrayList();
        }
        return appMenuMapper.selectBatchIds(ids);
    }

    /**
     * 校验父菜单是否合法
     * <p>
     * 1. 不能设置自己为父菜单
     * 2. 父菜单不存在
     * 3. 父菜单必须是 {@link MenuTypeEnum#MENU} 菜单类型
     *
     * @param parentId 父菜单编号
     * @param childId  当前菜单编号
     */
    @VisibleForTesting
    void validateParentMenu(Long parentId, Long childId) {
        if (parentId == null || ID_ROOT.equals(parentId)) {
            return;
        }
        // 不能设置自己为父菜单
        if (parentId.equals(childId)) {
            throw exception(MENU_PARENT_ERROR);
        }
        AppMenuDO menu = appMenuMapper.selectById(parentId);
        // 父菜单不存在
        if (menu == null) {
            throw exception(MENU_PARENT_NOT_EXISTS);
        }
        // 父菜单必须是目录或者菜单类型
        if (!MenuTypeEnum.DIR.getType().equals(menu.getType())
                && !MenuTypeEnum.MENU.getType().equals(menu.getType())) {
            throw exception(MENU_PARENT_NOT_DIR_OR_MENU);
        }
    }

    /**
     * 校验菜单是否合法
     * <p>
     * 1. 校验相同父菜单编号下,是否存在相同的菜单名
     *
     * @param name     菜单名字
     * @param parentId 父菜单编号
     * @param id       菜单编号
     */
    @VisibleForTesting
    void validateMenu(Long parentId, String name, Long id) {
        AppMenuDO menu = appMenuMapper.selectByParentIdAndName(parentId, name);
        if (menu == null) {
            return;
        }
        // 如果 id 为空,说明不用比较是否为相同 id 的菜单
        if (id == null) {
            throw exception(MENU_NAME_DUPLICATE);
        }
        if (!menu.getId().equals(id)) {
            throw exception(MENU_NAME_DUPLICATE);
        }
    }

    /**
     * 初始化菜单的通用属性。
     * <p>
     * 例如说,只有目录或者菜单类型的菜单,才设置 icon
     *
     * @param menu 菜单
     */
    private void initMenuProperty(AppMenuDO menu) {
        // 菜单为按钮类型时,无需 component、icon、path 属性,进行置空
        if (MenuTypeEnum.BUTTON.getType().equals(menu.getType())) {
            menu.setComponent("");
            menu.setComponentName("");
            menu.setIcon("");
            menu.setPath("");
        }
    }

}