package test;

import cn.com.countrygarden.bi.main.repository.ManageTopicRepository;
import cn.com.countrygarden.bi.main.repository.ResourceCommNumRepository;
import cn.com.countrygarden.bi.main.repository.ResourceRepository;
import cn.com.countrygarden.bi.main.repository.RoomStateRepository;
import cn.com.countrygarden.bi.main.util.RedisUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.smartframework.common.utils.ExDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import redis.clients.jedis.Jedis;

import java.io.IOException;
import java.util.*;
@Deprecated
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/spring/applicationContext.xml")
public class RedisTest {

	private static final String REDIS_URL = "localhost";
	private static final int REDIS_PORT = 6379;
	private static final String REDIS_PASSWORD = "bgy2017@";
	private Jedis jedis;

	private String hkeyPrefix = "bgywybi-";

	/* 所需要的数据库操作成员变量 */
	@Autowired
	private ResourceRepository resourceRepository;
	@Autowired
	private ResourceCommNumRepository resourceCommNumRepository;
	@Autowired
	private ManageTopicRepository manageTopicRepository;
	@Autowired
	private RoomStateRepository roomStateRepository;

	public final static ObjectMapper mapper = new ObjectMapper();

	public RedisTest() {
		jedis = new Jedis(REDIS_URL, REDIS_PORT);
		// jedis.auth(REDIS_PASSWORD);
	}

	public void testRedis() {
		Map<String, String> resultMap = jedis.hgetAll("getmapdatalist");
		System.out.println("result:" + resultMap);
	}

