@PostMapping("/deleteOneRegularByID")
public Result<Object> deleteOneRegularByID(@RequestBody Map<String, Object> params, HttpServletRequest request) {

    //权限验证
    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);
}  

To delete the corresponding specific rules asynchronously, you can use the @Async annotation in Spring.

Here's how you can modify the code:

  1. Add the @Async annotation to the method deleteOneRegularByID:
@Async
@PostMapping("/deleteOneRegularByID")
public CompletableFuture<Result<Object>> deleteOneRegularByID(@RequestBody Map<String, Object> params, HttpServletRequest request) {
    // Existing code...
    return CompletableFuture.completedFuture(new Result<>(ResultCode.SUCCESS));
}
  1. Create a separate method to delete the specific rules and annotate it with @Async:
@Async
public CompletableFuture<Result<Object>> deleteSpecificRules(Long regularId) {
    List<SpecificRegular> specificRegulars = specificRegularService.querySpecificRegularsByRegularId(regularId);
    // Existing code...
    return CompletableFuture.completedFuture(new Result<>(ResultCode.SUCCESS));
}
  1. Call the deleteSpecificRules method asynchronously after deleting the regular rule:
// Existing code...
    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);
        // Call the deleteSpecificRules method asynchronously
        deleteSpecificRules(regularId);
    }
    return new Result<>(ResultCode.SUCCESS);

By annotating the deleteSpecificRules method with @Async, it will execute asynchronously in a separate thread. The CompletableFuture.completedFuture method is used to return a completed CompletableFuture object with the desired result.

删除规则接口 - @PostMapping(

原文地址: https://www.cveoy.top/t/topic/gYaz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录