<p>@PostMapping(&quot;/deleteOneRegularByID&quot;)
public Result<Object> deleteOneRegularByID(@RequestBody Map&lt;String, Object&gt; params, HttpServletRequest request) {</p>
<pre><code>    //权限验证
    String token = (String) request.getAttribute(&quot;claims_admin&quot;);
    if (token == null || &quot;&quot;.equals(token)) {
        throw new RuntimeException(&quot;权限不足!&quot;);
    }

    log.info(&quot;deleteOneRegularByID请求报文:&quot; + params);

    //前端传递参数
    Long regularId;
    if (params.get(&quot;regularId&quot;) != null) {
        regularId = Long.valueOf(params.get(&quot;regularId&quot;).toString());
    } else {
        log.error(&quot;regularId为null&quot;);
        return new Result&lt;&gt;(ResultCode.FAIL);
    }

    //前端传递参数
    String phone = (String) params.get(&quot;phone&quot;);
    if (StringUtils.isEmpty(phone)) {
        log.error(&quot;phone为空!&quot;);
        return new Result&lt;&gt;(ResultCode.FAIL);
    }
    String userCode = (String) params.get(&quot;code&quot;);
    if (StringUtils.isEmpty(userCode)) {
        log.error(&quot;userCode为空!&quot;);
        return new Result&lt;&gt;(ResultCode.FAIL);
    }

    String regularDelete_uuid = redisTemplate.opsForValue().get(&quot;regularDelete_uuid&quot; + phone);
    String myCode = redisTemplate.opsForValue().get(&quot;regularDelete_&quot; + regularDelete_uuid + phone);
    log.info(&quot;myCode:&quot; + myCode);

    if (StringUtils.isEmpty(myCode)) {
        log.error(&quot;myCode为空!&quot;);
        return new Result&lt;&gt;(ResultCode.FAIL);
    }

    if (!userCode.equals(myCode)) {
        log.error(&quot;验证码错误!&quot;);
        return new Result&lt;&gt;(ResultCode.CODE_FALSE);
    }
    
    Regular regular = regularService.queryOneRegularByID(regularId);
    List&lt;DeviceEntity&gt; deviceEntities = deviceEntityService.queryDevicesByRegularId(regular.getRegularId());
    if (!deviceEntities.isEmpty()) {
        log.error(&quot;此规则已绑定设备,不能删除!&quot;);
        return new Result&lt;&gt;(ResultCode.REGULAR_DELETE);
    }

    //调用方法
    int ret = regularService.deleteOneRegularByID(regularId);
    //操作成功
    if (ret &lt; 0) {
        log.error(&quot;删除规则失败!&quot;);
        return new Result&lt;&gt;(ResultCode.FAIL);
    }
    log.info(&quot;删除规则成功!&quot;);
    //同时删除其对应的具体规则
    List&lt;SpecificRegular&gt; specificRegulars = specificRegularService.querySpecificRegularsByRegularId(regularId);

    if (!CollectionUtils.isEmpty(specificRegulars)) {
        List&lt;SpecificRegular&gt; specificRegularList = specificRegulars.stream()
                .map(specificRegular -&gt; {
                    specificRegular.setEnableState(0);
                    return specificRegular;
                })
                .collect(Collectors.toList());
        boolean batch = specificRegularService.updateBatchById(specificRegularList);
        if (!batch) {
            log.error(&quot;具体规则删除失败!&quot;);
            return new Result&lt;&gt;(ResultCode.FAIL);
        }
        log.info(&quot;具体规则删除成功!&quot;);
        springToGameService.reloadDeleteRegularWithSpecificRegular(regular, specificRegularList);
    }
    return new Result&lt;&gt;(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&lt;SpecificRegular&gt; specificRegularList) {
    // Perform the deletion logic here
    // ...
}

@PostMapping(&quot;/deleteOneRegularByID&quot;)
public Result&lt;Object&gt; deleteOneRegularByID(@RequestBody Map&lt;String, Object&gt; params, HttpServletRequest request) {
    // ...

    if (!CollectionUtils.isEmpty(specificRegulars)) {
        List&lt;SpecificRegular&gt; specificRegularList = specificRegulars.stream()
                .map(specificRegular -&gt; {
                    specificRegular.setEnableState(0);
                    return specificRegular;
                })
                .collect(Collectors.toList());
        boolean batch = specificRegularService.updateBatchById(specificRegularList);
        if (!batch) {
            log.error(&quot;具体规则删除失败!&quot;);
            return new Result&lt;&gt;(ResultCode.FAIL);
        }
        log.info(&quot;具体规则删除成功!&quot;);

        // 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>
Delete One Regular by ID API

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

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