可以对代码进行优化,同时变量命名也可以进行改进。

优化后的代码如下所示:

@Override
@Transactional
public void deleteProcessDefinition(String processDefinitionId) {
    ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
            .processDefinitionId(processDefinitionId)
            .suspended()
            .singleResult();
    if (processDefinition == null) {
        throw new ServiceException(400, "删除失败");
    }
    boolean isDeleted = UpdateChain.of(ProcessTemplate.class)
            .set(ProcessTemplate::getIsDeleted, 1)
            .where(ProcessTemplate::getProcessDefinitionId).eq(processDefinitionId)
            .update();
    if (!isDeleted) {
        throw new ServiceException(400, "删除失败");
    }
    repositoryService.deleteProcessDefinition(processDefinitionId);
}

优化说明:

  1. 将Optional改为ProcessDefinition类型,避免使用Optional的isPresent()方法。
  2. 将变量名isSuccess改为isDeleted,更准确地表示该变量的含义。
  3. 移除对Optional的使用,直接判断ProcessDefinition是否为null。
  4. 删除ProcessDefinition时,直接使用processDefinitionId参数,而不是再次查询获取ProcessDefinition对象。

注释说明:

  1. 在删除ProcessDefinition之前,先通过processDefinitionId查询对应的ProcessDefinition对象。
  2. 如果ProcessDefinition对象为空,则抛出ServiceException异常,表示删除失败。
  3. 使用UpdateChain.of()方法更新ProcessTemplate表的isDeleted字段,将其设置为1,表示已删除。
  4. 更新成功后,再调用repositoryService.deleteProcessDefinition()方法删除ProcessDefinition
Override Transactional public void deleteProcessDefinitionString processDefinitionId OptionalProcessDefinition processDefinition = OptionalofNullablerepositoryServicecreateProcessDefinit

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

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