Delete One Regular by ID API
<p>@PostMapping("/deleteOneRegularByID")
public Result<Object> deleteOneRegularByID(@RequestBody Map<String, Object> params, HttpServletRequest request) {</p>
<pre><code> //权限验证
String token = (String) request.getAttribute("claims_admin");
if (token == null || "".equals(token)) {
throw new RuntimeException("权限不足!");
}
log.info("deleteOneRegularByID请求报文:" + params);
//前端传递参数
Long regularId;
if (params.get("regularId") != null) {
regularId = Long.valueOf(params.get("regularId").toString());
} else {
log.error("regularId为null");
return new Result<>(ResultCode.FAIL);
}
//前端传递参数
String phone = (String) params.get("phone");
if (StringUtils.isEmpty(phone)) {
log.error("phone为空!");
return new Result<>(ResultCode.FAIL);
}
String userCode = (String) params.get("code");
if (StringUtils.isEmpty(userCode)) {
log.error("userCode为空!");
return new Result<>(ResultCode.FAIL);
}
String regularDelete_uuid = redisTemplate.opsForValue().get("regularDelete_uuid" + phone);
String myCode = redisTemplate.opsForValue().get("regularDelete_" + regularDelete_uuid + phone);
log.info("myCode:" + myCode);
if (StringUtils.isEmpty(myCode)) {
log.error("myCode为空!");
return new Result<>(ResultCode.FAIL);
}
if (!userCode.equals(myCode)) {
log.error("验证码错误!");
return new Result<>(ResultCode.CODE_FALSE);
}
Regular regular = regularService.queryOneRegularByID(regularId);
List<DeviceEntity> deviceEntities = deviceEntityService.queryDevicesByRegularId(regular.getRegularId());
if (!deviceEntities.isEmpty()) {
log.error("此规则已绑定设备,不能删除!");
return new Result<>(ResultCode.REGULAR_DELETE);
}
//调用方法
int ret = regularService.deleteOneRegularByID(regularId);
//操作成功
if (ret < 0) {
log.error("删除规则失败!");
return new Result<>(ResultCode.FAIL);
}
log.info("删除规则成功!");
//同时删除其对应的具体规则
List<SpecificRegular> specificRegulars = specificRegularService.querySpecificRegularsByRegularId(regularId);
if (!CollectionUtils.isEmpty(specificRegulars)) {
List<SpecificRegular> specificRegularList = specificRegulars.stream()
.map(specificRegular -> {
specificRegular.setEnableState(0);
return specificRegular;
})
.collect(Collectors.toList());
boolean batch = specificRegularService.updateBatchById(specificRegularList);
if (!batch) {
log.error("具体规则删除失败!");
return new Result<>(ResultCode.FAIL);
}
log.info("具体规则删除成功!");
springToGameService.reloadDeleteRegularWithSpecificRegular(regular, specificRegularList);
}
return new Result<>(ResultCode.SUCCESS);
}
</code></pre>
<p>异步执行 删除其对应的具体规则,不使用注解内容:To execute the deletion of specific rules asynchronously without using annotations, you can use the ' @Async ' annotation on a separate method that performs the deletion logic. Here's an example:</p>
<pre><code class="language-java">// Add the @Async annotation to enable asynchronous execution
@Async
public void deleteSpecificRules(Regular regular, List<SpecificRegular> specificRegularList) {
// Perform the deletion logic here
// ...
}
@PostMapping("/deleteOneRegularByID")
public Result<Object> deleteOneRegularByID(@RequestBody Map<String, Object> params, HttpServletRequest request) {
// ...
if (!CollectionUtils.isEmpty(specificRegulars)) {
List<SpecificRegular> specificRegularList = specificRegulars.stream()
.map(specificRegular -> {
specificRegular.setEnableState(0);
return specificRegular;
})
.collect(Collectors.toList());
boolean batch = specificRegularService.updateBatchById(specificRegularList);
if (!batch) {
log.error("具体规则删除失败!");
return new Result<>(ResultCode.FAIL);
}
log.info("具体规则删除成功!");
// Call the asynchronous method to delete specific rules
deleteSpecificRules(regular, specificRegularList);
}
// ...
}
</code></pre>
<p>Make sure to enable asynchronous execution in your application by adding the ' @EnableAsync ' annotation to a configuration class.</p>
原文地址: https://www.cveoy.top/t/topic/gX4E 著作权归作者所有。请勿转载和采集!