package com.xhpc.evcs.cdjgpc.notification;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.xhpc.common.api.ChargingStationService;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.data.redis.CacheOrderData;
import com.xhpc.common.dto.XhpcRateTimeDto;
import com.xhpc.evcs.domain.AuthSecretToken;
import com.xhpc.evcs.dto.*;
import com.xhpc.evcs.jpa.AuthSecretTokenRepository;
import com.xhpc.evcs.jpa.XhpcHistoryOrderRepository;
import com.xhpc.evcs.notification.CoreDispatcher;
import com.xhpc.evcs.notification.NotificationChargeOrderInfoTask;
import com.xhpc.evcs.utils.DateUtil;
import com.xhpc.evcs.utils.JSONUtil;
import com.xhpc.order.domain.XhpcHistoryOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.Instant;
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 2023-09-24 19:27
 */
@Component
public class CDNotificationChargeOrderInfoTask extends CoreDispatcher {
    @Autowired
    private XhpcHistoryOrderRepository xhpcHistoryOrderRepository;
    @Autowired
    private AuthSecretTokenRepository authSecretTokenRepository;
    @Autowired
    private ChargingStationService chargingStationService;

    private final Logger logger = LoggerFactory.getLogger(NotificationChargeOrderInfoTask.class);

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

        List<AuthSecretToken> authSecretTokenList = authSecretTokenRepository.findBySecretTokenType(SECRET_TOKEN_TYPE_OUT);
        List<XhpcHistoryOrder> xhpcHistoryOrderList =
                xhpcHistoryOrderRepository.findByConfirmResultNotAndSource(0, 1);
        for (XhpcHistoryOrder horder : xhpcHistoryOrderList) {
            for (AuthSecretToken authSecretToken : authSecretTokenList) {
                if(!"MA6CC2LK7".equals(horder.getInternetSerialNumber().substring(0, 9))){
                    continue;
                }
                String operatorId3rdptyEvcs = horder.getOperatorId3rdptyEvcs();
                if (operatorId3rdptyEvcs == null) {
                    if (horder.getInternetSerialNumber() != null) {
                        operatorId3rdptyEvcs = horder.getInternetSerialNumber().substring(0, 9);
                        horder.setOperatorId3rdptyEvcs(operatorId3rdptyEvcs);
                        xhpcHistoryOrderRepository.save(horder); // mending
                    }
                }
                if (authSecretToken.getOperatorId3irdpty().equals(operatorId3rdptyEvcs)) {
                    ChargeOrderInfo chargeOrderInfo = new ChargeOrderInfo(horder);
                    chargeOrderInfo.setChargeModel(null);

                    if(new BigDecimal(0).compareTo(horder.getTotalPrice())<0){
                        XhpcHistoryOrder horder1  =new XhpcHistoryOrder();
                        BeanUtils.copyProperties(horder,horder1);
                        chargeOrderInfo = calculateEmYu(horder1,chargeOrderInfo);
                    }
                    logger.info("==========开始推送====推送返回值============calculateEmYu=====");
                    final ChargeOrderInfoResponse pushResp = notify(chargeOrderInfo, authSecretToken);
                    if (pushResp != null) {
                        horder.setConfirmResult(pushResp.getConfirmResult());
                        logger.debug("3rd part order {} push result: {}", horder.getSerialNumber(),
                                pushResp.getConfirmResult());
                        //logger.info("==============推送返回值================="+pushResp.toString());
                        xhpcHistoryOrderRepository.save(horder);
                        REDIS.setCacheMapValue("pushOrder:".concat(horder.getSerialNumber()), "horderpushed", true);
                    } else {
                        logger.error("3dp[{}] order[{}/{}] push fail", operatorId3rdptyEvcs,
                                horder.getSerialNumber(), horder.getInternetSerialNumber());
                    }
                }
            }
        }
    }

    public  ChargeOrderInfo calculateEmYu(XhpcHistoryOrder horder,ChargeOrderInfo chargeOrderInfo) {
        logger.info("====111======开始推送====推送返回值============calculateEmYu=====");
        String startTime = chargeOrderInfo.getStartTime();
        String endTime = chargeOrderInfo.getEndTime();

        DateTime parse = cn.hutool.core.date.DateUtil.parse(startTime, "yyyy-MM-dd");
        DateTime parse1 = cn.hutool.core.date.DateUtil.parse(endTime, "yyyy-MM-dd");
        long betweenDay = cn.hutool.core.date.DateUtil.between(parse,parse1, DateUnit.DAY);

        if(betweenDay==0){
            logger.info("=====22=====开始推送====推送返回值============calculateEmYu=====");
            //没有跨天
            Stack<ChargeDetails> stack = extracted(horder, chargeOrderInfo);

            ChargeDetails[] cda = new ChargeDetails[stack.size()];
            chargeOrderInfo.setChargeDetails(stack.toArray(cda));
        }else{
            logger.info("=====333=====开始推送====推送返回值============calculateEmYu=====");
            Date endTime1 = horder.getEndTime();
            horder.setEndTime(cn.hutool.core.date.DateUtil.parse(startTime.substring(0,10)+" 23:59:59"));
            Stack<ChargeDetails> stack =extracted(horder, chargeOrderInfo);

            horder.setStartTime(cn.hutool.core.date.DateUtil.parse(endTime.substring(0,10)+" 00:00:00"));
            horder.setEndTime(endTime1);
            Stack<ChargeDetails> stack1 =extracted(horder, chargeOrderInfo);

            Stack<ChargeDetails> mergedStack = new Stack<>();

            // 先添加stack2的所有元素（保持原顺序）
            for (ChargeDetails detail : stack) {
                mergedStack.push(detail);
            }

            // 再添加stack1的所有元素（保持原顺序）
            for (ChargeDetails detail : stack1) {
                mergedStack.push(detail);
            }

            ChargeDetails[] cda = new ChargeDetails[mergedStack.size()];
            chargeOrderInfo.setChargeDetails(mergedStack.toArray(cda));
        }

        return chargeOrderInfo;
    }

    private Stack<ChargeDetails> extracted(XhpcHistoryOrder horder, ChargeOrderInfo chargeOrderInfo) {
        Stack<ChargeDetails> chargeDetailsStack =new Stack<>();
        R r = chargingStationService.getXhpcRateTimeOrder(horder.getRateModelId(), cn.hutool.core.date.DateUtil.formatTime(horder.getStartTime()), cn.hutool.core.date.DateUtil.formatTime(horder.getEndTime()),horder.getSerialNumber());
        logger.info("=====444=====开始推送====推送返回值============calculateEmYu=====");
        if(r.getCode()==200){
            logger.info("=====5555=====开始推送====推送返回值============calculateEmYu=====");
            Map<String, Object> cacheMapYu = REDIS.getCacheMap("Yu"+horder.getSerialNumber());
            List<XhpcRateTimeDto> xhpcRateTimes = (List<XhpcRateTimeDto>)cacheMapYu.get(horder.getSerialNumber());

            Map<String, Object> cacheMap = REDIS.getCacheMap("order:"+ horder.getSerialNumber());
            CacheOrderData cacheOrderData = (CacheOrderData)cacheMap.get("orderData");
            BigDecimal bigDecimal = new BigDecimal(10000);
            int t1=0;
            int t12 =2;
            int t13 =3;
            BigDecimal bigDecimal1 = new BigDecimal(cacheOrderData.getT1PowerQuantity()).divide(bigDecimal);
            int t2 =0;
            int t22 =2;
            int t23 =3;
            BigDecimal bigDecimal2 = new BigDecimal(cacheOrderData.getT2PowerQuantity()).divide(bigDecimal);
            int t3 =0;
            int t32 =2;
            int t33 =3;
            BigDecimal bigDecimal3 = new BigDecimal(cacheOrderData.getT3PowerQuantity()).divide(bigDecimal);
            int t4 =0;
            int t43 =3;
            int t42 =2;
            BigDecimal bigDecimal4 = new BigDecimal(cacheOrderData.getT4PowerQuantity()).divide(bigDecimal);


            for (XhpcRateTimeDto xpcRateTime:xhpcRateTimes) {
                String rateValue = xpcRateTime.getRateValue();
                if("00".equals(rateValue)){
                    t1=t1+1;
                } else if ("01".equals(rateValue)) {
                    t2=t2+1;
                }else if ("02".equals(rateValue)) {
                    t3=t3+1;
                }else if ("03".equals(rateValue)) {
                    t4=t4+1;
                }
            }
            BigDecimal decimalYu = new BigDecimal((horder.getEndTime().getTime() - horder.getStartTime().getTime())).divide(new BigDecimal(60000),4,BigDecimal.ROUND_HALF_UP);

            BigDecimal divideYu = new BigDecimal(cacheOrderData.getTotalPowerQuantity()).divide(new BigDecimal(10000),4,BigDecimal.ROUND_HALF_UP);
            logger.info("=====66666=====开始推送====推送返回值============decimalYu====="+decimalYu+"=========divideYu========="+divideYu);
            //每分钟多少度电
            BigDecimal quantity =new BigDecimal(0);
            if(decimalYu.compareTo(new BigDecimal(0))!=0){
                quantity = decimalYu.divide(divideYu,4,BigDecimal.ROUND_HALF_UP);
            }
            for (XhpcRateTimeDto xpcRateTime:xhpcRateTimes) {
                ChargeDetails chargeDetails = new ChargeDetails();
                String rateValue = xpcRateTime.getRateValue();

                String rateStartTime = cn.hutool.core.date.DateUtil.format(xpcRateTime.getStartTime(), "HH:mm:ss");

                String orderStartTime = cn.hutool.core.date.DateUtil.format(horder.getStartTime(), "yyyy-MM-dd HH:mm:ss");

                rateStartTime = orderStartTime.substring(0,10)+" "+rateStartTime;

                String rateEndTime = cn.hutool.core.date.DateUtil.format(xpcRateTime.getEndTime(), "HH:mm:ss");

                String orderEndTime = cn.hutool.core.date.DateUtil.format(horder.getEndTime(), "yyyy-MM-dd HH:mm:ss");

                rateEndTime = orderStartTime.substring(0,10)+" "+rateEndTime;

                DateTime startTime1 = cn.hutool.core.date.DateUtil.parse(rateStartTime, "yyyy-MM-dd HH:mm:ss");

                DateTime endTime1 = cn.hutool.core.date.DateUtil.parse(rateEndTime, "yyyy-MM-dd HH:mm:ss");

                if("00".equals(rateValue)){
                    boolean judgeStart =false;
                    boolean judgeEnd =false;

                    if(horder.getStartTime().getTime()-startTime1.getTime()>0){
                        chargeDetails.setDetailStartTime(orderStartTime);
                        judgeStart =true;
                    }else{
                        chargeDetails.setDetailStartTime(rateStartTime);
                    }
                    if(horder.getEndTime().getTime()-endTime1.getTime()>0){
                        chargeDetails.setDetailEndTime(rateEndTime);
                    }else{
                        chargeDetails.setDetailEndTime(orderEndTime);
                        judgeEnd =true;
                    }
                    chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                    chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());

                    if(t1==1){
                        BigDecimal divide = new BigDecimal(cacheOrderData.getT1PowerQuantity()).divide(bigDecimal);

                        BigDecimal multiply1 = new BigDecimal(cacheOrderData.getT1PowerQuantity()).divide(bigDecimal).multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                        BigDecimal multiply2 = new BigDecimal(cacheOrderData.getT1PowerQuantity()).divide(bigDecimal).multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                        chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                        chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                        chargeDetails.setDetailPower(divide.doubleValue());
                        chargeDetailsStack.push(chargeDetails);
                    }else if(t1==2){

                        if(t12==2){
                            if(judgeStart){
                                rateStartTime = orderStartTime;
                            }
                            if(judgeEnd){
                                rateEndTime = orderEndTime;
                            }

                            DateTime parse2 = cn.hutool.core.date.DateUtil.parse(rateStartTime, "yyyy-MM-dd HH:mm:ss");
                            DateTime parse3 = cn.hutool.core.date.DateUtil.parse(rateEndTime, "yyyy-MM-dd HH:mm:ss");
                            long betweenDay4 = cn.hutool.core.date.DateUtil.between(parse3,parse2, DateUnit.MINUTE);

                            //获得电量
                            BigDecimal multiply = quantity.multiply(new BigDecimal(betweenDay4));
                            //剩余电量
                            bigDecimal1 = bigDecimal1.subtract(multiply);

                            BigDecimal multiply1 = multiply.multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                            BigDecimal multiply2 = multiply.multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                            chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                            chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());
                            chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                            chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                            chargeDetails.setDetailPower(multiply.doubleValue());
                            chargeDetailsStack.push(chargeDetails);

                        }else{
                            BigDecimal multiply1 = bigDecimal1.multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                            BigDecimal multiply2 = bigDecimal1.multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                            chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                            chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());
                            chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                            chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                            chargeDetails.setDetailPower(bigDecimal1.doubleValue());
                            chargeDetailsStack.push(chargeDetails);
                        }
                        t12 = t12-1;
                    }else if(t1==3){
                        if(t13==3){
                            if(judgeStart){
                                rateStartTime = orderStartTime;
                            }
                            if(judgeEnd){
                                rateEndTime = orderEndTime;
                            }
                            DateTime parse2 = cn.hutool.core.date.DateUtil.parse(rateStartTime, "yyyy-MM-dd HH:mm:ss");
                            DateTime parse3 = cn.hutool.core.date.DateUtil.parse(rateEndTime, "yyyy-MM-dd HH:mm:ss");
                            long betweenDay4 = cn.hutool.core.date.DateUtil.between(parse3,parse2, DateUnit.MINUTE);
                            //获得电量
                            BigDecimal multiply = quantity.multiply(new BigDecimal(betweenDay4));
                            //剩余电量
                            bigDecimal1 = bigDecimal1.subtract(multiply);

                            BigDecimal multiply1 = multiply.multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                            BigDecimal multiply2 = multiply.multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                            chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                            chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());
                            chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                            chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                            chargeDetails.setDetailPower(multiply.doubleValue());
                            chargeDetailsStack.push(chargeDetails);
                        }else if(t13==2){

                            if(judgeStart){
                                rateStartTime = orderStartTime;
                            }
                            if(judgeEnd){
                                rateEndTime = orderEndTime;
                            }
                            DateTime parse2 = cn.hutool.core.date.DateUtil.parse(rateStartTime, "yyyy-MM-dd HH:mm:ss");
                            DateTime parse3 = cn.hutool.core.date.DateUtil.parse(rateEndTime, "yyyy-MM-dd HH:mm:ss");
                            long betweenDay4 = cn.hutool.core.date.DateUtil.between(parse3,parse2, DateUnit.MINUTE);
                            //获得电量
                            BigDecimal multiply = quantity.multiply(new BigDecimal(betweenDay4));


                            BigDecimal multiply1 = multiply.multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                            BigDecimal multiply2 = multiply.multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                            chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                            chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());
                            chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                            chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                            chargeDetails.setDetailPower(multiply.doubleValue());
                            chargeDetailsStack.push(chargeDetails);

                            //剩余电量
                            bigDecimal1 = bigDecimal1.subtract(multiply);
                        }else{
                            BigDecimal multiply1 = bigDecimal1.multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                            BigDecimal multiply2 = bigDecimal1.multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                            chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                            chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());
                            chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                            chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                            chargeDetails.setDetailPower(bigDecimal1.doubleValue());
                            chargeDetailsStack.push(chargeDetails);
                        }
                        t13 = t13-1;
                    }

                } else if ("01".equals(rateValue)) {
                    boolean judgeStart =false;
                    boolean judgeEnd =false;

                    if(horder.getStartTime().getTime()-startTime1.getTime()>0){
                        chargeDetails.setDetailStartTime(orderStartTime);
                        judgeStart =true;
                    }else{
                        chargeDetails.setDetailStartTime(rateStartTime);
                    }
                    if(horder.getEndTime().getTime()-endTime1.getTime()>0){
                        chargeDetails.setDetailEndTime(rateEndTime);
                    }else{
                        chargeDetails.setDetailEndTime(orderEndTime);
                        judgeEnd =true;
                    }
                    chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                    chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());

                    if(t2==1){
                        BigDecimal divide = new BigDecimal(cacheOrderData.getT2PowerQuantity()).divide(bigDecimal);

                        BigDecimal multiply1 = new BigDecimal(cacheOrderData.getT2PowerQuantity()).divide(bigDecimal).multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                        BigDecimal multiply2 = new BigDecimal(cacheOrderData.getT2PowerQuantity()).divide(bigDecimal).multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                        chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                        chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                        chargeDetails.setDetailPower(divide.doubleValue());
                        chargeDetailsStack.push(chargeDetails);
                    }else if(t2==2){

                        if(t22==2){
                            if(judgeStart){
                                rateStartTime = orderStartTime;
                            }
                            if(judgeEnd){
                                rateEndTime = orderEndTime;
                            }

                            DateTime parse2 = cn.hutool.core.date.DateUtil.parse(rateStartTime, "yyyy-MM-dd HH:mm:ss");
                            DateTime parse3 = cn.hutool.core.date.DateUtil.parse(rateEndTime, "yyyy-MM-dd HH:mm:ss");
                            long betweenDay4 = cn.hutool.core.date.DateUtil.between(parse3,parse2, DateUnit.MINUTE);

                            //获得电量
                            BigDecimal multiply = quantity.multiply(new BigDecimal(betweenDay4));


                            BigDecimal multiply1 = multiply.multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                            BigDecimal multiply2 = multiply.multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                            chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                            chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());
                            chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                            chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                            chargeDetails.setDetailPower(multiply.doubleValue());
                            chargeDetailsStack.push(chargeDetails);

                            //剩余电量
                            bigDecimal2 = bigDecimal2.subtract(multiply);

                        }else{
                            BigDecimal multiply1 = bigDecimal2.multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                            BigDecimal multiply2 = bigDecimal2.multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                            chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                            chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());
                            chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                            chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                            chargeDetails.setDetailPower(bigDecimal2.doubleValue());
                            chargeDetailsStack.push(chargeDetails);
                        }
                        t22 = t22-1;
                    }else if(t2==3){
                        if(t23==3){
                            if(judgeStart){
                                rateStartTime = orderStartTime;
                            }
                            if(judgeEnd){
                                rateEndTime = orderEndTime;
                            }
                            DateTime parse2 = cn.hutool.core.date.DateUtil.parse(rateStartTime, "yyyy-MM-dd HH:mm:ss");
                            DateTime parse3 = cn.hutool.core.date.DateUtil.parse(rateEndTime, "yyyy-MM-dd HH:mm:ss");
                            long betweenDay4 = cn.hutool.core.date.DateUtil.between(parse3,parse2, DateUnit.MINUTE);
                            //获得电量
                            BigDecimal multiply = quantity.multiply(new BigDecimal(betweenDay4));

                            BigDecimal multiply1 = multiply.multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                            BigDecimal multiply2 = multiply.multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                            chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                            chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());
                            chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                            chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                            chargeDetails.setDetailPower(multiply.doubleValue());
                            chargeDetailsStack.push(chargeDetails);

                            //剩余电量
                            bigDecimal2 = bigDecimal2.subtract(multiply);
                        }else if(t23==2){

                            if(judgeStart){
                                rateStartTime = orderStartTime;
                            }
                            if(judgeEnd){
                                rateEndTime = orderEndTime;
                            }
                            DateTime parse2 = cn.hutool.core.date.DateUtil.parse(rateStartTime, "yyyy-MM-dd HH:mm:ss");
                            DateTime parse3 = cn.hutool.core.date.DateUtil.parse(rateEndTime, "yyyy-MM-dd HH:mm:ss");
                            long betweenDay4 = cn.hutool.core.date.DateUtil.between(parse3,parse2, DateUnit.MINUTE);
                            //获得电量
                            BigDecimal multiply = quantity.multiply(new BigDecimal(betweenDay4));


                            BigDecimal multiply1 = multiply.multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                            BigDecimal multiply2 = multiply.multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                            chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                            chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());
                            chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                            chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                            chargeDetails.setDetailPower(multiply.doubleValue());
                            chargeDetailsStack.push(chargeDetails);

                            //剩余电量
                            bigDecimal2 = bigDecimal2.subtract(multiply);
                        }else{
                            BigDecimal multiply1 = bigDecimal2.multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                            BigDecimal multiply2 = bigDecimal2.multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                            chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                            chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());
                            chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                            chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                            chargeDetails.setDetailPower(bigDecimal2.doubleValue());
                            chargeDetailsStack.push(chargeDetails);
                        }
                        t23 = t23-1;
                    }

                }else if ("02".equals(rateValue)) {
                    boolean judgeStart =false;
                    boolean judgeEnd =false;

                    if(horder.getStartTime().getTime()-startTime1.getTime()>0){
                        chargeDetails.setDetailStartTime(orderStartTime);
                        judgeStart =true;
                    }else{
                        chargeDetails.setDetailStartTime(rateStartTime);
                    }
                    if(horder.getEndTime().getTime()-endTime1.getTime()>0){
                        chargeDetails.setDetailEndTime(rateEndTime);
                    }else{
                        chargeDetails.setDetailEndTime(orderEndTime);
                        judgeEnd =true;
                    }
                    chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                    chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());

                    if(t3==1){
                        BigDecimal divide = new BigDecimal(cacheOrderData.getT3PowerQuantity()).divide(bigDecimal);

                        BigDecimal multiply1 = new BigDecimal(cacheOrderData.getT3PowerQuantity()).divide(bigDecimal).multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                        BigDecimal multiply2 = new BigDecimal(cacheOrderData.getT3PowerQuantity()).divide(bigDecimal).multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                        chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                        chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                        chargeDetails.setDetailPower(divide.doubleValue());
                        chargeDetailsStack.push(chargeDetails);
                    }else if(t3==2){

                        if(t32==2){
                            if(judgeStart){
                                rateStartTime = orderStartTime;
                            }
                            if(judgeEnd){
                                rateEndTime = orderEndTime;
                            }

                            DateTime parse2 = cn.hutool.core.date.DateUtil.parse(rateStartTime, "yyyy-MM-dd HH:mm:ss");
                            DateTime parse3 = cn.hutool.core.date.DateUtil.parse(rateEndTime, "yyyy-MM-dd HH:mm:ss");
                            long betweenDay4 = cn.hutool.core.date.DateUtil.between(parse3,parse2, DateUnit.MINUTE);

                            //获得电量
                            BigDecimal multiply = quantity.multiply(new BigDecimal(betweenDay4));


                            BigDecimal multiply1 = multiply.multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                            BigDecimal multiply2 = multiply.multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                            chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                            chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());
                            chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                            chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                            chargeDetails.setDetailPower(multiply.doubleValue());
                            chargeDetailsStack.push(chargeDetails);

                            //剩余电量
                            bigDecimal3 = bigDecimal3.subtract(multiply);

                        }else{
                            BigDecimal multiply1 = bigDecimal3.multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                            BigDecimal multiply2 = bigDecimal3.multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                            chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                            chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());
                            chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                            chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                            chargeDetails.setDetailPower(bigDecimal3.doubleValue());
                            chargeDetailsStack.push(chargeDetails);
                        }
                        t32 = t32-1;
                    }else if(t3==3){
                        if(t33==3){
                            if(judgeStart){
                                rateStartTime = orderStartTime;
                            }
                            if(judgeEnd){
                                rateEndTime = orderEndTime;
                            }
                            DateTime parse2 = cn.hutool.core.date.DateUtil.parse(rateStartTime, "yyyy-MM-dd HH:mm:ss");
                            DateTime parse3 = cn.hutool.core.date.DateUtil.parse(rateEndTime, "yyyy-MM-dd HH:mm:ss");
                            long betweenDay4 = cn.hutool.core.date.DateUtil.between(parse3,parse2, DateUnit.MINUTE);
                            //获得电量
                            BigDecimal multiply = quantity.multiply(new BigDecimal(betweenDay4));

                            BigDecimal multiply1 = multiply.multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                            BigDecimal multiply2 = multiply.multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                            chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                            chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());
                            chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                            chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                            chargeDetails.setDetailPower(multiply.doubleValue());
                            chargeDetailsStack.push(chargeDetails);

                            //剩余电量
                            bigDecimal3 = bigDecimal3.subtract(multiply);
                        }else if(t33==2){

                            if(judgeStart){
                                rateStartTime = orderStartTime;
                            }
                            if(judgeEnd){
                                rateEndTime = orderEndTime;
                            }
                            DateTime parse2 = cn.hutool.core.date.DateUtil.parse(rateStartTime, "yyyy-MM-dd HH:mm:ss");
                            DateTime parse3 = cn.hutool.core.date.DateUtil.parse(rateEndTime, "yyyy-MM-dd HH:mm:ss");
                            long betweenDay4 = cn.hutool.core.date.DateUtil.between(parse3,parse2, DateUnit.MINUTE);
                            //获得电量
                            BigDecimal multiply = quantity.multiply(new BigDecimal(betweenDay4));


                            BigDecimal multiply1 = multiply.multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                            BigDecimal multiply2 = multiply.multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                            chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                            chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());
                            chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                            chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                            chargeDetails.setDetailPower(multiply.doubleValue());
                            chargeDetailsStack.push(chargeDetails);

                            //剩余电量
                            bigDecimal3 = bigDecimal3.subtract(multiply);
                        }else{
                            BigDecimal multiply1 = bigDecimal3.multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                            BigDecimal multiply2 = bigDecimal3.multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                            chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                            chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());
                            chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                            chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                            chargeDetails.setDetailPower(bigDecimal3.doubleValue());
                            chargeDetailsStack.push(chargeDetails);
                        }
                        t33 = t33-1;
                    }

                }else if ("03".equals(rateValue)) {
                    boolean judgeStart =false;
                    boolean judgeEnd =false;

                    if(horder.getStartTime().getTime()-startTime1.getTime()>0){
                        chargeDetails.setDetailStartTime(orderStartTime);
                        judgeStart =true;
                    }else{
                        chargeDetails.setDetailStartTime(rateStartTime);
                    }
                    if(horder.getEndTime().getTime()-endTime1.getTime()>0){
                        chargeDetails.setDetailEndTime(rateEndTime);
                    }else{
                        chargeDetails.setDetailEndTime(orderEndTime);
                        judgeEnd =true;
                    }
                    chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                    chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());

                    if(t4==1){
                        BigDecimal divide = new BigDecimal(cacheOrderData.getT4PowerQuantity()).divide(bigDecimal);

                        BigDecimal multiply1 = new BigDecimal(cacheOrderData.getT4PowerQuantity()).divide(bigDecimal).multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                        BigDecimal multiply2 = new BigDecimal(cacheOrderData.getT4PowerQuantity()).divide(bigDecimal).multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                        chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                        chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                        chargeDetails.setDetailPower(divide.doubleValue());
                        chargeDetailsStack.push(chargeDetails);
                    }else if(t4==2){

                        if(t42==2){
                            if(judgeStart){
                                rateStartTime = orderStartTime;
                            }
                            if(judgeEnd){
                                rateEndTime = orderEndTime;
                            }

                            DateTime parse2 = cn.hutool.core.date.DateUtil.parse(rateStartTime, "yyyy-MM-dd HH:mm:ss");
                            DateTime parse3 = cn.hutool.core.date.DateUtil.parse(rateEndTime, "yyyy-MM-dd HH:mm:ss");
                            long betweenDay4 = cn.hutool.core.date.DateUtil.between(parse3,parse2, DateUnit.MINUTE);

                            //获得电量
                            BigDecimal multiply = quantity.multiply(new BigDecimal(betweenDay4));


                            BigDecimal multiply1 = multiply.multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                            BigDecimal multiply2 = multiply.multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                            chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                            chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());
                            chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                            chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                            chargeDetails.setDetailPower(multiply.doubleValue());
                            chargeDetailsStack.push(chargeDetails);

                            //剩余电量
                            bigDecimal4 = bigDecimal4.subtract(multiply);

                        }else{
                            BigDecimal multiply1 = bigDecimal4.multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                            BigDecimal multiply2 = bigDecimal4.multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                            chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                            chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());
                            chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                            chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                            chargeDetails.setDetailPower(bigDecimal4.doubleValue());
                            chargeDetailsStack.push(chargeDetails);
                        }
                        t42 = t42-1;
                    }else if(t4==3){
                        if(t43==3){
                            if(judgeStart){
                                rateStartTime = orderStartTime;
                            }
                            if(judgeEnd){
                                rateEndTime = orderEndTime;
                            }
                            DateTime parse2 = cn.hutool.core.date.DateUtil.parse(rateStartTime, "yyyy-MM-dd HH:mm:ss");
                            DateTime parse3 = cn.hutool.core.date.DateUtil.parse(rateEndTime, "yyyy-MM-dd HH:mm:ss");
                            long betweenDay4 = cn.hutool.core.date.DateUtil.between(parse3,parse2, DateUnit.MINUTE);
                            //获得电量
                            BigDecimal multiply = quantity.multiply(new BigDecimal(betweenDay4));

                            BigDecimal multiply1 = multiply.multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                            BigDecimal multiply2 = multiply.multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                            chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                            chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());
                            chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                            chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                            chargeDetails.setDetailPower(multiply.doubleValue());
                            chargeDetailsStack.push(chargeDetails);

                            //剩余电量
                            bigDecimal4 = bigDecimal4.subtract(multiply);
                        }else if(t43==2){

                            if(judgeStart){
                                rateStartTime = orderStartTime;
                            }
                            if(judgeEnd){
                                rateEndTime = orderEndTime;
                            }
                            DateTime parse2 = cn.hutool.core.date.DateUtil.parse(rateStartTime, "yyyy-MM-dd HH:mm:ss");
                            DateTime parse3 = cn.hutool.core.date.DateUtil.parse(rateEndTime, "yyyy-MM-dd HH:mm:ss");
                            long betweenDay4 = cn.hutool.core.date.DateUtil.between(parse3,parse2, DateUnit.MINUTE);
                            //获得电量
                            BigDecimal multiply = quantity.multiply(new BigDecimal(betweenDay4));


                            BigDecimal multiply1 = multiply.multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                            BigDecimal multiply2 = multiply.multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                            chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                            chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());
                            chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                            chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                            chargeDetails.setDetailPower(multiply.doubleValue());
                            chargeDetailsStack.push(chargeDetails);

                            //剩余电量
                            bigDecimal4 = bigDecimal4.subtract(multiply);
                        }else{
                            BigDecimal multiply1 = bigDecimal4.multiply(xpcRateTime.getPowerFee()).setScale(2, BigDecimal.ROUND_HALF_UP);
                            BigDecimal multiply2 = bigDecimal4.multiply(xpcRateTime.getServiceFee()).setScale(2, BigDecimal.ROUND_HALF_UP);

                            chargeDetails.setElecPrice(xpcRateTime.getPowerFee().doubleValue());
                            chargeDetails.setSevicePrice(xpcRateTime.getServiceFee().doubleValue());
                            chargeDetails.setDetailElecMoney(multiply1.doubleValue());
                            chargeDetails.setDetailSeviceMoney(multiply2.doubleValue());
                            chargeDetails.setDetailPower(bigDecimal4.doubleValue());
                            chargeDetailsStack.push(chargeDetails);
                        }
                        t43 = t43-1;
                    }
                }
            }
        }
        logger.info("=====8888=====开始推送====推送返回值============calculateEmYu=====");
        //REDIS.deleteObject("Yu"+horder.getSerialNumber());
        return chargeDetailsStack;
    }

    public ChargeOrderInfoResponse notify(ChargeOrderInfo chargeOrderInfo, AuthSecretToken authSecretTokenOut) throws JsonProcessingException {
        logger.info("====111======开始推送====推送返回值============calculateEmYu=====");
        String data = JSONUtil.toJSONString(chargeOrderInfo);
        CommonRequest<ChargeOrderInfo> commonRequest = new CommonRequest<>();
        commonRequest.setData(data);
        String responseBody = ok(commonRequest, "/notification_charge_order_info", authSecretTokenOut);
        System.out.println("=======推送充电订单信息结果============"+responseBody);
        return DTOJsonHelper.parseResponseData(responseBody,
                ChargeOrderInfoResponse.class, authSecretTokenOut);
    }
}
