update 预约充电

This commit is contained in:
Guoqs
2024-09-06 16:13:50 +08:00
parent 8123e350f2
commit 12888c5b1f
3 changed files with 39 additions and 3 deletions

View File

@@ -9,6 +9,8 @@ public class CacheConstants {
// 缓存时间 1分钟
public static final int cache_expire_time_1m = 60;
public static final int cache_expire_time_70s = 70;
// 缓存时间 3分钟
public static final int cache_expire_time_3m = cache_expire_time_1m * 3;
@@ -52,6 +54,9 @@ public class CacheConstants {
// 需要推送的站点id
public static final String PUSH_STATION_CONNECTOR = "push_station_connector";
// 更新预约信息key
public static final String UPDATE_RESERVATION_INFO = "update_reservation_info:";
// 订单的微信支付参数
public static final String ORDER_WECHAT_PAY_PARAMETERS = "order_wechat_pay_parameters:";

View File

@@ -1,12 +1,18 @@
package com.jsowell.netty.handler.yunkuaichong;
import com.alibaba.fastjson2.JSON;
import com.jsowell.common.constant.CacheConstants;
import com.jsowell.common.core.domain.ykc.YKCDataProtocol;
import com.jsowell.common.core.domain.ykc.YKCFrameTypeCode;
import com.jsowell.common.core.redis.RedisCache;
import com.jsowell.common.util.BytesUtil;
import com.jsowell.common.util.YKCUtils;
import com.jsowell.netty.factory.YKCOperateFactory;
import com.jsowell.pile.domain.PileReservationInfo;
import com.jsowell.pile.service.PileReservationInfoService;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
@@ -18,6 +24,12 @@ public class ReservationChargingResponseHandler extends AbstractYkcHandler {
private final String type = YKCUtils.frameType2Str(YKCFrameTypeCode.RESERVATION_CHARGING_SETUP_ANSWER_CODE.getBytes());
@Autowired
private RedisCache redisCache;
@Autowired
private PileReservationInfoService pileReservationInfoService;
@Override
public void afterPropertiesSet() throws Exception {
YKCOperateFactory.register(type, this);
@@ -51,7 +63,7 @@ public class ReservationChargingResponseHandler extends AbstractYkcHandler {
byte[] connectorCodeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String connectorCode = BytesUtil.bcd2Str(connectorCodeByteArr);
// 启动结果
// 启动结果 0x00失败 0x01成功
startIndex += length;
length = 1;
byte[] resultCodeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
@@ -63,8 +75,20 @@ public class ReservationChargingResponseHandler extends AbstractYkcHandler {
byte[] failedReasonByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String failedReason = BytesUtil.bcd2Str(failedReasonByteArr);
// log.info("0x59预约充电响应, 交易流水号:{}, 桩SN:{}, 枪口号:{}, 结果:{}, 失败原因:{}",
// transactionCode, pileSn, connectorCode, resultCode, failedReason);
log.info("0x59预约充电响应, 交易流水号:{}, 桩SN:{}, 枪口号:{}, 结果:{}, 失败原因:{}",
transactionCode, pileSn, connectorCode, resultCode, failedReason);
// 如果收到成功, 从redis取值, 保存到数据库
if ("01".equals(resultCode)) {
// 预约成功, 删除redis中的预约信息
String redisKey = CacheConstants.UPDATE_RESERVATION_INFO + pileSn + connectorCode;
JSON cacheObject = redisCache.getCacheObject(redisKey);
if (cacheObject != null) {
log.debug("修改预约充电相应成功, 更新数据库");
PileReservationInfo pileReservationInfo = JSON.parseObject(cacheObject.toString(), PileReservationInfo.class);
pileReservationInfoService.insertOrUpdateSelective(pileReservationInfo);
}
}
return null;
}
}

View File

@@ -413,6 +413,8 @@ public class PileReservationInfoServiceImpl implements PileReservationInfoServic
return 0;
}
String redisKey = CacheConstants.UPDATE_RESERVATION_INFO + pileReservationInfo.getPileConnectorCode();
// 是否发指令 false:不发指令 true:发指令, 默认不发指令
boolean sendFlag = false;
if (StringUtils.equals(pileReservationInfo.getStatus(), "1")) {
@@ -488,11 +490,16 @@ public class PileReservationInfoServiceImpl implements PileReservationInfoServic
.build();
String result = pileRemoteService.reservationCharging(command);
sendResult = StringUtils.equals(result, Constants.ONE);
// 设置缓存1分钟, 如果更新数据库就删掉, 如果没有更新数据量缓存70秒
redisCache.setCacheObject(redisKey, JSON.toJSONString(pileReservationInfo), CacheConstants.cache_expire_time_70s);
}
log.info("修改预约充电信息, updateFlag:{}, sendFlag:{}, sendResult:{}", updateFlag, sendFlag, sendResult);
// 更新数据库, 一般情况下是都更新的, 只有在发送指令并发送失败的情况下才不更新
if (updateFlag && (sendFlag == sendResult)) {
log.debug("修改预约充电相应成功, 删除缓存并更新数据库");
redisCache.deleteObject(redisKey);
return this.insertOrUpdateSelective(pileReservationInfo);
}
return 0;