package com.ho.business.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ho.business.entity.Station;
import com.ho.business.service.DataIntervalProcessService;
import com.ho.business.service.PvStationService;
import com.ho.business.vo.resp.DeviceRespVO;
import com.ho.business.vo.resp.InverterResp.PVStationTodayGeneProfit;
import com.ho.business.vo.resp.InverterResp.PowerGenerateRespVO;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author fancl
 * @desc: 定时任务实现类
 * @date 2023/1/11
 */
@Service
@Slf4j
public class DataIntervalProcessServiceImpl implements DataIntervalProcessService {

    @Autowired
    PvStationService pvStationService;

    @Autowired
    RedisService redisService;

    /**
     * 加工电站维度的发电量和收益曲线
     * 异步执行
     * @param stations
     */
    @Async
    @Override
    public void stationGeneProfitCurveFromInverter(List<Station> stations) {
        //循环处理
        if (stations == null || stations.isEmpty()) {
            return;
        }
        Date day = new Date();
        for (Station station : stations) {
            Integer stationId = station.getId();
            log.info("处理电站:" + stationId);
            //先处理一个电站的
            List<DeviceRespVO> deviceRespVOS = pvStationService.getIntervalsByStationId(stationId);
            List<PowerGenerateRespVO> curveList = pvStationService.powerGenerationCurrentDay(stationId, deviceRespVOS, day);
            //计算一个当日总收益,放到对象里
            BigDecimal profitToday = BigDecimal.ZERO;
            for (PowerGenerateRespVO pw : curveList) {
                profitToday = profitToday.add(pw.getProfit());
            }
            //保留2位小数
            profitToday = profitToday.setScale(2, BigDecimal.ROUND_HALF_UP);
            //赋值到
            //塞值到缓存
            String key = RedisKeyConstant.PV.STATION_PV_TODAY_PROFIT + stationId;
            PVStationTodayGeneProfit curve = new PVStationTodayGeneProfit();
            curve.setCurveList(curveList);
            curve.setTodayProfit(profitToday);
            redisService.set(key, curve);
        }

    }
}
