Merge branch 'feature_lsl' of gitee.com:L-Shilin/go-fly-platform-services into feature_dzc
This commit is contained in:
+65
-4
@@ -20,6 +20,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -27,6 +28,7 @@ import org.springframework.web.bind.annotation.*;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 设备操作controller
|
||||
@@ -47,6 +49,9 @@ public class ApiDeviceController {
|
||||
@Autowired
|
||||
private RequestUtils requestUtils;
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate redisTemplate;
|
||||
|
||||
private ControlMessageConstant controlMessageConstant = new ControlMessageConstant();
|
||||
|
||||
@Autowired
|
||||
@@ -86,23 +91,36 @@ public class ApiDeviceController {
|
||||
return Result.failed("参数错误");
|
||||
}
|
||||
|
||||
// 2. 检查订单状态
|
||||
// 2. 幂等性检查
|
||||
String idempotencyKey = generateIdempotencyKey(deviceId, orderNo, control);
|
||||
log.info("生成幂等性key: deviceId={}, orderNo={}, control={}, key={}", deviceId, orderNo, control, idempotencyKey);
|
||||
|
||||
if (isRequestProcessed(idempotencyKey)) {
|
||||
log.info("检测到重复请求,返回幂等性响应: deviceId={}, orderNo={}, control={}", deviceId, orderNo, control);
|
||||
return Result.succeed("请求已处理,请勿重复操作");
|
||||
}
|
||||
|
||||
// 3. 立即标记请求已处理(防止并发请求)
|
||||
markRequestAsProcessed(idempotencyKey);
|
||||
log.info("幂等性检查通过,立即标记请求已处理: deviceId={}, orderNo={}, control={}", deviceId, orderNo, control);
|
||||
|
||||
// 4. 检查订单状态
|
||||
String orderStatus = orderCenterService.getOrderStatusByOrderNo(orderNo);
|
||||
if (!isValidOrderStatus(orderStatus)) {
|
||||
return Result.failed("设备已处理,请关注订单详情");
|
||||
}
|
||||
|
||||
// 3. 构建控制消息
|
||||
// 5. 构建控制消息
|
||||
JsonObject controlMessage = buildControlMessage(control, orderNo, deviceId);
|
||||
if (controlMessage == null) {
|
||||
return Result.failed("暂未开放控制权限");
|
||||
}
|
||||
|
||||
// 4. 发送设备控制请求
|
||||
// 6. 发送设备控制请求
|
||||
String url = baseUrl + "/device/instance/" + deviceId + "/property";
|
||||
Map<String, String> result = requestUtils.getPostRequestResultMap(url, controlMessage.toString());
|
||||
|
||||
// 5. 处理响应结果
|
||||
// 7. 处理响应结果
|
||||
return handleDeviceResponse(result, orderNo);
|
||||
|
||||
} catch (Exception e) {
|
||||
@@ -305,4 +323,47 @@ public class ApiDeviceController {
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成幂等性key
|
||||
*/
|
||||
private String generateIdempotencyKey(String deviceId, String orderNo, String control) {
|
||||
return String.format("device:writeProperties:%s:%s:%s", deviceId, orderNo, control);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查请求是否已处理
|
||||
*/
|
||||
private boolean isRequestProcessed(String idempotencyKey) {
|
||||
try {
|
||||
Boolean hasKey = redisTemplate.hasKey(idempotencyKey);
|
||||
log.info("检查幂等性key: key={}, hasKey={}", idempotencyKey, hasKey);
|
||||
|
||||
if (hasKey != null && hasKey) {
|
||||
// 获取key的值进行额外验证
|
||||
String value = redisTemplate.opsForValue().get(idempotencyKey);
|
||||
log.info("幂等性key已存在: key={}, value={}", idempotencyKey, value);
|
||||
return true;
|
||||
}
|
||||
|
||||
log.info("幂等性key不存在: key={}", idempotencyKey);
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
log.error("检查幂等性key异常: key={}, error={}", idempotencyKey, e.getMessage(), e);
|
||||
return false; // Redis异常时不阻止请求
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 标记请求已处理
|
||||
*/
|
||||
private void markRequestAsProcessed(String idempotencyKey) {
|
||||
try {
|
||||
// 设置5分钟过期时间,防止Redis内存占用过多
|
||||
redisTemplate.opsForValue().set(idempotencyKey, "processed", 5, TimeUnit.MINUTES);
|
||||
log.info("标记请求已处理: key={}", idempotencyKey);
|
||||
} catch (Exception e) {
|
||||
log.error("标记幂等性key异常: key={}, error={}", idempotencyKey, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user