避免因数据库错误造成第三方订单丢失

This commit is contained in:
ZZ 2022-03-14 15:41:08 +08:00
parent 2f5be831b5
commit 946caa1f63
3 changed files with 87 additions and 54 deletions

View File

@ -573,29 +573,30 @@ public class XhpcChargeOrderServiceImpl extends BaseService implements IXhpcChar
xhpcChargeOrder.setStatus(status); xhpcChargeOrder.setStatus(status);
xhpcChargeOrder.setCreateTime(new Date()); xhpcChargeOrder.setCreateTime(new Date());
xhpcChargeOrder.setTerminalId(terminalId); xhpcChargeOrder.setTerminalId(terminalId);
try { xhpcChargeOrder.setPower(power == null ? "120" : power.toString());
xhpcChargeOrder.setPower(power.toString());
}catch (Exception e){
}
xhpcChargeOrder.setSource(1); xhpcChargeOrder.setSource(1);
xhpcChargeOrder.setUserId(userId); xhpcChargeOrder.setUserId(userId);
xhpcChargeOrder.setChargingMode(chargingMode); xhpcChargeOrder.setChargingMode(chargingMode);
int res =xhpcChargeOrderMapper.addXhpcChargeOrder(xhpcChargeOrder); int res = xhpcChargeOrderMapper.addXhpcChargeOrder(xhpcChargeOrder);
//int res = xhpcChargeOrderMapper.addBy3rd(internetSerialNumber, orderNo, driverId, chargingAmt, plateNum, status, date,null, terminalId, power, chargingStationId, chargingMode, 1, userId); //int res = xhpcChargeOrderMapper.addBy3rd(internetSerialNumber, orderNo, driverId, chargingAmt, plateNum, status,
// date,null, terminalId, power, chargingStationId, chargingMode, 1, userId);
R r1 = powerPileService.startCharging(startChargingData); R r1 = powerPileService.startCharging(startChargingData);
logger.info("<<<<<<<<<<<<<<<<<<<<<<<<第三方充电返回>>>>>>>>>>>>>>>>>"); logger.info("<<<<<<<<<<<<<<<<<<<<<<<<第三方充电返回>>>>>>>>>>>>>>>>>");
logger.info("<<<<<<<<<<<<<<<<<<<<<<<<" + r1.getCode() + ">>>>>>>>>>>>>>>>>"); logger.info("<<<<<<<<<<<<<<<<<<<<<<<<" + r1.getCode() + ">>>>>>>>>>>>>>>>>");
logger.info("<<<<<<<<<<<<<<<<<<<<<<<<" + r1.getMsg() + ">>>>>>>>>>>>>>>>>"); logger.info("<<<<<<<<<<<<<<<<<<<<<<<<" + r1.getMsg() + ">>>>>>>>>>>>>>>>>");
logger.info("<<<<<<<<<<<<<<<<<<<<<<<<" + r1.getData() + ">>>>>>>>>>>>>>>>>"); logger.info("<<<<<<<<<<<<<<<<<<<<<<<<" + r1.getData() + ">>>>>>>>>>>>>>>>>");
logger.info("<<<<<<<<<<<<<<<<<<<<<<<<第三方充电返回>>>>>>>>>>>>>>>>>"); logger.info("<<<<<<<<<<<<<<<<<<<<<<<<第三方充电返回>>>>>>>>>>>>>>>>>");
Long rateModelId = Long.valueOf(r1.getData().toString()); try {
xhpcChargeOrder.setRateModelId(rateModelId); Long rateModelId = Long.valueOf(r1.getData().toString());
xhpcChargeOrderMapper.updateXhpcChargeOrder(xhpcChargeOrder); xhpcChargeOrder.setRateModelId(rateModelId);
xhpcChargeOrderMapper.updateXhpcChargeOrder(xhpcChargeOrder);
} catch (Exception e) {
logger.error("update order[{}] failed.", orderNo);
}
Map<String, Object> data = new HashMap<>(); Map<String, Object> data = new HashMap<>();
data.put("orderNo", orderNo);
data.put("startTime", startTime); data.put("startTime", startTime);
data.put("orderNo", orderNo);
r.setData(data); r.setData(data);
if (res != 0) { if (res != 0) {
r.setCode(200); r.setCode(200);

View File

@ -285,8 +285,8 @@ public class ChargingController {
} }
@GetMapping("native/charging/stop/{pileNo}/{connectorId}/{version}") @GetMapping("native/charging/stop/{pileNo}/{connectorId}/{version}")
public R nativeStopCharging(@PathVariable("pileNo") String pileNo, @PathVariable("connectorId") String connectorId, public R stopInvalidOrder(@PathVariable("pileNo") String pileNo, @PathVariable("connectorId") String connectorId,
@PathVariable("version") String version) { @PathVariable("version") String version) {
ClientHandler handler = ChargingPileServer.getHandler(pileNo); ClientHandler handler = ChargingPileServer.getHandler(pileNo);
if (handler == null) { if (handler == null) {
@ -309,8 +309,6 @@ public class ChargingController {
return R.ok("充电桩已离线"); return R.ok("充电桩已离线");
} else { } else {
handler.sendClientBinary(msg); handler.sendClientBinary(msg);
Map<String, Object> cacheGun = REDIS.getCacheMap(gunkey);
REDIS.setCacheMap(gunkey, cacheGun);
return R.ok("停止充电指令已下发"); return R.ok("停止充电指令已下发");
} }
} catch (IOException e) { } catch (IOException e) {
@ -319,6 +317,35 @@ public class ChargingController {
} }
} }
public void stopInvalidOrder(String orderNo) {
String pileNo = orderNo.substring(0, 14);
ClientHandler handler = ChargingPileServer.getHandler(pileNo);
if (handler != null) {
try {
String connectorId = pileNo.substring(0, 16);
String gunkey = "gun:".concat(connectorId);
String skey = gunkey.concat(".seqhex");
String seq = seqHex(skey);
byte[] msg = translateStop(connectorId, seq);
if (!handler.isOpen()) {
log.error("stop invalid order failed. [{}]({}) connection lost", handler.getName(), pileNo);
removeHandler(pileNo);
} else {
String orderkey = REDIS.getCacheMapValue(gunkey, "orderkey");
Integer sts = REDIS.getCacheMapValue(orderkey, "sts");
if (orderkey != null && orderNo.equals(orderkey.substring(6)) && (sts == null)) {
handler.sendClientBinary(msg);
log.error("invalid orderNo [{}], stop msg sent to pile |{}|", pileNo, HexUtils.toHex(msg));
REDIS.setCacheMapValue(orderkey, "sts", 1);
}
}
} catch (IOException e) {
log.error("send message failed. " + e.getMessage(), e);
}
}
}
private byte[] translateStart(StartChargingData startChargingData, String seq) { private byte[] translateStart(StartChargingData startChargingData, String seq) {
byte[] data = new byte[0]; byte[] data = new byte[0];
@ -328,7 +355,8 @@ public class ChargingController {
data = ArrayUtils.addAll(data, HexUtils.toBytes(startChargingData.getOrderNo())); data = ArrayUtils.addAll(data, HexUtils.toBytes(startChargingData.getOrderNo()));
data = ArrayUtils.addAll(data, HexUtils.toBytes(startChargingData.getPileNo())); data = ArrayUtils.addAll(data, HexUtils.toBytes(startChargingData.getPileNo()));
data = ArrayUtils.addAll(data, HexUtils.toBytes(startChargingData.getGunId())); data = ArrayUtils.addAll(data, HexUtils.toBytes(startChargingData.getGunId()));
data = ArrayUtils.addAll(data, HexUtils.toBytes(String.format("%016X", Long.parseLong(startChargingData.getLogicCardNo(), 16)))); data = ArrayUtils.addAll(data, HexUtils.toBytes(String.format("%016X",
Long.parseLong(startChargingData.getLogicCardNo(), 16))));
data = ArrayUtils.addAll(data, HexUtils.toBytes(String.format("%016X", Long.parseLong(startChargingData.getPhysicCardNo(), 16)))); data = ArrayUtils.addAll(data, HexUtils.toBytes(String.format("%016X", Long.parseLong(startChargingData.getPhysicCardNo(), 16))));
data = ArrayUtils.addAll(data, HexUtils.toBytes(toHexInt(startChargingData.getBalance()))); data = ArrayUtils.addAll(data, HexUtils.toBytes(toHexInt(startChargingData.getBalance())));
} else { } else {

View File

@ -147,48 +147,52 @@ public class RealtimeDataLogic implements ServiceLogic {
cacheOrder.put("totalPower", cacheRealtimeData.getChargingDegree() / 10000.0); cacheOrder.put("totalPower", cacheRealtimeData.getChargingDegree() / 10000.0);
cacheOrder.put("totalMoney", cacheRealtimeData.getAmountCharged() / 10000.0); cacheOrder.put("totalMoney", cacheRealtimeData.getAmountCharged() / 10000.0);
cacheOrder.put("realtimeDataList", realtimeDataList); cacheOrder.put("realtimeDataList", realtimeDataList);
pileOrderService.pileRimeOrder(orderNo); R r1 = pileOrderService.pileRimeOrder(orderNo);
Integer vul = (Integer) cachePile.get("voltageUpperLimits"); if (r1.getMsg().contains("无效订单")) {
Integer cul = (Integer) cachePile.get("currentLimit"); chargingController.stopInvalidOrder(pileNo);
if (wc > cul || wv > vul) {
R r = chargingController.nativeStopCharging(pileNo, gunId, default_version);
if (r.getCode() == 200) {
Integer vcpcnt = (Integer) cacheOrder.get("vcpcnt");
cacheOrder.put("vcpcnt", vcpcnt == null ? 1 : vcpcnt++);
log.error("{} alert: v[{}], c[{}]", orderNo, wv, wc);
}
} else { } else {
Integer stopSoc = (Integer) cacheOrder.get("stopSoc"); Integer vul = (Integer) cachePile.get("voltageUpperLimits");
if (stopSoc != null && socInt >= stopSoc) { Integer cul = (Integer) cachePile.get("currentLimit");
R r = chargingController.nativeStopCharging(pileNo, gunId, default_version); if (wc > cul || wv > vul) {
String alerted = (String) cacheOrder.get("socalerted"); R r = chargingController.stopInvalidOrder(pileNo, gunId, default_version);
String tel = (String) cacheOrder.get("tel"); if (r.getCode() == 200) {
if (alerted == null && tel != null) { Integer vcpcnt = (Integer) cacheOrder.get("vcpcnt");
if (r.getCode() == 200) { cacheOrder.put("vcpcnt", vcpcnt == null ? 1 : vcpcnt++);
HashMap<String, String> paramMap = new HashMap<>(); log.error("{} alert: v[{}], c[{}]", orderNo, wv, wc);
paramMap.put("battery", stopSoc.toString()); }
paramMap.put("phone", tel); } else {
paramMap.put("content", "【小华充电】尊敬的用户你的车辆已充电达至设定的SOC(" + stopSoc + "%)。"); Integer stopSoc = (Integer) cacheOrder.get("stopSoc");
smsService.sendNotice(paramMap); if (stopSoc != null && socInt >= stopSoc) {
cacheOrder.put("socalerted", "true"); R r = chargingController.stopInvalidOrder(pileNo, gunId, default_version);
String alerted = (String) cacheOrder.get("socalerted");
String tel = (String) cacheOrder.get("tel");
if (alerted == null && tel != null) {
if (r.getCode() == 200) {
HashMap<String, String> paramMap = new HashMap<>();
paramMap.put("battery", stopSoc.toString());
paramMap.put("phone", tel);
paramMap.put("content", "【小华充电】尊敬的用户你的车辆已充电达至设定的SOC(" + stopSoc + "%)。");
smsService.sendNotice(paramMap);
cacheOrder.put("socalerted", "true");
}
} }
} }
} if (balance < 50000) {
if (balance < 50000) { String alerted = (String) cacheOrder.get("lt5alerted");
String alerted = (String) cacheOrder.get("lt5alerted"); String tel = (String) cacheOrder.get("tel");
String tel = (String) cacheOrder.get("tel"); if (alerted == null && tel != null) {
if (alerted == null && tel != null) { HashMap<String, String> paramMap = new HashMap<>();
HashMap<String, String> paramMap = new HashMap<>(); paramMap.put("money", "5");
paramMap.put("money", "5"); paramMap.put("phone", tel);
paramMap.put("phone", tel); paramMap.put("content", "【小华充电】尊敬的用户你的账户余额小于5元,为不影响您的正常充电,请您尽快充值交费,谢谢。");
paramMap.put("content", "【小华充电】尊敬的用户你的账户余额小于5元,为不影响您的正常充电,请您尽快充值交费,谢谢。"); smsService.sendNotice(paramMap);
smsService.sendNotice(paramMap); cacheOrder.put("lt5alerted", "true");
cacheOrder.put("lt5alerted", "true"); }
}
if ("true".equals(cacheOrder.get("lordiss"))) {
cacheOrder.put("lordiss", "false");
pileOrderService.pileStop(orderNo, 4, "订单实时数据恢复");
} }
}
if ("true".equals(cacheOrder.get("lordiss"))) {
cacheOrder.put("lordiss", "false");
pileOrderService.pileStop(orderNo, 4, "订单实时数据恢复");
} }
} }
} else { } else {