package com.ssm.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.ssm.model.system.SysMenu;
import com.ssm.model.system.SysRole;
import com.ssm.service.system.SysRoleService;

public class SpringInitConfig implements ApplicationContextAware {
	
	@Autowired
	private SysRoleService sysRoleService;
	
	public static Map<String, SysRole> roleMap = new HashMap<String, SysRole>();
	public static Map<String, List<SysMenu>> menuMap  = new HashMap<String, List<SysMenu>>();
	
	
	/**
	 * 初始化  角色 和菜单的对象关系
	 * 将 关系 存到 servletContext
	 * 考虑到servletContext 修改 数据不方便，
	 * 改成 Map
	 */
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {

		List<SysRole> roleList = sysRoleService.selectRoleAndMenuList();
		if( !roleList.isEmpty() ) {
			for(SysRole role : roleList) {
				roleMap.put(role.getRoleId().toString(), role);
				menuMap.put(role.getRoleId().toString(), role.getMenuList());
			}
			
		}
		//下面代码 可以 注释
//		for(String key : menuMap.keySet() ){
//			System.out.println( "==================================================" );
//			SysRole role = roleMap.get(key);
//			System.out.print( "角色 :"+ role.getRoleName()  +" 菜单>>>");
//			List<SysMenu> menuList = menuMap.get(key);
//			if(!menuList.isEmpty()) {
//				for(SysMenu menu : menuList) {
//					System.out.println(  menu.getMenucnname() );
//				}
//			}
//		}
	}
	
	
}
