package com.ruoyi.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.report.vo.ClassStatisticsInfoVo;
import com.ruoyi.system.domain.BladeRegion;
import com.ruoyi.system.service.IBladeRegionService;
import com.ruoyi.system.service.impl.BladeRegionServiceImpl;

public class BladeRegionUtils {

	private static Map<String, BladeRegion>localReginCache=new HashMap<String, BladeRegion>();
	
	private static List<String> allCodes;
	
	private static final Logger logger=LoggerFactory.getLogger(BladeRegionUtils.class);
	
	private static IBladeRegionService _service;
	
	private static IBladeRegionService getService() {
		if(_service==null) {
			_service=SpringUtils.getBean(IBladeRegionService.class);
		}
		return _service;
	}
	
	
	private static class SortedKeyData {
		private String key;
		private Integer sort;
	}
	
	public static List<String> sortedKey(Set<String> areaCodes) {
		logger.debug("开始执行sortedKey");
		long begin = System.currentTimeMillis();
		List<String> list = new ArrayList<String>();
		if (areaCodes == null || areaCodes.isEmpty()) {
			return list;
		}
		List<SortedKeyData> dataList = new ArrayList<SortedKeyData>();
		for (String area : areaCodes) {
			BladeRegion br = BladeRegionUtils.selectByCode(area);
			SortedKeyData sd = new SortedKeyData();
			sd.key = area;
			if (br != null) {
				sd.sort = br.getSort();
			} else {
				sd.sort = Integer.MAX_VALUE;
			}
			dataList.add(sd);
		}

//		list.addAll(areaMap.keySet());
		Collections.sort(dataList, new Comparator<SortedKeyData>() {
			@Override
			public int compare(SortedKeyData br1, SortedKeyData br2) {
				// TODO Auto-generated method stub
//				BladeRegion br1=BladeRegionUtils.selectByCode(o1);
//				BladeRegion br2=BladeRegionUtils.selectByCode(o2);

				if (br1 == null && br2 == null) {
					return 0;
				} else if (br1 == null) {
					return -1;
				} else if (br2 == null) {
					return 1;
				} else {
					return br1.sort - br2.sort;
				}
//				long total1 = ViewStatisticsInfoVo.getTotal(areaMap.get(o1));
//				long total2 = ViewStatisticsInfoVo.getTotal(areaMap.get(o2));
//				return (int)(total2 - total1);
			}

		});
		for (SortedKeyData d : dataList) {
			list.add(d.key);
		}
		logger.debug("sortedKey用时:" + (System.currentTimeMillis() - begin));
		return list;

	}

	public static String getDistrictShortName(String code) {
		BladeRegion br=selectByCode(code);
		if(br!=null) {
			return br.getDistrictShortName();
		}
		return null;
	}
	
	public static BladeRegion selectByCode(String code) {

		if (StringUtils.isBlank(code) ) {
			return null;
		}
		
		if (BladeRegionServiceImpl.useLocalCache) {
			
			if(localReginCache.isEmpty()) {
				localReginCache.putAll(getService().selectAll());
			}
			
			if(localReginCache.get(code)!=null) {
				return localReginCache.get(code);
			}
		}
		
//		BladeRegion qo = new BladeRegion();
//		qo.setCode(code);
//		List<BladeRegion> list = selectList(qo);
//		if (list != null && !list.isEmpty()) {
//			BladeRegion bladeRegion = list.get(0);
//			localReginCache.put(code, bladeRegion);
//			return bladeRegion;
//		}
		return null;
		
	
	}
	
	
	public static void setNotThisArea(List<Map> mapList, String city_code) {
		long time=System.currentTimeMillis();
		logger.debug("开始判断非本区划,city_code:"+city_code+",总数:"+mapList);
		if(allCodes==null ) {
			allCodes=new ArrayList<String>();
			allCodes.addAll(getService().getAllAreaCode());
		}
		Map<String,Boolean>alreadyMap=new HashMap<String,Boolean>();
		for (Map map : mapList) {
			String area_code = getStr(map, "area_code");
			if (StringUtils.isBlank(city_code)) {
				city_code = getStr(map, "city_code");
			}

			boolean res=isNotThisArea(city_code, area_code,allCodes,alreadyMap);
			map.put("not_this_area", res);
		}
		logger.debug("判断非本区划完成,用时"+(System.currentTimeMillis()-time));
		
	}
	
	
	public static Boolean isNotThisArea(String cityCode, String areaCode, List<String> allCodeList,
			Map<String, Boolean> alreadyMap) {
		if (StringUtils.isBlank(areaCode)) {
			return true;
		}
		Boolean rrr = alreadyMap.get(areaCode);
		if (rrr != null) {
			return rrr;
		}

		areaCode = areaCode.trim();
		if (StringUtils.isNotBlank(cityCode) && !areaCode.startsWith(cityCode)) {
			return true;
		}

		Boolean res = false;
//		String key = "Region:isNotThisArea:" + cityCode + ":areaCode:" + areaCode;

//		logger.info("判断非本区划:" + cityCode + "," + areaCode);
//		List<String>allCodeList=getAllArea();

//		logger.info("开始循环判断非本区划:" + cityCode + "," + areaCode);
		if (!allCodeList.contains(areaCode)) {
			res = true;
		}
//		logger.info("判断非本区划完成:" + cityCode + "," + areaCode);

//		if (useLocalCache) {
//			localBooleanVar.get().put(key, res);
//		}
//		redisCache.setCacheObject(key, res);
		return res;
	}
	
	private static String getStr(Map map, String key) {
		if (map == null || key == null || !map.containsKey(key)) {
			return "";
		}
		return map.get(key).toString().trim();
	}

	
	
	

}
