

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


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcloud.common.utils.MapUtils;
import com.zcloud.modules.sys.dao.SysMenuDao;
import com.zcloud.modules.sys.entity.SysMenuEntity;
import com.zcloud.modules.sys.service.SysMenuService;
import com.zcloud.modules.sys.service.SysRoleMenuService;
import com.zcloud.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenuEntity> implements SysMenuService {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysRoleMenuService sysRoleMenuService;

	@Override
	public List<SysMenuEntity> queryListParentId(Integer parentId, List<Integer> menuIdList) {
		List<SysMenuEntity> menuList = queryListParentId(parentId);
		if(menuIdList == null){
			return menuList;
		}
		List<SysMenuEntity> userMenuList = new ArrayList<>();
		for(SysMenuEntity menu : menuList){
			if(menuIdList.contains(menu.getMenuId())){
				userMenuList.add(menu);
			}
		}
		return userMenuList;
	}

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

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

	@Override
	public List<SysMenuEntity> getUserMenuList(String userId) {
		//系统管理员，拥有最高权限
		if(userId.length()==1){
			return getMenuList(null);
		}

		//用户菜单列表
		List<String> menuIdList = sysUserService.queryAllMenuId(userId);
		return getMenuList(menuIdList);
	}

	/**
	 * 获取拥有的菜单列表
	 * @param menuIdList
	 * @return
	 */
	private List<SysMenuEntity> getMenuList(List<String> menuIdList) {
		// 查询拥有的所有菜单
		List<SysMenuEntity> menus = this.baseMapper.selectList(new QueryWrapper<SysMenuEntity>()
				.in(Objects.nonNull(menuIdList), "menu_id", menuIdList).in("type", 0, 1).eq("is_delete",0));
		//查询完成 对此list直接排序
		Collections.sort(menus);

		// 将id和菜单绑定
		HashMap<Integer, SysMenuEntity> menuMap = new HashMap<>(12);
		for (SysMenuEntity s : menus) {
			menuMap.put(s.getMenuId(), s);
		}
		// 使用迭代器,组装菜单的层级关系
		Iterator<SysMenuEntity> iterator = menus.iterator();
		while (iterator.hasNext()) {
			SysMenuEntity menu = iterator.next();
			SysMenuEntity parent = menuMap.get(menu.getParentId());
			if (Objects.nonNull(parent)) {
				parent.getList().add(menu);
				// 将这个菜单从当前节点移除
				iterator.remove();
			}
		}

		return menus;
	}

	@Override
	@Transactional
	public void delete(Integer menuId){

		baseMapper.deleteById(menuId);
		//删除菜单与角色关联
		sysRoleMenuService.removeByMap(new MapUtils().put("menu_id", menuId));
	}

	@Override
	public List<SysMenuEntity> getAllList(Integer parentId){
		return this.getAllMenuList(parentId);
	}

	@Override
	public List<SysMenuEntity> getAllListNotButton(Integer parentId){
		return this.getAllMenuListNotButton(parentId);
	}

	/**
	 * 获取所有菜单列表
	 */
	private List<SysMenuEntity> getAllMenuList(Integer parentId){
		//查询根菜单列表
		List<SysMenuEntity> menuList = queryListParentId(parentId);
		//递归获取子菜单
		getMenuTreeList(menuList);

		return menuList;
	}

	/**
	 * 递归
	 */
	private List<SysMenuEntity> getMenuTreeList(List<SysMenuEntity> menuList){
		List<SysMenuEntity> subMenuList = new ArrayList<SysMenuEntity>();

		for(SysMenuEntity entity : menuList){
			entity.setList(getMenuTreeList(queryListParentId(entity.getMenuId())));
			subMenuList.add(entity);
		}

		return subMenuList;
	}

	/**
	 * 获取所有菜单列表(不包括按钮)
	 */
	private List<SysMenuEntity> getAllMenuListNotButton(Integer parentId){
		//查询根菜单列表
		List<SysMenuEntity> menuList = queryNotButtonList(parentId);
		//递归获取子菜单
		getMenuTreeNotButtonList(menuList);

		return menuList;
	}

	/**
	 * 递归
	 */
	private List<SysMenuEntity> getMenuTreeNotButtonList(List<SysMenuEntity> menuList){
		List<SysMenuEntity> subMenuList = new ArrayList<SysMenuEntity>();

		for(SysMenuEntity entity : menuList){
			entity.setList(getMenuTreeNotButtonList(queryNotButtonList(entity.getMenuId())));
			subMenuList.add(entity);
		}

		return subMenuList;
	}
}
