From 7a4f9541331bef779a506b38a27ed5c3373c0bec Mon Sep 17 00:00:00 2001
From: Pu Zhibing <393733352@qq.com>
Date: 星期四, 15 五月 2025 13:53:22 +0800
Subject: [PATCH] 开发二级等保功能及心跳消息队列修改

---
 ruoyi-service/ruoyi-integration/src/main/java/com/ruoyi/integration/drainage/TCECUtil.java |  369 ++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 267 insertions(+), 102 deletions(-)

diff --git a/ruoyi-service/ruoyi-integration/src/main/java/com/ruoyi/integration/drainage/TCECUtil.java b/ruoyi-service/ruoyi-integration/src/main/java/com/ruoyi/integration/drainage/TCECUtil.java
index 0cadb76..2478762 100644
--- a/ruoyi-service/ruoyi-integration/src/main/java/com/ruoyi/integration/drainage/TCECUtil.java
+++ b/ruoyi-service/ruoyi-integration/src/main/java/com/ruoyi/integration/drainage/TCECUtil.java
@@ -3,14 +3,26 @@
 import cn.hutool.http.*;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
+import com.alibaba.nacos.common.utils.JacksonUtils;
+import com.ruoyi.common.core.utils.SpringUtils;
+import com.ruoyi.integration.drainage.kuaidian.model.NotificationStationChangeResult;
+import com.ruoyi.integration.drainage.kuaidian.model.StationChange;
 import com.ruoyi.integration.drainage.model.*;
+import com.ruoyi.integration.drainage.model.enu.InterfaceUrlEnum;
+import com.ruoyi.other.api.domain.Operator;
+import com.ruoyi.other.api.feignClient.OperatorClient;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.data.redis.core.RedisTemplate;
 
