package com.xhpc.evcs.cdjgpc.notification;

import cn.hutool.core.date.DateUtil;
import com.xhpc.common.api.PowerPileService;
import com.xhpc.common.core.domain.R;
import com.xhpc.evcs.cdjgpc.dto.CDStationStatslnfo;
import com.xhpc.evcs.cdjgpc.dto.CDSupStationPowerInfo;
import com.xhpc.evcs.domain.AuthSecretToken;
import com.xhpc.evcs.dto.CommonRequest;
import com.xhpc.evcs.jpa.AuthSecretTokenRepository;
import com.xhpc.evcs.notification.CoreDispatcher;
import com.xhpc.evcs.utils.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;
import static com.xhpc.evcs.domain.AuthSecretToken.SECRET_TOKEN_TYPE_OUT;

/**
 * 推送充电站实时功率
 * @author yuyang
 * @date 2023-10-07 11:04
 */
@Component
@Slf4j
public class CDSuperviseNotificationRealtimePowerInfoTask extends CoreDispatcher {

    @Resource
    AuthSecretTokenRepository authSecretTokenRepository;
    @Resource
    PowerPileService powerPileService;

    private final Logger logger = LoggerFactory.getLogger(CDSuperviseNotificationOperationStatusInfoTask.class);
    //不推送
    //@Scheduled(cron = "0 0/15 * * * ? ")
    protected void run() throws IOException {
        String replace = UUID.randomUUID().toString().replace("-", "");
        AuthSecretToken authSecretTokenOut =
                authSecretTokenRepository.findByOperatorId3irdptyAndOperatorIdAndSecretTokenType(
                        "MA6CC2LK7", "MA6DFCTD5", SECRET_TOKEN_TYPE_OUT).orElse(null);

        R r = powerPileService.getSuperviseNotificationRealtimePowerInfo(authSecretTokenOut.getOperatorId3irdpty(),replace);
        if(r.getCode() !=200){
           // logger.info("========推送充电站实时功率失败，时间："+ DateUtil.format(DateUtil.yesterday(), "yyyy-MM-dd"));
        }
        String key ="cdjgpc:station:"+replace+".getSuperviseNotificationRealtimePowerInfo";
        List<CDSupStationPowerInfo> supStationPowerInfos =REDIS.getCacheObject(key);

        Map<String, Object> map =new HashMap<>();
        map.put("SupStationPowerInfos",supStationPowerInfos);
        String data = JSONUtil.toJSONString(map);
        CommonRequest<CDSupStationPowerInfo> commonRequest = new CommonRequest<>();
        commonRequest.setData(data);
        String ok = ok(commonRequest, "/supervise_notification_realtime_power_info", authSecretTokenOut);
        //System.out.println("=====ok======"+ok);
        //logger.info("========推送充电站实时功率成功，时间：========="+DateUtil.format(DateUtil.yesterday(), "yyyy-MM-dd"));
        REDIS.deleteObject(key);
    }
}
