package com.gyee.frame.controller.report;


import com.gyee.frame.common.conf.AjaxStatus;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.model.auto.Misdailydata;
import com.gyee.frame.model.auto.Noscaledata;
import com.gyee.frame.model.auto.Noscwfdata;
import com.gyee.frame.model.auto.Wfpfdata;
import com.gyee.frame.service.report.MisDailyDateService;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.excel.MisDailyData;
import com.gyee.frame.util.excel.MisDailyUtil;
import io.swagger.annotations.Api;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * mis日报数据导入
 */
@CrossOrigin
@RestController
@RequestMapping("/misreport")
@Api(value = "mis报表数据",tags =  "mis报表数据")
public class MisReportController {

    @Resource
    private MisDailyDateService misDailyDateService;

    /**
     * 导入数据
     * @param file
     * @return
     */
    @PostMapping("/import/data")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) {
        if (file.isEmpty()) {
            return AjaxResult.error("导入文件为空！");
        }

        MisDailyUtil util = new MisDailyUtil();
        List<MisDailyData> list = util.importStream(file);
        misDailyDateService.insert(list);
        return AjaxResult.success();
    }
    /**
     * 导入故障数据数据
     * @return
     */
    @PostMapping("/import/datagz")
    @ResponseBody
    public AjaxResult importgz(MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return AjaxResult.error("导入文件为空！");
        }

        MisDailyUtil util = new MisDailyUtil();
        List<Misdailydata> list = util.Stream1(file);
        misDailyDateService.insertgz(list);
        return AjaxResult.success();
    }
    /**
     * 查询功率预测数据
     * @return
     */
    @PostMapping("/import/datagl")
    @ResponseBody
    public AjaxResult importgl(){
        List<Noscwfdata> list = misDailyDateService.query();
        return AjaxResult.successData(200,list);
    }
    /**
     * 写入数据
     * @return
     */
    @Scheduled(cron = "0 0 0 1 * ?")
    @GetMapping("/import/dataxr")
    @ResponseBody
    public AjaxResult writexr(){
        misDailyDateService.gl();
        List<Noscwfdata> write = misDailyDateService.write();
        return AjaxResult.successData(AjaxStatus.success.code,write);
    }
    /**
     * 计算数据
     * @return
     */
    @GetMapping("/caldata")
    public AjaxResult getData(String st, String et){
        Date sDate = DateUtils.parseStrtoDate(st, DateUtils.YYYY_MM_DD);
        Date eDate = DateUtils.parseStrtoDate(et, DateUtils.YYYY_MM_DD);

        List<Object> list = misDailyDateService.getCalData(sDate, eDate);
        return AjaxResult.successData(200, list);
    }


    /**
     * 根据时间查询当前日期数据
     * @param time
     * @return
     */
    @GetMapping("/glyc/data")
    public AjaxResult getGLYCData(String time){
        Date date = DateUtils.parseStrtoDate(time, DateUtils.YYYY_MM_DD);

        Misdailydata data = misDailyDateService.selectItemByDate(date);
        return AjaxResult.successData(200, data);
    }


    /**
     * 保存对象数据
     * @return
     */
    @PostMapping("/save/glyc")
    public AjaxResult saveGLYCData(@RequestBody Misdailydata data){
        String str = DateUtils.format(data.getImportdate(), DateUtils.YYYY_MM_DD_HH_MM_SS);
        data.setImportdate(DateUtils.parseStrtoDate(str, DateUtils.YYYY_MM_DD));
        boolean flag = misDailyDateService.insertOrUpdate(data);

        if (flag)
            return AjaxResult.success();

        return AjaxResult.error("保存失败");
    }


}
