

package com.nantian.sc.cloud.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nantian.sc.cloud.admin.api.dto.MenuTree;
import com.nantian.sc.cloud.admin.api.entity.SysMenu;
import com.nantian.sc.cloud.admin.api.entity.SysRoleMenu;
import com.nantian.sc.cloud.admin.api.vo.MenuVO;
import com.nantian.sc.cloud.admin.mapper.SysMenuMapper;
import com.nantian.sc.cloud.admin.mapper.SysRoleMenuMapper;
import com.nantian.sc.cloud.admin.service.SysMenuService;
import com.nantian.sc.cloud.common.core.constant.CacheConstants;
import com.nantian.sc.cloud.common.core.util.R;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单权限表 服务实现类
 * </p>
 *
 *
 * @since 2017-10-29
 */
@Service
@AllArgsConstructor
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
	private final SysRoleMenuMapper sysRoleMenuMapper;

	@Override
	@Cacheable(value = CacheConstants.MENU_DETAILS, key = "#roleId  + '_menu'", unless = "#result == null")
	public List<MenuVO> findMenuByRoleId(Integer roleId) {
		return baseMapper.listMenusByRoleId(roleId);
	}

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

		sysRoleMenuMapper
				.delete(Wrappers.<SysRoleMenu>query()
						.lambda().eq(SysRoleMenu::getMenuId, id));

		//删除当前菜单及其子菜单
		return R.ok(this.removeById(id));
	}

	@Override
	@CacheEvict(value = CacheConstants.MENU_DETAILS, allEntries = true)
	public Boolean updateMenuById(SysMenu sysMenu) {
		return this.updateById(sysMenu);
	}

	@Override
	public List<MenuTree> findMenuTreeByRoleId(Integer roleId) {
		//return baseMapper.findMenuTreeByRoleId(roleId);
		return baseMapper.findMenuTreeByRoleId(roleId)
				.stream()
				.map(menu -> {
					Map<String, Object> meta = new HashMap<>();
					meta.put("title", menu.getName());
					menu.setMeta(meta);
					menu.setId(menu.getMenuId());
					return menu;
				}).sorted(Comparator.comparingInt(MenuTree::getId))
				.collect(Collectors.toList());
	}

	@Override
	public List<MenuTree> findMenuTreeByRoles(List<Integer> roles) {
		int[] reoleIds = roles.stream().mapToInt(i->i).toArray();
		return baseMapper.findMenuTreeByRoles(reoleIds).stream()
				.map(menu -> {
					Map<String, Object> meta = new HashMap<>();
					meta.put("title", menu.getName());
					menu.setMeta(meta);
					menu.setId(menu.getMenuId());
					return menu;
				}).sorted(Comparator.comparingInt(MenuTree::getId))
				.collect(Collectors.toList());
	}

	@Override
	public boolean saveMenu(SysMenu sysMenu) {
		return baseMapper.saveMenu(sysMenu);
	}

	@Override
	public boolean deleteMenuById(Integer id) {
		return baseMapper.deleteMenuById(id);
	}
}
