diff --git a/evcs-modules/evcs-common/src/main/java/com/xhpc/evcs/domain/EtPushFailedOrder.java b/evcs-modules/evcs-common/src/main/java/com/xhpc/evcs/domain/EtPushFailedOrder.java new file mode 100644 index 00000000..86a536ca --- /dev/null +++ b/evcs-modules/evcs-common/src/main/java/com/xhpc/evcs/domain/EtPushFailedOrder.java @@ -0,0 +1,21 @@ +package com.xhpc.evcs.domain; + +import lombok.Getter; +import lombok.Setter; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Getter +@Setter +@Entity +@Table(name = "ET_PUSH_FAILED_ORDER") +public class EtPushFailedOrder { + + @Id + private Long id; + + private String operatorId3rdpty; + +} diff --git a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/jpa/EtPushFailedOrderRepository.java b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/jpa/EtPushFailedOrderRepository.java new file mode 100644 index 00000000..9407bc79 --- /dev/null +++ b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/jpa/EtPushFailedOrderRepository.java @@ -0,0 +1,13 @@ +package com.xhpc.evcs.jpa; + +import com.xhpc.evcs.domain.EtPushFailedOrder; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.repository.query.QueryByExampleExecutor; +import org.springframework.stereotype.Repository; + +@Repository +public interface EtPushFailedOrderRepository extends JpaRepository, + QueryByExampleExecutor, JpaSpecificationExecutor { + +} diff --git a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/jpa/XhpcHistoryOrderRepository.java b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/jpa/XhpcHistoryOrderRepository.java index b26d749a..ed5188a7 100644 --- a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/jpa/XhpcHistoryOrderRepository.java +++ b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/jpa/XhpcHistoryOrderRepository.java @@ -3,6 +3,7 @@ package com.xhpc.evcs.jpa; import com.xhpc.order.domain.XhpcHistoryOrder; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.QueryByExampleExecutor; import org.springframework.stereotype.Repository; @@ -18,4 +19,8 @@ public interface XhpcHistoryOrderRepository extends JpaRepository findByConfirmResultNotAndSource(int confirmResult, int source); + @Query("select ho from XhpcHistoryOrder ho join EtPushFailedOrder fo on ho.historyOrderId = fo.id where fo" + + ".operatorId3rdpty = ?1") + List findJoinPushFailedOrders(String operatorId3irdpty); + } diff --git a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/CoreDispatcher.java b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/CoreDispatcher.java index c851d04b..8f62f774 100644 --- a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/CoreDispatcher.java +++ b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/CoreDispatcher.java @@ -46,6 +46,12 @@ public class CoreDispatcher { @Transactional public String ok(Object object, String url, AuthSecretToken authSecretTokenOut) { + return ok(object, url, authSecretTokenOut, null); + } + + @Transactional + public String ok(Object object, String url, AuthSecretToken authSecretTokenOut, String stationOperatorId) { + if (authSecretTokenOut == null) { log.error("authSecretTokenOut is null"); throw new RuntimeException(); @@ -65,7 +71,11 @@ public class CoreDispatcher { try { if (object.getClass().getSimpleName().equals("CommonRequest")) { CommonRequest commonRequest = (CommonRequest) object; - commonRequest.setOperatorId(authSecretTokenOut.getOperatorId()); + if (stationOperatorId != null) { + commonRequest.setOperatorId(stationOperatorId); + } else { + commonRequest.setOperatorId(authSecretTokenOut.getOperatorId()); + } Date tokenExpiry = authSecretTokenOut.getTokenExpiry(); String originalData = commonRequest.getData(); String tData; diff --git a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/NotificationChargeOrderInfo4BonusTask.java b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/NotificationChargeOrderInfo4BonusTask.java index c983a9e7..11a24054 100644 --- a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/NotificationChargeOrderInfo4BonusTask.java +++ b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/NotificationChargeOrderInfo4BonusTask.java @@ -1,9 +1,6 @@ package com.xhpc.evcs.notification; -import com.xhpc.evcs.domain.AuthSecretToken; -import com.xhpc.evcs.domain.EtOrderMapping; -import com.xhpc.evcs.domain.XhpcChargingStation; -import com.xhpc.evcs.domain.XhpcStatisticsTimeInterval; +import com.xhpc.evcs.domain.*; import com.xhpc.evcs.dto.CDChargeOrder4BonusInfoRes; import com.xhpc.evcs.dto.CDChargeOrderInfo4BonusReq; import com.xhpc.evcs.dto.CommonRequest; @@ -12,6 +9,7 @@ import com.xhpc.evcs.jpa.*; import com.xhpc.evcs.utils.DateUtil; import com.xhpc.evcs.utils.JSONUtil; import com.xhpc.order.domain.XhpcHistoryOrder; +import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -25,11 +23,14 @@ import java.util.List; import static com.xhpc.evcs.domain.AuthSecretToken.SECRET_TOKEN_TYPE_OUT; @Component +@Slf4j public class NotificationChargeOrderInfo4BonusTask extends CoreDispatcher { @Autowired private XhpcHistoryOrderRepository xhpcHistoryOrderRepository; @Autowired + private EtPushFailedOrderRepository etPushFailedOrderRepository; + @Autowired private AuthSecretTokenRepository authSecretTokenRepository; @Autowired private StatisticTimeIntervalRepository statisticTimeIntervalRepository; @@ -37,7 +38,7 @@ public class NotificationChargeOrderInfo4BonusTask extends CoreDispatcher { private OrderMappingRepository orderMappingRepository; @Autowired private XhpcChargingStationRepository chargingStationRepo; - private Logger logger = LoggerFactory.getLogger(NotificationChargeOrderInfo4BonusTask.class); + private final Logger logger = LoggerFactory.getLogger(NotificationChargeOrderInfo4BonusTask.class); @Scheduled(fixedDelay = 1000 * 60) public void run() throws IOException { @@ -53,12 +54,23 @@ public class NotificationChargeOrderInfo4BonusTask extends CoreDispatcher { List statisticTimeIntervalList = statisticTimeIntervalRepository.findByHistoryOrderId(xhpcHistoryOrder.getHistoryOrderId()); xhpcHistoryOrder.setXhpcStatisticsTimeIntervalList(statisticTimeIntervalList); - notify(xhpcHistoryOrder, authSecretTokenOut); + notify(xhpcHistoryOrder, authSecretTokenOut, false); + } + final List pushFailedOrders = + xhpcHistoryOrderRepository.findJoinPushFailedOrders(authSecretTokenOut.getOperatorId3irdpty()); + for (XhpcHistoryOrder xhpcHistoryOrder : pushFailedOrders) { + List statisticTimeIntervalList = + statisticTimeIntervalRepository.findByHistoryOrderId(xhpcHistoryOrder.getHistoryOrderId()); + xhpcHistoryOrder.setXhpcStatisticsTimeIntervalList(statisticTimeIntervalList); + final boolean notify = notify(xhpcHistoryOrder, authSecretTokenOut, true); + if (notify) { + etPushFailedOrderRepository.deleteById(xhpcHistoryOrder.getHistoryOrderId()); + } } } } - public void notify(XhpcHistoryOrder xhpcHistoryOrder, AuthSecretToken authSecretTokenOut) throws IOException { + public boolean notify(XhpcHistoryOrder xhpcHistoryOrder, AuthSecretToken authSecretTokenOut, boolean isRepush) throws IOException { String operatorIdEvcs = xhpcHistoryOrder.getOperatorIdEvcs(); if (operatorIdEvcs == null) { @@ -67,7 +79,7 @@ public class NotificationChargeOrderInfo4BonusTask extends CoreDispatcher { String stationOperatorIdEvcs = station.getOperatorIdEvcs(); if (stationOperatorIdEvcs == null) { logger.error("station[{}] operator id evcs not set", chargingStationId); - return; + return false; } operatorIdEvcs = stationOperatorIdEvcs.substring(8, 17); } @@ -78,15 +90,26 @@ public class NotificationChargeOrderInfo4BonusTask extends CoreDispatcher { String data = JSONUtil.toJSONString(cdChargeOrderInfo4BonusReq); CommonRequest commonRequest = new CommonRequest<>(); commonRequest.setData(data); - String responseBody = ok(commonRequest, "/notification_charge_order_info_for_bonus", authSecretTokenOut); + String responseBody = ok(commonRequest, "/notification_charge_order_info_for_bonus", authSecretTokenOut, + operatorIdEvcs); CDChargeOrder4BonusInfoRes cdChargeOrder4BonusInfoRes = DTOJsonHelper.parseResponseData(responseBody, CDChargeOrder4BonusInfoRes.class, authSecretTokenOut); - if (cdChargeOrder4BonusInfoRes != null && cdChargeOrder4BonusInfoRes.getConfirmResult() == 0) { + if (!isRepush) { authSecretTokenOut.setLastPushOrder(xhpcHistoryOrder.getHistoryOrderId()); authSecretTokenRepository.save(authSecretTokenOut); + } + if (cdChargeOrder4BonusInfoRes != null && cdChargeOrder4BonusInfoRes.getConfirmResult() == 0) { + return true; } else { - throw new RuntimeException(String.format("push CD notification order[%s] failed: %s", - xhpcHistoryOrder.getSerialNumber(), responseBody)); + if (!isRepush) { + EtPushFailedOrder etPushFailedOrder = new EtPushFailedOrder(); + etPushFailedOrder.setId(xhpcHistoryOrder.getHistoryOrderId()); + etPushFailedOrder.setOperatorId3rdpty(authSecretTokenOut.getOperatorId3irdpty()); + etPushFailedOrderRepository.save(etPushFailedOrder); + } + logger.error(String.format("push CD notification order[%s] failed: %s", xhpcHistoryOrder.getSerialNumber(), + responseBody)); + return false; } }