package com.lutao.cloud.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lutao.cloud.admin.api.entity.SysMenu;
import com.lutao.cloud.admin.api.entity.SysRoleMenu;
import com.lutao.cloud.admin.mapper.SysMenuMapper;
import com.lutao.cloud.admin.mapper.SysRoleMenuMapper;
import com.lutao.cloud.admin.service.SysMenuService;
import com.lutao.cloud.common.core.constant.CacheConstants;
import com.lutao.cloud.common.core.constant.CommonConstants;
import com.lutao.cloud.common.core.constant.enums.MenuTypeEnum;
import com.lutao.cloud.common.core.util.MessageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单权限表 服务实现类
 * </p>
 *

 * @since 2017-10-29
 */
@Service
@AllArgsConstructor
@Slf4j
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

	private final SysRoleMenuMapper sysRoleMenuMapper;

	/**
	 * 根据角色查询角色拥有的菜单
	 * @param roleId 角色ID
	 * @return
	 */
	@Override
	@Cacheable(value = CacheConstants.MENU_DETAILS, key = "#roleId", unless = "#result.isEmpty()")
	public List<SysMenu> findMenuByRoleId(Integer roleId){
		return baseMapper.listMenusByRoleId(roleId);
	}

	/**
	 * 根据菜单id删除菜单
	 * @param id 菜单ID
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(value = CacheConstants.MENU_DETAILS, allEntries = true)
	public MessageInfo removeMenuById(Integer id){
		// 查询父节点为当前节点的节点
		List<SysMenu> menuList = this.list(Wrappers.<SysMenu>query().lambda().eq(SysMenu::getParentId, id));
		if (CollUtil.isNotEmpty(menuList)){
			return MessageInfo.failed("菜单含有下级不能删除");
		}

		sysRoleMenuMapper.delete(Wrappers.<SysRoleMenu>query().lambda().eq(SysRoleMenu::getMenuId, id));
		// 删除当前菜单及其子菜单
		return MessageInfo.ok(this.removeById(id));
	}

	/**
	 * 根据菜单id更新菜单
	 * @param sysMenu 菜单信息
	 * @return
	 */
	@Override
	@CacheEvict(value = CacheConstants.MENU_DETAILS, allEntries = true)
	public Boolean updateMenuById(SysMenu sysMenu){
		return this.updateById(sysMenu);
	}

	/**
	 * 构建树查询 1. 不是懒加载情况，查询全部 2. 是懒加载，根据parentId 查询 2.1 父节点为空，则查询ID -1
	 * @param lazy 是否是懒加载
	 * @param parentId 父节点ID
	 * @return
	 */
	@Override
	public List<Tree<Integer>> treeMenu(boolean lazy, Integer parentId){
		if (!lazy){
			List<TreeNode<Integer>> collect = baseMapper
					.selectList(Wrappers.<SysMenu>lambdaQuery().orderByAsc(SysMenu::getSort)).stream()
					.map(getNodeFunction()).collect(Collectors.toList());

			return TreeUtil.build(collect, CommonConstants.MENU_TREE_ROOT_ID);
		}

		Integer parent = parentId == null ? CommonConstants.MENU_TREE_ROOT_ID : parentId;

		List<TreeNode<Integer>> collect = baseMapper
				.selectList(
						Wrappers.<SysMenu>lambdaQuery().eq(SysMenu::getParentId, parent).orderByAsc(SysMenu::getSort))
				.stream().map(getNodeFunction()).collect(Collectors.toList());

		return TreeUtil.build(collect, parent);
	}

	/**
	 * 查询菜单
	 * @param all 全部菜单
	 * @param type 类型
	 * @param parentId 父节点ID
	 * @return
	 */
	@Override
	public List<Tree<Integer>> filterMenu(Set<SysMenu> all, String type, Integer parentId){
		List<TreeNode<Integer>> collect = all.stream().filter(menuTypePredicate(type)).map(getNodeFunction())
				.collect(Collectors.toList());
		Integer parent = parentId == null ? CommonConstants.MENU_TREE_ROOT_ID : parentId;
		return TreeUtil.build(collect, parent);
	}

	/**
	 * 根据父级id保存菜单
	 * @param sysMenu
	 * @return
	 */
	@Override
	public Boolean saveWithParent(SysMenu sysMenu){
		if (null == sysMenu.getMenuPermissionsType() || sysMenu.getMenuPermissionsType().equals("")){
			Integer result = baseMapper.insert(sysMenu);
			return null != result && result >= 1;
		}
		// 父级菜单同步处理
		baseMapper.insert(sysMenu);
		List<SysMenu> parentSysMenuList = getParentSysMenu(sysMenu, new ArrayList<>());
		parentSysMenuList.forEach(parentSysMenu ->{
			if (null == parentSysMenu.getMenuPermissionsType() ||
					!parentSysMenu.getMenuPermissionsType().equals(sysMenu.getMenuPermissionsType())){
				parentSysMenu.setMenuPermissionsType(sysMenu.getMenuPermissionsType());
				baseMapper.updateById(parentSysMenu);
			}
		});
		return true;
	}

	/**
	 * 根据父级菜单更新
	 * @param sysMenu
	 * @return
	 */
	@Override
	public Boolean updateWithParent(SysMenu sysMenu){
		SysMenu baseSysMenu = getById(sysMenu.getMenuId());
		if (null == sysMenu.getMenuPermissionsType() || baseSysMenu.getMenuPermissionsType().equals(sysMenu.getMenuPermissionsType())){
			Integer result = baseMapper.updateById(sysMenu);
			return null != result && result >= 1;
		}
		// 父级菜单同步处理
		baseMapper.updateById(sysMenu);
		List<SysMenu> parentSysMenuList = getParentSysMenu(sysMenu, new ArrayList<>());
		parentSysMenuList.forEach(parentSysMenu ->{
			if (null == parentSysMenu.getMenuPermissionsType() ||
					!parentSysMenu.getMenuPermissionsType().equals(sysMenu.getMenuPermissionsType())){
				parentSysMenu.setMenuPermissionsType(sysMenu.getMenuPermissionsType());
				baseMapper.updateById(parentSysMenu);
			}
		});
		return true;
	}

	/**
	 *  递归查找父级菜单
	 * @param sysMenu
	 * @param parentSysMenuList
	 * @return
	 */
	@Override
	public List<SysMenu> getParentSysMenu(SysMenu sysMenu, List<SysMenu> parentSysMenuList){
		if (!sysMenu.getParentId().equals(CommonConstants.MENU_TREE_ROOT_ID)){
			SysMenu parentMenu = getById(sysMenu.getParentId());
			parentSysMenuList.add(parentMenu);
			return getParentSysMenu(parentMenu, parentSysMenuList);
		}
		return parentSysMenuList;
	}

	/**
	 * 对菜单节点进行封装
	 * @return
	 */
	@NotNull
	private Function<SysMenu, TreeNode<Integer>> getNodeFunction(){
		return menu ->{
			TreeNode<Integer> node = new TreeNode<>();
			node.setId(menu.getMenuId());
			node.setName(menu.getName());
			node.setParentId(menu.getParentId());
			node.setWeight(menu.getSort());
			// 扩展属性
			Map<String, Object> extra = new HashMap<>();
			extra.put("icon", menu.getIcon());
			extra.put("path", menu.getPath());
			extra.put("type", menu.getType());
			extra.put("permission", menu.getPermission());
			extra.put("label", menu.getName());
			extra.put("sort", menu.getSort());
			extra.put("menuFrom", menu.getMenuFrom());
			extra.put("routePath", menu.getRoutePath());
			extra.put("keepAlive", menu.getKeepAlive());
			extra.put("menuPermissionsType", menu.getMenuPermissionsType());
			node.setExtra(extra);
			return node;
		};
	}

	/**
	 * menu 类型断言
	 * @param type 类型
	 * @return Predicate
	 */
	private Predicate<SysMenu> menuTypePredicate(String type){
		return vo ->{
			if (MenuTypeEnum.TOP_MENU.getDescription().equals(type)){
				return MenuTypeEnum.TOP_MENU.getType().equals(vo.getType());
			}
			// 其他查询 左侧 + 顶部
			return !MenuTypeEnum.BUTTON.getType().equals(vo.getType());
		};
	}
}
