package com.yonge.cooleshow.auth.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.yonge.toolset.mybatis.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonge.cooleshow.auth.api.dto.MenuQueryInfo;
import com.yonge.cooleshow.auth.api.entity.SysMenu;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.auth.dal.dao.SysMenuDao;
import com.yonge.cooleshow.auth.dal.dao.SysUserDao;
import com.yonge.cooleshow.auth.service.SysMenuService;
import com.yonge.toolset.mybatis.dal.BaseDAO;

@Service
public class SysMenuServiceImpl extends BaseServiceImpl<Long, SysMenu> implements SysMenuService {
	
	@Autowired
	private SysMenuDao sysMenuDao;
	@Autowired
	private SysUserDao sysUserDao;

	@Override
	public BaseDAO<Long, SysMenu> getDAO() {
		return sysMenuDao;
	}

	@Override
	public List<SysMenu> getMenusByRoleId(Long roleId) {
		return sysMenuDao.getMenusByRoleId(roleId);
	}

	public List<SysMenu> findByUser1(MenuQueryInfo menuQueryInfo) {
		SysUser sysUser = sysUserDao.get(menuQueryInfo.getUserId());
		List<SysMenu> sysMenus = null;
		if(sysUser.getIsSuperAdmin()){
			menuQueryInfo.setUserId(null);
			sysMenus = sysMenuDao.findList(menuQueryInfo.getParentId(),menuQueryInfo.getDelFlag(),menuQueryInfo.getHid());
		}else {
			sysMenus = sysMenuDao.findByParentId(menuQueryInfo.getParentId(),menuQueryInfo.getDelFlag(),menuQueryInfo.getUserId(),menuQueryInfo.getHid());
		}
		for (SysMenu menu:sysMenus) {
			menu = getTree(menu,menuQueryInfo.getDelFlag(),menuQueryInfo.getUserId(),menuQueryInfo.getHid());
		}
		return sysMenus;
	}

	public List<SysMenu> findByUser(MenuQueryInfo menuQueryInfo) {
		SysUser sysUser = sysUserDao.get(menuQueryInfo.getUserId());
		List<SysMenu> sysMenus;
		List<SysMenu> sysMenusAllList;

		if(sysUser.getIsSuperAdmin()){
			sysMenus = sysMenuDao.findList(menuQueryInfo.getParentId(),menuQueryInfo.getDelFlag(),menuQueryInfo.getHid());
			sysMenusAllList = sysMenuDao.findList(null,menuQueryInfo.getDelFlag(),menuQueryInfo.getHid());
		}else {
			sysMenus = sysMenuDao.findByParentId(menuQueryInfo.getParentId(),menuQueryInfo.getDelFlag(),menuQueryInfo.getUserId(),menuQueryInfo.getHid());
			sysMenusAllList = sysMenuDao.findByParentId(null,menuQueryInfo.getDelFlag(),menuQueryInfo.getUserId(),menuQueryInfo.getHid());
		}

		for (SysMenu menu:sysMenus) {
			menu = getTree1(menu,sysMenusAllList);
		}
		return sysMenus;
	}

	private SysMenu getTree1(SysMenu menu,List<SysMenu> sysMenusAllList){
		//得到根节点对象
		//获取子节点list
		List<SysMenu> sysMenus = sysMenusAllList.stream().filter(e->e.getParentId().equals(menu.getId())).collect(Collectors.toList());
		//如果存在子节点
		if(sysMenus != null && sysMenus.size() > 0) {
			//将子节点list放入父节点对象
			menu.setSysMenus(sysMenus);
			//遍历子节点....
			for (SysMenu sysMenu : sysMenus) {
				getTree1(sysMenu,sysMenusAllList);
			}
		}
		return menu;
	}

	private SysMenu getTree(SysMenu menu, String delFlag,Long userId,Integer hid){
		//得到根节点对象
		//获取子节点list
		List<SysMenu> sysMenus = null;
		if(userId == null){
			sysMenus = sysMenuDao.findList(menu.getId(),delFlag,hid);
		}else {
			sysMenus = sysMenuDao.findByParentId(menu.getId(),delFlag,userId,hid);
		}
		//如果存在子节点
		if(sysMenus != null && sysMenus.size() > 0) {
			//将子节点list放入父节点对象
			menu.setSysMenus(sysMenus);
			//遍历子节点....
			for (SysMenu sysMenu : sysMenus) {
				getTree(sysMenu,delFlag,userId,hid);
			}
		}
		return menu;
	}

	@Override
	public SysMenu findMenuByPermission(String permission){
		return sysMenuDao.findMenuByPermission(permission);
	}

	@Override
	public void recursiveDel(Long parentId) {
		List<SysMenu> sysMenus = sysMenuDao.findList(parentId, "0",0);
		for (SysMenu menu:sysMenus) {
			menu = getTree(menu,"0",null,null);
		}
		List<Long> collect = sysMenus.stream().map(sysMenu -> sysMenu.getId()).collect(Collectors.toList());
		if(collect == null || collect.size() <= 0){
			sysMenuDao.delete(parentId);
			return;
		}
		collect.add(parentId);
		sysMenuDao.batchDel(collect);
	}
}