package com.pqs9000.controller.responsibility;

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.controller.area.MonitorScopeController;
import com.pqs9000.pojo.responsibility.OverLimitMonitor;
import com.pqs9000.service.responsibility.OverLimitMonitorService;
import com.pqs9000.util.ComplexHeadExcel;
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/10/30 10:57
 */

@Controller
@RequestMapping("responsibility")
public class ResponsibilityOverLimitMonitorController {

    // 日志记录
    private static final Logger logger = LoggerFactory.getLogger(ResponsibilityOverLimitMonitorController.class);

    @Autowired
    private OverLimitMonitorService overLimitMonitorService;

    @Autowired
    private JedisManager jedisManager;

    /**
     * 监测点明细展示界面
     */
    @GetMapping("overlimitmonitor")
    public String overlimitmonitor() {
        return "business/responsibility/overLimitMonitor";
    }

    /**
     * 监测点稳态超标数据
     * ps：获取到的监测点数据会有重复的，因为有的监测点几个地区都有使用，统计的时候，有区域的条件，所以没有去重！
     */
    @PostMapping("/overLimitMonitorData")
    @ResponseBody
    public HttpResult overlimitmonitorData(String startTime, String endTime, String tigName, String area, String vollevel,boolean statFlag){
        HttpResult result;
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "overLimitMonitorData");
        if (!datePojo.getValidity()) {
            result = PubUtils.assignmentResult(null, 500, "校验前台传递时间参数报错");
            return result;
        }
        List<OverLimitMonitor> monitorList;
        try {
            monitorList = overLimitMonitorService.getOverLimitMonitorData(startTime,endTime,tigName,area,vollevel,statFlag);
            result = PubUtils.assignmentResult(monitorList, 200, "获取监测点稳态超标数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取监测点信息数据为异常,异常为：" + e.getMessage());
            result = PubUtils.assignmentResult(null, 500, "获取监测点稳态超标数据异常");
        }
        return result;
    }

    /**
     * 导出功能
     * @param response
     * @param request
     */
    @PostMapping("/exportMonitorExcel")
    @ResponseBody
    public void export(HttpServletResponse response, HttpServletRequest request,String startTime,String endTime){
        startTime = startTime.replaceAll("-","");
        endTime = endTime.replaceAll("-","");
        String re = jedisManager.getValueByKey(RedisDB.REPORTID,RedisDB.MONITORREPORTNAME);
        List<OverLimitMonitor> list = JSON.parseArray(re,OverLimitMonitor.class);
        ComplexHeadExcel complexHeadExcel = new ComplexHeadExcel();
        complexHeadExcel.exportMonitorReport(response,request,list,startTime,endTime);
    }

    /**
     * 手动计算功能
     */
    @PostMapping("/manualCalculation")
    @ResponseBody
    public HttpResult manualCalculation(String startTime, String endTime){
        HttpResult result;
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "manualCalculation");
        if (!datePojo.getValidity()) {
            result = PubUtils.assignmentResult(null, 500, "校验前台传递时间参数报错");
            return result;
        }
        try {
            overLimitMonitorService.manualCalculation(startTime,endTime);
            result = PubUtils.assignmentResult(null, 200, "计算每日稳态超标详情数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("计算每日稳态超标详情数据异常,异常为：" + e.getMessage());
            result = PubUtils.assignmentResult(null, 500, "计算每日稳态超标详情数据异常");
        }
        return result;
    }
}
