/**
 * Copyright (c) 2020 fumeiai All rights reserved.
 *
 *
 *
 * 版权所有，侵权必究！
 */

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


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.lz.common.utils.Constant;
import com.lz.common.utils.MapUtils;
import com.lz.modules.flow.entity.FlowDepartment;
import com.lz.modules.flow.entity.StaffRole;
import com.lz.modules.flow.service.FlowDepartmentService;
import com.lz.modules.flow.service.StaffRoleService;
import com.lz.modules.sys.dao.SysMenuDao;
import com.lz.modules.sys.entity.SysMenuEntity;
import com.lz.modules.sys.entity.SysUserEntity;
import com.lz.modules.sys.service.SysMenuService;
import com.lz.modules.sys.service.SysRoleMenuService;
import com.lz.modules.sys.service.SysUserService;
import com.lz.modules.sys.service.app.ChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


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

	@Autowired
	private FlowDepartmentService flowDepartmentService ;

	@Autowired
	private StaffRoleService staffRoleService;

	@Autowired
	private ChartService chartService;

	@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<>();
		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(SysUserEntity user) {
		//系统管理员，拥有最高权限
		if(user.getUserId() == Constant.SUPER_ADMIN){
			return getAllMenuList(null);
		}
		//用户菜单列表
		List<Long> menuIdList = null;
		if (user.getType() == 0) {
			menuIdList = sysUserService.queryAllMenuId(user.getUserId());
		} else {
			Long roleId = 13l;        //普通员工角色
			FlowDepartment flowDepartment = flowDepartmentService.selectByStaffId(user.getUserId());
			if (flowDepartment != null) {
				roleId = 14l;
			} else {
				StaffRole staffRole = staffRoleService.selectByStaffId(user.getUserId());
				if (staffRole != null) {
					roleId = 14l;
				}
			}
			Set<Long> roleIds = Sets.newHashSet(roleId);
			if(chartService.hrOrBoss(user.getUserId())){
				roleIds.add(19L);
				roleIds.add(20L);
			}
			if(chartService.leader(user.getUserId())){
				roleIds.add(20L);
			}

			menuIdList = sysUserService.queryMenuIdListByRoleIds(roleIds);
		}
		return getAllMenuList(menuIdList);
	}

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

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

		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;
	}
}
