package com.xintong;

import com.xintong.visualinspection.service.ConstantService;
import com.xintong.visualinspection.service.DepartmentService;
import com.xintong.visualinspection.service.ItemService;
import com.xintong.visualinspection.service.JobService;
import com.xintong.visualinspection.service.UserService;
import com.xintong.visualinspection.service.impl.ConstantServiceImpl;
import com.xintong.visualinspection.service.impl.DepartmentServiceImpl;
import com.xintong.visualinspection.service.impl.ItemServiceImpl;
import com.xintong.visualinspection.service.impl.JobServiceImpl;
import com.xintong.visualinspection.service.impl.UserServiceImpl;
import com.xintong.visualinspection.util.CacheUtil;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

public class SystemInit implements ApplicationListener<ContextRefreshedEvent> {

	ConstantService constantService;
	UserService userService;
	DepartmentService deptService;
	JobService jobService;
	ItemService itemService;
	private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SystemInit.class);
	@Override
	public void onApplicationEvent(ContextRefreshedEvent arg0) {
		
		constantService = arg0.getApplicationContext().getBean(ConstantServiceImpl.class);
		//加载常量信息
		CacheUtil.refreshCodeMap(constantService);
		
//		List<Constant> constantList = constantService.getAll();
//		for(Constant c:constantList){
//			CacheUtil.codeMap.put(c.getCode_flag()+"_"+c.getCode_value(), c);
//		}
//		logger.info("加载用户信息成功，数据数："+CacheUtil.userMap.size());
		
		userService = arg0.getApplicationContext().getBean(UserServiceImpl.class);
		//加载用户信息
		CacheUtil.refreshUserMap(userService);
//		List<User> userList = userService.getAll();
//		for(User user:userList){
//			CacheUtil.userMap.put(new Long(user.getId()), user);
//		}
//		logger.info("加载用户信息成功，数据数："+CacheUtil.userMap.size());
		//加载部门信息
		deptService = arg0.getApplicationContext().getBean(DepartmentServiceImpl.class);
		CacheUtil.refreshDeptMap(deptService);
		CacheUtil.refreshSAList(deptService);
//		List<Organ> deptList = deptService.getAll();
//		for(Organ organ:deptList){
//			CacheUtil.deptMap.put(new Long(organ.getId()), organ);
//		}
//		logger.info("加载部门信息成功，数据数："+CacheUtil.deptMap.size());
		
		jobService = arg0.getApplicationContext().getBean(JobServiceImpl.class);
		CacheUtil.refreshJobMap(jobService);

		itemService = arg0.getApplicationContext().getBean(ItemServiceImpl.class);
		CacheUtil.refreshItemMap(itemService);
		CacheUtil.refreshItemNameMap(itemService);
	}
}
