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.pojo.common.PowerQualityData;
import com.pqs9000.pojo.responsibility.OverLimitMonitor;
import com.pqs9000.pojo.responsibility.WarningSubstation;
import com.pqs9000.service.responsibility.WarningSubstationService;
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/9 19:03
 */

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

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

    @Autowired
    private WarningSubstationService warningSubstationService;

    @Autowired
    private JedisManager jedisManager;

    /**
     * 告警变电站明细展示界面
     */
    @GetMapping("warningsubstation")
    public String warningSubstationList() {
        return "business/responsibility/substationWarning";
    }


    /**
     * 请求变电站信息
     */
    @PostMapping("/warningSubstationData")
    @ResponseBody
    public HttpResult substationData(String startTime, String endTime, String tigName, String area, String vollevel,boolean statFlag){
        HttpResult result;
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "warningSubstationData");
        if (!datePojo.getValidity()) {
            result = PubUtils.assignmentResult(null, 500, "校验前台传递时间参数报错");
            return result;
        }
        List<WarningSubstation> subList = new ArrayList<WarningSubstation>();
        try {
            subList = warningSubstationService.getWarningSubstationData(startTime,endTime,tigName,area,vollevel,statFlag);
            result = PubUtils.assignmentResult(subList, 200, "获取告警变电站明细成功");
        } catch (Exception e) {
            result = PubUtils.assignmentResult(null, 500, "获取告警变电站明细异常");
        }
        return result;
    }

    /**
     * 导出功能
     * @param response
     * @param request
     */
    @PostMapping("/exportSubExcel")
    @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.SUBREPORTNAME);
        List<WarningSubstation> list = JSON.parseArray(re,WarningSubstation.class);
        ComplexHeadExcel complexHeadExcel = new ComplexHeadExcel();
        complexHeadExcel.exportSubReport(response,request,list,startTime,endTime);

    }
}
