From 2bbba5c77282f696eee46e38cf2ce9c9a65ba4a2 Mon Sep 17 00:00:00 2001 From: Lemon Date: Tue, 29 Oct 2024 15:01:06 +0800 Subject: [PATCH] =?UTF-8?q?update=20=20=E5=AE=81=E6=B3=A2=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0Service?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/ZheJiangPlatformServiceImpl.java | 1 + .../ZhongDianLianPlatformServiceImpl.java | 914 +++++++++++++++++- 2 files changed, 908 insertions(+), 7 deletions(-) diff --git a/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/platform/service/impl/ZheJiangPlatformServiceImpl.java b/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/platform/service/impl/ZheJiangPlatformServiceImpl.java index 29fdebb8e..223ab2135 100644 --- a/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/platform/service/impl/ZheJiangPlatformServiceImpl.java +++ b/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/platform/service/impl/ZheJiangPlatformServiceImpl.java @@ -431,6 +431,7 @@ public class ZheJiangPlatformServiceImpl implements ThirdPartyPlatformService { * 推送充电设备接口状态信息 * supervise_notification_connector_status */ + @Override public String notificationStationStatus(String stationId, String pileConnectorCode, String status, ThirdPartySecretInfoVO secretInfoVO) { // 查询充电枪口状态 PileConnectorInfoVO connectorInfo = pileConnectorInfoService.getPileConnectorInfoByConnectorCode(pileConnectorCode); diff --git a/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/platform/service/impl/ZhongDianLianPlatformServiceImpl.java b/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/platform/service/impl/ZhongDianLianPlatformServiceImpl.java index 93757e180..148aa7687 100644 --- a/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/platform/service/impl/ZhongDianLianPlatformServiceImpl.java +++ b/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/platform/service/impl/ZhongDianLianPlatformServiceImpl.java @@ -1,26 +1,61 @@ package com.jsowell.thirdparty.platform.service.impl; +import cn.hutool.core.util.PageUtil; import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONObject; +import com.github.pagehelper.PageInfo; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.jsowell.common.constant.Constants; +import com.jsowell.common.core.domain.ykc.RealTimeMonitorData; +import com.jsowell.common.enums.thirdparty.BusinessInformationExchangeEnum; +import com.jsowell.common.enums.thirdparty.ThirdPartyOperatorIdEnum; import com.jsowell.common.enums.thirdparty.ThirdPlatformTypeEnum; -import com.jsowell.common.util.JWTUtils; -import com.jsowell.common.util.StringUtils; -import com.jsowell.pile.thirdparty.CommonParamsDTO; +import com.jsowell.common.enums.ykc.OrderStatusEnum; +import com.jsowell.common.enums.ykc.PileConnectorDataBaseStatusEnum; +import com.jsowell.common.enums.ykc.ReturnCodeEnum; +import com.jsowell.common.exception.BusinessException; +import com.jsowell.common.util.*; +import com.jsowell.pile.domain.*; +import com.jsowell.pile.domain.ykcCommond.StartChargingCommand; +import com.jsowell.pile.dto.*; +import com.jsowell.pile.service.*; +import com.jsowell.pile.thirdparty.*; import com.jsowell.pile.vo.ThirdPartySecretInfoVO; -import com.jsowell.thirdparty.lianlian.vo.AccessTokenVO; +import com.jsowell.pile.vo.base.ConnectorInfoVO; +import com.jsowell.pile.vo.base.MerchantInfoVO; +import com.jsowell.pile.vo.base.ThirdPartyStationInfoVO; +import com.jsowell.pile.vo.base.ThirdPartyStationRelationVO; +import com.jsowell.pile.vo.lianlian.AccumulativeInfoVO; +import com.jsowell.pile.vo.uniapp.customer.BillingPriceVO; +import com.jsowell.pile.vo.web.PileConnectorInfoVO; +import com.jsowell.pile.vo.web.PileModelInfoVO; +import com.jsowell.pile.vo.zdl.EquipBusinessPolicyVO; +import com.jsowell.thirdparty.lianlian.domain.*; +import com.jsowell.thirdparty.lianlian.vo.*; +import com.jsowell.thirdparty.platform.domain.SupStationInfo; import com.jsowell.thirdparty.platform.factory.ThirdPartyPlatformFactory; import com.jsowell.thirdparty.platform.service.ThirdPartyPlatformService; import com.jsowell.thirdparty.platform.util.Cryptos; +import com.jsowell.thirdparty.platform.util.HttpRequestUtil; import com.jsowell.thirdparty.platform.util.ThirdPartyPlatformUtils; import com.jsowell.thirdparty.service.ThirdpartySecretInfoService; +import com.jsowell.thirdparty.yongchengboche.dto.YCBCGetTokenDTO; +import com.jsowell.thirdparty.zhongdianlian.dto.ZDLGetTokenDTO; +import org.apache.commons.collections4.CollectionUtils; +import org.bouncycastle.crypto.CryptoException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.util.List; -import java.util.Map; +import java.math.BigDecimal; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.spec.InvalidKeySpecException; +import java.util.*; +import java.util.stream.Collectors; /** - * 中电联 + * 中电联(宁波平台) */ @Service public class ZhongDianLianPlatformServiceImpl implements ThirdPartyPlatformService { @@ -30,6 +65,33 @@ public class ZhongDianLianPlatformServiceImpl implements ThirdPartyPlatformServi @Autowired private ThirdpartySecretInfoService thirdpartySecretInfoService; + @Autowired + private PileStationInfoService pileStationInfoService; + + @Autowired + private PileMerchantInfoService pileMerchantInfoService; + + @Autowired + private PileModelInfoService pileModelInfoService; + + @Autowired + private PileBasicInfoService pileBasicInfoService; + + @Autowired + private PileConnectorInfoService pileConnectorInfoService; + + @Autowired + private YKCPushCommandService ykcPushCommandService; + + @Autowired + private OrderBasicInfoService orderBasicInfoService; + + @Autowired + private PileBillingTemplateService pileBillingTemplateService; + + @Autowired + private PileRemoteService pileRemoteService; + @Override public void afterPropertiesSet() throws Exception { ThirdPartyPlatformFactory.register(thirdPlatformType, this); @@ -94,4 +156,842 @@ public class ZhongDianLianPlatformServiceImpl implements ThirdPartyPlatformServi return resultMap; } + /** + * 推送站点信息 + * @param stationId 充电站id + * @return + */ + @Override + public String notificationStationInfo(String stationId) { + // 通过id查询站点相关信息 + PileStationInfo pileStationInfo = pileStationInfoService.selectPileStationInfoById(Long.parseLong(stationId)); + // 查询相关配置信息 + ThirdPartySecretInfoVO ningBoSecretInfoVO = getNingBoSecretInfoVO(); + + String operatorId = ningBoSecretInfoVO.getTheirOperatorId(); + String operatorSecret = ningBoSecretInfoVO.getTheirOperatorSecret(); + String signSecret = ningBoSecretInfoVO.getTheirSigSecret(); + String dataSecret = ningBoSecretInfoVO.getTheirDataSecret(); + String dataSecretIv = ningBoSecretInfoVO.getTheirDataSecretIv(); + String urlAddress = ningBoSecretInfoVO.getTheirUrlPrefix(); + + // 组装中电联平台所需要的数据格式 + SupStationInfo info = SupStationInfo.builder() + .stationID(stationId) + .operatorID(Constants.OPERATORID_JIANG_SU) + // .equipmentOwnerId(Constants.OPERATORID_JIANG_SU) + .stationName(pileStationInfo.getStationName()) + .countryCode(pileStationInfo.getCountryCode()) + .areaCode(pileStationInfo.getAreaCode()) + .address(pileStationInfo.getAddress()) + .serviceTel(pileStationInfo.getStationTel()) + .stationType(Integer.valueOf(pileStationInfo.getStationType())) + .stationStatus(Integer.valueOf(pileStationInfo.getStationStatus())) + .parkNums(Integer.valueOf(pileStationInfo.getParkNums())) + .stationLng(new BigDecimal(pileStationInfo.getStationLng())) + .stationLat(new BigDecimal(pileStationInfo.getStationLat())) + .construction(Integer.valueOf(pileStationInfo.getConstruction())) + .build(); + String areaCode = pileStationInfo.getAreaCode(); // 330000,330200,330213 + // 根据逗号分组 + String[] split = StringUtils.split(areaCode, ","); + // 只取最后一部分 330213 + String subAreaCode = split[split.length - 1]; + info.setAreaCode(subAreaCode); + // 截取运营商组织机构代码(去除最后一位后的最后九位) + MerchantInfoVO merchantInfo = pileMerchantInfoService.getMerchantInfoVO(String.valueOf(pileStationInfo.getMerchantId())); + String organizationCode = merchantInfo.getOrganizationCode(); + if (StringUtils.isNotBlank(organizationCode) && organizationCode.length() == 18) { + String equipmentOwnerId = StringUtils.substring(organizationCode, organizationCode.length() - 10, organizationCode.length() - 1); + info.setEquipmentOwnerID(equipmentOwnerId); + } + // 站点图片 + if (StringUtils.isNotBlank(pileStationInfo.getPictures())) { + info.setPictures(Lists.newArrayList(pileStationInfo.getPictures().split(","))); + } + + List pileList = getPileList(pileStationInfo); + if (CollectionUtils.isNotEmpty(pileList)) { + info.setEquipmentInfos(pileList); // 充电设备信息列表 + } + + // 调用中电联平台接口 + String url = urlAddress + "notification_stationInfo"; + + JSONObject data = new JSONObject(); + data.put("StationInfo", info); + + String jsonString = JSON.toJSONString(data); + System.out.println("jsonString : " + jsonString); + + // 获取令牌 + ZDLGetTokenDTO tokenDTO = ZDLGetTokenDTO.builder() + .urlAddress(urlAddress) + .operatorId(operatorId) + .operatorSecret(operatorSecret) + .dataSecret(dataSecret) + .dataSecretIv(dataSecretIv) + .signSecret(signSecret) + .build(); + String token = getToken(urlAddress, operatorId, operatorSecret, dataSecretIv, signSecret, dataSecret); + String result = HttpRequestUtil.sendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret); + + return result; + } + + /** + * 查询站点信息 + * @param dto 查询站点信息dto + * @return + */ + @Override + public Map queryStationsInfo(QueryStationInfoDTO dto) { + int pageNo = dto.getPageNo() == null ? 1 : dto.getPageNo(); + int pageSize = dto.getPageSize() == null ? 10 : dto.getPageSize(); + dto.setThirdPlatformType(thirdPlatformType); + PageUtils.startPage(pageNo, pageSize); + List stationInfos = pileStationInfoService.selectStationInfosByThirdParty(dto); + if (CollectionUtils.isEmpty(stationInfos)) { + // 未查到数据 + return null; + } + // ThirdPartyPlatformConfig configInfo = thirdPartyPlatformConfigService.getInfoByOperatorId(dto.getOperatorId()); + ThirdPartySecretInfoVO thirdPartySecretInfoVO = getNingBoSecretInfoVO(); + PageInfo pageInfo = new PageInfo<>(stationInfos); + List resultList = new ArrayList<>(); + for (ThirdPartyStationInfoVO pileStationInfo : pageInfo.getList()) { + SupStationInfo stationInfo = new SupStationInfo(); + String stationId = String.valueOf(pileStationInfo.getId()); + stationInfo.setStationID(String.valueOf(pileStationInfo.getId())); + stationInfo.setOperatorID(Constants.OPERATORID_JIANG_SU); // 组织机构代码 + String organizationCode = pileStationInfo.getOrganizationCode(); + if (StringUtils.isNotBlank(organizationCode) && organizationCode.length() == 18) { + String equipmentOwnerId = StringUtils.substring(organizationCode, organizationCode.length() - 10, organizationCode.length() - 1); + stationInfo.setEquipmentOwnerID(equipmentOwnerId); + }else { + stationInfo.setEquipmentOwnerID(Constants.OPERATORID_JIANG_SU); + } + stationInfo.setStationName(pileStationInfo.getStationName()); + stationInfo.setCountryCode(pileStationInfo.getCountryCode()); + String areaCode = pileStationInfo.getAreaCode(); // 330000,330200,330213 + // 根据逗号分组 + String[] split = StringUtils.split(areaCode, ","); + // 只取最后一部分 330213 + String subAreaCode = split[split.length - 1]; + stationInfo.setAreaCode(subAreaCode); + stationInfo.setAddress(pileStationInfo.getAddress()); + stationInfo.setServiceTel(pileStationInfo.getStationTel()); + stationInfo.setStationType(Integer.parseInt(pileStationInfo.getStationType())); + stationInfo.setStationStatus(Integer.parseInt(pileStationInfo.getStationStatus())); + stationInfo.setParkNums(Integer.parseInt(pileStationInfo.getParkNums())); + stationInfo.setStationLng(new BigDecimal(pileStationInfo.getStationLng())); + stationInfo.setStationLat(new BigDecimal(pileStationInfo.getStationLat())); + stationInfo.setConstruction(Integer.parseInt(pileStationInfo.getConstruction())); + // 停车费率描述 + if (StringUtils.isNotBlank(pileStationInfo.getParkFeeDescribe())) { + stationInfo.setParkFee(pileStationInfo.getParkFeeDescribe()); + } + // 站点图片 + if (StringUtils.isNotBlank(pileStationInfo.getPictures())) { + stationInfo.setPictures(Lists.newArrayList(pileStationInfo.getPictures().split(","))); + } + + List pileList = getPileList(pileStationInfo); + if (CollectionUtils.isNotEmpty(pileList)) { + stationInfo.setEquipmentInfos(pileList); // 充电设备信息列表 + } + + resultList.add(stationInfo); + } + Map map = new LinkedHashMap<>(); + map.put("PageNo", pageInfo.getPageNum()); + map.put("PageCount", pageInfo.getPages()); + map.put("ItemSize", resultList.size()); + map.put("StationInfos", resultList); + + return ThirdPartyPlatformUtils.generateResultMap(map, thirdPartySecretInfoVO); + + } + + /** + * 查询统计信息 query_station_stats + * @param dto 查询站点信息dto + * @return + */ + @Override + public Map queryStationStats(QueryStationInfoDTO dto) { + ThirdPartySecretInfoVO ningBoSecretInfoVO = getNingBoSecretInfoVO(); + // 根据站点id 查出这段时间的充电量 + List list = orderBasicInfoService.getAccumulativeInfoForLianLian(dto); + if (CollectionUtils.isEmpty(list)) { + return null; + } + + // 根据充电桩编号分组 key=充电桩编号 + Map> pileMap = list.stream() + .collect(Collectors.groupingBy(AccumulativeInfoVO::getPileSn)); + + // 存放所有充电桩设备 + List equipmentStatsInfoList = Lists.newArrayList(); + // 站点用电量 + BigDecimal stationElectricity = BigDecimal.ZERO; + // 用于记录枪口用电量 在循环每个枪口的时候初始化 + BigDecimal pileElec; + for (String pileSn : pileMap.keySet()) { + // 该充电桩下 所有枪口的用电数据 + List accumulativeInfoVOS = pileMap.get(pileSn); + if (CollectionUtils.isEmpty(accumulativeInfoVOS)) { + continue; + } + + // 存放充电桩用电量 + pileElec = BigDecimal.ZERO; + + // key=枪口编号 value 该枪口的用电数据 + Map> collect = accumulativeInfoVOS.stream() + .collect(Collectors.groupingBy(AccumulativeInfoVO::getPileConnectorCode)); + + List connectorStatsInfos = Lists.newArrayList(); + for (Map.Entry> entry : collect.entrySet()) { + String pileConnectorCode = entry.getKey(); + List value = entry.getValue(); + // 枪口用电量求和 + BigDecimal connectorElec = value.stream() + .map(AccumulativeInfoVO::getConnectorElectricity) + .map(BigDecimal::new) + .reduce(BigDecimal.ZERO, BigDecimal::add); + + connectorStatsInfos.add( + ConnectorStatsInfo.builder() + .connectorID(pileConnectorCode) + .connectorElectricity(connectorElec) + .build() + ); + // 充电桩电量为枪口用电量累计 + pileElec = pileElec.add(connectorElec); + } + + EquipmentStatsInfo build = EquipmentStatsInfo.builder() + .equipmentID(pileSn) + .equipmentElectricity(pileElec) + .connectorStatsInfos(connectorStatsInfos) + .build(); + equipmentStatsInfoList.add(build); + + // 所有充电桩用电量之和 + stationElectricity = stationElectricity.add(pileElec); + } + + StationStatsInfo stationStatsInfo = StationStatsInfo.builder() + .stationID(dto.getStationID()) + .startTime(dto.getStartTime()) + .endTime(dto.getEndTime()) + .stationElectricity(stationElectricity) + .equipmentStatsInfos(equipmentStatsInfoList) // 设备列表 + .build(); + + Map map = new LinkedHashMap<>(); + map.put("StationStats", stationStatsInfo); + + return ThirdPartyPlatformUtils.generateResultMap(map, ningBoSecretInfoVO); + } + + /** + * 设备接口状态查询 query_station_status + * @param dto 查询站点信息dto + * @return + */ + @Override + public Map queryStationStatus(QueryStationInfoDTO dto) { + List stationIds = dto.getStationIds(); + List StationStatusInfos = new ArrayList<>(); + List ConnectorStatusInfos = new ArrayList<>(); + ThirdPartySecretInfoVO ningBoSecretInfoVO = getNingBoSecretInfoVO(); + + ConnectorStatusInfo connectorStatusInfo; + for (String stationId : stationIds) { + StationStatusInfo stationStatusInfo= new StationStatusInfo(); + stationStatusInfo.setStationId(stationId); + // 根据站点id查询 + List list = pileConnectorInfoService.getConnectorListForLianLian(Long.parseLong(stationId)); + for (ConnectorInfoVO connectorInfoVO : list) { + + String connectorStatus = connectorInfoVO.getConnectorStatus(); + if (StringUtils.equals(connectorStatus, PileConnectorDataBaseStatusEnum.OCCUPIED_CHARGING.getValue())) { + // 充电中 + ConnectorChargeStatusInfo info = new ConnectorChargeStatusInfo(); + OrderBasicInfo orderBasicInfo = orderBasicInfoService.queryChargingByPileConnectorCode(connectorInfoVO.getPileConnectorCode()); + if (orderBasicInfo == null) { + continue; + } + List chargingRealTimeData = orderBasicInfoService.getChargingRealTimeData(orderBasicInfo.getTransactionCode()); + if(CollectionUtils.isNotEmpty(chargingRealTimeData)) { + RealTimeMonitorData realTimeMonitorData = chargingRealTimeData.get(0); + + info.setStartChargeSeq(orderBasicInfo.getOrderCode()); + info.setConnectorID(orderBasicInfo.getPileConnectorCode()); + info.setConnectorStatus(Integer.valueOf(connectorInfoVO.getConnectorStatus())); + info.setCurrentA(new BigDecimal(realTimeMonitorData.getOutputCurrent())); + info.setVoltageA(new BigDecimal(realTimeMonitorData.getOutputVoltage())); + info.setSoc(new BigDecimal(realTimeMonitorData.getSOC())); + info.setStartTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, orderBasicInfo.getChargeStartTime())); + info.setEndTime(DateUtils.getDateTime()); // 本次采样时间 + info.setTotalPower(new BigDecimal(realTimeMonitorData.getChargingDegree())); // 累计充电量 + // info.setElecMoney(); // 累计电费 + // info.setSeviceMoney(); // 累计服务费 + info.setTotalMoney(new BigDecimal(realTimeMonitorData.getChargingAmount())); + + ConnectorStatusInfos.add(info); + } + } else { + // 其他 + connectorStatusInfo = new ConnectorStatusInfo(); + connectorStatusInfo.setConnectorID(connectorInfoVO.getPileConnectorCode()); + connectorStatusInfo.setStatus(Integer.parseInt(connectorInfoVO.getConnectorStatus())); + + ConnectorStatusInfos.add(connectorStatusInfo); + } + } + stationStatusInfo.setConnectorStatusInfos(ConnectorStatusInfos); + StationStatusInfos.add(stationStatusInfo); + } + // 将 StationStatusInfos 分页 + int pageNum = 1; + int pageSize = 10; + List collect = StationStatusInfos.stream() + .skip((pageNum - 1) * pageSize) + .limit(pageSize) + .collect(Collectors.toList()); + + int total = StationStatusInfos.size(); + int pages = PageUtil.totalPage(total, pageSize); + + Map map = new LinkedHashMap<>(); + map.put("Total", total); + map.put("StationStatusInfos", collect); + + return ThirdPartyPlatformUtils.generateResultMap(map, ningBoSecretInfoVO); + } + + /** + * 枪口状态变化 notification_station_status + * @param dto + * @return + */ + @Override + public String notificationStationStatus(PushRealTimeInfoDTO dto) { + String pileConnectorCode = dto.getPileConnectorCode(); + String status = dto.getStatus(); + + ThirdPartySecretInfoVO ningBoSecretInfoVO = getNingBoSecretInfoVO(); + String operatorId = ningBoSecretInfoVO.getTheirOperatorId(); + String operatorSecret = ningBoSecretInfoVO.getTheirOperatorSecret(); + String signSecret = ningBoSecretInfoVO.getTheirSigSecret(); + String dataSecret = ningBoSecretInfoVO.getTheirDataSecret(); + String dataSecretIv = ningBoSecretInfoVO.getTheirDataSecretIv(); + String urlAddress = ningBoSecretInfoVO.getTheirUrlPrefix(); + + String url = ningBoSecretInfoVO.getTheirUrlPrefix() + BusinessInformationExchangeEnum.NOTIFICATION_STATION_STATUS.getValue(); + ConnectorStatusInfo info = ConnectorStatusInfo.builder() + .connectorID(pileConnectorCode) + .status(Integer.parseInt(status)) + .build(); + // 调用联联平台接口 + JSONObject json = new JSONObject(); + json.put("ConnectorStatusInfo", info); + String jsonString = JSON.toJSONString(json); + String token = getToken(urlAddress, operatorId, operatorSecret, dataSecretIv, signSecret, dataSecret); + String result = HttpRequestUtil.sendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret); + + return result; + } + + /** + * 推送订单数据 notificationChargeOrderInfo + * @param orderCode 订单编号 + * @return + * @throws NoSuchAlgorithmException + * @throws InvalidKeySpecException + * @throws NoSuchProviderException + * @throws CryptoException + */ + @Override + public String notificationChargeOrderInfo(String orderCode) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException, CryptoException { + OrderBasicInfo orderBasicInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode); + OrderDetail orderDetail = orderBasicInfoService.getOrderDetailByOrderCode(orderCode); + + ThirdPartySecretInfoVO ningBoSecretInfoVO = getNingBoSecretInfoVO(); + String operatorId = ningBoSecretInfoVO.getTheirOperatorId(); + String operatorSecret = ningBoSecretInfoVO.getTheirOperatorSecret(); + String signSecret = ningBoSecretInfoVO.getTheirSigSecret(); + String dataSecret = ningBoSecretInfoVO.getTheirDataSecret(); + String dataSecretIv = ningBoSecretInfoVO.getTheirDataSecretIv(); + String urlAddress = ningBoSecretInfoVO.getTheirUrlPrefix(); + + String url = urlAddress + BusinessInformationExchangeEnum.NOTIFICATION_CHARGE_ORDER_INFO.getValue(); + Date chargeStartTime = orderBasicInfo.getChargeStartTime(); + if (chargeStartTime == null) { + chargeStartTime = orderBasicInfo.getCreateTime(); + } + Date chargeEndTime = orderBasicInfo.getChargeEndTime(); + if (chargeEndTime == null) { + chargeEndTime = orderBasicInfo.getCreateTime(); + } + + JSONObject json = new JSONObject(); + json.put("StartChargeSeq", orderCode); + json.put("ConnectorID", orderBasicInfo.getPileConnectorCode()); + json.put("StartTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, chargeStartTime)); + json.put("EndTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, chargeEndTime)); + json.put("TotalPower", orderDetail.getTotalUsedElectricity().setScale(2, BigDecimal.ROUND_HALF_UP)); + json.put("TotalElecMoney", orderDetail.getTotalElectricityAmount().setScale(2, BigDecimal.ROUND_HALF_UP)); + json.put("TotalSeviceMoney", orderDetail.getTotalServiceAmount().setScale(2, BigDecimal.ROUND_HALF_UP)); + json.put("TotalMoney", orderDetail.getTotalOrderAmount().setScale(2, BigDecimal.ROUND_HALF_UP)); + json.put("StopReason", 2); // 2:BMS 停止充电 + + String jsonString = JSON.toJSONString(json); + String token = getToken(urlAddress, operatorId, operatorSecret, dataSecretIv, signSecret, dataSecret); + String result = HttpRequestUtil.sendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret); + + return result; + } + + + /** + * 请求设备认证 + * @param dto 请求设备认证 + * @return + */ + @Override + public Map queryEquipAuth(QueryEquipmentDTO dto) { + ThirdPartySecretInfoVO ningBoSecretInfoVO = getNingBoSecretInfoVO(); + EquipmentAuthVO vo = new EquipmentAuthVO(); + + String equipAuthSeq = dto.getEquipAuthSeq(); // MA1X78KH5202311071202015732 + String pileConnectorCode = dto.getConnectorID(); + + // 根据桩编号查询数据 + String pileSn = YKCUtils.getPileSn(pileConnectorCode); + vo.setSuccStat(1); // 1-失败 0-成功 默认失败 + PileBasicInfo pileBasicInfo = pileBasicInfoService.selectPileBasicInfoBySN(pileSn); + if (pileBasicInfo != null) { + // 查询当前枪口数据 + PileConnectorInfoVO connectorInfo = pileConnectorInfoService.getPileConnectorInfoByConnectorCode(pileConnectorCode); + if (StringUtils.equals(PileConnectorDataBaseStatusEnum.OCCUPIED_NOT_CHARGED.getValue(), String.valueOf(connectorInfo.getStatus())) + || StringUtils.equals(PileConnectorDataBaseStatusEnum.OCCUPIED_CHARGING.getValue(), String.valueOf(connectorInfo.getStatus())) + || StringUtils.equals(PileConnectorDataBaseStatusEnum.OCCUPIED_RESERVED_LOCK.getValue(), String.valueOf(connectorInfo.getStatus())) + ) { + vo.setSuccStat(0); + vo.setFailReason(0); + } else { + vo.setSuccStat(1); + vo.setFailReason(1); // 1- 此设备尚未插枪; + } + vo.setFailReasonMsg(""); + vo.setEquipAuthSeq(equipAuthSeq); + vo.setConnectorID(pileConnectorCode); + } else { + vo.setFailReason(2); // 设备检测失败 + vo.setFailReasonMsg("未查到该桩的数据"); + } + + return ThirdPartyPlatformUtils.generateResultMap(vo, ningBoSecretInfoVO); + } + + /** + * 请求启动充电 query_start_charge + * @param dto 请求启动充电DTO + * @return + */ + @Override + public Map queryStartCharge(QueryStartChargeDTO dto) { + // 通过传过来的订单号和枪口号生成订单 + String pileConnectorCode = dto.getConnectorID(); + OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByOrderCode(dto.getStartChargeSeq()); + if (orderInfo != null) { + // 平台已存在订单 + return null; + } + ThirdPartySecretInfoVO ningBoSecretInfoVO = getNingBoSecretInfoVO(); + // 生成订单 + Map map = orderBasicInfoService.generateOrderForThirdParty(dto); + String orderCode = (String) map.get("orderCode"); + String transactionCode = (String) map.get("transactionCode"); + OrderBasicInfo orderBasicInfo = (OrderBasicInfo) map.get("orderBasicInfo"); + + // 发送启机指令 + StartChargingCommand command = StartChargingCommand.builder() + .pileSn(orderBasicInfo.getPileSn()) + .connectorCode(orderBasicInfo.getConnectorCode()) + .transactionCode(transactionCode) + .chargeAmount(orderBasicInfo.getPayAmount()) + .logicCardNum(null) + .physicsCardNum(null) + .build(); + ykcPushCommandService.pushStartChargingCommand(command); + + // 拼装对应的数据并返回 + QueryStartChargeVO vo = QueryStartChargeVO.builder() + .startChargeSeq(orderCode) + .startChargeSeqStat(2) // 1、启动中 ;2、充电中;3、停止中;4、已结束;5、未知 + .connectorID(pileConnectorCode) + .succStat(0) + .failReason(0) + + .build(); + return ThirdPartyPlatformUtils.generateResultMap(vo, ningBoSecretInfoVO); + } + + /** + * 查询业务策略 query_equip_business_policy + * @param dto 请求启动充电DTO + * @return + */ + @Override + public Map queryEquipBusinessPolicy(QueryStartChargeDTO dto) { + List policyInfoList = new ArrayList<>(); + String pileConnectorCode = dto.getConnectorID(); + ThirdPartySecretInfoVO ningBoSecretInfoVO = getNingBoSecretInfoVO(); + + // 截取桩号 + // String pileSn = StringUtils.substring(pileConnectorCode, 0, 14); + String pileSn = YKCUtils.getPileSn(pileConnectorCode); + // 查询该桩的站点id + PileBasicInfo pileBasicInfo = pileBasicInfoService.selectPileBasicInfoBySN(pileSn); + // 根据桩号查询正在使用的计费模板 + List billingPriceVOList = pileBillingTemplateService.queryBillingPrice(String.valueOf(pileBasicInfo.getStationId())); + + if (CollectionUtils.isEmpty(billingPriceVOList)) { + return null; + } + EquipBusinessPolicyVO.PolicyInfo policyInfo = null; + for (BillingPriceVO billingPriceVO : billingPriceVOList) { + // 将时段开始时间、电费、服务费信息进行封装 + policyInfo = new EquipBusinessPolicyVO.PolicyInfo(); + String startTime = billingPriceVO.getStartTime() + ":00"; // 00:00:00 格式 + // 需要将中间的冒号去掉,改为 000000 格式 + String replace = StringUtils.replace(startTime, ":", ""); + policyInfo.setStartTime(replace); + policyInfo.setElecPrice(new BigDecimal(billingPriceVO.getElectricityPrice()).setScale(4, BigDecimal.ROUND_HALF_UP)); + policyInfo.setServicePrice(new BigDecimal(billingPriceVO.getServicePrice()).setScale(4, BigDecimal.ROUND_HALF_UP)); + + policyInfoList.add(policyInfo); + } + + // 拼装所需要的数据格式 + EquipBusinessPolicyVO vo = EquipBusinessPolicyVO.builder() + .equipBizSeq(dto.getEquipBizSeq()) + .connectorId(dto.getConnectorID()) + .succStat(0) + .failReason(0) + .sumPeriod(policyInfoList.size()) + .policyInfos(policyInfoList) + + .build(); + return ThirdPartyPlatformUtils.generateResultMap(vo, ningBoSecretInfoVO); + } + + /** + * 查询充电状态 + * @param dto 查询充电状态DTO + * @return + */ + @Override + public Map queryEquipChargeStatus(QueryEquipChargeStatusDTO dto) { + String operatorID = dto.getOperatorID(); + // 通过订单号查询订单信息 + OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByOrderCode(dto.getStartChargeSeq()); + // logger.info(operatorName + "查询订单信息 orderInfo:{}", orderInfo); + if (orderInfo == null) { + return null; + } + ThirdPartySecretInfoVO ningBoSecretInfoVO = getNingBoSecretInfoVO(); + OrderDetail orderDetail = orderBasicInfoService.getOrderDetailByOrderCode(orderInfo.getOrderCode()); + // 通过订单号查询实时数据 + List realTimeData = orderBasicInfoService.getChargingRealTimeData(orderInfo.getTransactionCode()); + QueryChargingStatusVO vo; + if (CollectionUtils.isEmpty(realTimeData)) { + vo = new QueryChargingStatusVO(); + } else { + RealTimeMonitorData data = realTimeData.get(0); + String orderStatus = orderInfo.getOrderStatus(); + if (StringUtils.equals(orderStatus, OrderStatusEnum.IN_THE_CHARGING.getValue())) { + // 充电中 + orderStatus = "2"; + }else if (StringUtils.equals(orderStatus, OrderStatusEnum.ORDER_COMPLETE.getValue())) { + // 充电完成 + orderStatus = "4"; + } else { + // 直接给 5-未知 + orderStatus = "5"; + } + String status = data.getConnectorStatus(); + int connectorStatus = 0; + if (StringUtils.isBlank(status)) { + // 查询当前枪口状态 + PileConnectorInfoVO connectorInfoVO = pileConnectorInfoService.getPileConnectorInfoByConnectorCode(orderInfo.getPileConnectorCode()); + connectorStatus = connectorInfoVO.getStatus(); + }else { + connectorStatus = Integer.parseInt(status); + } + BigDecimal totalElectricityAmount = orderDetail.getTotalElectricityAmount() == null ? BigDecimal.ZERO : orderDetail.getTotalElectricityAmount(); + BigDecimal totalServiceAmount = orderDetail.getTotalServiceAmount() == null ? BigDecimal.ZERO : orderDetail.getTotalServiceAmount(); + // 拼装联联平台数据 + vo = QueryChargingStatusVO.builder() + .startChargeSeq(dto.getStartChargeSeq()) // 订单号 + .startChargeSeqStat(Integer.parseInt(orderStatus)) // 订单状态 + .connectorID(orderInfo.getPileConnectorCode()) // 枪口编码 + .connectorStatus(connectorStatus) // 枪口状态 + .currentA(new BigDecimal(data.getOutputCurrent())) // 电流 + .voltageA(new BigDecimal(data.getOutputVoltage())) // 电压 + .soc(new BigDecimal(data.getSOC())) + .startTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, orderInfo.getChargeStartTime())) // 开始时间 + .endTime(DateUtils.getDateTime()) // 本次采样时间 + .totalPower(new BigDecimal(data.getChargingDegree()).setScale(2, BigDecimal.ROUND_HALF_UP)) // 累计充电量 + .elecMoney(totalElectricityAmount.setScale(2, BigDecimal.ROUND_HALF_UP)) // 累计电费 + .seviceMoney(totalServiceAmount.setScale(2, BigDecimal.ROUND_HALF_UP)) // 累计服务费 + .totalMoney(new BigDecimal(data.getChargingAmount())) // 已充金额 + + .build(); + } + return ThirdPartyPlatformUtils.generateResultMap(vo, ningBoSecretInfoVO); + } + + /** + * 请求停止充电 query_stop_charge + * @param dto + * @return + */ + @Override + public Map queryStopCharge(QueryStartChargeDTO dto) { + QueryStopChargeVO vo = new QueryStopChargeVO(); + String orderCode = dto.getStartChargeSeq(); + + ThirdPartySecretInfoVO ningBoSecretInfoVO = getNingBoSecretInfoVO(); + // 根据订单号查询订单信息 + OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode); + if (orderInfo == null) { + return null; + } + // 若状态为充电中,则发送停机指令 + if (StringUtils.equals(OrderStatusEnum.IN_THE_CHARGING.getValue(), orderInfo.getOrderStatus())) { + // 充电中 + pileRemoteService.remoteStopCharging(orderInfo.getPileSn(), orderInfo.getConnectorCode(), orderInfo.getTransactionCode()); + vo.setStartChargeSeq(orderCode); + vo.setStartChargeSeqStat(4); // 1、启动中 ;2、充电中;3、停止中;4、已结束;5、未知 + } + vo.setSuccStat(0); + vo.setFailReason(0); + + return ThirdPartyPlatformUtils.generateResultMap(vo, ningBoSecretInfoVO); + } + + /** + * 推送启动充电结果 notification_start_charge_result + * @param orderCode 订单编号 + * @return + */ + @Override + public String notificationStartChargeResult(String orderCode) { + // 根据订单号查询订单信息 + OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode); + if (orderInfo == null) { + return null; + } + ThirdPartySecretInfoVO ningBoSecretInfoVO = getNingBoSecretInfoVO(); + String operatorId = ningBoSecretInfoVO.getTheirOperatorId(); + String operatorSecret = ningBoSecretInfoVO.getTheirOperatorSecret(); + String signSecret = ningBoSecretInfoVO.getTheirSigSecret(); + String dataSecret = ningBoSecretInfoVO.getTheirDataSecret(); + String dataSecretIv = ningBoSecretInfoVO.getTheirDataSecretIv(); + String urlAddress = ningBoSecretInfoVO.getTheirUrlPrefix(); + + // 推送启动充电结果(调用接口 notification_start_charge_result) + String url = urlAddress + BusinessInformationExchangeEnum.NOTIFICATION_START_CHARGE_RESULT.getValue(); + // 拼装参数 + JSONObject json = new JSONObject(); + json.put("StartChargeSeq", orderCode); + json.put("ConnectorID", orderInfo.getPileConnectorCode()); + json.put("StartChargeSeqStat", 2); // 一定要给 2-充电中 + json.put("StartTime", DateUtils.getDateTime()); + + String jsonString = JSON.toJSONString(json); + + String token = getToken(urlAddress, operatorId, operatorSecret, dataSecretIv, signSecret, dataSecret); + String result = HttpRequestUtil.sendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret); + + return result; + } + + /** + * 推送停止充电结果 notification_stop_charge_result + * @param orderCode 订单编号 + * @return + */ + @Override + public String notificationStopChargeResult(String orderCode) { + // 根据订单号查询订单信息 + OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode); + if (orderInfo == null) { + return null; + } + ThirdPartySecretInfoVO ningBoSecretInfoVO = getNingBoSecretInfoVO(); + String operatorId = ningBoSecretInfoVO.getTheirOperatorId(); + String operatorSecret = ningBoSecretInfoVO.getTheirOperatorSecret(); + String signSecret = ningBoSecretInfoVO.getTheirSigSecret(); + String dataSecret = ningBoSecretInfoVO.getTheirDataSecret(); + String dataSecretIv = ningBoSecretInfoVO.getTheirDataSecretIv(); + String urlAddress = ningBoSecretInfoVO.getTheirUrlPrefix(); + + String url = urlAddress + BusinessInformationExchangeEnum.NOTIFICATION_STOP_CHARGE_RESULT.getValue(); + + // 拼装联联平台参数 + JSONObject json = new JSONObject(); + json.put("StartChargeSeq", orderCode); + json.put("StartChargeSeqStat", 4); // 只能给 4-已结束 + json.put("ConnectorID", orderInfo.getPileConnectorCode()); + json.put("SuccStat", 0); + json.put("FailReason", 0); + + String jsonString = JSON.toJSONString(json); + + String token = getToken(urlAddress, operatorId, operatorSecret, dataSecretIv, signSecret, dataSecret); + String result = HttpRequestUtil.sendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret); + + return result; + } + + @Override + public String notificationEquipChargeStatus(String orderCode) { + // 根据订单号查询订单信息 + OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode); + OrderDetail orderDetail = orderBasicInfoService.getOrderDetailByOrderCode(orderInfo.getOrderCode()); + // 查询枪口状态 + PileConnectorInfoVO info = pileConnectorInfoService.getPileConnectorInfoByConnectorCode(orderInfo.getPileConnectorCode()); + BigDecimal current = info.getCurrent() == null ? BigDecimal.ZERO : info.getCurrent(); + BigDecimal voltage = info.getVoltage() == null ? BigDecimal.ZERO : info.getVoltage(); + String soc = info.getSOC() == null ? Constants.ZERO : info.getSOC(); + // 查询相关配置信息 + ThirdPartySecretInfoVO ningBoSecretInfoVO = getNingBoSecretInfoVO(); + + String operatorId = ningBoSecretInfoVO.getTheirOperatorId(); + String operatorSecret = ningBoSecretInfoVO.getTheirOperatorSecret(); + String signSecret = ningBoSecretInfoVO.getTheirSigSecret(); + String dataSecret = ningBoSecretInfoVO.getTheirDataSecret(); + String dataSecretIv = ningBoSecretInfoVO.getTheirDataSecretIv(); + String urlAddress = ningBoSecretInfoVO.getTheirUrlPrefix(); + + BigDecimal totalElectricityAmount = orderDetail.getTotalElectricityAmount() == null ? BigDecimal.ZERO : orderDetail.getTotalElectricityAmount(); + BigDecimal totalServiceAmount = orderDetail.getTotalServiceAmount() == null ? BigDecimal.ZERO : orderDetail.getTotalServiceAmount(); + + QueryChargingStatusVO vo = QueryChargingStatusVO.builder() + .startChargeSeq(orderInfo.getOrderCode()) // 订单号 + .startChargeSeqStat(Integer.parseInt(orderInfo.getOrderStatus())) // 订单状态 + .connectorID(orderInfo.getPileConnectorCode()) // 枪口编码 + .connectorStatus(info.getStatus()) // 枪口状态 + .currentA(info.getCurrent()) // 电流 + .voltageA(info.getVoltage()) // 电压 + .soc(new BigDecimal(info.getSOC())) + .startTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, orderInfo.getChargeStartTime())) // 开始时间 + .endTime(DateUtils.getDateTime()) // 本次采样时间 + .totalPower(info.getChargingDegree()) // 累计充电量 + .elecMoney(totalElectricityAmount.setScale(2, BigDecimal.ROUND_HALF_UP)) // 累计电费 + .seviceMoney(totalServiceAmount.setScale(2, BigDecimal.ROUND_HALF_UP)) // 累计服务费 + .totalMoney(info.getChargingAmount()) // 已充金额 + + .build(); + String url = urlAddress + BusinessInformationExchangeEnum.NOTIFICATION_EQUIP_CHARGE_STATUS.getValue(); + // 调用平台接口 + String jsonString = JSON.toJSONString(vo); + + String token = getToken(urlAddress, operatorId, operatorSecret, dataSecretIv, signSecret, dataSecret); + String result = HttpRequestUtil.sendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret); + + return result; + } + + /** + * 获取桩列表信息 + * + * @param pileStationInfo + * @return + */ + private List getPileList(PileStationInfo pileStationInfo) { + List resultList = new ArrayList<>(); + // 通过站点id查询桩基本信息 + List list = pileBasicInfoService.getPileListByStationId(String.valueOf(pileStationInfo.getId())); + // 封装成中电联平台对象 + for (PileBasicInfo pileBasicInfo : list) { + EquipmentInfo equipmentInfo = new EquipmentInfo(); + String pileSn = pileBasicInfo.getSn(); + + equipmentInfo.setEquipmentID(pileSn); + + PileModelInfoVO modelInfo = pileModelInfoService.getPileModelInfoByPileSn(pileSn); + equipmentInfo.setEquipmentType(Integer.parseInt(modelInfo.getSpeedType())); + equipmentInfo.setPower(new BigDecimal(modelInfo.getRatedPower()).setScale(1, BigDecimal.ROUND_HALF_UP)); + + List connectorList = getConnectorList(pileBasicInfo); + equipmentInfo.setConnectorInfos(connectorList); + + resultList.add(equipmentInfo); + } + return resultList; + } + + /** + * 获取枪口列表 + * + * @param pileBasicInfo + * @return + */ + private List getConnectorList(PileBasicInfo pileBasicInfo) { + List resultList = new ArrayList<>(); + + List list = pileConnectorInfoService.selectPileConnectorInfoList(pileBasicInfo.getSn()); + for (PileConnectorInfo pileConnectorInfo : list) { + ConnectorInfo connectorInfo = new ConnectorInfo(); + + connectorInfo.setConnectorID(pileConnectorInfo.getPileConnectorCode()); + String pileSn = pileConnectorInfo.getPileSn(); + PileModelInfoVO modelInfo = pileModelInfoService.getPileModelInfoByPileSn(pileSn); + int connectorType = StringUtils.equals("1", modelInfo.getSpeedType()) ? 4 : 3; + + connectorInfo.setConnectorType(connectorType); + // 车位号 + if (StringUtils.isNotBlank(pileConnectorInfo.getParkNo())) { + connectorInfo.setParkNo(pileConnectorInfo.getParkNo()); + } + connectorInfo.setVoltageUpperLimits(Integer.valueOf(modelInfo.getRatedVoltage())); + connectorInfo.setVoltageLowerLimits(Integer.valueOf(modelInfo.getRatedVoltage())); + connectorInfo.setCurrent(Integer.valueOf(modelInfo.getRatedCurrent())); + connectorInfo.setNationalStandard(2); + // if (!StringUtils.equals(modelInfo.getConnectorNum(), "1")) { + // // 如果不是单枪,则枪口功率需要除以枪口数量 + // String ratedPowerStr = modelInfo.getRatedPower(); + // BigDecimal ratedPower = new BigDecimal(ratedPowerStr); + // connectorInfo.setPower(ratedPower.divide(new BigDecimal(modelInfo.getConnectorNum()), 1, BigDecimal.ROUND_HALF_UP)); + // }else { + // } + connectorInfo.setPower(new BigDecimal(modelInfo.getRatedPower()).setScale(1, BigDecimal.ROUND_HALF_UP)); + + resultList.add(connectorInfo); + } + + return resultList; + } + + /** + * 获取宁波平台密钥信息 + * @return + */ + private ThirdPartySecretInfoVO getNingBoSecretInfoVO() { + // 通过第三方平台类型查询相关配置信息 + ThirdPartySecretInfoVO thirdPartySecretInfoVO = thirdpartySecretInfoService.queryByThirdPlatformType(ThirdPlatformTypeEnum.NING_BO_PLATFORM.getTypeCode()); + if (thirdPartySecretInfoVO == null) { + throw new BusinessException(ReturnCodeEnum.CODE_SELECT_INFO_IS_NULL); + } + thirdPartySecretInfoVO.setOurOperatorId(Constants.OPERATORID_JIANG_SU); + return thirdPartySecretInfoVO; + } }