+import java.time.Instant;
 import java.time.LocalDateTime;
 import java.time.ZoneOffset;
 import java.time.format.DateTimeFormatter;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
 
 /**
  * 中电联TCEC标准
@@ -19,49 +31,57 @@
  */
 @Slf4j
 public class TCECUtil {
-	/**
-	 * 运营商标识
-	 */
-	private static final String OperatorID = "";
-	/**
-	 * 运营商秘钥
-	 */
-	private static final String OperatorSecret = "";
-	/**
-	 * tokne
-	 */
-	private static String token = "";
-	/**
-	 * token过期时间,秒
-	 */
-	private static Long TokenAvailableTime = 0L;
 	
-	private static Map<Long, Integer> map = new HashMap<>();
-	
+	private static OperatorClient operatorClient = SpringUtils.getBean(OperatorClient.class);
+
+
 	
 	/**
 	 * 获取token
 	 */
-	public static void queryToken(){
-		HttpRequest post = HttpUtil.createPost("");
+	public static String queryToken(Operator operator){
+		HttpRequest post = HttpUtil.createPost(operator.getUrl() + InterfaceUrlEnum.QUERY_TOKEN.getUrl());
+		JSONObject info = new JSONObject();
+		info.put("OperatorID", operator.getOurOperatorId());
+		info.put("OperatorSecret", operator.getOperatorSecret());
+		Long timeStamp = Long.valueOf(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
 		post.contentType("application/json;charset=utf-8");
-		JSONObject body = new JSONObject();
-		body.put("OperatorID", OperatorID);
-		body.put("OperatorSecret", OperatorSecret);
-		post.body(body.toJSONString());
+		BaseRequest baseRequest = new BaseRequest();
+		baseRequest.setOperatorID(operator.getOurOperatorId());
+		baseRequest.setTimeStamp(timeStamp);
+		baseRequest.setSeq("0001");
+		String jsonString = JacksonUtils.toJson(info);
+		String encrypt = AESUtil.encrypt(jsonString, operator.getDataSecret(), operator.getDataSecretIv());
+		baseRequest.setData(encrypt);
+		baseRequest.setOperator(operator);
+		baseRequest.setSig(buildSign(baseRequest));
+		String request_json = JacksonUtils.toJson(baseRequest);
+		log.info("获取三方平台授权token请求地址:" + post.getUrl());
+		log.info("获取三方平台授权token请求参数:" + request_json);
+		log.info("获取三方平台授权token请求Data:" + jsonString);
+		post.body(request_json);
 		HttpResponse execute = post.execute();
 		if(200 != execute.getStatus()){
-			log.error("获取token失败:" + execute.body());
-			return;
+			log.error("获取三方平台授权token失败:" + execute.body());
+			return null;
 		}
-		JSONObject jsonObject = JSON.parseObject(execute.body());
-		Integer succStat = jsonObject.getInteger("SuccStat");
-		if(0 != succStat){
-			log.error("获取token失败:" + jsonObject.getString("FailReason"));
-			return;
+		log.info("获取三方平台授权token响应参数:" + execute.body());
+		BaseResult baseResult = JSON.parseObject(execute.body(), BaseResult.class);
+		Integer Ret = baseResult.getRet();
+		if(0 != Ret){
+			log.error("获取三方平台授权token失败:" + baseResult.getMsg());
+			return null;
 		}
-		token = jsonObject.getString("AccessToken");
-		TokenAvailableTime = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) + jsonObject.getLong("TokenAvailableTime");
+		//解密参数
+		String decrypt = AESUtil.decrypt(baseResult.getData(), operator.getDataSecret(), operator.getDataSecretIv());
+		log.info("获取三方平台授权token响应Data:" + decrypt);
+		QueryTokenResult queryTokenResult = JSON.parseObject(decrypt, QueryTokenResult.class);
+		String token = queryTokenResult.getAccessToken();
+		Long tokenAvailableTime = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) + queryTokenResult.getTokenAvailableTime();
+		operator.setAccessToken(token);
+		operator.setTokenAvailableTime(tokenAvailableTime);
+		operatorClient.editOperator(operator);
+		return token;
 	}
 	
 	
@@ -69,12 +89,12 @@
 	 * 获取token
 	 * @return
 	 */
-	public static String getToken(){
-		long second = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC);
-		if(second >= TokenAvailableTime){
-			queryToken();
+	public static String getToken(Operator operator){
+		if(null != operator.getTokenAvailableTime() && operator.getTokenAvailableTime() > LocalDateTime.now().toEpochSecond(ZoneOffset.UTC)){
+			return operator.getAccessToken();
+		}else{
+			return queryToken(operator);
 		}
-		return token;
 	}
 	
 	
@@ -83,16 +103,31 @@
 	 * 设备状态变化推送
 	 * @param info
 	 */
-	public static NotificationStationStatusResult notificationStationStatus(ConnectorStatusInfo info) {
-		HttpRequest post = HttpUtil.createPost("");
-		buildBody(post, info);
+	public static NotificationStationStatusResult notificationStationStatus(Operator operator, ConnectorStatusInfo info) {
+		HttpRequest post = HttpUtil.createPost(operator.getUrl() + InterfaceUrlEnum.NOTIFICATION_STATION_STATUS.getUrl());
+		buildBody(post, info, operator);
 		HttpResponse execute = post.execute();
 		if(200 != execute.getStatus()){
-			log.error("设备状态变化推送失败:" + execute.body());
+			log.error("推送三方平台设备状态失败:" + execute.body());
 			return null;
 		}
-		return JSON.parseObject(execute.body(), NotificationStationStatusResult.class);
+		log.info("推送三方平台设备状态响应:" + execute.body());
+		BaseResult baseResult = JSON.parseObject(execute.body(), BaseResult.class);
+		Integer Ret = baseResult.getRet();
+		if(0 != Ret){
+			log.error("推送三方平台设备状态失败:" + baseResult.getMsg());
+			return null;
+		}
+		//解密参数
+		String decrypt = AESUtil.decrypt(baseResult.getData(), operator.getDataSecret(), operator.getDataSecretIv());
+		log.info("推送三方平台设备状态Data:" + decrypt);
+		NotificationStationStatusResult notificationStationStatusResult = JSON.parseObject(decrypt, NotificationStationStatusResult.class);
+		return notificationStationStatusResult;
 	}
+	
+	
+	
+	
 	
 	
 	/**
@@ -100,21 +135,26 @@
 	 * @param info
 	 * @return
 	 */
-	public static NotificationStartChargeResult notificationStartChargeResult(NotificationStartCharge info){
-		HttpRequest post = HttpUtil.createPost("");
-		buildBody(post, info);
+	public static NotificationStartChargeResult notificationStartChargeResult(Operator operator, NotificationStartCharge info){
+		HttpRequest post = HttpUtil.createPost(operator.getUrl() + InterfaceUrlEnum.NOTIFICATION_START_CHARGE_RESULT.getUrl());
+		buildBody(post, info, operator);
 		HttpResponse execute = post.execute();
 		if(200 != execute.getStatus()){
-			log.error("启动充电结果推送失败:" + execute.body());
+			log.error("推送三方平台启动充电结果失败:" + execute.body());
 			return null;
 		}
-		JSONObject jsonObject = JSON.parseObject(execute.body());
-		Integer ret = jsonObject.getInteger("Ret");
-		if(0 != ret){
-			log.error("启动充电结果推送失败:" + execute.body());
+		log.info("推送三方平台启动充电结果响应:" + execute.body());
+		BaseResult baseResult = JSON.parseObject(execute.body(), BaseResult.class);
+		Integer Ret = baseResult.getRet();
+		if(0 != Ret){
+			log.error("推送三方平台启动充电结果失败:" + baseResult.getMsg());
 			return null;
 		}
-		return jsonObject.getObject("Data", NotificationStartChargeResult.class);
+		//解密参数
+		String decrypt = AESUtil.decrypt(baseResult.getData(), operator.getDataSecret(), operator.getDataSecretIv());
+		log.info("推送三方平台启动充电结果Data:" + decrypt);
+		NotificationStartChargeResult notificationStartChargeResult = JSON.parseObject(decrypt, NotificationStartChargeResult.class);
+		return notificationStartChargeResult;
 	}
 	
 	
@@ -128,21 +168,26 @@
 	 * @param info
 	 * @return
 	 */
-	public static NotificationEquipChargeStatusResult notificationEquipChargeStatus(QueryEquipChargeStatusResult info){
-		HttpRequest post = HttpUtil.createPost("");
-		buildBody(post, info);
+	public static NotificationEquipChargeStatusResult notificationEquipChargeStatus(Operator operator, QueryEquipChargeStatusResult info){
+		HttpRequest post = HttpUtil.createPost(operator.getUrl() + InterfaceUrlEnum.NOTIFICATION_EQUIP_CHARGE_STATUS.getUrl());
+		buildBody(post, info, operator);
 		HttpResponse execute = post.execute();
 		if(200 != execute.getStatus()){
-			log.error("推送充电状态失败:" + execute.body());
+			log.error("推送三方平台充电状态失败:" + execute.body());
 			return null;
 		}
-		JSONObject jsonObject = JSON.parseObject(execute.body());
-		Integer ret = jsonObject.getInteger("Ret");
-		if(0 != ret){
-			log.error("推送充电状态失败:" + execute.body());
+		log.info("推送三方平台充电状态响应:" + execute.body());
+		BaseResult baseResult = JSON.parseObject(execute.body(), BaseResult.class);
+		Integer Ret = baseResult.getRet();
+		if(0 != Ret){
+			log.error("推送三方平台充电状态失败:" + baseResult.getMsg());
 			return null;
 		}
-		return jsonObject.getObject("Data", NotificationEquipChargeStatusResult.class);
+		//解密参数
+		String decrypt = AESUtil.decrypt(baseResult.getData(), operator.getDataSecret(), operator.getDataSecretIv());
+		log.info("推送三方平台充电状态Data:" + decrypt);
+		NotificationEquipChargeStatusResult notificationEquipChargeStatusResult = JSON.parseObject(decrypt, NotificationEquipChargeStatusResult.class);
+		return notificationEquipChargeStatusResult;
 	}
 	
 	
@@ -151,21 +196,26 @@
 	 * @param info
 	 * @return
 	 */
-	public static NotificationStopCharge notificationStopChargeResult(NotificationStopChargeResult info){
-		HttpRequest post = HttpUtil.createPost("");
-		buildBody(post, info);
+	public static NotificationStopCharge notificationStopChargeResult(Operator operator, NotificationStopChargeResult info){
+		HttpRequest post = HttpUtil.createPost(operator.getUrl() + InterfaceUrlEnum.NOTIFICATION_STOP_CHARGE_RESULT.getUrl());
+		buildBody(post, info, operator);
 		HttpResponse execute = post.execute();
 		if(200 != execute.getStatus()){
-			log.error("推送停止充电结果失败:" + execute.body());
+			log.error("推送三方平台停止充电结果失败:" + execute.body());
 			return null;
 		}
-		JSONObject jsonObject = JSON.parseObject(execute.body());
-		Integer ret = jsonObject.getInteger("Ret");
-		if(0 != ret){
-			log.error("推送停止充电结果失败:" + execute.body());
+		log.info("推送三方平台停止充电结果响应:" + execute.body());
+		BaseResult baseResult = JSON.parseObject(execute.body(), BaseResult.class);
+		Integer Ret = baseResult.getRet();
+		if(0 != Ret){
+			log.error("推送三方平台停止充电结果失败:" + baseResult.getMsg());
 			return null;
 		}
-		return jsonObject.getObject("Data", NotificationStopCharge.class);
+		//解密参数
+		String decrypt = AESUtil.decrypt(baseResult.getData(), operator.getDataSecret(), operator.getDataSecretIv());
+		log.info("推送三方平台停止充电结果Data:" + decrypt);
+		NotificationStopCharge notificationStopCharge = JSON.parseObject(decrypt, NotificationStopCharge.class);
+		return notificationStopCharge;
 	}
 	
 	
@@ -176,21 +226,26 @@
 	 * @param info
 	 * @return
 	 */
-	public static NotificationChargeOrderInfoResult notificationChargeOrderInfo(NotificationChargeOrderInfo info){
-		HttpRequest post = HttpUtil.createPost("");
-		buildBody(post, info);
+	public static NotificationChargeOrderInfoResult notificationChargeOrderInfo(Operator operator, NotificationChargeOrderInfo info){
+		HttpRequest post = HttpUtil.createPost(operator.getUrl() + InterfaceUrlEnum.NOTIFICATION_CHARGE_ORDER_INFO.getUrl());
+		buildBody(post, info, operator);
 		HttpResponse execute = post.execute();
 		if(200 != execute.getStatus()){
-			log.error("推送充电订单信息失败:" + execute.body());
+			log.error("推送三方平台充电订单信息失败:" + execute.body());
 			return null;
 		}
-		JSONObject jsonObject = JSON.parseObject(execute.body());
-		Integer ret = jsonObject.getInteger("Ret");
-		if(0 != ret){
-			log.error("推送充电订单信息失败:" + execute.body());
+		log.info("推送三方平台充电订单信息响应:" + execute.body());
+		BaseResult baseResult = JSON.parseObject(execute.body(), BaseResult.class);
+		Integer Ret = baseResult.getRet();
+		if(0 != Ret){
+			log.error("推送三方平台充电订单信息失败:" + baseResult.getMsg());
 			return null;
 		}
-		return jsonObject.getObject("Data", NotificationChargeOrderInfoResult.class);
+		//解密参数
+		String decrypt = AESUtil.decrypt(baseResult.getData(), operator.getDataSecret(), operator.getDataSecretIv());
+		log.info("推送三方平台充电订单信息Data:" + decrypt);
+		NotificationChargeOrderInfoResult notificationChargeOrderInfoResult = JSON.parseObject(decrypt, NotificationChargeOrderInfoResult.class);
+		return notificationChargeOrderInfoResult;
 	}
 	
 	
@@ -199,30 +254,140 @@
 	 * @param post
 	 * @param o
 	 */
-	public static void buildBody(HttpRequest post, Object o){
-		Long key = Long.valueOf(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
-		Integer integer = map.get(key);
-		if(null == integer){
-			integer = 1;
-		}else{
-			integer++;
-		}
-		map.put(key, integer);
+	public static void buildBody(HttpRequest post, Object o, Operator operator){
+		Long timeStamp = Long.valueOf(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
 		post.contentType("application/json;charset=utf-8");
-		post.header("Authorization", "Bearer " + getToken());
-		JSONObject body = new JSONObject();
-		body.put("OperatorID", OperatorID);
-		body.put("Data", AESUtil.encrypt(JSON.toJSONString(o)));
-		body.put("TimeStamp", key);
-		body.put("Seq", String.format("%04d", integer));
-		body.put("Sig", "");
-		post.body(body.toJSONString());
-		
-		//清空小于当前时间的map中的无效数据
-		for (Long k : map.keySet()) {
-			if(k <= (key - 10)){
-				map.remove(k);
-			}
+		post.header("Authorization", "Bearer " + getToken(operator));
+		BaseRequest baseRequest = new BaseRequest();
+		baseRequest.setOperatorID(operator.getOurOperatorId());
+		baseRequest.setTimeStamp(timeStamp);
+		baseRequest.setSeq("0001");
+		String jsonString = JacksonUtils.toJson(o);
+		String encrypt = AESUtil.encrypt(jsonString, operator.getDataSecret(), operator.getDataSecretIv());
+		baseRequest.setData(encrypt);
+		baseRequest.setOperator(operator);
+		baseRequest.setSig(buildSign(baseRequest));
+		String request_json = JacksonUtils.toJson(baseRequest);
+		post.body(request_json);
+		log.info("推送三方平台请求地址:" + post.getUrl());
+		log.info("推送三方平台请求参数:" + request_json);
+		log.info("推送三方平台请求Data:" + jsonString);
+	}
+
+	
+	/**
+	 * 构建签名字符串
+	 * @param model
+	 * @return
+	 */
+	public static String buildSign(BaseModel model){
+		Operator operator = model.getOperator();
+		if(null == operator){
+			return "";
 		}
+		//签名秘钥SigSecret
+		String key = operator.getSigSecret();
+		String sign = "";
+		switch (operator.getName()){
+			case "XinDianTu":
+				//进行字符串拼接、计算
+				String m = new StringBuilder(model.getOperatorID()).append(model.getData()).append(model.getTimeStamp()).append(model.getSeq()).toString();
+				byte[] hmacMd5 = SignUtil.getHMacMD5Bytes(key.getBytes(), m.getBytes());
+				// 打印计算得到的签名Sig
+				sign = SignUtil.bytesToHexString(hmacMd5);
+				break;
+			case "KuaiDian":
+				//进行字符串拼接、计算
+				String m1 = new StringBuilder(model.getOperatorID()).append(model.getData()).append(model.getTimeStamp()).append(model.getSeq()).toString();
+				// 打印计算得到的签名Sig
+				sign = SignUtil.hmacSign(m1, key);
+				break;
+		}
+		return sign;
+	}
+	
+	
+	
+	/**
+	 * 构建签名字符串
+	 * @param model
+	 * @return
+	 */
+	public static String ourBuildSign(BaseModel model){
+		Operator operator = model.getOperator();
+		if(null == operator){
+			return "";
+		}
+		//签名秘钥SigSecret
+		String key = operator.getOurSigSecret();
+		String sign = "";
+		switch (operator.getName()){
+			case "XinDianTu":
+				//进行字符串拼接、计算
+				String m = new StringBuilder(model.getOperatorID()).append(model.getData()).append(model.getTimeStamp()).append(model.getSeq()).toString();
+				byte[] hmacMd5 = SignUtil.getHMacMD5Bytes(key.getBytes(), m.getBytes());
+				// 打印计算得到的签名Sig
+				sign = SignUtil.bytesToHexString(hmacMd5);
+				break;
+			case "KuaiDian":
+				//进行字符串拼接、计算
+				String m1 = new StringBuilder(model.getOperatorID()).append(model.getData()).append(model.getTimeStamp()).append(model.getSeq()).toString();
+				sign = SignUtil.hmacSign(m1, key);
+				break;
+		}
+		return sign;
+	}
+	public static String ourBuildSignJianGuan(BaseModel model){
+		Operator operator = model.getOperator();
+		if(null == operator){
+			return "";
+		}
+		//签名秘钥SigSecret
+		String key = operator.getOurSigSecret();
+		String sign = "";
+		//进行字符串拼接、计算
+		String m1 = new StringBuilder(model.getOperatorID()).append(model.getData()).append(model.getTimeStamp()).append(model.getSeq()).toString();
+		sign = SignUtil.hmacSign(m1, key);
+		return sign;
+	}
+
+
+	
+	
+	public static void main(String[] args) {
+		BaseModel model = new BaseModel();
+		model.setOperatorID("MA25CNM38");
+		model.setData("AoArdDDcmHcmOMkCLHodTpY1xLtt9yhLqxvKPyfdlmEOBj1LJnQM+Z4JOZllt3Pj9rubfgxJ51zMAfzquQegJzHGAT9Y7JrKFzFe6jGtXo0=");
+		model.setTimeStamp(20250205120800L);
+		model.setSeq("0001");
+		Operator operator = new Operator();
+		operator.setName("XinDianTu");
+		operator.setSigSecret("S94xUpTpOIlLJBk8");
+		model.setOperator(operator);
+		String key = operator.getSigSecret();
+
+		String m = new StringBuilder(model.getOperatorID()).append(model.getData()).append(model.getTimeStamp()).append(model.getSeq()).toString();
+		byte[] hmacMd5 = SignUtil.getHMacMD5Bytes(key.getBytes(), m.getBytes());
+		// 打印计算得到的签名Sig
+		String s = SignUtil.bytesToHexString(hmacMd5);
+		System.err.println(s);
+		
+		
+//		BaseModel model = new BaseModel();
+//		model.setOperatorID("MA25CNM38");
+//		model.setData("xQYYEPiwoc4JENnQsF50qP6Tmnj7uU/AshWn3QbIgrMIk8zvFpy6fWLIiLzQHcLPFCihqH1uzoNhGVNcS8Wvf5gIx9+cWJvYtjwtsjAGMEwNQ6y+rF1jjKnqCNcQbHbhqJ2l76sD54QCFKyenKxymrXri19bDV8hizn3nBw+jcHBtqKHWzjdSHwPDWPIw4rXnrbWLya2PXVgYtVg93WPtliw+0xsqW0BxJamE70/Ilbg4wB5fOAOvXmbLFI+v9jRjpcA7ImhprqPMYkXYAfa2QlICrJEBK7DfpJvRMzKzeT90kOAITqIqFvbfV0ti+greRqToLP6ni4o6w3ID42UmKasIpLTnx+qtR4bVTBTRuDzXvNXDA92RqHaJO21xbSen1eHN/yGfrchBIM0gmav7EpaYAerfU4ubnmJ6xujR6okYepJwBD5DiSIrObfS+GWVPRekvnjvm7BC02NXTrGKA==");
+//		model.setTimeStamp(20250205161426L);
+//		model.setSeq("0001");
+//		Operator operator = new Operator();
+//		operator.setOurOperatorId("2921700136");
+//		operator.setOurSigSecret("KBm5J2fbGzyhX023");
+//		model.setOperator(operator);
+//		String key = operator.getOurSigSecret();
+//
+//		String m = new StringBuilder(operator.getOurOperatorId()).append(model.getData()).append(model.getTimeStamp()).append(model.getSeq()).toString();
+//		byte[] hmacMd5 = SignUtil.getHMacMD5Bytes(key.getBytes(), m.getBytes());
+//		// 打印计算得到的签名Sig
+//		String s = SignUtil.bytesToHexString(hmacMd5);
+//		System.err.println(s);
 	}
 }

--
Gitblit v1.7.1