ChargingReceptionServiceImpl.java 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. package com.zsElectric.boot.charging.service.impl;
  2. import cn.hutool.core.util.ObjUtil;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  6. import com.fasterxml.jackson.databind.JsonNode;
  7. import com.fasterxml.jackson.databind.ObjectMapper;
  8. import com.zsElectric.boot.business.mapper.*;
  9. import com.zsElectric.boot.business.model.entity.*;
  10. import com.zsElectric.boot.business.service.ChargeOrderInfoService;
  11. import com.zsElectric.boot.charging.entity.*;
  12. import com.zsElectric.boot.charging.mapper.ThirdPartyChargeStatusMapper;
  13. import com.zsElectric.boot.charging.mapper.ThirdPartyConnectorInfoMapper;
  14. import com.zsElectric.boot.charging.mapper.ThirdPartyPolicyInfoMapper;
  15. import com.zsElectric.boot.charging.service.ChargingBusinessService;
  16. import com.zsElectric.boot.charging.service.ChargingReceptionService;
  17. import com.zsElectric.boot.charging.vo.ChargeResponseVO;
  18. import com.zsElectric.boot.charging.vo.QueryStationStatusVO;
  19. import com.zsElectric.boot.common.constant.ConnectivityConstants;
  20. import com.zsElectric.boot.common.constant.SystemConstants;
  21. import com.zsElectric.boot.common.util.DateUtils;
  22. import com.zsElectric.boot.common.util.electric.ChargingUtil;
  23. import com.zsElectric.boot.common.util.electric.RequestParmsEntity;
  24. import com.zsElectric.boot.common.util.electric.ResponseParmsEntity;
  25. import com.zsElectric.boot.core.exception.BusinessException;
  26. import com.zsElectric.boot.system.mapper.DictItemMapper;
  27. import com.zsElectric.boot.system.model.entity.DictItem;
  28. import lombok.RequiredArgsConstructor;
  29. import lombok.extern.slf4j.Slf4j;
  30. import org.redisson.api.RLock;
  31. import org.redisson.api.RedissonClient;
  32. import org.springframework.stereotype.Service;
  33. import org.springframework.util.CollectionUtils;
  34. import java.math.BigDecimal;
  35. import java.math.RoundingMode;
  36. import java.time.LocalDateTime;
  37. import java.time.format.DateTimeFormatter;
  38. import java.util.HashMap;
  39. import java.util.List;
  40. import java.util.Map;
  41. import java.util.Objects;
  42. import java.util.concurrent.TimeUnit;
  43. import java.util.function.Consumer;
  44. import static com.zsElectric.boot.common.constant.ConnectivityConstants.FAIL_REASON_NONE;
  45. import static com.zsElectric.boot.common.constant.ConnectivityConstants.STATUS_OK;
  46. import static com.zsElectric.boot.common.util.HmacMD5Util.genSign;
  47. import static com.zsElectric.boot.common.util.HmacMD5Util.verify;
  48. /**
  49. * 第三方充电推送接收服务实现
  50. *
  51. * @author system
  52. * @since 2025-12-11
  53. */
  54. @Slf4j
  55. @Service
  56. @RequiredArgsConstructor
  57. public class ChargingReceptionServiceImpl implements ChargingReceptionService {
  58. private final ChargingUtil chargingUtil;
  59. private final ThirdPartyConnectorInfoMapper connectorInfoMapper;
  60. private final ThirdPartyChargeStatusMapper chargeStatusMapper;
  61. private final ObjectMapper objectMapper;
  62. private final ChargeOrderInfoService chargeOrderInfoService;
  63. private final RedissonClient redissonClient;
  64. private final UserAccountMapper userAccountMapper;
  65. private final UserFirmMapper userFirmMapper;
  66. private final PolicyFeeMapper policyFeeMapper;
  67. private final DictItemMapper dictItemMapper;
  68. private final ThirdPartyEquipmentInfoMapper thirdPartyEquipmentInfoMapper;
  69. private final ChargingBusinessService chargingBusinessService;
  70. private final ThirdPartyStationInfoMapper thirdPartyStationInfoMapper;
  71. private final ThirdPartyPolicyInfoMapper thirdPartyPolicyInfoMapper;
  72. private final DiscountsActivityMapper discountsActivityMapper;
  73. /**
  74. * 熔断检查锁前缀
  75. */
  76. private static final String BREAK_CHECK_LOCK_KEY = "charging:break:check:";
  77. /**
  78. * 锁等待时间(秒)
  79. */
  80. private static final long LOCK_WAIT_TIME = 3;
  81. /**
  82. * 锁持有时间(秒)
  83. */
  84. private static final long LOCK_LEASE_TIME = 10;
  85. private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  86. // ==================== 接口实现 ====================
  87. @Override
  88. public ResponseParmsEntity chargeResponse(RequestParmsEntity requestDTO) {
  89. log.info("接收推送启动充电结果请求参数:{}", requestDTO);
  90. return processStartChargeResultRequest(requestDTO);
  91. }
  92. @Override
  93. public ResponseParmsEntity chargeStatusResponse(RequestParmsEntity requestDTO) {
  94. log.info("接收推送充电状态请求参数:{}", requestDTO);
  95. return processChargeStatusRequest(requestDTO);
  96. }
  97. @Override
  98. public ResponseParmsEntity stopChargeResponse(RequestParmsEntity requestDTO) {
  99. log.info("接收推送停止充电结果请求参数:{}", requestDTO);
  100. return processStopChargeResultRequest(requestDTO);
  101. }
  102. @Override
  103. public ResponseParmsEntity chargeOrderResponse(RequestParmsEntity requestDTO) throws Exception {
  104. log.info("接收推送充电订单信息请求参数:{}", requestDTO);
  105. return processChargeRequest(requestDTO, jsonNode -> {
  106. log.debug("充电订单信息 - StartChargeSeq: {}", getTextValue(jsonNode, "StartChargeSeq"));
  107. });
  108. }
  109. @Override
  110. public ResponseParmsEntity stationStatus(RequestParmsEntity requestDTO) {
  111. log.info("接收设备状态变化推送请求参数:{}", requestDTO);
  112. return processStationStatusRequest(requestDTO);
  113. }
  114. // ==================== 公共处理方法 ====================
  115. /**
  116. * 通用充电请求处理模板
  117. */
  118. private ResponseParmsEntity processChargeRequest(RequestParmsEntity requestDTO, Consumer<JsonNode> businessHandler) {
  119. try {
  120. JsonNode jsonNode = verifyAndDecrypt(requestDTO);
  121. //查询订单
  122. String startChargeSeq = getTextValue(jsonNode, "StartChargeSeq");
  123. String stopReason = getTextValue(jsonNode, "StopReason");
  124. String endTime = getTextValue(jsonNode, "EndTime");
  125. String startTime = getTextValue(jsonNode, "StartTime");
  126. String totalPower = getTextValue(jsonNode, "TotalPower");
  127. String totalElecMoney = getTextValue(jsonNode, "TotalElecMoney");
  128. String totalMoney = getTextValue(jsonNode, "TotalMoney");
  129. String totalSeviceMoney = getTextValue(jsonNode, "TotalSeviceMoney");
  130. String connectorID = getTextValue(jsonNode, "ConnectorID");
  131. ChargeOrderInfo chargeOrderInfo = chargeOrderInfoService.getOne(new LambdaQueryWrapper<ChargeOrderInfo>()
  132. .eq(ChargeOrderInfo::getStartChargeSeq, startChargeSeq).last("LIMIT 1"));
  133. //推送订单明细
  134. chargeOrderInfo.setChargeDetails(jsonNode.toString());
  135. //优惠单价
  136. BigDecimal discountPrice = BigDecimal.ZERO;
  137. //判断当前订单是否为首单
  138. Long userId = chargeOrderInfo.getUserId();
  139. List<ChargeOrderInfo> list = chargeOrderInfoService.list(Wrappers.<ChargeOrderInfo>lambdaQuery().eq(ChargeOrderInfo::getUserId, userId).eq(ChargeOrderInfo::getStatus,
  140. SystemConstants.STATUS_THREE));
  141. if(ObjectUtil.isEmpty(list)){
  142. DiscountsActivity discountsActivity = discountsActivityMapper.selectOne(Wrappers.<DiscountsActivity>lambdaQuery()
  143. .eq(DiscountsActivity::getType, SystemConstants.STATUS_ONE)
  144. .eq(DiscountsActivity::getStatus, SystemConstants.STATUS_ONE)
  145. .last("LIMIT 1"));
  146. if(ObjectUtil.isNotEmpty(discountsActivity)){
  147. chargeOrderInfo.setDiscountInfoId(discountsActivity.getId());
  148. chargeOrderInfo.setDiscountDesc(discountsActivity.getActivityDesc());
  149. discountPrice = discountsActivity.getDiscount();
  150. }
  151. }
  152. chargeOrderInfo.setStopReason(stopReason);
  153. chargeOrderInfo.setStartTime(startTime);
  154. chargeOrderInfo.setEndTime(endTime);
  155. chargeOrderInfo.setTotalCharge(new BigDecimal(totalPower));
  156. chargeOrderInfo.setThirdPartyTotalCost(new BigDecimal(totalMoney));
  157. chargeOrderInfo.setThirdPartyServerfee(new BigDecimal(totalSeviceMoney));
  158. chargeOrderInfo.setThirdPartyElecfee(new BigDecimal(totalElecMoney));
  159. ThirdPartyConnectorInfo thirdPartyConnectorInfo = connectorInfoMapper.selectOne(Wrappers.<ThirdPartyConnectorInfo>lambdaQuery()
  160. .eq(ThirdPartyConnectorInfo::getConnectorId, connectorID).last("LIMIT 1"));
  161. String stationId = thirdPartyConnectorInfo.getStationId();
  162. ThirdPartyStationInfo thirdPartyStationInfo = thirdPartyStationInfoMapper.selectOne(Wrappers.<ThirdPartyStationInfo>lambdaQuery()
  163. .eq(ThirdPartyStationInfo::getStationId, stationId).last("LIMIT 1"));
  164. if (ObjectUtil.isEmpty(thirdPartyConnectorInfo)) {
  165. log.error("thirdPartyConnectorInfo" +"为空===============================================");
  166. }
  167. //平台服务费
  168. BigDecimal serviceFee = BigDecimal.ZERO;
  169. //优惠金额
  170. BigDecimal discountFee = BigDecimal.ZERO;
  171. JsonNode chargeDetails = jsonNode.get("ChargeDetails");
  172. if (ObjectUtil.isNotEmpty(chargeDetails)) {
  173. for (JsonNode node : chargeDetails) {
  174. //提取字段值
  175. String itemFlag = node.get("ItemFlag").asText();
  176. node.get("DetailPower").asText();
  177. BigDecimal detailPower = new BigDecimal(node.get("DetailPower").asText());
  178. PolicyFee policyFee = policyFeeMapper.selectOne(Wrappers.<PolicyFee>lambdaQuery()
  179. .eq(PolicyFee::getStationInfoId, thirdPartyStationInfo.getId())
  180. .eq(PolicyFee::getPeriodFlag, Integer.parseInt(itemFlag))
  181. .last("LIMIT 1"));
  182. if (ObjectUtil.isNotEmpty(policyFee)) {
  183. BigDecimal opFee = policyFee.getOpFee();
  184. if(ObjectUtil.isNotEmpty(chargeOrderInfo.getDiscountInfoId())){
  185. opFee = opFee.subtract(discountPrice);
  186. }
  187. log.info("策略费用:{}",opFee);
  188. serviceFee = serviceFee.add(opFee.multiply(detailPower));
  189. discountFee = discountFee.add(discountPrice.multiply(detailPower));
  190. }
  191. }
  192. }
  193. log.info("计算后的平台服务费:{}", serviceFee);
  194. chargeOrderInfo.setDiscountMoney(discountFee);
  195. chargeOrderInfo.setRealServiceCost(serviceFee.setScale(2, RoundingMode.HALF_UP));
  196. //订单结算:平台实际收取金额 = 互联互通金额 + 中数电动金额(平台总服务费)
  197. chargeOrderInfo.setRealCost(chargeOrderInfo.getRealServiceCost().add(chargeOrderInfo.getThirdPartyTotalCost()));
  198. //订单状态->已完成
  199. chargeOrderInfo.setStatus(SystemConstants.STATUS_THREE);
  200. //计算充电时间
  201. chargeOrderInfo.setChargeTime(DateUtils.getDuration(chargeOrderInfo.getStartTime(), chargeOrderInfo.getEndTime()));
  202. //修改订单
  203. chargeOrderInfoService.updateById(chargeOrderInfo);
  204. //账户余额扣减(积分增加)
  205. log.info("执行账户余额扣减(积分增加)");
  206. chargeOrderInfoService.orderSettlement(chargeOrderInfo.getId());
  207. // 执行业务处理
  208. businessHandler.accept(jsonNode);
  209. // 构建响应
  210. return buildChargeResponse(getTextValue(jsonNode, "StartChargeSeq"));
  211. } catch (BusinessException e) {
  212. throw e;
  213. } catch (Exception e) {
  214. log.error("处理请求失败:{}", e.getMessage());
  215. throw new BusinessException("处理请求失败:" + e.getMessage(), e);
  216. }
  217. }
  218. /**
  219. * 处理启动充电结果推送请求
  220. * 数据格式:{"ConnectorID":"xxx","StartChargeSeq":"xxx","StartChargeSeqStat":2,"StartTime":"xxx"}
  221. */
  222. private ResponseParmsEntity processStartChargeResultRequest(RequestParmsEntity requestDTO) {
  223. try {
  224. JsonNode jsonNode = verifyAndDecrypt(requestDTO);
  225. // 启动充电结果业务处理
  226. String startChargeSeq = getTextValue(jsonNode, "StartChargeSeq");
  227. Integer startChargeSeqStat = getIntValue(jsonNode, "StartChargeSeqStat");
  228. String startTime = getTextValue(jsonNode, "StartTime");
  229. ChargeOrderInfo chargeOrderInfo = chargeOrderInfoService.getOne(new LambdaQueryWrapper<ChargeOrderInfo>()
  230. .eq(ChargeOrderInfo::getStartChargeSeq, startChargeSeq).last("LIMIT 1"));
  231. if (startChargeSeqStat != null) {
  232. switch (startChargeSeqStat) {
  233. case 1 -> log.info("启动中 - StartChargeSeq: {}", startChargeSeq);
  234. case 2 -> {
  235. log.info("充电中 - StartChargeSeq: {}", startChargeSeq);
  236. // 修改订单状态
  237. if (ObjectUtil.isNotEmpty(chargeOrderInfo)) {
  238. if (Objects.equals(chargeOrderInfo.getStatus(), SystemConstants.STATUS_ZERO)) {
  239. chargeOrderInfo.setStatus(SystemConstants.STATUS_ONE);
  240. chargeOrderInfo.setStartTime(startTime);
  241. chargeOrderInfoService.updateById(chargeOrderInfo);
  242. }
  243. }
  244. }
  245. case 3 -> log.info("停止中 - StartChargeSeq: {}", startChargeSeq);
  246. case 4 -> log.info("已结束 - StartChargeSeq: {}", startChargeSeq);
  247. case 5 -> log.info("未知 - StartChargeSeq: {}", startChargeSeq);
  248. default -> log.warn("未知状态 - StartChargeSeq: {}, Stat: {}", startChargeSeq, startChargeSeqStat);
  249. }
  250. }
  251. // 构建响应
  252. return buildChargeResponse(startChargeSeq);
  253. } catch (BusinessException e) {
  254. throw e;
  255. } catch (Exception e) {
  256. log.error("处理启动充电结果推送失败:{}", e.getMessage(), e);
  257. throw new BusinessException("处理启动充电结果推送失败:" + e.getMessage(), e);
  258. }
  259. }
  260. /**
  261. * 处理停止充电结果推送请求
  262. * 数据格式:{"ConnectorID":"xxx","FailReason":0,"StartChargeSeq":"xxx","StartChargeSeqStat":4,"SuccStat":0}
  263. */
  264. private ResponseParmsEntity processStopChargeResultRequest(RequestParmsEntity requestDTO) {
  265. try {
  266. JsonNode jsonNode = verifyAndDecrypt(requestDTO);
  267. // 停止充电结果业务处理
  268. String startChargeSeq = getTextValue(jsonNode, "StartChargeSeq");
  269. Integer startChargeSeqStat = getIntValue(jsonNode, "StartChargeSeqStat");
  270. Integer succStat = getIntValue(jsonNode, "SuccStat");
  271. Integer failReason = getIntValue(jsonNode, "FailReason");
  272. log.info("停止充电结果 - StartChargeSeq: {}, Stat: {}, SuccStat: {}, FailReason: {}",
  273. startChargeSeq, startChargeSeqStat, succStat, failReason);
  274. ChargeOrderInfo chargeOrderInfo = chargeOrderInfoService.getOne(new LambdaQueryWrapper<ChargeOrderInfo>()
  275. .eq(ChargeOrderInfo::getStartChargeSeq, startChargeSeq).last("LIMIT 1"));
  276. if (startChargeSeqStat != null && ObjectUtil.isNotEmpty(chargeOrderInfo)) {
  277. switch (startChargeSeqStat) {
  278. case 1 -> log.info("启动中 - StartChargeSeq: {}", startChargeSeq);
  279. case 2 -> log.info("充电中 - StartChargeSeq: {}", startChargeSeq);
  280. case 3 -> log.info("停止中 - StartChargeSeq: {}", startChargeSeq);
  281. case 4 -> {
  282. log.info("已结束 - StartChargeSeq: {}", startChargeSeq);
  283. // 修改订单状态为结算中
  284. if (Objects.equals(chargeOrderInfo.getStatus(), SystemConstants.STATUS_ONE) ||
  285. Objects.equals(chargeOrderInfo.getStatus(), SystemConstants.STATUS_THREE)) {
  286. chargeOrderInfo.setStatus(SystemConstants.STATUS_TWO);
  287. chargeOrderInfoService.updateById(chargeOrderInfo);
  288. log.info("更新订单状态为结算中 - orderId: {}", chargeOrderInfo.getId());
  289. }
  290. }
  291. case 5 -> log.info("未知 - StartChargeSeq: {}", startChargeSeq);
  292. default -> log.warn("未知状态 - StartChargeSeq: {}, Stat: {}", startChargeSeq, startChargeSeqStat);
  293. }
  294. }
  295. // 构建响应
  296. return buildChargeResponse(startChargeSeq);
  297. } catch (BusinessException e) {
  298. throw e;
  299. } catch (Exception e) {
  300. log.error("处理停止充电结果推送失败:{}", e.getMessage(), e);
  301. throw new BusinessException("处理停止充电结果推送失败:" + e.getMessage(), e);
  302. }
  303. }
  304. /**
  305. * 处理实时充电状态推送请求
  306. * 数据格式:{"ConnectorID":"xxx","ConnectorStatus":3,"TotalPower":1.95,"ElecMoney":1.89,...}
  307. */
  308. private ResponseParmsEntity processChargeStatusRequest(RequestParmsEntity requestDTO) {
  309. try {
  310. JsonNode jsonNode = verifyAndDecrypt(requestDTO);
  311. // 保存或更新充电状态
  312. saveOrUpdateChargeStatus(jsonNode);
  313. // 构建响应
  314. return buildChargeResponse(getTextValue(jsonNode, "StartChargeSeq"));
  315. } catch (BusinessException e) {
  316. throw e;
  317. } catch (Exception e) {
  318. log.error("处理充电状态推送失败:{}", e.getMessage(), e);
  319. throw new BusinessException("处理充电状态推送失败:" + e.getMessage(), e);
  320. }
  321. }
  322. /**
  323. * 处理设备状态变化推送请求
  324. */
  325. private ResponseParmsEntity processStationStatusRequest(RequestParmsEntity requestDTO) {
  326. try {
  327. String decryptData = verifyAndDecryptRaw(requestDTO);
  328. // 解析并更新设备状态
  329. QueryStationStatusVO stationStatusVO = objectMapper.readValue(decryptData, QueryStationStatusVO.class);
  330. updateConnectorStatus(stationStatusVO);
  331. // 构建响应
  332. return buildStatusResponse();
  333. } catch (BusinessException e) {
  334. throw e;
  335. } catch (Exception e) {
  336. log.error("处理设备状态推送失败:{}", e.getMessage());
  337. throw new BusinessException("处理设备状态推送失败:" + e.getMessage(), e);
  338. }
  339. }
  340. /**
  341. * 验签并解密请求数据
  342. */
  343. private JsonNode verifyAndDecrypt(RequestParmsEntity requestDTO) throws Exception {
  344. String decryptData = verifyAndDecryptRaw(requestDTO);
  345. return objectMapper.readTree(decryptData);
  346. }
  347. /**
  348. * 验签并解密请求数据(返回原始字符串)
  349. */
  350. private String verifyAndDecryptRaw(RequestParmsEntity requestDTO) throws Exception {
  351. String signData = requestDTO.getOperatorID() + requestDTO.getData() + requestDTO.getTimeStamp() + requestDTO.getSeq();
  352. if (!verify(signData, ConnectivityConstants.SIG_SECRET, requestDTO.getSig())) {
  353. log.error("数据验签失败");
  354. throw new BusinessException("数据验签失败");
  355. }
  356. String decryptData = chargingUtil.decryptData(requestDTO.getData());
  357. log.info("==================== 解密数据开始 ====================");
  358. log.info("操作员ID: {}", requestDTO.getOperatorID());
  359. log.info("解密后的数据:{}", decryptData);
  360. log.info("==================== 解密数据结束 ====================");
  361. return decryptData;
  362. }
  363. // ==================== 响应构建 ====================
  364. /**
  365. * 构建充电响应
  366. */
  367. private ResponseParmsEntity buildChargeResponse(String startChargeSeq) throws Exception {
  368. ChargeResponseVO chargeResponseVO = new ChargeResponseVO();
  369. chargeResponseVO.setStartChargeSeq(startChargeSeq);
  370. chargeResponseVO.setSuccStat(STATUS_OK);
  371. chargeResponseVO.setFailReason(FAIL_REASON_NONE);
  372. String encryptData = chargingUtil.encryptData(objectMapper.writeValueAsString(chargeResponseVO));
  373. String sign = genSign(STATUS_OK, "请求成功", encryptData, ConnectivityConstants.SIG_SECRET);
  374. ResponseParmsEntity response = new ResponseParmsEntity();
  375. response.setRet(STATUS_OK);
  376. response.setMsg("请求成功");
  377. response.setData(encryptData);
  378. response.setSig(sign);
  379. return response;
  380. }
  381. /**
  382. * 构建设备状态响应
  383. */
  384. private ResponseParmsEntity buildStatusResponse() throws Exception {
  385. Map<String, Integer> statusMap = new HashMap<>();
  386. statusMap.put("Status", 0);
  387. String encryptData = chargingUtil.encryptData(objectMapper.writeValueAsString(statusMap));
  388. String sign = genSign(STATUS_OK, "", encryptData, ConnectivityConstants.SIG_SECRET);
  389. ResponseParmsEntity response = new ResponseParmsEntity();
  390. response.setRet(STATUS_OK);
  391. response.setMsg("");
  392. response.setData(encryptData);
  393. response.setSig(sign);
  394. return response;
  395. }
  396. // ==================== 状态更新 ====================
  397. /**
  398. * 更新充电接口状态
  399. */
  400. private void updateConnectorStatus(QueryStationStatusVO stationStatusVO) {
  401. if (stationStatusVO == null) {
  402. return;
  403. }
  404. // 处理单个连接器状态推送格式:{"ConnectorStatusInfo":{"ConnectorID":"xxx","Status":2}}
  405. ConnectorStatusInfo singleConnector = stationStatusVO.getConnectorStatusInfo();
  406. if (singleConnector != null && singleConnector.getConnectorID() != null) {
  407. connectorInfoMapper.update(null, Wrappers.<ThirdPartyConnectorInfo>lambdaUpdate()
  408. .eq(ThirdPartyConnectorInfo::getConnectorId, singleConnector.getConnectorID())
  409. .set(ThirdPartyConnectorInfo::getStatus, singleConnector.getStatus())
  410. .set(ThirdPartyConnectorInfo::getUpdateTime, LocalDateTime.now()));
  411. log.info("更新充电接口状态(单个) - connectorId: {}, status: {}",
  412. singleConnector.getConnectorID(), singleConnector.getStatus());
  413. return;
  414. }
  415. // 处理批量连接器状态推送格式
  416. if (CollectionUtils.isEmpty(stationStatusVO.getStationStatusInfos())) {
  417. return;
  418. }
  419. for (StationStatusInfo stationStatusInfo : stationStatusVO.getStationStatusInfos()) {
  420. List<ConnectorStatusInfo> connectorStatusInfos = stationStatusInfo.getConnectorStatusInfos();
  421. if (CollectionUtils.isEmpty(connectorStatusInfos)) {
  422. continue;
  423. }
  424. for (ConnectorStatusInfo connectorStatus : connectorStatusInfos) {
  425. connectorInfoMapper.update(null, Wrappers.<ThirdPartyConnectorInfo>lambdaUpdate()
  426. .eq(ThirdPartyConnectorInfo::getConnectorId, connectorStatus.getConnectorID())
  427. .set(ThirdPartyConnectorInfo::getStatus, connectorStatus.getStatus())
  428. .set(ThirdPartyConnectorInfo::getUpdateTime, LocalDateTime.now()));
  429. log.debug("更新充电接口状态 - connectorId: {}, status: {}",
  430. connectorStatus.getConnectorID(), connectorStatus.getStatus());
  431. }
  432. }
  433. }
  434. /**
  435. * 保存或更新充电状态数据
  436. */
  437. private void saveOrUpdateChargeStatus(JsonNode jsonNode) {
  438. try {
  439. log.info("保存或更新充电状态数据 - StartChargeSeq: {}", jsonNode);
  440. String startChargeSeq = jsonNode.get("StartChargeSeq").asText();
  441. String connectorId = getTextValue(jsonNode, "ConnectorID");
  442. // 获取第三方推送的实时数据
  443. BigDecimal totalPower = getDecimalValue(jsonNode, "TotalPower"); // 实际充电度数
  444. BigDecimal totalMoney = getDecimalValue(jsonNode, "TotalMoney"); // 第三方总费用
  445. BigDecimal elecMoney = getDecimalValue(jsonNode, "ElecMoney"); // 第三方电费
  446. BigDecimal serviceMoney = getDecimalValue(jsonNode, "SeviceMoney"); // 第三方服务费
  447. // 修改订单状态并实时更新消费字段
  448. ChargeOrderInfo chargeOrderInfo = chargeOrderInfoService.getOne(new LambdaQueryWrapper<ChargeOrderInfo>()
  449. .eq(ChargeOrderInfo::getStartChargeSeq, startChargeSeq).last("limit 1"));
  450. if(ObjUtil.isNotEmpty(chargeOrderInfo)){
  451. Integer connectorStatus = getIntValue(jsonNode, "ConnectorStatus");
  452. // 计算平台实际收取金额(根据度数和平台价格策略计算)
  453. BigDecimal realCost = calculateRealCost(chargeOrderInfo, totalPower);
  454. if (Objects.equals(connectorStatus, SystemConstants.STATUS_THREE) && Objects.equals(chargeOrderInfo.getStatus(), SystemConstants.STATUS_ZERO)) {
  455. // 充电中
  456. log.info("充电中 - StartChargeSeq: {}", startChargeSeq);
  457. chargeOrderInfo.setStatus(SystemConstants.STATUS_ONE);
  458. }
  459. if (Objects.equals(connectorStatus, SystemConstants.STATUS_FOUR) && Objects.equals(chargeOrderInfo.getStatus(),
  460. SystemConstants.STATUS_ONE)) {
  461. // 结算中
  462. log.info("结算中 - StartChargeSeq: {}", startChargeSeq);
  463. chargeOrderInfo.setStatus(SystemConstants.STATUS_TWO);
  464. }
  465. chargeOrderInfoService.updateById(chargeOrderInfo);
  466. log.info("实时更新订单消费 - startChargeSeq: {}, totalPower: {}, realCost: {}",
  467. startChargeSeq, totalPower, realCost);
  468. }
  469. // 查询是否已存在该充电状态记录
  470. ThirdPartyChargeStatus existing = chargeStatusMapper.selectOne(
  471. Wrappers.<ThirdPartyChargeStatus>lambdaQuery()
  472. .eq(ThirdPartyChargeStatus::getStartChargeSeq, startChargeSeq)
  473. );
  474. ThirdPartyChargeStatus chargeStatus = (existing != null) ? existing : new ThirdPartyChargeStatus();
  475. // 设置字段值
  476. chargeStatus.setStartChargeSeq(startChargeSeq);
  477. chargeStatus.setConnectorId(connectorId);
  478. chargeStatus.setConnectorStatus(getIntValue(jsonNode, "ConnectorStatus"));
  479. chargeStatus.setStartChargeSeqStat(getIntValue(jsonNode, "StartChargeSeqStat"));
  480. chargeStatus.setStartTime(parseDateTime(getTextValue(jsonNode, "StartTime")));
  481. chargeStatus.setEndTime(parseDateTime(getTextValue(jsonNode, "EndTime")));
  482. chargeStatus.setTotalPower(totalPower);
  483. chargeStatus.setTotalMoney(totalMoney);
  484. chargeStatus.setElecMoney(elecMoney);
  485. chargeStatus.setServiceMoney(serviceMoney);
  486. chargeStatus.setSoc(getIntValue(jsonNode, "Soc"));
  487. chargeStatus.setVoltageA(getDecimalValue(jsonNode, "VoltageA"));
  488. chargeStatus.setVoltageB(getDecimalValue(jsonNode, "VoltageB"));
  489. chargeStatus.setVoltageC(getDecimalValue(jsonNode, "VoltageC"));
  490. chargeStatus.setCurrentA(getDecimalValue(jsonNode, "CurrentA"));
  491. chargeStatus.setCurrentB(getDecimalValue(jsonNode, "CurrentB"));
  492. chargeStatus.setCurrentC(getDecimalValue(jsonNode, "CurrentC"));
  493. // 兼容处理:如果有ChargeDetails字段,则存储为JSON字符串
  494. if (jsonNode.has("ChargeDetails") && !jsonNode.get("ChargeDetails").isNull()) {
  495. chargeStatus.setChargeDetails(jsonNode.get("ChargeDetails").toString());
  496. }
  497. if (existing != null) {
  498. chargeStatus.setUpdateTime(LocalDateTime.now());
  499. chargeStatusMapper.updateById(chargeStatus);
  500. log.info("更新充电状态成功 - startChargeSeq: {}", startChargeSeq);
  501. } else {
  502. chargeStatus.setCreateTime(LocalDateTime.now());
  503. chargeStatusMapper.insert(chargeStatus);
  504. log.info("新增充电状态成功 - startChargeSeq: {}", startChargeSeq);
  505. }
  506. // 熔断保护 - 余额不足判断
  507. isNeedBreak(chargeStatus, chargeOrderInfo);
  508. } catch (Exception e) {
  509. log.error("保存充电状态数据失败", e);
  510. }
  511. }
  512. /**
  513. * 计算平台实际收取金额
  514. * 根据充电度数 * 当前时段的平台价格策略计算(支持跨时段计费)
  515. */
  516. private BigDecimal calculateRealCost(ChargeOrderInfo chargeOrderInfo, BigDecimal totalPower) {
  517. if (totalPower == null || totalPower.compareTo(BigDecimal.ZERO) <= 0) {
  518. return BigDecimal.ZERO;
  519. }
  520. try {
  521. // 获取用户企业信息
  522. UserFirm userFirm = userFirmMapper.selectOne(Wrappers.<UserFirm>lambdaQuery()
  523. .eq(UserFirm::getUserId, chargeOrderInfo.getUserId())
  524. .eq(UserFirm::getIsDeleted, 0));
  525. // 获取设备信息
  526. ThirdPartyEquipmentInfo equipmentInfo = thirdPartyEquipmentInfoMapper.selectOne(
  527. Wrappers.<ThirdPartyEquipmentInfo>lambdaQuery()
  528. .eq(ThirdPartyEquipmentInfo::getEquipmentId, chargeOrderInfo.getEquipmentId())
  529. .eq(ThirdPartyEquipmentInfo::getIsDeleted, 0));
  530. if (equipmentInfo == null) {
  531. log.warn("未找到设备信息 - equipmentId: {}", chargeOrderInfo.getEquipmentId());
  532. return BigDecimal.ZERO;
  533. }
  534. // 获取站点信息
  535. ThirdPartyStationInfo stationInfo = thirdPartyStationInfoMapper.selectOne(
  536. Wrappers.<ThirdPartyStationInfo>lambdaQuery()
  537. .eq(ThirdPartyStationInfo::getStationId, equipmentInfo.getStationId())
  538. .eq(ThirdPartyStationInfo::getIsDeleted, 0));
  539. if (stationInfo == null) {
  540. log.warn("未找到站点信息 - stationId: {}", equipmentInfo.getStationId());
  541. return BigDecimal.ZERO;
  542. }
  543. // 获取当前时间,格式为 HHmmss
  544. String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HHmmss"));
  545. // 根据当前时间查询当前时段的价格策略(核心:支持跨时段计费)
  546. ThirdPartyPolicyInfo currentPeriodPolicy = thirdPartyPolicyInfoMapper
  547. .selectCurrentPeriodPolicyByStation(stationInfo.getId(), currentTime);
  548. if (currentPeriodPolicy == null || currentPeriodPolicy.getPeriodFlag() == null) {
  549. log.warn("未找到当前时段价格策略 - stationInfoId: {}, currentTime: {}",
  550. stationInfo.getId(), currentTime);
  551. return BigDecimal.ZERO;
  552. }
  553. Integer currentPeriodFlag = currentPeriodPolicy.getPeriodFlag();
  554. log.info("当前时段 - stationInfoId: {}, currentTime: {}, periodFlag: {}",
  555. stationInfo.getId(), currentTime, currentPeriodFlag);
  556. // 查询当前时段对应的价格策略
  557. Integer salesType = (userFirm != null) ? 1 : 0; // 1-企业 0-平台
  558. Long firmId = (userFirm != null) ? userFirm.getFirmId() : null;
  559. PolicyFee policyFee = policyFeeMapper.selectOne(Wrappers.<PolicyFee>lambdaQuery()
  560. .eq(PolicyFee::getStationInfoId, stationInfo.getId())
  561. .eq(PolicyFee::getPeriodFlag, currentPeriodFlag)
  562. .eq(PolicyFee::getSalesType, salesType)
  563. .eq(salesType == 1, PolicyFee::getFirmId, firmId)
  564. .eq(PolicyFee::getIsDeleted, 0)
  565. .last("LIMIT 1"));
  566. if (policyFee == null) {
  567. log.warn("未找到当前时段的价格策略 - stationInfoId: {}, periodFlag: {}, salesType: {}",
  568. stationInfo.getId(), currentPeriodFlag, salesType);
  569. return BigDecimal.ZERO;
  570. }
  571. // 获取综合销售费作为单价
  572. BigDecimal unitPrice = policyFee.getCompSalesFee() != null ? policyFee.getCompSalesFee() : BigDecimal.ZERO;
  573. // 实际收取金额 = 充电度数 * 当前时段单价
  574. BigDecimal realCost = totalPower.multiply(unitPrice).setScale(2, BigDecimal.ROUND_HALF_UP);
  575. log.info("计算实际收取金额 - totalPower: {}, periodFlag: {}, unitPrice: {}, realCost: {}",
  576. totalPower, currentPeriodFlag, unitPrice, realCost);
  577. return realCost;
  578. } catch (Exception e) {
  579. log.error("计算平台实际收取金额失败", e);
  580. return BigDecimal.ZERO;
  581. }
  582. }
  583. // ==================== JSON解析工具方法 ====================
  584. private String getTextValue(JsonNode node, String fieldName) {
  585. JsonNode field = node.get(fieldName);
  586. return (field != null && !field.isNull()) ? field.asText() : null;
  587. }
  588. private Integer getIntValue(JsonNode node, String fieldName) {
  589. JsonNode field = node.get(fieldName);
  590. return (field != null && !field.isNull()) ? field.asInt() : null;
  591. }
  592. private BigDecimal getDecimalValue(JsonNode node, String fieldName) {
  593. JsonNode field = node.get(fieldName);
  594. return (field != null && !field.isNull()) ? new BigDecimal(field.asText()) : null;
  595. }
  596. private LocalDateTime parseDateTime(String dateTimeStr) {
  597. if (dateTimeStr == null || dateTimeStr.isEmpty()) {
  598. return null;
  599. }
  600. try {
  601. return LocalDateTime.parse(dateTimeStr, DATE_TIME_FORMATTER);
  602. } catch (Exception e) {
  603. log.warn("解析时间失败: {}", dateTimeStr);
  604. return null;
  605. }
  606. }
  607. /**
  608. * 根据充电订单号StartChargeSeq 获取订单信息判断是否需要熔断,提前跳枪
  609. * 使用分布式锁防止并发重复检查
  610. *
  611. * @param chargeStatus 充电状态
  612. * @param chargeOrderInfo 订单信息
  613. */
  614. private void isNeedBreak(ThirdPartyChargeStatus chargeStatus, ChargeOrderInfo chargeOrderInfo) {
  615. // 订单为空或不是充电中状态,不需要熔断检查
  616. if (chargeOrderInfo == null || !Objects.equals(chargeOrderInfo.getStatus(), SystemConstants.STATUS_ONE)) {
  617. return;
  618. }
  619. String lockKey = BREAK_CHECK_LOCK_KEY + chargeStatus.getStartChargeSeq();
  620. RLock lock = redissonClient.getLock(lockKey);
  621. try {
  622. // 尝试获取锁,最多等待3秒,持有锁最多10秒
  623. if (lock.tryLock(LOCK_WAIT_TIME, LOCK_LEASE_TIME, TimeUnit.SECONDS)) {
  624. try {
  625. // 获取安全阈值配置
  626. DictItem dictItem = dictItemMapper.selectOne(
  627. new LambdaQueryWrapper<DictItem>()
  628. .eq(DictItem::getDictCode, "up_recharge")
  629. .eq(DictItem::getStatus, 1)
  630. .last("LIMIT 1"));
  631. BigDecimal safetyThreshold = BigDecimal.ZERO;
  632. if (dictItem != null && dictItem.getValue() != null) {
  633. safetyThreshold = new BigDecimal(dictItem.getValue());
  634. }
  635. // 获取用户余额
  636. UserAccount userAccount = userAccountMapper.selectOne(Wrappers.<UserAccount>lambdaQuery()
  637. .eq(UserAccount::getUserId, chargeOrderInfo.getUserId())
  638. .eq(UserAccount::getIsDeleted, 0));
  639. if (userAccount == null) {
  640. log.warn("未找到用户账户信息 - userId: {}", chargeOrderInfo.getUserId());
  641. return;
  642. }
  643. BigDecimal balance = userAccount.getBalance() != null ? userAccount.getBalance() : BigDecimal.ZERO;
  644. // 获取当前实时消费金额
  645. BigDecimal realCost = chargeOrderInfo.getRealCost() != null ? chargeOrderInfo.getRealCost() : BigDecimal.ZERO;
  646. // 剩余余额 = 账户余额 - 实时消费
  647. BigDecimal remainingBalance = balance.subtract(realCost);
  648. log.info("熔断检查 - startChargeSeq: {}, 用户余额: {}, 实时消费: {}, 安全阈值: {}, 剩余余额: {}",
  649. chargeStatus.getStartChargeSeq(), balance, realCost, safetyThreshold, remainingBalance);
  650. // 熔断条件:剩余余额 < 安全阈值 且 已产生实际消费(实时消费 > 0)
  651. // 这样可以避免刚开始充电(消费为0)时因余额小于阈值而被误熔断
  652. if (remainingBalance.compareTo(safetyThreshold) < 0 && realCost.compareTo(BigDecimal.ZERO) > 0) {
  653. log.warn("余额不足,触发熔断停止充电 - startChargeSeq: {}, 用户余额: {}, 实时消费: {}",
  654. chargeStatus.getStartChargeSeq(), balance, realCost);
  655. // 调用第三方停止充电接口
  656. try {
  657. chargingBusinessService.stopCharging(
  658. chargeStatus.getStartChargeSeq(),
  659. chargeStatus.getConnectorId());
  660. log.info("已发送停止充电请求 - startChargeSeq: {}, connectorId: {}",
  661. chargeStatus.getStartChargeSeq(), chargeStatus.getConnectorId());
  662. // 更新订单停止类型为余额不足停止
  663. chargeOrderInfo.setStopType(3); // 3-余额不足停止
  664. chargeOrderInfo.setStopReason("余额不足,系统自动停止充电");
  665. chargeOrderInfoService.updateById(chargeOrderInfo);
  666. } catch (Exception e) {
  667. log.error("调用第三方停止充电接口失败 - startChargeSeq: {}",
  668. chargeStatus.getStartChargeSeq(), e);
  669. }
  670. }
  671. } finally {
  672. lock.unlock();
  673. }
  674. } else {
  675. log.warn("获取熔断检查锁超时 - startChargeSeq: {}", chargeStatus.getStartChargeSeq());
  676. }
  677. } catch (InterruptedException e) {
  678. Thread.currentThread().interrupt();
  679. log.error("获取熔断检查锁被中断 - startChargeSeq: {}", chargeStatus.getStartChargeSeq(), e);
  680. } catch (Exception e) {
  681. log.error("熔断检查失败 - startChargeSeq: {}", chargeStatus.getStartChargeSeq(), e);
  682. }
  683. }
  684. }