package com.saye.hrs.service.system.impl;

import com.saye.hrs.commons.string.StringDUtil;
import com.saye.hrs.mapper.system.MenuMapper;
import com.saye.hrs.mapper.system.MenuRoleMapper;
import com.saye.hrs.model.MenuRole;
import com.saye.hrs.model.ResourceInfo;
import com.saye.hrs.service.system.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService{

	@Autowired
	private MenuMapper menuMapper;

	@Autowired
	private MenuRoleMapper menuRoleMapper;

	@Override
	public ResourceInfo getMenuByRole(HashMap<Object, Object> map) throws Exception{
		
		ResourceInfo resourceInfo = new ResourceInfo();
			
		List<HashMap<Object, Object>> list = (List<HashMap<Object, Object>>) this.menuMapper.findMenuByRole(map);
		//根据level将数据添加到对象中
		for (HashMap<Object, Object> hashMap : list) {
			String parentid = StringDUtil.changeNullToEmpty(hashMap.get("PARENTID"));
			//生成主节点
			 if(resourceInfo.getId() == null){
				 resourceInfo = this.createResourceInfo(hashMap);
				 continue;
			 }
			 else if(resourceInfo.getId().equals(parentid)){
				 //当为1级子节点时，parentid 等于 主节点的ID
				 ResourceInfo nodeInfo = this.createResourceInfo(hashMap);
				 resourceInfo.getChildren().add(nodeInfo);
			}
			 else
				 this.createReourceChildrenNode(hashMap,resourceInfo.getChildren());
		}
		
		
		return resourceInfo;
	}
	
	private ResourceInfo createResourceInfo(HashMap<Object, Object> hashMap){
		ResourceInfo nodeInfo = new ResourceInfo();
		nodeInfo.setId(StringDUtil.changeNullToEmpty(hashMap.get("ID")));
		nodeInfo.setSysid(StringDUtil.changeNullToEmpty(hashMap.get("sysid")));
		nodeInfo.setTitle(StringDUtil.changeNullToEmpty(hashMap.get("TEXT")));
		nodeInfo.setAttributes(hashMap);
		return nodeInfo;
	}
	
	private void createReourceChildrenNode(HashMap<Object, Object> hashMap,ArrayList<ResourceInfo> resourceInfoList){
		if(null == resourceInfoList || resourceInfoList.size() == 0) {
			return;
		}
		/*
		取第一个节点，判断节点的level是否与当前节点的父节点的level相同.
			如果相同则当前节点肯定为该级别下某个节点的子节点。
			如果不同则继续判断前节点的所有子节点。
		*/
		ResourceInfo resourceInfo = resourceInfoList.get(0);
		int parentlevel = Integer.valueOf(hashMap.get("RESLEVEL").toString())  - 1;//获取当前节点的父节点level
		String parentid = StringDUtil.changeNullToEmpty(hashMap.get("PARENTID"));
		int resourceInfoLevel = Integer.valueOf(resourceInfo.getAttributes().get("RESLEVEL").toString());
		if(resourceInfoLevel == parentlevel){
			//判断list中节点的ID是否与当前节点的parentid相同，如果相同则问该节点的子节点
			for (ResourceInfo parentNode : resourceInfoList)
				if (parentNode.getId().equals(parentid)) {
					ResourceInfo nodeInfo = this.createResourceInfo(hashMap);
					parentNode.getChildren().add(nodeInfo);
					return;//添加完成后跳出循环
				}
			
		}
		else{
			for (ResourceInfo parentNode : resourceInfoList)
				this.createReourceChildrenNode(hashMap, parentNode.getChildren());
		}
	}

	/**
	 * @description 根据角色id获取权限数据
	 * @author thuang       
	 * @created 2019年11月12日 上午11:20:47
	 */
	@Override
	public List<MenuRole> getMenuIdByRoleId(HashMap<Object, Object> map) {
		List<MenuRole> list=menuRoleMapper.getMenuRolesByRoleId(map);
		
		return list;
	}
}
