package cn.com.countrygarden.bi.main.controller;

import cn.com.countrygarden.bi.main.repository.ResourceRepository;
import cn.com.countrygarden.bi.main.util.RedisUtils;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.smartframework.common.utils.ExDateUtils;
import org.smartframework.common.utils.ExRequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

@Controller("takeOverDataController")
@RequestMapping("/api/biapp/data/takeOver")
public class TakeOverDataController {

    private String hkeyPrefix = "bgywybi-";// redis 的大KEY的前缀

    @Autowired
    private ResourceRepository resourceRepository;

    public final static ObjectMapper mapper = new ObjectMapper();

    /**
     * 传入小key ，获取redis 中的值
     *
     * @param field
     * @return
     */
    private String hgetFromRedis(String field) {
        String hkeySuffix = ExDateUtils.format(new Date(), "yyyyMMdd");
        String HKEY = hkeyPrefix + hkeySuffix;
        // String HKEY = hkeyPrefix + "20170623";
        String getmapdatalistJson = RedisUtils.hget(HKEY, field);
        return getmapdatalistJson;
    }

    /**
     * 获取泛型的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);
    }

    /**
     * 查询接管面积
     *
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/distribution", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Object takeOver(HttpServletRequest request) throws IOException {
        Map<String, Object> paramMap = ExRequestUtils.buildParametersMap(request);
        Map<String, Object> resultMap = new HashMap<String, Object>();

        List<Map<String, Object>> list;

        // 从 redis 读取
        String dataJson = hgetFromRedis("queryTakeOverArea");
        if (dataJson != null && !"".equals(dataJson)) {
            JavaType javaType = getCollectionType(ArrayList.class, Map.class);
            list = mapper.readValue(dataJson, javaType);
        } else {
            list = resourceRepository.queryTakeOverArea(paramMap);
        }

        resultMap.put("resultCode", 0);
        resultMap.put("resultMsg", "OK");
        resultMap.put("data", list);

        return resultMap;
    }

    /**
     * 查询各区域接管项目分布
     *
     * @return
     */
    @RequestMapping(value = "/distribution/project", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Object projectDistribution(HttpServletRequest request) throws IOException {
        Map<String, Object> paramMap = ExRequestUtils.buildParametersMap(request);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        String dataJson = hgetFromRedis("queryTakeOverProjectDistribution");
        if (dataJson != null && !"".equals(dataJson)) {
            JavaType javaType = getCollectionType(ArrayList.class, Map.class);
            try {
                data = mapper.readValue(dataJson, javaType);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            data = resourceRepository.queryTakeOverProjectDistribution(paramMap);
        }

        resultMap.put("resultCode", 0);
        resultMap.put("resultMsg", "OK");
        resultMap.put("rows", data);
        return resultMap;
    }
}
