package com.gyee.frame.controller.export;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.gyee.frame.common.conf.AjaxStatus;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.common.feign.RemoteServiceBuilder;
import com.gyee.frame.model.auto.EarlywarningAnalysisPush;
import com.gyee.frame.model.excel.AlarmStat;
import com.gyee.frame.service.WindpowerstationService;
import com.gyee.frame.service.export.EarlywarnAnalysisPushService;
import com.gyee.frame.service.export.GoldenService;
import com.gyee.frame.util.excel.FileUtils;
import com.gyee.frame.util.excel.ZipUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.lang.reflect.Type;
import java.util.*;

@RestController
@RequestMapping("/export")
@Api(value = "golden数据导出功能", tags = "golden数据导出功能")
public class GoldenController {

    @Autowired
    private GoldenService goldenService;
    @Autowired
    private WindpowerstationService stationService;
    @Autowired
    private EarlywarnAnalysisPushService earlywarnAnalysisPushService;

    @Autowired
    private RemoteServiceBuilder remoteServiceBuilder;
    @Autowired
    FileUtils fileUtils;

    private AjaxResult earlyWarning;
    private String startdate;


    /**
     * 查询所有的场站
     *
     * @return
     */
    @GetMapping("/databases")
    @ApiOperation(value = "查询风电场", notes = "查询风电场")
    public AjaxResult getDatabases() {
        List<Map<String, String>> list = stationService.findStationAll();
        return AjaxResult.successData(AjaxStatus.success.code, list);
    }


    /**
     * 通过模板导出单个风机数据
     *
     * @param station  场站
     * @param wtId     风机ID
     * @param id       模板ID
     * @param startTs  开始时间
     * @param endTs    结束时间
     * @param interval 时间间隔
     * @return
     */
//    @GetMapping("/history/snap")
//    public AjaxResult getHistory(
//            @RequestParam(value = "station") String station,
//            @RequestParam(value = "wtId", required = false) String wtId,
//            @RequestParam(value = "templateId") Integer id,
//            @RequestParam(value = "startTs") Long startTs,
//            @RequestParam(value = "endTs") Long endTs,
//            @RequestParam(value = "interval", required = false) Optional<Integer> interval) {
//
//        int val = interval.isPresent() ? interval.get() : 1800;
//
//        List<Object> list = goldenService.getHistoryDataSingle(station, wtId, id, startTs, endTs, val);
//        return AjaxResult.successData(AjaxStatus.success.code, list);
//    }

    /**
     * 通过模板导出所有风机数据
     *
     * @param startTs 开始时间
     * @param endTs   结束时间
     * @return
     */
    @GetMapping("/history/all")
    public AjaxResult getHistoryAll(
            @RequestParam(value = "station") String station,
            @RequestParam(value = "startTs") Long startTs,
            @RequestParam(value = "endTs") Long endTs) {

        boolean tag = goldenService.getHistoryDataAll(station, startTs, endTs);
        return AjaxResult.successData(AjaxStatus.success.code, tag);
    }


    /**
     * @param request
     * @return
     */
    @GetMapping("/downloadFile")
    public AjaxResult downloadFile(HttpServletRequest request,
                                   @RequestParam(value = "station") String[] stations,
                                   @RequestParam(value = "templateId") String templateId,
                                   @RequestParam(value = "month") String[] month,
                                   @RequestParam(value = "interval") int[] interval) throws Exception {

        String zipName = "";
        List<File> srcFiles = new ArrayList<>();
        for (int i = 0; i < stations.length; i++) {
            String path = fileUtils.getFilePath(stations[i], templateId, month[i], interval[i]);
            if ("1".equals(templateId)) {
                ZipUtils.toZip(fileUtils.getFilePathNoEx(path), path, true);
                zipName = month[0] + "_单机数据.zip";
            } else {
                zipName = month[0] + "_测风塔数据.zip";
            }
            srcFiles.add(new File(path));
        }
        ZipUtils.toZip(srcFiles, fileUtils.getFilePath2(request,"path", month[0], interval[0]), zipName);
        Map map = new HashMap();
        map.put("path", fileUtils.getFilePath2(request,"url", month[0], interval[0]));
        map.put("name", zipName);

        AjaxResult result = AjaxResult.successData(200, map);
        return result;
    }

    /**
     * 预警分析推
     * @return
     */
    @GetMapping("/earlywarnanalysispush")
    public AjaxResult earlywarnanalysispush() {
        return earlywarnAnalysisPushService.getEarlywarnanalysispush(startdate,earlyWarning);
    }

    /**
     * 预警分析推送间隔天数设置
     * @param jgts
     * @return
     */
    @GetMapping("/jgts/{jgts}")
    public AjaxResult jgts(@PathVariable int jgts) {
        EarlywarnAnalysisPushService.setJgts(jgts);
        return AjaxResult.success();
    }


    @GetMapping(value = "/count/query/new2")
    public AjaxResult querynew2(
            @RequestParam String stationid,
            @RequestParam String startdate,
            @RequestParam String enddate
    ) {
        this.startdate = startdate;
        earlyWarning = remoteServiceBuilder.earlyService().getEarlyWarning(stationid, startdate, enddate);
        return earlyWarning;
    }

    @GetMapping("/getearlywarnanalysis")
    public AjaxResult getEarlywarnanalysis(
            @RequestParam String stationid,
            @RequestParam String startdate,
            @RequestParam String enddate
    ) {
        return AjaxResult.successData(200, earlywarnAnalysisPushService.getEarlywarnanalysis(stationid, startdate, enddate));
    }

    @PostMapping("/saveearlywarnanalysis")
    public AjaxResult saveEarlywarnanalysis(List<EarlywarningAnalysisPush> analysisPushes){
        if(earlywarnAnalysisPushService.saveEarlywarnanalysis(analysisPushes)>0){
            return AjaxResult.success();
        }
        return AjaxResult.error("保存0条！");
    }

}