package com.xhpc.evcs.notification.kuaidian;

import com.xhpc.common.api.dto.ChargingStationDto;
import com.xhpc.common.data.redis.CacheRateModel;
import com.xhpc.evcs.cdjgpc.dto.CDEquipmentInfo;
import com.xhpc.evcs.cdjgpc.dto.CDStationInfo;
import com.xhpc.evcs.domain.AuthSecretToken;
import com.xhpc.evcs.domain.XhpcStationInternetBlacklist;
import com.xhpc.evcs.dto.CommonRequest;
import com.xhpc.evcs.jpa.AuthSecretTokenRepository;
import com.xhpc.evcs.jpa.XhpcStationInternetBlacklistRepository;
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.*;

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

/**
 * 推送场站信息
 * @author yuyang
 * @Date 2025-07-25 10:06
 */
@Component
@Slf4j
public class NotificationStationInfoKuaiDianTask extends CoreDispatcher {

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


    //9009开放定时任务  9099不开放
    @Scheduled(cron = "0 0 0 1/1 * ? ")
    public void run() throws IOException {
        try{
            List<XhpcStationInternetBlacklist> xhpcStationInternetBlacklist = xhpcStationInternetBlacklistRepo.findByInternetUserId(1L);
            List<String> stationKeys = new ArrayList<>(REDIS.keys("station:*"));
            AuthSecretToken authSecretTokenOut =
                    authSecretTokenRepository.findByOperatorId3irdptyAndOperatorIdAndSecretTokenType("MA005DBW1", "MA6DFCTD5", SECRET_TOKEN_TYPE_OUT).orElse(null); //todo

            List<String> validStationKeys = new ArrayList<>();
            for (String stationKey : stationKeys) {
                boolean isValid = true;
                //查询不合作的电桩
                for (XhpcStationInternetBlacklist xhpcStationInternetBlack : xhpcStationInternetBlacklist) {
                    if (stationKey.substring(8).equals(xhpcStationInternetBlack.getChargingStationId().toString())) {
                        isValid = false;
                    }
                }
                ChargingStationDto chargingStationDto = REDIS.getCacheObject(stationKey);
                if (chargingStationDto.getPiles() != null) {
                    if (isValid) {
                        validStationKeys.add(stationKey);
                    }
                }
            }
            for (String stationKey : validStationKeys) {
                // maybe对接第三方
                if (authSecretTokenOut != null) {
                    String chargingStationId = stationKey.substring(8);
                    //获取场站信息
                    String key = "cdjgpc:station:".concat(chargingStationId)+".notification";
                    CDStationInfo cdStationInfo =REDIS.getCacheObject(key);
                    cdStationInfo.setEquipmentInfos(new ArrayList<>());
                    //查询场站是桩是否对应上
                    ChargingStationDto stationDto = REDIS.getCacheObject("station:"+chargingStationId);
                    CacheRateModel cacheRateModel = REDIS.getCacheObject("rateModel:" + stationDto.getRateModelId());

                    StringBuilder price = new StringBuilder();
                    StringBuilder svcPrice = new StringBuilder();

                    Integer t1Price = cacheRateModel.getT1Price();
                    Integer t1SvcPrice = cacheRateModel.getT1SvcPrice();
                    Integer t2Price = cacheRateModel.getT2Price();
                    Integer t2SvcPrice = cacheRateModel.getT2SvcPrice();
                    Integer t3Price = cacheRateModel.getT3Price();
                    Integer t3SvcPrice = cacheRateModel.getT3SvcPrice();
                    Integer t4Price = cacheRateModel.getT4Price();
                    Integer t4SvcPrice = cacheRateModel.getT4SvcPrice();

                    if(cacheRateModel.getTfPricesSeq() !=null && cacheRateModel.getTfPricesSeq().length>0){
                        price.append("{");
                        svcPrice.append("{");
                        for (int i = 0; i < cacheRateModel.getTfPricesSeq().length; i++) {
                            String time = getTime(i);
                            String s = cacheRateModel.getTfPricesSeq()[i];
                            if("00".equals(s)){
                                price.append(time).append(t1Price.toString()).append(",");
                                svcPrice.append(time).append(t1SvcPrice.toString()).append(",");
                            }else if("01".equals(s)){
                                price.append(time).append(t2Price.toString()).append(",");
                                svcPrice.append(time).append(t2SvcPrice.toString()).append(",");
                            }else if("02".equals(s)){
                                price.append(time).append(t3Price.toString()).append(",");
                                svcPrice.append(time).append(t3SvcPrice.toString()).append(",");
                            }else{
                                price.append(time).append(t4Price.toString()).append(",");
                                svcPrice.append(time).append(t4SvcPrice.toString()).append(",");
                            }
                        }
                        price.deleteCharAt(price.length()-1).append("}");
                        svcPrice.deleteCharAt(svcPrice.length()-1).append("}");
                    }
                    cdStationInfo.setElectricityFee(price.toString());
                    cdStationInfo.setServiceFee(svcPrice.toString());
                    Set<String> pks = stationDto.getPiles();
                    if (pks != null) {
                        List<CDEquipmentInfo> cdEquipmentInfos = new ArrayList<>();
                        for (String pileNo : pks) {
                            String pk = "pile:".concat(pileNo);
                            String skey = "cdjgpc:".concat(pk)+".notification";
                            CDEquipmentInfo cdEquipmentInfo = REDIS.getCacheObject(skey);
                            if(cdEquipmentInfo==null || "".equals(cdEquipmentInfo.getEquipmentID())){
                                logger.info("该场站下面："+pileNo+"桩未录入");
                            }else{
                                cdEquipmentInfos.add(cdEquipmentInfo);
                            }
                        }
                        cdStationInfo.setEquipmentInfos(cdEquipmentInfos);
                    }
                    cdStationInfo.setSwapMatchCarsName(null);
                    CommonRequest<CDStationInfo> commonRequest = new CommonRequest<>();
                    Map<String,Object> mapStationInfo =new HashMap<>();
                    mapStationInfo.put("StationInfo",cdStationInfo);
                    String data = JSONUtil.toJSONString(mapStationInfo);
                    System.out.println("==快电====data========"+data);
                    commonRequest.setData(data);
                    String result =ok(commonRequest,"/notification_stationInfo",authSecretTokenOut);
                    System.out.println("===快电==推送充电站信息==result========"+result);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private String getTime(Integer number){
        String time ="";
        if(number ==0){
            time ="00:00:00-00:30:00:";
        }else if(number ==1) {
            time = "00:30:00-01:00:00:";
        }else if(number ==2) {
            time = "01:00:00-01:30:00:";
        }else if(number ==3) {
            time = "01:30:00-02:00:00:";
        }else if(number ==4) {
            time = "02:00:00-02:30:00:";
        }else if(number ==5) {
            time = "02:30:00-03:00:00:";
        }else if(number ==6) {
            time = "03:00:00-03:30:00:";
        }else if(number ==7) {
            time = "03:30:00-04:00:00:";
        }else if(number ==8) {
            time = "04:00:00-04:30:00:";
        }else if(number ==9) {
            time = "04:30:00-05:00:00:";
        }else if(number ==10) {
            time = "05:00:00-05:30:00:";
        }else if(number ==11) {
            time = "05:30:00-06:00:00:";
        }else if(number ==12) {
            time = "06:00:00-06:30:00:";
        }else if(number ==13) {
            time = "06:30:00-07:00:00:";
        }else if(number ==14) {
            time = "07:00:00-07:30:00:";
        }else if(number ==15) {
            time = "07:30:00-08:00:00:";
        }else if(number ==16) {
            time = "08:00:00-08:30:00:";
        }else if(number ==17) {
            time = "08:30:00-09:00:00:";
        }else if(number ==18) {
            time = "09:00:00-09:30:00:";
        }else if(number ==19) {
            time = "09:30:00-10:00:00:";
        }else if(number ==20) {
            time = "10:00:00-10:30:00:";
        }else if(number ==21) {
            time = "10:30:00-11:00:00:";
        }else if(number ==22) {
            time = "11:00:00-11:30:00:";
        }else if(number ==23) {
            time = "11:30:00-12:00:00:";
        }else if(number ==24) {
            time = "12:00:00-12:30:00:";
        }else if(number ==25) {
            time = "12:30:00-13:00:00:";
        }else if(number ==26) {
            time = "13:00:00-13:30:00:";
        }else if(number ==27) {
            time = "13:30:00-14:00:00:";
        }else if(number ==28) {
            time = "14:00:00-14:30:00:";
        }else if(number ==29) {
            time = "14:30:00-15:00:00:";
        }else if(number ==30){
            time ="15:00:00-15:30:00:";
        }else if(number ==31) {
            time = "15:30:00-16:00:00:";
        }else if(number ==32) {
            time = "16:00:00-16:30:00:";
        }else if(number ==33) {
            time = "16:30:00-17:00:00:";
        }else if(number ==34) {
            time = "17:00:00-17:30:00:";
        }else if(number ==35) {
            time = "17:30:00-18:00:00:";
        }else if(number ==36) {
            time = "18:00:00-18:30:00:";
        }else if(number ==37) {
            time = "18:30:00-19:00:00:";
        }else if(number ==38) {
            time = "19:00:00-19:30:00:";
        }else if(number ==39) {
            time = "19:30:00-20:00:00:";
        }else if(number ==40){
            time ="20:00:00-20:30:00:";
        }else if(number ==41) {
            time = "20:30:00-21:00:00:";
        }else if(number ==42) {
            time = "21:00:00-21:30:00:";
        }else if(number ==43) {
            time = "21:30:00-22:00:00";
        }else if(number ==44) {
            time = "22:00:00-22:30:00:";
        }else if(number ==45) {
            time = "22:30:00-23:00:00:";
        }else if(number ==46) {
            time = "23:00:00-23:30:00";
        }else if(number ==47) {
            time = "23:30:00-24:00:00:";
        }
        return time;
    }
}
