package com.pqs9000.controller.responsibility;

import cn.hutool.json.JSONArray;
import com.alibaba.fastjson.JSON;
import com.njcn.pojo.commons.DatePojo;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.pojo.commons.RedisDB;
import com.njcn.utils.PubUtils;
import com.njcn.utils.redis.JedisManager;
import com.pqs9000.pojo.responsibility.AreaOverNoraml;
import com.pqs9000.pojo.responsibility.OverLimitMonitor;
import com.pqs9000.service.responsibility.AreaOverNoramlService;
import com.pqs9000.util.ComplexHeadExcel;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xuyang
 * @date 2020/9/18 13:55
 * 区域稳态指标超标分类
 */
@Controller
@RequestMapping("responsibility")
public class ResponsibilityAreaOverNormalController {

    private static final Logger logger = LoggerFactory.getLogger(ResponsibilityAreaOverNormalController.class);

    @Autowired
    private AreaOverNoramlService areaOverNoramlService;

    @Autowired
    private JedisManager jedisManager;

    /**
     * 区域稳态指标超标分类统计展示界面
     */
    @GetMapping("areaovernoraml")
    public String responsibilityList() {
        return "business/responsibility/areaOverLimit";
    }


    @PostMapping("/overLimitData")
    @ResponseBody
    public HttpResult overLimit(String startTime, String endTime, String type, int statistic, boolean statFlag, Integer monitorNature){
        HttpResult result;
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "overLimitData");
        if (!datePojo.getValidity()) {
            result = PubUtils.assignmentResult(null, 500, "校验前台传递时间参数报错");
            return result;
        }
        List<AreaOverNoraml> list;
        try {
            list = areaOverNoramlService.getOverLimitData(startTime,endTime,type,statistic,statFlag,monitorNature);
            result = PubUtils.assignmentResult(list, 200, "获取区域稳态指标超标成功");
        } catch (Exception e) {
            result = PubUtils.assignmentResult(null, 500, "获取区域稳态指标超标异常");
        }
        return result;
    }


    @PostMapping("/exportAreaExcel")
    @ResponseBody
    public void export(HttpServletResponse response, HttpServletRequest request,String statistic,String startTime,String endTime){
        startTime = startTime.replaceAll("-","");
        endTime = endTime.replaceAll("-","");
        String re = jedisManager.getValueByKey(RedisDB.REPORTID,RedisDB.AREAREPORTNAME);
        List<AreaOverNoraml> list = JSON.parseArray(re,AreaOverNoraml.class);
        ComplexHeadExcel complexHeadExcel = new ComplexHeadExcel();
        complexHeadExcel.exportAreaReport(response,request,list,startTime,endTime,statistic);
    }
}
