1.增加主链路编码,实现多个主链路

This commit is contained in:
chenweilong 2022-12-19 11:54:58 +08:00
parent 3a3540079b
commit 1e01f892a3
7 changed files with 148 additions and 91 deletions

View File

@ -23,7 +23,11 @@ public class BaseChainEntity {
*/ */
@TableField("template_id") @TableField("template_id")
private String templateId; private String templateId;
/**
* 主链路编码
*/
@TableField("main_chain_code")
private String mainChainCode;
/** /**
* 链路名称 * 链路名称
*/ */

View File

@ -27,7 +27,11 @@ public class BaseNodeEntity {
*/ */
@TableField("template_id") @TableField("template_id")
private String templateId; private String templateId;
/**
* 主链路编码
*/
@TableField("main_chain_code")
private String mainChainCode;
/** /**
* 节点名称 * 节点名称
*/ */

View File

@ -1,5 +1,6 @@
package com.example.liteflow.mysql.model.baseChain; package com.example.liteflow.mysql.model.baseChain;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data; import lombok.Data;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@ -13,7 +14,10 @@ public class BaseChainVO {
private String id; private String id;
private String templateId; private String templateId;
/**
* 主链路编码
*/
private String mainChainCode;
private String templateName; private String templateName;
private String chainName; private String chainName;

View File

@ -22,6 +22,10 @@ public class BaseNodeVO {
* 所属模板id * 所属模板id
*/ */
private String templateId; private String templateId;
/**
* 主链路编码
*/
private String mainChainCode;
/** /**
* 所属模板名称 * 所属模板名称
*/ */

View File

@ -20,7 +20,10 @@ public class CreateNodeFrom {
* 所属模板id * 所属模板id
*/ */
private String templateId; private String templateId;
/**
* 主链路编码
*/
private String mainChainCode;
/** /**
* 节点名称 * 节点名称
*/ */

View File

@ -17,6 +17,10 @@ public class UpdateNodeFrom {
* 节点对应输出节点id * 节点对应输出节点id
*/ */
private String nodeContentId; private String nodeContentId;
/**
* 主链路编码
*/
private String mainChainCode;
/** /**
* 规则 * 规则
*/ */

View File

@ -27,6 +27,7 @@ import com.yomahub.liteflow.flow.FlowBus;
import com.yomahub.liteflow.flow.LiteflowResponse; import com.yomahub.liteflow.flow.LiteflowResponse;
import com.yomahub.liteflow.flow.element.Chain; import com.yomahub.liteflow.flow.element.Chain;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -89,8 +90,22 @@ public class BaseChainServiceImpl extends ServiceImpl<BaseChainMapper, BaseChain
String templateName = templateEntity.getTemplateName(); String templateName = templateEntity.getTemplateName();
String templateId = templateEntity.getId(); String templateId = templateEntity.getId();
List<BaseNodeEntity> nodeList = templateEntity.getBaseNodeEntityList(); List<BaseNodeEntity> nodeList = templateEntity.getBaseNodeEntityList();
// 将所有节点注入 Map<String, List<BaseNodeEntity>> nodeMap = new HashMap<>();
nodeList.forEach(node -> { nodeList.forEach(node -> {
String mainChainCode = node.getMainChainCode();
if (nodeMap.containsKey(mainChainCode)) {
List<BaseNodeEntity> nodeMapList = nodeMap.get(mainChainCode);
nodeMapList.add(node);
} else {
ArrayList<BaseNodeEntity> nodeMapList = Lists.newArrayList(node);
nodeMap.put(mainChainCode, nodeMapList);
}
});
for (Map.Entry<String, List<BaseNodeEntity>> entry : nodeMap.entrySet()) {
String mainChainCode = entry.getKey();
List<BaseNodeEntity> mapNodeList = entry.getValue();
// 将所有节点注入
mapNodeList.forEach(node -> {
String nodeType = node.getNodeType(); String nodeType = node.getNodeType();
String nodeId = node.getNodeId(); String nodeId = node.getNodeId();
String nodeName = node.getNodeName(); String nodeName = node.getNodeName();
@ -110,11 +125,11 @@ public class BaseChainServiceImpl extends ServiceImpl<BaseChainMapper, BaseChain
} }
}); });
// 输出节点 // 输出节点
Map<String, BaseNodeEntity> commonNodeMap = nodeList.stream() Map<String, BaseNodeEntity> commonNodeMap = mapNodeList.stream()
.filter(node -> NodeEnum.CommonNode.getName().equals(node.getNodeType())) .filter(node -> NodeEnum.CommonNode.getName().equals(node.getNodeType()))
.collect(Collectors.toMap(BaseNodeEntity::getId, BaseNodeEntity -> BaseNodeEntity)); .collect(Collectors.toMap(BaseNodeEntity::getId, BaseNodeEntity -> BaseNodeEntity));
// 判断节点 // 判断节点
List<BaseNodeEntity> ifNodeList = nodeList.stream().filter(node -> NodeEnum.IfNode.getName().equals(node.getNodeType())).collect(Collectors.toList()); List<BaseNodeEntity> ifNodeList = mapNodeList.stream().filter(node -> NodeEnum.IfNode.getName().equals(node.getNodeType())).collect(Collectors.toList());
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
int leftBracesNum = 0; int leftBracesNum = 0;
int num = 0; int num = 0;
@ -140,29 +155,33 @@ public class BaseChainServiceImpl extends ServiceImpl<BaseChainMapper, BaseChain
for (int i = 0; i < leftBracesNum; i++) { for (int i = 0; i < leftBracesNum; i++) {
stringBuilder.append(")"); stringBuilder.append(")");
} }
String chainName = mainChainCode + "_" + templateName;
log.info("templateName = {}, stringBuilder = {}", templateName, stringBuilder); log.info("templateName = {}, stringBuilder = {}", templateName, stringBuilder);
BaseChainEntity baseChainEntity = new BaseChainEntity(); BaseChainEntity baseChainEntity = new BaseChainEntity();
baseChainEntity.setTemplateId(templateId); baseChainEntity.setTemplateId(templateId);
baseChainEntity.setChainName(templateName); baseChainEntity.setMainChainCode(mainChainCode);
baseChainEntity.setChainName(chainName);
baseChainEntity.setElData(stringBuilder.toString()); baseChainEntity.setElData(stringBuilder.toString());
if (!FlowBus.containChain(templateName)) { if (!FlowBus.containChain(templateName)) {
LiteFlowChainELBuilder.createChain() LiteFlowChainELBuilder.createChain()
.setChainId(templateName) .setChainId(chainName)
.setEL(stringBuilder.toString()) .setEL(stringBuilder.toString())
.build(); .build();
flowExecutor.reloadRule(); flowExecutor.reloadRule();
} }
LambdaUpdateWrapper<BaseChainEntity> baseChainQueryWrapper = new LambdaUpdateWrapper<>(); LambdaUpdateWrapper<BaseChainEntity> baseChainQueryWrapper = new LambdaUpdateWrapper<>();
baseChainQueryWrapper.eq(BaseChainEntity::getTemplateId, templateId) baseChainQueryWrapper.eq(BaseChainEntity::getTemplateId, templateId)
.eq(BaseChainEntity::getChainName, chainName)
.eq(BaseChainEntity::getMainChainCode, mainChainCode)
.eq(BaseChainEntity::getEnableMark, EnableMarkEnum.Enable.getValue()); .eq(BaseChainEntity::getEnableMark, EnableMarkEnum.Enable.getValue());
BaseChainEntity checkExists = this.getOne(baseChainQueryWrapper); BaseChainEntity checkExists = this.getOne(baseChainQueryWrapper);
if (ObjectUtil.isNull(checkExists)) { if (ObjectUtil.isNull(checkExists)) {
this.save(baseChainEntity); this.save(baseChainEntity);
} else { } else {
checkExists.setChainName(templateName);
checkExists.setElData(stringBuilder.toString()); checkExists.setElData(stringBuilder.toString());
this.updateById(checkExists); this.updateById(checkExists);
} }
}
}); });
} }
@ -199,28 +218,42 @@ public class BaseChainServiceImpl extends ServiceImpl<BaseChainMapper, BaseChain
@Override @Override
public void initMainChain() { public void initMainChain() {
log.info("进入【初始化主链路】方法"); log.info("进入【初始化主链路】方法");
String mainChainName = "mainChain";
LambdaQueryWrapper<BaseChainEntity> queryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<BaseChainEntity> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(BaseChainEntity::getEnableMark, EnableMarkEnum.Enable.getValue()) queryWrapper.eq(BaseChainEntity::getEnableMark, EnableMarkEnum.Enable.getValue());
.ne(BaseChainEntity::getChainName, "mainChain");
List<BaseChainEntity> chainList = this.list(queryWrapper); List<BaseChainEntity> chainList = this.list(queryWrapper);
log.info("chainList size = {}", chainList.size()); log.info("chainList size = {}", chainList.size());
if (CollUtil.isNotEmpty(chainList)) { if (CollUtil.isNotEmpty(chainList)) {
Map<String, List<BaseChainEntity>> nodeMap = new HashMap<>();
// 根据链路编码进行分类
chainList.forEach(chain -> {
String mainChainCode = chain.getMainChainCode();
if (nodeMap.containsKey(mainChainCode)) {
List<BaseChainEntity> nodeMapList = nodeMap.get(mainChainCode);
nodeMapList.add(chain);
} else {
ArrayList<BaseChainEntity> nodeMapList = Lists.newArrayList(chain);
nodeMap.put(mainChainCode, nodeMapList);
}
});
// 生成链路
for (Map.Entry<String, List<BaseChainEntity>> entry : nodeMap.entrySet()) {
String mainChainCode = entry.getKey();
String chainName = mainChainCode + "_mainChain";
List<BaseChainEntity> nodeMapList = entry.getValue();
StringJoiner elData = new StringJoiner(",", "THEN(", ")"); StringJoiner elData = new StringJoiner(",", "THEN(", ")");
chainList.forEach(chain -> elData.add(chain.getChainName())); nodeMapList.forEach(chain -> elData.add(chain.getChainName()));
if (!FlowBus.containChain(mainChainName)) { log.info("elData = {}", elData);
LiteFlowChainELBuilder.createChain() LiteFlowChainELBuilder.createChain()
.setChainId(mainChainName) .setChainId(chainName)
.setEL(elData.toString()) .setEL(elData.toString())
.build(); .build();
flowExecutor.reloadRule(); flowExecutor.reloadRule();
}
LambdaQueryWrapper<BaseChainEntity> mainQueryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<BaseChainEntity> mainQueryWrapper = new LambdaQueryWrapper<>();
mainQueryWrapper.eq(BaseChainEntity::getChainName, mainChainName); mainQueryWrapper.eq(BaseChainEntity::getChainName, chainName);
BaseChainEntity mainChain = this.getOne(mainQueryWrapper); BaseChainEntity mainChain = this.getOne(mainQueryWrapper);
if (ObjectUtil.isNull(mainChain)) { if (ObjectUtil.isNull(mainChain)) {
mainChain = new BaseChainEntity(); mainChain = new BaseChainEntity();
mainChain.setChainName(mainChainName); mainChain.setChainName(chainName);
mainChain.setElData(elData.toString()); mainChain.setElData(elData.toString());
this.save(mainChain); this.save(mainChain);
} else { } else {
@ -229,6 +262,7 @@ public class BaseChainServiceImpl extends ServiceImpl<BaseChainMapper, BaseChain
} }
} }
} }
}
/** /**
* 执行链路 * 执行链路