package com.xhpc.evcs.cdjgpc.notification;

import com.xhpc.evcs.cdjgpc.dto.CDStationFee;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

/**
 * 成都城投能源投资管理集团有限公司 推送费率
 */
@Component
@Slf4j
public class CDNotificationStationFeeTask extends CoreDispatcher {

    @Resource
    AuthSecretTokenRepository authSecretTokenRepository;
    private final Logger logger = LoggerFactory.getLogger(CDNotificationStationFeeTask.class);

    //9099开放定时任务  9009不开放
    //@Scheduled(fixedDelay = 1000 * 60)
    public void run() throws IOException {

        AuthSecretToken authSecretTokenOut =
                authSecretTokenRepository.findByOperatorId3irdptyAndOperatorIdAndSecretTokenType(
                        "MA6CC2LK7", "MA6DFCTD5", SECRET_TOKEN_TYPE_OUT).orElse(null);
        // maybe对接第三方
        if (authSecretTokenOut != null) {
            List<String> stationKeys = new ArrayList<>(REDIS.keys("cdjgpc:stationFee:*"));
            for (String stationKey : stationKeys) {
                CDStationFee cdStationFeeRequest = REDIS.getCacheObject(stationKey);
                Map<String, Object> map =new HashMap<>();
                map.put("StationFee",cdStationFeeRequest);
                String data = JSONUtil.toJSONString(map);
                CommonRequest<CDStationFee> commonRequest = new CommonRequest<>();
                commonRequest.setData(data);
                String ok = ok(commonRequest, "/notification_stationFee", authSecretTokenOut);
               // log.info("======站点费率推送=========notification_stationFee=================="+ok);
                REDIS.deleteObject(stationKey);
            }
        }
    }

}
