package com.ruoyi.predict.api;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.common.utils.DateTimeUtil;
import com.ruoyi.predict.domain.DataFormat;
import com.ruoyi.predict.domain.WfYcHistoryData1;
import com.ruoyi.predict.domain.WfYcHistoryData2;
import com.ruoyi.predict.domain.Windplantproject;
import com.ruoyi.predict.service.IWfYcHistoryData1Service;
import com.ruoyi.predict.service.IWfYcHistoryData2Service;
import com.ruoyi.predict.service.IWindplantprojectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
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.ResponseBody;

import java.util.*;

/**
 * @author hlf
 * @date 2023/1/12 10:56
 * 文件说明：数据导出
 */
@Api(value = "数据导出Controller", tags = {"数据导出操作接口"})
@Controller
@RequestMapping("/predict/api/dec")
public class DataExportController extends BaseController {

    @Autowired
    private IWindplantprojectService windplantprojectService;

    @Autowired
    private IWfYcHistoryData1Service wfYcHistoryData1Service;

    @Autowired
    private IWfYcHistoryData2Service wfYcHistoryData2Service;

    @ApiOperation("数据导出展示")
    @GetMapping("/displayData")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "多场站编号", required = true),
            @ApiImplicitParam(name = "beginDataTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endDataTime", value = "结束时间", required = true),
    })
    public AjaxResult displayData(@RequestParam(value = "ids") String ids,
                                  @RequestParam(value = "beginDataTime") String beginDataTime,
                                  @RequestParam(value = "endDataTime") String endDataTime) {
        JSONObject json = new JSONObject();
        //获取所有时间
        List<Date> dateList = DateTimeUtil.getDayBetween(DateUtil.parse(beginDataTime), DateUtil.parse(endDataTime));
        LinkedMultiValueMap<String, Double> map = new LinkedMultiValueMap<String, Double>();
        String[] sns = ids.split(",");
        for (Date dTime : dateList) {
            Date beginOfDay = DateUtil.date(DateUtil.beginOfDay(dTime).getTime() + 600000);
            Date endOfDay = DateUtil.date(DateUtil.endOfDay(dTime).getTime() + 1000);
            Map<String, Object> params = new HashMap<>();
            params.put("beginDataTime", DateUtil.formatDateTime(beginOfDay));
            params.put("endDataTime", DateUtil.formatDateTime(endOfDay));
            for (int i = 0; i < sns.length; i++) {
                List<Windplantproject> windplantprojectList = windplantprojectService.selectWindplantprojectByWindplantNo(Convert.toLong(sns[i]));
                for (Windplantproject windplantproject : windplantprojectList) {
                    //实际功率
                    WfYcHistoryData1 wfYcHistoryData1 = new WfYcHistoryData1();
                    wfYcHistoryData1.setAnalogNo(windplantproject.getAnalogNo());
                    wfYcHistoryData1.setParams(params);
                    List<WfYcHistoryData1> wfYcHistoryData1List = wfYcHistoryData1Service.selectWfYcHistoryData1List(wfYcHistoryData1);
                    for (WfYcHistoryData1 w : wfYcHistoryData1List) {
                        String dataTime = DateUtil.format(w.getDataTime(), "yyyy-MM-dd HH:mm");
                        ShareController.addMapValue(map, w.getYcVal(), dataTime, 0, 1);
                    }
                    //实际电量
                    WfYcHistoryData2 wfYcHistoryData2 = new WfYcHistoryData2();
                    wfYcHistoryData2.setAnalogNo(windplantproject.getBcrNo());
                    wfYcHistoryData2.setParams(params);
                    List<WfYcHistoryData2> wfYcHistoryData2List = wfYcHistoryData2Service.selectWfYcHistoryData2List(wfYcHistoryData2);
                    for (WfYcHistoryData2 w : wfYcHistoryData2List) {
                        String dataTime = DateUtil.format(w.getDataTime(), "yyyy-MM-dd HH:mm");
                        ShareController.addMapValue(map, w.getYcRawVal(), dataTime, 1, 2);
                    }
                }
            }
        }
        List<DataFormat> dataFormatList = new ArrayList<>();
        String[] nameStr = {"实际功率", "实际电量"};
        int count1 = dateList.size();
        int v = 1;
        for (int i = 0; i < 2; i++) {
            Double[] data = new Double[count1];
            for (int k = 0; k < count1; k++) {
                List<Double> entry = map.get(DateUtil.format(dateList.get(k), "yyyy-MM-dd HH:mm"));
                if (entry != null) {
                    if (entry.size() >= v) {
                        data[k] = entry.get(i);
                    }
                }
            }
            DataFormat dataFormat = new DataFormat();
            dataFormat.setName(nameStr[i]);
            dataFormat.setData(data);
            dataFormatList.add(dataFormat);
            v++;
        }
        json.put("dataFormatList", dataFormatList);
        json.put("dateList", dateList);
        return AjaxResult.success(json);
    }

}
