package com.pqs9900.controller.webtool;

import com.njcn.pojo.commons.DatePojo;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.utils.PubUtils;
import com.njcn.utils.XssFilterUtil;
import com.pqs9900.service.webtool.CsvDataService;
import com.pqs9900.task.MySchudler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author cdf
 * @Date 2020/6/3 15:00
 **/
@Controller
@RequestMapping(value = "csv")
public class CsvDataController {
    private static final Logger logger = LoggerFactory.getLogger(CsvDataController.class);

    @Autowired
    private CsvDataService csvDataService;
    /**
     * @Author chendaofei
     * @Description 导出监测点详细数据
     * @Date 16:22 2020/4/20
     **/
    @GetMapping(value = "getCsvAsSession")
    @ResponseBody
    public HttpResult getCsvAsSession(@RequestParam("lineid")Integer lineid, @RequestParam("beginTime")String beginTime, @RequestParam("endTime")String endTime, HttpServletResponse response, HttpServletRequest request){
        if(lineid == null || PubUtils.isBlank(beginTime) || PubUtils.isBlank(endTime)){
            return PubUtils.assignmentResult(null,500,"参数不可为空");
        }

        DatePojo datePojo;
        datePojo = PubUtils.validateDate(beginTime, endTime, "getCsvAsSession");
        if (!datePojo.getValidity()) {
            return PubUtils.assignmentResult(null,500,datePojo.getMsg());
        }

        try {
            String res = csvDataService.getCsvAsSession(lineid,datePojo.getStartTime(),datePojo.getEndTime(),response,request);
            if("数据为空".equals(res)){
                return PubUtils.assignmentResult(null,500,res);
            }
        } catch (IOException e) {
            return PubUtils.assignmentResult(null,500,"未知错误");
        }
        return null;
    }

    /**
     * @Author chendaofei
     * @Description 批量导出csv
     * @Date 11:15 2020/4/21
     **/
    @RequestMapping(value="getCsvBatch", method = RequestMethod.GET)
    @ResponseBody
    public HttpResult getCsvBatch(@RequestParam("list") List<Integer> list, @RequestParam("beginTime")String beginTime, @RequestParam("endTime")String endTime, HttpServletResponse response){
        HttpResult httpResult = null;
        if(list==null || list.size()==0  || PubUtils.isBlank(beginTime) || PubUtils.isBlank(endTime)){
            PubUtils.assignmentResult(null,500,"参数不可为空");
        }

        DatePojo datePojo;
        datePojo = PubUtils.validateDate(beginTime, endTime, "getCsvBatch");
        if (!datePojo.getValidity()) {
            return PubUtils.assignmentResult(null,500,datePojo.getMsg());
        }


        try {
            String res = csvDataService.getCsvBatch(list,datePojo.getStartTime(),datePojo.getEndTime(),response);
            if("存在数据".equals(res)){
               httpResult = PubUtils.assignmentResult(null,200,res);
            }else {
                httpResult = PubUtils.assignmentResult(null,500,res);
            }
        } catch (IOException e) {
            logger.error("导出csv发生异常，异常是"+e.getMessage());
        }
        return httpResult;
    }

    /**
     * 进度条刷新
     */
    @GetMapping(value = "/flushProgress2")
    @ResponseBody
    public HttpResult flushProgress(HttpServletRequest request,HttpServletResponse response)  {
        Map<String,Object> map = csvDataService.flushProgress(request,response);
        return PubUtils.assignmentResult(map,200,"获取成功");
    }


    /**
     * @Author chendaofei
     * @Description 分页获取所有监测信息
     * @Date 15:07 2020/4/8
     **/
    @GetMapping(value = "/getAllInfo")
    @ResponseBody
    public HttpResult getAllInfo(@RequestParam("lineid") Integer lineid, @RequestParam("page")Integer page,@RequestParam("pageSize")Integer pageSize,@RequestParam("beginTime")String beginTime, @RequestParam("endTime")String endTime){
        if(lineid == null || PubUtils.isBlank(beginTime) || PubUtils.isBlank(endTime) || page ==null || pageSize ==null){
            return PubUtils.assignmentResult(null,500,"参数不可为空");
        }

        beginTime = XssFilterUtil.dealString(beginTime);
        endTime = XssFilterUtil.dealString(endTime);

        DatePojo datePojo;
        datePojo = PubUtils.validateDate(beginTime, endTime, "getAllInfo");
        if (!datePojo.getValidity()) {
            return PubUtils.assignmentResult(null,500,datePojo.getMsg());
        }

       Map<String,Object> map = csvDataService.getAllInfo(lineid,page,pageSize,datePojo.getStartTime(),datePojo.getEndTime());
       if(map!=null){
          return PubUtils.assignmentResult(map,200,"获取成功");
       }else {
           return PubUtils.assignmentResult(null,500,"获取失败");
       }
    }


    /*数据完成性手动操作*/
    @GetMapping("startHistoryTask")
    @ResponseBody
    public HttpResult startHistoryTask(@RequestParam("lineNo")Integer lineNo, @RequestParam("beginTime")String beginTime, @RequestParam("endTime")String endTime){
        if(lineNo == null || PubUtils.isBlank(beginTime) || PubUtils.isBlank(endTime)){
            return PubUtils.assignmentResult(null,500,"参数不可为空");
        }

        DatePojo datePojo;
        datePojo = PubUtils.validateDate(beginTime, endTime, "startHistoryTask");
        if (!datePojo.getValidity()) {
            return PubUtils.assignmentResult(null,500,datePojo.getMsg());
        }

        HttpResult httpResult = null;
        String res = csvDataService.startHistoryTask(lineNo,datePojo.getStartTime(),datePojo.getEndTime());
        if("执行成功".equals(res)){
            httpResult = PubUtils.assignmentResult(null,200,res);
        }else {
            httpResult = PubUtils.assignmentResult(null,500,res);
        }
        return httpResult;
    }


    /**
     * 导出暂态波形数据
     * @param type 1.瞬时波形 2.rms波形
     *
     */
    @RequestMapping(value = "getAllWavedataToCsv", method = RequestMethod.GET)
    @ResponseBody
    public HttpResult getAllWavedataToCsv(String startTime,String endTime,Integer type) {
        HttpResult result;

        String waveData;
        try {
            waveData = csvDataService.getAllWavedataToCsv(startTime,endTime, 1,type);
            if("成功".equals(waveData)){
                result = PubUtils.assignmentResult(waveData, 200, "导出时间段波形数据成功");
            }else {
                result = PubUtils.assignmentResult(waveData, 500, waveData);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = PubUtils.assignmentResult(null, 500, "因为异常导致获取波形数据失败");
        }

        return result;
    }

    @GetMapping("downloadDataZip")
    public void downloadDataZip(HttpServletResponse response){
        csvDataService.downloadDataZip(response);
    }

}
