package com.cloudcross.ssp.listener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import com.cloudcross.ssp.model.Place;
import com.cloudcross.ssp.model.PlaceClass;
import com.cloudcross.ssp.model.PlaceClassInformation;
import com.cloudcross.ssp.service.IPlaceClassInformationService;
import com.cloudcross.ssp.service.IPlaceClassService;
import com.cloudcross.ssp.service.impl.LocationService;
import com.cloudcross.ssp.service.impl.PlaceService;

/**
 * 让项目一启动就加载地域，数聚场景和场景的信息，放进内存里
 * 
 * @author chenyou
 *
 */
@Service
public class InitDataListener implements
		ApplicationListener<ContextRefreshedEvent> {
	// private static final Logger LOGGER =
	// Logger.getLogger(PointRateListener.class);

	@Autowired
	LocationService locationService;
	@Autowired
	PlaceService placeService;
	@Autowired
	IPlaceClassInformationService placeClassInformationService;
	@Autowired
	IPlaceClassService placeClassService;

	/**
	 * 启动加载执行
	 */
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		// 这个防止被加载两次
		if (event.getApplicationContext().getParent() == null) {
			// 加载地域信息 ，其中value的值为"cn,cn_city"，key的值为location
			//注：当location字符串的后两位以00结尾时这时的value值只有cn，因为以00结尾的要么是cn和cnCity一样要么是其他
			List<Map<String, String>> locationList = locationService
					.findAllLocationAndCnAndCnCity();
			for (Map<String, String> map : locationList) {
				if(map.get("location").endsWith("00")) {
					InitData.locationMap.put(
							map.get("location"),
							map.get("cn"));
				} else {
				InitData.locationMap.put(
						map.get("location"),
						map.get("cn") + "," + map.get("cnCity"));
				}
			}

			// 加载数聚子场景信息，key值为数聚子场景的id,value值为数聚子场景名称
			List<Place> placeList = placeService.findAll();
			for (int i = 0; i < placeList.size(); i++) {
				InitData.placeMap.put(placeList.get(i).getId().intValue(),
						placeList.get(i).getName());
			}

			// 加载数聚场景信息，key值为数聚场景id，value值为数聚场景名称
			List<PlaceClassInformation> placeClassInformationList = placeClassInformationService
					.findAll();
			for (int i = 0; i < placeClassInformationList.size(); i++) {
				InitData.placeClassInfoMap
						.put(placeClassInformationList.get(i).getId()
								.intValue(), placeClassInformationList.get(i)
								.getName());
			}

			// 加载数聚场景和数聚子场景的对应关系key值为数聚场景的id,value值为该数聚场景下的数聚子场景的id的list，如果该数聚场景下的数聚子场景没有则这个list的大小为0
			List<PlaceClass> placeClassList = placeClassService.findAll();
			List<Long> list = null;
			for (int i = 0; i < placeClassList.size(); i++) {
				if (null == InitData.placeClassMap.get(placeClassList.get(i)
						.getClassId().intValue())) {
					list = new ArrayList<Long>();
					InitData.placeClassMap.put(placeClassList.get(i)
							.getClassId().intValue(), list);
				} else {
					list = InitData.placeClassMap.get(placeClassList.get(i)
							.getClassId().intValue());
				}

				list.add(placeClassList.get(i).getPlaceId());
			}
		}
	}

}
