package com.jsowell.thirdparty.common; import com.google.common.collect.Lists; import com.jsowell.common.enums.thirdparty.ThirdPlatformTypeEnum; import com.jsowell.common.enums.ykc.ReturnCodeEnum; import com.jsowell.common.exception.BusinessException; import com.jsowell.common.util.StringUtils; import com.jsowell.pile.dto.nanrui.PushAlarmInfoDTO; import com.jsowell.pile.vo.ThirdPartySecretInfoVO; import com.jsowell.thirdparty.platform.service.ThirdPartyPlatformService; import com.jsowell.thirdparty.platform.factory.ThirdPartyPlatformFactory; import com.jsowell.thirdparty.service.ThirdpartySecretInfoService; import org.apache.commons.collections4.CollectionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * 主动通知Service * 说明:创建此接口目的是为了简化开发 * 在需要通知第三方平台的地方,异步调用此类中对应的方法 * 通知方法中应该根据对接平台,自动找到响应的平台处理逻辑 */ @Service public class NotificationService { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private ThirdpartySecretInfoService thirdpartySecretInfoService; /** * 充电站信息变化推送 * notification_stationInfo */ public String notificationStationInfo(NotificationDTO dto) { String stationId = dto.getStationId(); String platformType = dto.getPlatformType(); if (StringUtils.isBlank(stationId)) { throw new BusinessException(ReturnCodeEnum.CODE_PARAM_NOT_NULL_ERROR); } // 通过stationId 查询该站点需要对接的平台配置 List secretInfoVOS = thirdpartySecretInfoService.queryStationToPlatformList(stationId); if (CollectionUtils.isEmpty(secretInfoVOS)) { return null; } // 调用相应平台的处理方法 StringBuilder result = new StringBuilder(); for (ThirdPartySecretInfoVO secretInfoVO : secretInfoVOS) { if (StringUtils.isNotBlank(platformType) && !StringUtils.equals(platformType, secretInfoVO.getPlatformType())) { // 如果dto中的platformType不为空,并且不等于secretInfoVO.getPlatformType(),continue continue; } try { ThirdPartyPlatformService platformService = ThirdPartyPlatformFactory.getInvokeStrategy(secretInfoVO.getPlatformType()); String postResult = platformService.notificationStationInfo(stationId); result.append(postResult).append("\n"); } catch (Exception e) { logger.error("平台类型:{}, 平台名称:{}, 站点id:{}, 充电站信息变化推送error:{}", secretInfoVO.getPlatformType(), secretInfoVO.getPlatformName(), stationId, e.getMessage()); } } return result.toString(); } /** * 设备状态变化推送 * notification_stationStatus */ public void notificationStationStatus(NotificationDTO dto) { String stationId = dto.getStationId(); String pileConnectorCode = dto.getPileConnectorCode(); String status = dto.getStatus(); String platformType = dto.getPlatformType(); if (StringUtils.isBlank(stationId) || StringUtils.isBlank(pileConnectorCode) || StringUtils.isBlank(status)) { throw new BusinessException(ReturnCodeEnum.CODE_PARAM_NOT_NULL_ERROR); } // 通过stationId 查询该站点需要对接的平台配置 List secretInfoVOS = thirdpartySecretInfoService.queryStationToPlatformList(stationId); if (CollectionUtils.isEmpty(secretInfoVOS)) { return; } // 调用相应平台的处理方法 for (ThirdPartySecretInfoVO secretInfoVO : secretInfoVOS) { if (StringUtils.isNotBlank(platformType) && !StringUtils.equals(platformType, secretInfoVO.getPlatformType())) { // 如果dto中的platformType不为空,并且不等于secretInfoVO.getPlatformType(),continue continue; } try { // 根据平台类型获取Service ThirdPartyPlatformService platformService = ThirdPartyPlatformFactory.getInvokeStrategy(secretInfoVO.getPlatformType()); platformService.notificationStationStatus(stationId, pileConnectorCode, status, secretInfoVO); } catch (Exception e) { logger.error("平台类型:{}, 平台名称:{}, 站点id:{}, 枪口编号:{}, 设备状态变化推送error:{}", secretInfoVO.getPlatformType(), secretInfoVO.getPlatformName(), stationId, pileConnectorCode, e.getMessage()); } } } /** * 设备充电中状态变化推送 * notification_connector_charge_status * notification_equip_charge_status */ public void notificationConnectorChargeStatus(NotificationDTO dto) { String stationId = dto.getStationId(); String orderCode = dto.getOrderCode(); String platformType = dto.getPlatformType(); if (StringUtils.isBlank(stationId) || StringUtils.isBlank(orderCode)) { throw new BusinessException(ReturnCodeEnum.CODE_PARAM_NOT_NULL_ERROR); } // 通过stationId 查询该站点需要对接的平台配置 List secretInfoVOS = thirdpartySecretInfoService.queryStationToPlatformList(stationId); if (CollectionUtils.isEmpty(secretInfoVOS)) { return; } // 调用相应平台的处理方法 for (ThirdPartySecretInfoVO secretInfoVO : secretInfoVOS) { if (StringUtils.isNotBlank(platformType) && !StringUtils.equals(platformType, secretInfoVO.getPlatformType())) { // 如果dto中的platformType不为空,并且不等于secretInfoVO.getPlatformType(),continue continue; } // 根据平台类型获取Service ThirdPartyPlatformService platformService = ThirdPartyPlatformFactory.getInvokeStrategy(secretInfoVO.getPlatformType()); try { platformService.notificationConnectorChargeStatus(orderCode, secretInfoVO); } catch (Exception e) { logger.error("平台类型:{}, 平台名称:{}, 站点id:{}, 订单编号:{}, 设备充电中状态变化推送error:{}", secretInfoVO.getPlatformType(), secretInfoVO.getPlatformName(), stationId, orderCode, e.getMessage()); } finally { try { platformService.notificationEquipChargeStatus(orderCode); }catch (Exception e){ logger.error("notification_equip_charge_status error", e); } } } } /** * 充电订单信息推送 * notification_orderInfo/notification_charge_order_info */ public void notificationChargeOrderInfo(NotificationDTO dto) { String stationId = dto.getStationId(); String orderCode = dto.getOrderCode(); String platformType = dto.getPlatformType(); if (StringUtils.isBlank(stationId) || StringUtils.isBlank(orderCode)) { throw new BusinessException(ReturnCodeEnum.CODE_PARAM_NOT_NULL_ERROR); } // 通过stationId 查询该站点需要对接的平台配置 List secretInfoVOS = thirdpartySecretInfoService.queryStationToPlatformList(stationId); if (CollectionUtils.isEmpty(secretInfoVOS)) { return; } // 调用相应平台的处理方法 for (ThirdPartySecretInfoVO secretInfoVO : secretInfoVOS) { if (StringUtils.isNotBlank(platformType) && !StringUtils.equals(platformType, secretInfoVO.getPlatformType())) { // 如果dto中的platformType不为空,并且不等于secretInfoVO.getPlatformType(),continue continue; } try { // 根据平台类型获取Service ThirdPartyPlatformService platformService = ThirdPartyPlatformFactory.getInvokeStrategy(secretInfoVO.getPlatformType()); String type = secretInfoVO.getPlatformType(); //充电订单信息推送 //判断实现此接口没有 // 25 , 20 ,17 ,18,7,24,3,1,10,13,26,16,23,6, 21,15,2 if(type.equals("25") || type.equals("17") ||type.equals("20") || type.equals("7") || type.equals("24") || type.equals("3") || type.equals("1") || type.equals("10") || type.equals("13") || type.equals("26") || type.equals("16") || type.equals("23") || type.equals("6") || type.equals("21") || type.equals("15") || type.equals("2")){ platformService.notificationChargeOrderInfo(orderCode, secretInfoVO); } //订单信息推送 //12,7,1,10,13,11,6 if(type.equals("12") || type.equals("7") || type.equals("1") || type.equals("10") || type.equals("13") || type.equals("11") || type.equals("6")){ platformService.notificationChargeOrderInfo(orderCode); } //停止充电结果推送 //25 , 7,1,23,6,4,2 if(type.equals("25") || type.equals("7") || type.equals("1") || type.equals("23") || type.equals("6") || type.equals("4") || type.equals("2")){ platformService.notificationStopChargeResult(orderCode); } //推送充换电站用能统计信息 //18,10,13,26 if(type.equals("18") || type.equals("10") || type.equals("13") || type.equals("26")){ platformService.notificationOperationStatsInfo(stationId); } //推送充电账单信息 //25 if(type.equals("25")){ platformService.notificationPayOrderInfo(orderCode); } } catch (Exception e) { logger.error("充电订单信息推送error", e); } } } public void commonPushStartChargeResult(NotificationDTO dto) { logger.info("开始调用commonPushStartChargeResult接口"); String stationId = dto.getStationId(); String orderCode = dto.getOrderCode(); String platformType = dto.getPlatformType(); if (StringUtils.isBlank(stationId) || StringUtils.isBlank(orderCode)) { throw new BusinessException(ReturnCodeEnum.CODE_PARAM_NOT_NULL_ERROR); } // 通过stationId 查询该站点需要对接的平台配置 List secretInfoVOS = thirdpartySecretInfoService.queryStationToPlatformList(stationId); if (CollectionUtils.isEmpty(secretInfoVOS)) { return; } // 调用相应平台的处理方法 for (ThirdPartySecretInfoVO secretInfoVO : secretInfoVOS) { if (StringUtils.isNotBlank(platformType) && !StringUtils.equals(platformType, secretInfoVO.getPlatformType())) { // 如果dto中的platformType不为空,并且不等于secretInfoVO.getPlatformType(),continue continue; } try { // 根据平台类型获取Service ThirdPartyPlatformService platformService = ThirdPartyPlatformFactory.getInvokeStrategy(secretInfoVO.getPlatformType()); platformService.notificationStartChargeResult(orderCode); } catch (Exception e) { logger.error("充电订单信息推送error", e); } } } /** * 站点功率信息推送 * notification_orderInfo/notification_charge_order_info */ public void notificationStationPowerInfo(NotificationDTO dto) { String stationId = dto.getStationId(); String platformType = dto.getPlatformType(); // 通过stationId 查询该站点需要对接的平台配置 List secretInfoVOS = thirdpartySecretInfoService.queryStationToPlatformList(stationId); if (CollectionUtils.isEmpty(secretInfoVOS)) { return; } // 调用相应平台的处理方法 for (ThirdPartySecretInfoVO secretInfoVO : secretInfoVOS) { if (StringUtils.isNotBlank(platformType) && !StringUtils.equals(platformType, secretInfoVO.getPlatformType())) { // 如果dto中的platformType不为空,并且不等于secretInfoVO.getPlatformType(),continue continue; } try { // 根据平台类型获取Service ThirdPartyPlatformService platformService = ThirdPartyPlatformFactory.getInvokeStrategy(secretInfoVO.getPlatformType()); platformService.notificationPowerInfo(Lists.newArrayList(stationId)); } catch (Exception e) { logger.error("站点功率信息推送error", e); } } } /** * 站点费率变化推送 * notification_stationFee */ public void notificationStationFee(NotificationDTO dto) { String stationId = dto.getStationId(); String platformType = dto.getPlatformType(); if (StringUtils.isBlank(stationId)) { throw new BusinessException(ReturnCodeEnum.CODE_PARAM_NOT_NULL_ERROR); } // 通过stationId 查询该站点需要对接的平台配置 List secretInfoVOS = thirdpartySecretInfoService.queryStationToPlatformList(stationId); if (CollectionUtils.isEmpty(secretInfoVOS)) { return; } // 调用相应平台的处理方法 for (ThirdPartySecretInfoVO secretInfoVO : secretInfoVOS) { if (StringUtils.isNotBlank(platformType) && !StringUtils.equals(platformType, secretInfoVO.getPlatformType())) { // 如果dto中的platformType不为空,并且不等于secretInfoVO.getPlatformType(),continue continue; } try { // 根据平台类型获取Service ThirdPartyPlatformService platformService = ThirdPartyPlatformFactory.getInvokeStrategy(secretInfoVO.getPlatformType()); platformService.notificationStationFee(stationId, secretInfoVO); } catch (Exception e) { logger.error("站点费率变化推送error", e); } } } /** * 历史充电订单信息推送 * notification_orderInfo/notification_charge_order_info */ public void notificationChargeOrderInfoHistory(NotificationDTO dto) { String stationId = dto.getStationId(); String orderCode = dto.getOrderCode(); String platformType = dto.getPlatformType(); if (StringUtils.isBlank(stationId) || StringUtils.isBlank(orderCode)) { throw new BusinessException(ReturnCodeEnum.CODE_PARAM_NOT_NULL_ERROR); } // 通过stationId 查询该站点需要对接的平台配置 List secretInfoVOS = thirdpartySecretInfoService.queryStationToPlatformList(stationId); if (CollectionUtils.isEmpty(secretInfoVOS)) { return; } // 调用相应平台的处理方法 for (ThirdPartySecretInfoVO secretInfoVO : secretInfoVOS) { if (StringUtils.isNotBlank(platformType) && !StringUtils.equals(platformType, secretInfoVO.getPlatformType())) { // 如果dto中的platformType不为空,并且不等于secretInfoVO.getPlatformType(),continue continue; } try { // 根据平台类型获取Service ThirdPartyPlatformService platformService = ThirdPartyPlatformFactory.getInvokeStrategy(secretInfoVO.getPlatformType()); platformService.notificationChargeOrderInfoHistory(orderCode); } catch (Exception e) { logger.error("历史充电订单信息推送error", e); } } } /** * 充电设备告警信息推送 * notification_alarmInfo * @param dto */ public void notificationAlarmInfo(NotificationDTO dto) { String stationId = dto.getStationId(); String pileConnectorCode = dto.getPileConnectorCode(); String status = dto.getStatus(); String platformType = dto.getPlatformType(); if (StringUtils.isBlank(status) || StringUtils.isBlank(pileConnectorCode)) { throw new BusinessException(ReturnCodeEnum.CODE_PARAM_NOT_NULL_ERROR); } // 通过stationId 查询该站点需要对接的平台配置 List secretInfoVOS = thirdpartySecretInfoService.queryStationToPlatformList(stationId); if (CollectionUtils.isEmpty(secretInfoVOS)) { return; } // 调用相应平台的处理方法 for (ThirdPartySecretInfoVO secretInfoVO : secretInfoVOS) { if (StringUtils.isNotBlank(platformType) && !StringUtils.equals(platformType, secretInfoVO.getPlatformType())) { // 如果dto中的platformType不为空,并且不等于secretInfoVO.getPlatformType(),continue continue; } try { // 根据平台类型获取Service ThirdPartyPlatformService platformService = ThirdPartyPlatformFactory.getInvokeStrategy(secretInfoVO.getPlatformType()); PushAlarmInfoDTO pushAlarmInfoDTO = new PushAlarmInfoDTO(); pushAlarmInfoDTO.setPileConnectorCode(pileConnectorCode); pushAlarmInfoDTO.setConnectorStatus(status); platformService.notificationAlarmInfo(pushAlarmInfoDTO); } catch (Exception e) { logger.error("充电设备告警信息推送error", e); } } } }