package com.jiayue.ipp.idp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiayue.ipp.common.data.entity.DqIntervene;
import com.jiayue.ipp.common.data.entity.ElectricField;
import com.jiayue.ipp.common.data.entity.ForecastPowerShortTerm;
import com.jiayue.ipp.idp.dto.DqInterveneDto;
import com.jiayue.ipp.idp.mapper.DqInterveneMapper;
import com.jiayue.ipp.idp.service.DqInterveneService;
import com.jiayue.ipp.idp.service.ElectricFieldService;
import com.jiayue.ipp.idp.service.ForecastPowerShortTermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * idp_inverter_info
 *
 * @author whc
 * @date 2022-03-18 15:49:11
 */
@Service
public class DqInterveneServiceImpl extends ServiceImpl<DqInterveneMapper, DqIntervene> implements DqInterveneService {
    @Autowired
    DqInterveneMapper dqInterveneMapper;
    @Autowired
    ForecastPowerShortTermService forecastPowerShortTermService;
    @Autowired
    ElectricFieldService electricFieldService;

    public List<DqInterveneDto> getShort(Long startTime, Long endTime,String stationCode){
        ElectricField electricField = electricFieldService.findByStationCode(stationCode);
        // 先获取实时短期
        QueryWrapper<ForecastPowerShortTerm> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("forecast_time", new Date(startTime), new Date(endTime));
        queryWrapper.eq("station_code",stationCode);
        queryWrapper.eq("forecast_manufactor","SYJY");
        List<ForecastPowerShortTerm> ForecastPowerShortTermList = forecastPowerShortTermService.list(queryWrapper);
        Map<Long, ForecastPowerShortTerm> ForecastPowerShortTermMap = ForecastPowerShortTermList.stream()
                .collect(Collectors.toMap(
                        myObject -> myObject.getForecastTime().getTime(), // keyMapper，将Date转换为long
                        myObject -> myObject, // valueMapper，保持原对象
                        (existing, replacement) -> existing // mergeFunction，处理重复key的情况，这里简单地保留现有的value
                ));
        // 再获取预设短期
        QueryWrapper<DqIntervene> presetsWrapper = new QueryWrapper<>();
        presetsWrapper.between("forecast_time", new Date(startTime), new Date(endTime));
        List<DqIntervene> bizPresetsShortList = this.list(presetsWrapper);
        Map<Long, DqIntervene> bizPresetsShortMap = bizPresetsShortList.stream()
                .collect(Collectors.toMap(
                        myObject -> myObject.getForecastTime().getTime(), // keyMapper，将Date转换为long
                        myObject -> myObject, // valueMapper，保持原对象
                        (existing, replacement) -> existing // mergeFunction，处理重复key的情况，这里简单地保留现有的value
                ));
        // 整合数据返给前端
        List<DqInterveneDto> presetsShortDtoList = new ArrayList<>();
        Long momentTime = 15 * 60 * 1000L;
        for (Long tempTime = startTime; tempTime <= endTime; tempTime = tempTime + momentTime) {
            DqInterveneDto dqInterveneDto = new DqInterveneDto();
            dqInterveneDto.setForecastTime(new Date(tempTime));
            dqInterveneDto.setCapacity(electricField.getCapacity());
            if (ForecastPowerShortTermMap.get(tempTime)!=null){
                // 实时值
                dqInterveneDto.setActivePower(ForecastPowerShortTermMap.get(tempTime).getFpValue());
            }
            if (bizPresetsShortMap.get(tempTime)!=null){
                // 干预值
                dqInterveneDto.setPresetsPower(bizPresetsShortMap.get(tempTime).getActivePower());
            }
            else{
                // 干预值
                dqInterveneDto.setPresetsPower(ForecastPowerShortTermMap.get(tempTime).getFpValue());
            }
            presetsShortDtoList.add(dqInterveneDto);
        }
        return presetsShortDtoList;
    }

    /**
     * 时间段删除短期干预
     * @param startTime
     * @param endTime
     */
    public void deleteShort(Date startTime, Date endTime,String stationCode){
        dqInterveneMapper.deleteShort(startTime,endTime,stationCode);
    }
}
