添加流程修改相关代码

release_1.0.0
xueqingkun 8 months ago
parent 19b1e27d19
commit 55c1cf864b

@ -1,12 +1,36 @@
package com.supervision.knowsub.service;
import com.supervision.knowsub.dto.flow.SystemFlowRuleDTO;
import com.supervision.knowsub.vo.flow.NodeInfo;
import java.util.List;
public interface FlowRuleManageService {
/**
*
* @param flowId id
* @param nodeInfoList
* @return
*/
boolean saveFlowRule(String flowId, List<NodeInfo> nodeInfoList);
void saveFlowRule(List<SystemFlowRuleDTO> systemFlowRuleDTOList);
void updateFlowRule(List<SystemFlowRuleDTO> systemFlowRuleDTOList);
/**
*
* @param flowId id
* @param nodeInfoList
* @return
*/
boolean updateFlowRule(String flowId, List<NodeInfo> nodeInfoList);
List<NodeInfo> listFlowRule(String flowId);
void deleteFlowRuleByRuleIds(List<String> ruleIdList);
}

@ -3,6 +3,7 @@ package com.supervision.knowsub.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.supervision.knowsub.dto.flow.SystemFlowRuleDTO;
import com.supervision.knowsub.model.SystemFlowRule;
import com.supervision.knowsub.model.SystemFlowRuleUser;
import com.supervision.knowsub.service.FlowRuleManageService;
@ -12,11 +13,9 @@ import com.supervision.knowsub.vo.flow.NodeInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.sql.Struct;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
@Service
@ -30,6 +29,7 @@ public class FlowRuleManageServiceImpl implements FlowRuleManageService {
@Override
@Transactional(rollbackFor = Exception.class)
public boolean saveFlowRule(String flowId, List<NodeInfo> nodeInfoList) {
Assert.notEmpty(flowId, "流程id不能为空");
Assert.notEmpty(nodeInfoList, "节点信息不能为空");
@ -55,6 +55,99 @@ public class FlowRuleManageServiceImpl implements FlowRuleManageService {
return true;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void saveFlowRule(List<SystemFlowRuleDTO> systemFlowRuleDTOList) {
if (CollUtil.isEmpty(systemFlowRuleDTOList)){
return;
}
for (SystemFlowRuleDTO systemFlowRuleDTO : systemFlowRuleDTOList) {
SystemFlowRule systemFlowRule = systemFlowRuleDTO.toSystemFlowRule();
systemFlowRuleService.save(systemFlowRule);
List<SystemFlowRuleUser> systemFlowRuleUserList = systemFlowRuleDTO.toSystemFlowRuleUserList();
if (CollUtil.isEmpty(systemFlowRuleUserList)){
for (SystemFlowRuleUser systemFlowRuleUser : systemFlowRuleUserList) {
systemFlowRuleUser.setFlowId(systemFlowRule.getId());
systemFlowRuleUserService.save(systemFlowRuleUser);
}
}
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateFlowRule(List<SystemFlowRuleDTO> systemFlowRuleDTOList) {
if (CollUtil.isEmpty(systemFlowRuleDTOList)){
return;
}
systemFlowRuleDTOList.forEach(systemFlowRuleDTO -> Assert.notEmpty(systemFlowRuleDTO.getId(), "流程id不能为空"));
for (SystemFlowRuleDTO systemFlowRuleDTO : systemFlowRuleDTOList) {
SystemFlowRule systemFlowRule = systemFlowRuleDTO.toSystemFlowRule();
systemFlowRuleService.updateById(systemFlowRule);
systemFlowRuleUserService.lambdaUpdate().eq(SystemFlowRuleUser::getFlowId, systemFlowRuleDTO.getId()).remove();
List<SystemFlowRuleUser> systemFlowRuleUserList = systemFlowRuleDTO.toSystemFlowRuleUserList();
if (CollUtil.isEmpty(systemFlowRuleUserList)){
for (SystemFlowRuleUser systemFlowRuleUser : systemFlowRuleUserList) {
systemFlowRuleUser.setFlowId(systemFlowRule.getId());
systemFlowRuleUserService.save(systemFlowRuleUser);
}
}
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean updateFlowRule(String flowId, List<NodeInfo> nodeInfoList) {
Assert.notEmpty(flowId, "流程id不能为空");
List<String> nodeIds = nodeInfoList.stream().map(NodeInfo::getId).filter(StrUtil::isNotEmpty).toList();
List<SystemFlowRule> flowRuleList = systemFlowRuleService.lambdaQuery().eq(SystemFlowRule::getFlowId, flowId).list();
List<String> flowRuleIdList = flowRuleList.stream().map(SystemFlowRule::getId).toList();
Set<String> flowRuleIdSet = flowRuleList.stream().map(SystemFlowRule::getId).collect(Collectors.toSet());
FlowRuleDTOPair flowRuleDTOPair = pickInsertAndUpdateFlowRuleDTO(flowId, nodeInfoList, flowRuleIdSet);
// 新增数据
if (CollUtil.isNotEmpty(flowRuleDTOPair.needAddRuleList())){
saveFlowRule(flowRuleDTOPair.needAddRuleList());
}
// 更新数据
if (CollUtil.isEmpty(flowRuleDTOPair.needUpdateRuleList())){
updateFlowRule(flowRuleDTOPair.needUpdateRuleList());
}
//如果数据库中存在,列表中不存在,则删除
List<String> needDeleteRuleIdList = flowRuleIdList.stream().filter(ruleId -> !nodeIds.contains(ruleId)).toList();
if (CollUtil.isNotEmpty(needDeleteRuleIdList)){
deleteFlowRuleByRuleIds(needDeleteRuleIdList);
}
return false;
}
private static FlowRuleDTOPair pickInsertAndUpdateFlowRuleDTO(String flowId, List<NodeInfo> nodeInfoList, Set<String> flowRuleIdSet) {
List<SystemFlowRuleDTO> needUpdateRuleList = new ArrayList<>();
List<SystemFlowRuleDTO> needAddRuleList = new ArrayList<>();
int order = 0;
for (NodeInfo nodeInfo : nodeInfoList) {
nodeInfo.setOrder(order++);
if (StrUtil.isNotEmpty(nodeInfo.getId()) && flowRuleIdSet.contains(nodeInfo.getId())){
// 需要更新的数据
needUpdateRuleList.add(SystemFlowRuleDTO.builderWithNodeInfo(flowId, nodeInfo));
}else {
// id为空或者不包含在数据库中需要增加的数据
needAddRuleList.add(SystemFlowRuleDTO.builderWithNodeInfo(flowId, nodeInfo));
}
}
return new FlowRuleDTOPair(needUpdateRuleList, needAddRuleList);
}
private record FlowRuleDTOPair(List<SystemFlowRuleDTO> needUpdateRuleList, List<SystemFlowRuleDTO> needAddRuleList) {
}
@Override
public List<NodeInfo> listFlowRule(String flowId) {
if (StrUtil.isEmpty(flowId)){
@ -79,4 +172,15 @@ public class FlowRuleManageServiceImpl implements FlowRuleManageService {
});
return nodeInfoList;
}
@Override
public void deleteFlowRuleByRuleIds(List<String> ruleIdList) {
if (CollUtil.isEmpty(ruleIdList)){
log.info("流程id为空不执行删除操作");
return;
}
systemFlowRuleUserService.removeBatchByIds(ruleIdList);
systemFlowRuleUserService.lambdaUpdate().in(SystemFlowRuleUser::getRuleId, ruleIdList).remove();
}
}

@ -0,0 +1,54 @@
package com.supervision.knowsub.dto.flow;
import com.supervision.knowsub.model.SystemFlowRule;
import com.supervision.knowsub.model.SystemFlowRuleUser;
import com.supervision.knowsub.vo.flow.NodeInfo;
import lombok.Data;
import java.util.List;
@Data
public class SystemFlowRuleDTO {
private String id;
private String flowId;
private String ruleName;
private Integer ruleOrder;
private List<SystemFlowRuleUser> userList;
public SystemFlowRuleDTO() {
}
public SystemFlowRule toSystemFlowRule() {
SystemFlowRule systemFlowRule = new SystemFlowRule();
systemFlowRule.setId(id);
systemFlowRule.setFlowId(flowId);
systemFlowRule.setRuleName(ruleName);
systemFlowRule.setRuleOrder(ruleOrder);
return systemFlowRule;
}
public List<SystemFlowRuleUser> toSystemFlowRuleUserList() {
return userList;
}
public static SystemFlowRuleDTO builderWithNodeInfo(String flowId, NodeInfo nodeInfo) {
SystemFlowRuleDTO systemFlowRuleDTO = new SystemFlowRuleDTO();
systemFlowRuleDTO.setId(nodeInfo.getId());
systemFlowRuleDTO.setRuleName(nodeInfo.getNodeName());
List<SystemFlowRuleUser> ruleUserList = nodeInfo.getUserIdList().stream().map(userId -> {
SystemFlowRuleUser flowRuleUser = new SystemFlowRuleUser();
flowRuleUser.setFlowId(flowId);
flowRuleUser.setRuleId(nodeInfo.getId());
flowRuleUser.setUserId(userId);
return flowRuleUser;
}).toList();
systemFlowRuleDTO.setUserList(ruleUserList);
return systemFlowRuleDTO;
}
}
Loading…
Cancel
Save