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

import cn.com.countrygarden.bi.main.core.ResultGenerator;
import cn.com.countrygarden.bi.main.repository.ManageTopicRepository;
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 org.springframework.web.bind.annotation.ResponseBody;

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

@Controller("parkingDataController")
@RequestMapping("/api/biapp/data/parking")
public class ParkingDataController {

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

    @Autowired
    private ManageTopicRepository manageTopicRepository;

    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 = "/overview", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public @ResponseBody
    Object overview(HttpServletRequest request) throws IOException {
        Map<String, Object> paramMap = ExRequestUtils.buildParametersMap(request);
        List<Map<String, Object>> parkCarMoneyList = new ArrayList<Map<String, Object>>();
        String queryParkCarMoneyJson = hgetFromRedis("queryParkCarMoney");
        if (queryParkCarMoneyJson != null && !"".equals(queryParkCarMoneyJson)) {
            /* 将JSON文件转换成List */
            JavaType javaType = getCollectionType(ArrayList.class, Map.class);
            try {
                parkCarMoneyList = mapper.readValue(queryParkCarMoneyJson, javaType);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            parkCarMoneyList = manageTopicRepository.queryParkCarMoney(paramMap);
        }

        return ResultGenerator.genSuccessResult(parkCarMoneyList);
    }

}
