package com.gyee.frame.controller.file;

import com.gyee.frame.common.conf.AjaxStatus;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.common.file.FileZip;
import com.gyee.frame.model.config.FilePathConfig;
import com.gyee.frame.model.excel.LoadrateHourly;
import com.gyee.frame.service.export.LoadrateHourlyService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/loadrate")
public class LoadrateHourlyController {

    @Resource
    private LoadrateHourlyService loadrateHourlyService;

    /**
     * 光伏逆变器负荷率
     */
    @GetMapping("/info")
    public AjaxResult getInfo(@RequestParam("stationid") String stationid,
                              @RequestParam("date") String date,
                              @RequestParam("time") String time){
        List<LoadrateHourly> loadrateHourlies = loadrateHourlyService.readExcel(stationid, date, time);
        if(loadrateHourlies==null){
            return AjaxResult.error("文件不存在！");
        }
        return AjaxResult.successData(AjaxStatus.success.code,loadrateHourlies);
    }
    @GetMapping("/down")
    public AjaxResult getDownInfo(HttpServletResponse response,
                                  @RequestParam(value = "stationid",required = false) String stationid,
                                  @RequestParam("date") String date,
                                  @RequestParam(value = "time",required = false) String time){

        if(time!=null){
            File path = loadrateHourlyService.getPath(date, time);
            if(!path.exists()) return AjaxResult.error("文件不存在！");
            FileZip.download(path.getPath(),response);
            return AjaxResult.success();
        }else{
            String s = FilePathConfig.getDataPath() + "\\光伏逆变器负荷率利用小时计算\\" + date.replaceAll("-", "") + ".zip";
            File zipfile = new File(s);
            List<File> values = loadrateHourlyService.getAllPath(date).entrySet().stream().map(kv -> kv.getValue()).collect(Collectors.toList());
            if(values.size()<=0) return AjaxResult.error("文件不存在！");
            String s1 = FileZip.zipFiles(values, zipfile);
            FileZip.download(s1,response);
            return AjaxResult.success();
        }
    }
}
