
package com.platform.modules.sys.service.impl;


import java.util.*;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.platform.common.utils.Constant;
import com.platform.common.utils.MapUtils;
import com.platform.modules.sys.dao.SysMenuDao;
import com.platform.modules.sys.entity.SysMenuEntity;
import com.platform.modules.sys.service.SysMenuService;
import com.platform.modules.sys.service.SysRoleMenuService;
import com.platform.modules.sys.service.SysUserService;


@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenuEntity> implements SysMenuService {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysRoleMenuService sysRoleMenuService;
	
	@Override
	public List<SysMenuEntity> queryListParentId(Long parentId, List<Long> menuIdList) {
		List<SysMenuEntity> menuList = queryListParentId(parentId);
		if(menuIdList == null){
			return menuList;
		}
		
		List<SysMenuEntity> userMenuList = new ArrayList<SysMenuEntity>();
		for(SysMenuEntity menu : menuList){
			if(menuIdList.contains(menu.getMenuId())){
				userMenuList.add(menu);
			}
		}
		return userMenuList;
	}

	@Override
	public List<SysMenuEntity> queryListParentId(Long parentId) {
		return baseMapper.queryListParentId(parentId);
	}

	@Override
	public List<SysMenuEntity> queryNotButtonList() {
		return baseMapper.queryNotButtonList();
	}

	@Override
	public List<SysMenuEntity> getUserMenuList(Long userId) {
		//系统管理员，拥有最高权限
		if(userId == Constant.SUPER_ADMIN){
			return getAllMenuList(null);
		}
		
		//用户菜单列表
//		List<Long> menuIdList = sysUserService.queryAllMenuId(userId);
//		return getAllMenuList(menuIdList);
		return getUserMenuNav(userId);
	}

	@Override
	public void delete(Long menuId){
		//删除菜单
		this.deleteById(menuId);
		//删除菜单与角色关联
		sysRoleMenuService.deleteByMap(new MapUtils().put("menu_id", menuId));
	}

	private List<SysMenuEntity> getUserMenuNav(Long userId) {
		if (null == userId) {
			return null;
		}
		List<SysMenuEntity> dbMenuList = this.baseMapper.queryAllMenu(userId);
		if (null==dbMenuList || dbMenuList.size()<=0) {
			return null;
		}
		Map<Long, SysMenuEntity> userMenuMap = dbMenuList.stream().collect(Collectors.toMap(SysMenuEntity::getMenuId,
				a->a));
		// 呈现用户菜单
		Map<Long, SysMenuEntity> menuMap = new HashMap<>();
		// 一二级节点ID
		List<Long> parentList = new ArrayList<>();
		for (SysMenuEntity menu : dbMenuList) {
			findParentMenu(menu, menuMap, parentList, userMenuMap);
		}
		List<SysMenuEntity> menuList = new ArrayList<>(menuMap.values());
		Collections.sort(menuList, Comparator.comparing(SysMenuEntity::getOrderNum));
		return menuList;
	}

	/**
	 * 递归获取一二级菜单信息
	 * @param menu
	 * @param menuMap
	 * @param parentList
	 * @param userMenuMap
	 */
	private boolean findParentMenu(SysMenuEntity menu, Map<Long, SysMenuEntity> menuMap, List<Long> parentList,
								   Map<Long, SysMenuEntity> userMenuMap) {
		if (null==menu || null==menu.getParentId()) {
			return false;
		}
		// 临时对象存储
		if (!userMenuMap.containsKey(menu.getMenuId())) {
			userMenuMap.put(menu.getMenuId(), menu);
		}
		Long parentId = menu.getParentId();
		// 第一层级判断
		if (Long.valueOf(0).equals(parentId)) {
			if (!parentList.contains(menu.getMenuId())) {
				parentList.add(menu.getMenuId());
				menuMap.put(menu.getMenuId(), menu);
			}
			return true;
		}
		SysMenuEntity parent = userMenuMap.containsKey(parentId) ? userMenuMap.get(parentId) : this.selectById
				(parentId);
		boolean exist = findParentMenu(parent, menuMap, parentList, userMenuMap);
		if (exist && !menu.getType().equals(Constant.MenuType.BUTTON.getValue())) {
			List childList = (null == parent.getList()) ? new ArrayList() : parent.getList();
			if (!childList.contains(menu)) {
				childList.add(menu);
				Collections.sort(childList, Comparator.comparing(SysMenuEntity::getOrderNum));
				parent.setList(childList);
			}
		}
		return false;
	}

	/**
	 * 获取所有菜单列表
	 */
	private List<SysMenuEntity> getAllMenuList(List<Long> menuIdList){
		//查询根菜单列表
		List<SysMenuEntity> menuList = queryListParentId(0L, menuIdList);
		//递归获取子菜单
		getMenuTreeList1(menuList, menuIdList);
		getMenuTreeList(menuList, menuIdList);

		// 20190411 TROY 菜单逻辑导致菜单排序失效，后续优化
		Collections.sort(menuList, Comparator.comparing(SysMenuEntity::getOrderNum));
		return menuList;
	}

	/**
	 * 递归获取子菜单
	 */
	private List<SysMenuEntity> getMenuTreeList(List<SysMenuEntity> menuList, List<Long> menuIdList){
		List<SysMenuEntity> subMenuList = new ArrayList<SysMenuEntity>();
		
		for(SysMenuEntity entity : menuList){
			//目录
			if(entity.getType() == Constant.MenuType.CATALOG.getValue()){
				entity.setList(getMenuTreeList(queryListParentId(entity.getMenuId(), menuIdList), menuIdList));
			}
			subMenuList.add(entity);
		}
		
		return subMenuList;
	}
	
	private List<SysMenuEntity> getMenuTreeList1(List<SysMenuEntity> menuList, List<Long> menuIdList){
		List<Long> parentMenuIdList = new ArrayList<Long>();
		if(menuIdList!=null && menuIdList.size()>0){
			for(Long menuId : menuIdList){
				SysMenuEntity entity = this.selectById(menuId);
				if(entity!=null && entity.getParentId()!=null && entity.getParentId()!=0){
					SysMenuEntity menu = this.selectById(entity.getParentId());
					if(menu!=null && !parentMenuIdList.contains(menu.getMenuId()) && !menuIdList.contains(menu.getMenuId())){
						if(menu.getParentId()==0){
							menuList.add(menu);
							parentMenuIdList.add(menu.getMenuId());
						}
						if(menu.getParentId()!=0){
							List<Long> menuIds = new ArrayList<Long>();
							menuIds.add(menu.getParentId());
							getMenuTreeList1(menuList, menuIds);
						}
					}
				}
			}
		}
		return menuList;
	}
}