	/**
	 * 获取泛型的Collection Type
	 * 
	 * @param collectionClass
	 *            泛型的Collection
	 * @param elementClasses
	 *            元素类
	 * @return JavaType Java类型
	 * @since 1.0
	 */
	public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
		return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
	}

	/**
	 * 更新redis中的数据
	 * 
	 * @return
	 */
	public boolean updateRedisData() {
		boolean updateFlag = false;
		String hkeySuffix = ExDateUtils.format(new Date(), "yyyyMMdd");
		String HKEY = hkeyPrefix + hkeySuffix;
		// System.out.println("hkeysuffix--->" + hkeySuffix + "-------hkey---->"
		// + HKEY);

		/* mapdata */
		List<Map<String, Object>> mapDataList = resourceCommNumRepository.queryProNumAndBuildArea(null);
		List<Map<String, String>> mapDataListString = new ArrayList<Map<String, String>>();
		for (int i = 0; i < mapDataList.size(); i++) {
			Map<String, Object> map = mapDataList.get(i);
			Iterator<String> iterator = map.keySet().iterator();
			Map<String, String> newmap = new HashMap<String, String>();
			while (iterator.hasNext()) {
				String key = iterator.next();
				newmap.put(key, "" + map.get(key));
			}
			mapDataListString.add(newmap);
		}
		System.out.println("newmapdatalist---->" + mapDataListString.toString());

		String jsonString = JSONObject.toJSONString(mapDataListString);
		RedisUtils.hset(HKEY, "getmapdatalist", jsonString);
		String getmapdatalistJson = RedisUtils.hget(HKEY, "getmapdatalist");
		System.out.println("getmapdatalistJson--->" + getmapdatalistJson);
		/* 将JSON文件转换成List */
		JavaType javaType = getCollectionType(ArrayList.class, Map.class);
		try {
			List<Map<String, Object>> lst = (List<Map<String, Object>>) mapper.readValue(jsonString, javaType);
			System.out.println("list----->" + lst.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		// ========
		String getcommondatalistJson = RedisUtils.hget(HKEY, "getcommondatalist");
		JavaType javaType1 = getCollectionType(Map.class, String.class, String.class);
		try {
			Map<String, Object> mapstr = (Map<String, Object>) mapper.readValue(getcommondatalistJson, javaType1);
			System.out.println("mapstr----->" + mapstr.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		Map<String, Object> commonDataList = getCommonDataList();
		String jsonString2 = JSON.toJSONString(commonDataList);
		System.out.println("jsonstring2:--->" + jsonString2);

		/* 设置超时时间 */
		RedisUtils.expire(HKEY, 60 * 60 * 24);// 设置大KEY的有效时间，第二个参数是秒， 60*60*24

		return updateFlag;
	}

	@Test
	public void testupdateRedisData() {
		updateRedisData();
	}

	public static String mapToJson(Map<String, String> map) {
		Set<String> keys = map.keySet();
		String key = "";
		String value = "";
		StringBuffer jsonBuffer = new StringBuffer();
		jsonBuffer.append("{");
		for (Iterator<String> it = keys.iterator(); it.hasNext();) {
			key = (String) it.next();
			value = map.get(key);
			jsonBuffer.append(key + ":" + "\"" + value + "\"");
			if (it.hasNext()) {
				jsonBuffer.append(",");
			}
		}
		jsonBuffer.append("}");
		return jsonBuffer.toString();
	}

	/**
	 * 获取基础数据
	 * 
	 * @return
	 */
	private Map<String, Object> getCommonDataList() {
		// 首先获取资源主题的基础数据
		List<Map<String, Object>> resourceDataList = resourceRepository.queryResourceData(null);
		List<Map<String, Object>> projectInfo = resourceRepository.queryProjectInfo(null);

		List<Map<String, Object>> manageTopicCommDataList = manageTopicRepository.queryManageTopicCommData(null);
		Map<String, Object> resourceDataMap = null;
		Map<String, Object> manageTopicCommDataMap = null;
		if (resourceDataList != null && resourceDataList.size() > 0) {
			resourceDataMap = resourceDataList.get(0);
			if (projectInfo != null && projectInfo.size() > 0) {
				Map<String, Object> map = projectInfo.get(0);
				resourceDataMap.put("zyzt_xms", map.get("zyzt_xms"));
				resourceDataMap.put("zyzt_byzjxms", map.get("zyzt_byzjxms"));
			}
		}
		if (manageTopicCommDataList != null && manageTopicCommDataList.size() > 0) {
			manageTopicCommDataMap = manageTopicCommDataList.get(0);
		}
		Map<String, Object> resultData = new HashMap<String, Object>();
		resultData.put("zyzt", resourceDataMap);
		resultData.put("jyzt", manageTopicCommDataMap);
		return resultData;
	}

	public static void main(String[] args) {
		String jsonstr = "[{Province=安徽, CommNum=37, BuildArea=10433227.62}, {Province=北京, CommNum=6, BuildArea=null}, {Province=福建, CommNum=26, BuildArea=1634788.11}, {Province=甘肃, CommNum=7, BuildArea=916664.81}, {Province=广东, CommNum=233, BuildArea=49086198.63}, {Province=广西, CommNum=23, BuildArea=1100424.04}, {Province=贵州, CommNum=19, BuildArea=1074101.98}, {Province=海南, CommNum=9, BuildArea=633532.52}, {Province=河北, CommNum=23, BuildArea=736108.12}, {Province=河南, CommNum=37, BuildArea=1363820.94}, {Province=黑龙江, CommNum=2, BuildArea=346208.98}, {Province=湖北, CommNum=46, BuildArea=6649309.58}, {Province=湖南, CommNum=43, BuildArea=5307588.51}, {Province=吉林, CommNum=2, BuildArea=281707.27}, {Province=江苏, CommNum=85, BuildArea=8388176.77}, {Province=江西, CommNum=14, BuildArea=751946.46}, {Province=辽宁, CommNum=9, BuildArea=5041622.65}, {Province=内蒙古, CommNum=4, BuildArea=1675368.69}, {Province=山东, CommNum=12, BuildArea=1682829.04}, {Province=山西, CommNum=6, BuildArea=79675.69}, {Province=陕西, CommNum=6, BuildArea=115592.84}, {Province=上海, CommNum=2, BuildArea=null}, {Province=四川, CommNum=12, BuildArea=1189928.75}, {Province=天津, CommNum=13, BuildArea=1791964.79}, {Province=云南, CommNum=10, BuildArea=175052.40}, {Province=浙江, CommNum=41, BuildArea=1537366.81}, {Province=重庆, CommNum=12, BuildArea=1219061.45}]";
		JSONObject parseObject = JSONObject.parseObject(jsonstr);
		List<Map<String, Object>> resultList = JSONObject.parseObject(jsonstr, List.class);
		for (int i = 0; i < resultList.size(); i++) {
			Map<String, Object> map = resultList.get(i);
			System.out.println("map" + i + "-->" + map.toString());
		}
	}
}
