package cn.git.rules.service.drag.impl;
import cn.git.common.drag.CommonDragRuleConstant;
import cn.git.common.drag.dto.DragChainDTO;
import cn.git.common.drag.dto.DragRuleDTO;
import cn.git.common.exception.ServiceException;
import cn.git.common.page.CustomPageUtil;
import cn.git.common.page.PageBean;
import cn.git.common.page.PaginationContext;
import cn.git.common.sequence.SequenceConstant;
import cn.git.common.util.CommonDragRuleUtil;
import cn.git.common.util.LogUtil;
import cn.git.redis.RedisUtil;
import cn.git.rules.constant.RuleConstant;
import cn.git.rules.drag.DragRuleServerUtil;
import cn.git.rules.drag.enums.DragRuleSequenceEnum;
import cn.git.rules.dto.drag.DragRuleChainAllParamDTO;
import cn.git.rules.entity.TbSysDragChain;
import cn.git.rules.entity.TbSysDragRule;
import cn.git.rules.entity.TbSysProduct;
import cn.git.rules.entity.TbSysProductDragChain;
import cn.git.rules.mapper.TbSysDragChainMapper;
import cn.git.rules.mapper.TbSysDragRuleMapper;
import cn.git.rules.mapper.TbSysProductDragChainMapper;
import cn.git.rules.mapper.TbSysProductMapper;
import cn.git.rules.mapstruct.DragRuleConvert;
import cn.git.rules.service.drag.DragRuleService;
import cn.git.rules.vo.drag.DragRuleChainAllParamDescOutVO;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.model.bpmn.Bpmn;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.camunda.bpm.model.bpmn.instance.FlowElement;
import org.camunda.bpm.model.bpmn.instance.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
@Slf4j
@Service
public class DragRuleServiceImpl implements DragRuleService {
@Autowired
private CustomPageUtil customPageUtil;
@Autowired
private RedisUtil redisUtil;
@Autowired
private DragRuleServerUtil dragRuleServerUtil;
@Autowired
private CommonDragRuleUtil commonDragRuleUtil;
@Autowired
private DragRuleConvert dragRuleConvert;
@Autowired
private TbSysDragRuleMapper tbSysDragRuleMapper;
@Autowired
private TbSysDragChainMapper tbSysDragChainMapper;
@Autowired
private TbSysProductDragChainMapper productDragChainMapper;
@Autowired
private TbSysProductMapper tbSysProductMapper;
@Autowired
private HttpServletResponse response;
@Override
public void addDragRule(DragRuleDTO dragRuleDTO) {
if (ObjectUtil.isNotNull(tbSysDragRuleMapper.selectById(dragRuleDTO.getDragRuleId()))) {
throw new ServiceException(StrUtil.format("规则id[{}]已经存在,不允许重复添加!",
dragRuleDTO.getDragRuleId()));
}
Object dragRuleDTOObj = redisUtil.hget(RuleConstant.DRAG_RULES, dragRuleDTO.getDragRuleId()).toString();
if (ObjectUtil.isNull(dragRuleDTOObj)) {
throw new ServiceException(StrUtil.format("通过规则id[{}]获取拖拽规则缓存信息失败!",
dragRuleDTO.getDragRuleId()));
}
DragRuleDTO cacheDragRuleDTO = JSONObject.parseObject(dragRuleDTOObj.toString(), DragRuleDTO.class);
JSONObject outputParamJSON = JSON.parseObject(cacheDragRuleDTO.getOutputParam());
String dragOptionParamsStr = null;
if (ObjectUtil.isNotEmpty(dragRuleDTO.getCustomOptionParamList())) {
List<List<DragRuleDTO.CustomOptionParam>> customOptionParamsList = dragRuleDTO.getCustomOptionParamList();
for (List<DragRuleDTO.CustomOptionParam> paramList : customOptionParamsList) {
if (ObjectUtil.isEmpty(paramList)) {
continue;
}
dragRuleServerUtil.checkCustomOptionParam(paramList);
}
dragOptionParamsStr = JSONObject.toJSONString(dragRuleDTO.getCustomOptionParamList());
dragRuleDTO.setCustomOptionParam(dragOptionParamsStr);
}
String crossParamStr = null;
if (ObjectUtil.isNotEmpty(dragRuleDTO.getCrossParamDescList())) {
List<String> crossParamList = dragRuleDTO.getCrossParamDescList().stream().map(
DragRuleDTO.DragRuleParamsDesc::getParamName
).collect(Collectors.toList());
dragRuleDTO.setCrossParamList(crossParamList);
for (String crossParam : dragRuleDTO.getCrossParamList()) {
if (!outputParamJSON.containsKey(crossParam)) {
String errorMessage = StrUtil.format("拖拽规则传递参数必须为规则方法出参参数,[{}]不满足条件,请确认!",
crossParam);
throw new ServiceException(errorMessage);
}
}
crossParamStr = dragRuleDTO.getCrossParamList().stream().map(
String::toString
).collect(Collectors.joining(StrUtil.COMMA));
Map<String, DragRuleDTO.DragRuleParamsDesc> outParamDescMap = dragRuleDTO.getOutputParamDescList().stream()
.collect(
Collectors.toMap(DragRuleDTO.DragRuleParamsDesc::getParamName,
Function.identity())
);
List<DragRuleDTO.DragRuleParamsDesc> crossParamDescList = new ArrayList<>();
dragRuleDTO.getCrossParamList().forEach(paramName -> {
DragRuleDTO.DragRuleParamsDesc dragRuleParamsDesc = outParamDescMap.get(paramName);
if (ObjectUtil.isNotNull(dragRuleParamsDesc)) {
crossParamDescList.add(dragRuleParamsDesc);
}
});
dragRuleDTO.setCrossParamDescList(crossParamDescList);
}
TbSysDragRule tbSysDragRule = dragRuleConvert.dragRuleDTOToDragRule(dragRuleDTO);
tbSysDragRule.setCrossParam(crossParamStr);
tbSysDragRuleMapper.insert(tbSysDragRule);
cacheDragRuleDTO.setCrossParam(crossParamStr);
cacheDragRuleDTO.setCrossParamList(dragRuleDTO.getCrossParamList());
cacheDragRuleDTO.setIfLoadDB(RuleConstant.NUM_1_STR);
cacheDragRuleDTO.setCrossParamDescList(dragRuleDTO.getCrossParamDescList());
cacheDragRuleDTO.setCustomOptionParam(dragOptionParamsStr);
cacheDragRuleDTO.setCustomOptionParamList(dragRuleDTO.getCustomOptionParamList());
redisUtil.hset(RuleConstant.DRAG_RULES, dragRuleDTO.getDragRuleId(), JSONObject.toJSONString(cacheDragRuleDTO));
}
@Override
public DragRuleDTO findById(String dragRuleId) {
Object dragRuleDTOObj = redisUtil.hget(RuleConstant.DRAG_RULES, dragRuleId);
if (ObjectUtil.isNull(dragRuleDTOObj)) {
throw new ServiceException(StrUtil.format("通过规则id[{}]获取拖拽规则缓存信息失败!", dragRuleId));
}
DragRuleDTO dragRuleDTO = JSONObject.parseObject(dragRuleDTOObj.toString(), DragRuleDTO.class);
TbSysDragRule tbSysDragRule = tbSysDragRuleMapper.selectById(dragRuleId);
if (ObjectUtil.isNotNull(tbSysDragRule)) {
dragRuleDTO.setIfLoadDB(RuleConstant.NUM_1_STR);
if (StrUtil.isNotBlank(tbSysDragRule.getCrossParam())) {
dragRuleDTO.setCrossParam(tbSysDragRule.getCrossParam());
dragRuleDTO.setCrossParamList(Arrays.asList(dragRuleDTO.getCrossParam().split(StrUtil.COMMA)));
Map<String, DragRuleDTO.DragRuleParamsDesc> outParamDescMap = dragRuleDTO.getOutputParamDescList().stream()
.collect(
Collectors.toMap(DragRuleDTO.DragRuleParamsDesc::getParamName,
Function.identity())
);
List<DragRuleDTO.DragRuleParamsDesc> crossParamDescList = new ArrayList<>();
dragRuleDTO.getCrossParamList().forEach(paramName -> {
DragRuleDTO.DragRuleParamsDesc dragRuleParamsDesc = outParamDescMap.get(paramName);
if (ObjectUtil.isNotNull(dragRuleParamsDesc)) {
crossParamDescList.add(dragRuleParamsDesc);
}
});
dragRuleDTO.setCrossParamDescList(crossParamDescList);
}
if (StrUtil.isNotBlank(tbSysDragRule.getCustomOptionParam())) {
String customOptionParam = tbSysDragRule.getCustomOptionParam();
dragRuleDTO.setCustomOptionParam(customOptionParam);
List customOptionParamsList = JSONObject.parseArray(customOptionParam, List.class);
List<List<DragRuleDTO.CustomOptionParam>> typeList = new ArrayList<>();
for (Object customParamList : customOptionParamsList) {
List<JSONObject> jsonParamList = (List<JSONObject>) customParamList;
List<DragRuleDTO.CustomOptionParam> innerParamList = jsonParamList.stream().map(jsonParam -> {
return JSONObject.parseObject(jsonParam.toJSONString(), DragRuleDTO.CustomOptionParam.class);
}).collect(Collectors.toList());
typeList.add(innerParamList);
}
dragRuleDTO.setCustomOptionParamList(typeList);
}
}
return dragRuleDTO;
}
@Override
public void updateDragRule(DragRuleDTO dragRuleDTO) {
Object dragRuleDTOObj = redisUtil.hget(RuleConstant.DRAG_RULES, dragRuleDTO.getDragRuleId());
if (ObjectUtil.isNull(dragRuleDTOObj)) {
throw new ServiceException(StrUtil.format("通过规则id[{}]获取拖拽规则缓存信息失败!",
dragRuleDTO.getDragRuleId()));
}
DragRuleDTO cacheDragRuleDTO = JSONObject.parseObject(dragRuleDTOObj.toString(), DragRuleDTO.class);
JSONObject outputParamJSON = JSON.parseObject(cacheDragRuleDTO.getOutputParam());
String dragOptionParamsStr = null;
if (ObjectUtil.isNotEmpty(dragRuleDTO.getCustomOptionParamList())) {
List<List<DragRuleDTO.CustomOptionParam>> customOptionParamsList = dragRuleDTO.getCustomOptionParamList();
for (List<DragRuleDTO.CustomOptionParam> paramList : customOptionParamsList) {
if (ObjectUtil.isEmpty(paramList)) {
continue;
}
dragRuleServerUtil.checkCustomOptionParam(paramList);
}
dragOptionParamsStr = JSONObject.toJSONString(dragRuleDTO.getCustomOptionParamList());
dragRuleDTO.setCustomOptionParam(dragOptionParamsStr);
} else if (ObjectUtil.isEmpty(dragRuleDTO.getCustomOptionParamList())) {
dragRuleDTO.setCustomOptionParam(null);
dragRuleDTO.setCustomOptionParamList(null);
}
String crossParamStr = null;
if (ObjectUtil.isNotEmpty(dragRuleDTO.getCrossParamDescList())) {
List<String> crossParamList = dragRuleDTO.getCrossParamDescList().stream().map(
DragRuleDTO.DragRuleParamsDesc::getParamName
).collect(Collectors.toList());
dragRuleDTO.setCrossParamList(crossParamList);
for (String crossParam : dragRuleDTO.getCrossParamList()) {
if (!outputParamJSON.containsKey(crossParam)) {
String errorMessage = StrUtil.format("拖拽规则传递参数必须为规则方法出参参数,[{}]不满足条件,请确认!",
crossParam);
throw new ServiceException(errorMessage);
}
}
crossParamStr = dragRuleDTO.getCrossParamList().stream().map(
String::toString
).collect(Collectors.joining(StrUtil.COMMA));
dragRuleDTO.setCrossParam(crossParamStr);
Map<String, DragRuleDTO.DragRuleParamsDesc> outParamDescMap = dragRuleDTO
.getOutputParamDescList().stream()
.collect(Collectors.toMap(
DragRuleDTO.DragRuleParamsDesc::getParamName,
Function.identity(),
(k1, k2) -> k2)
);
List<DragRuleDTO.DragRuleParamsDesc> crossParamDescList = new ArrayList<>();
dragRuleDTO.getCrossParamList().forEach(paramName -> {
DragRuleDTO.DragRuleParamsDesc dragRuleParamsDesc = outParamDescMap.get(paramName);
if (ObjectUtil.isNotNull(dragRuleParamsDesc)) {
crossParamDescList.add(dragRuleParamsDesc);
}
});
dragRuleDTO.setCrossParamDescList(crossParamDescList);
} else {
dragRuleDTO.setCrossParamList(null);
dragRuleDTO.setCrossParam(null);
dragRuleDTO.setCrossParamDescList(null);
}
QueryWrapper<TbSysDragChain> chainQueryWrapper = new QueryWrapper<>();
chainQueryWrapper.lambda().like(TbSysDragChain::getDragRuleChain, dragRuleDTO.getDragRuleId());
List<TbSysDragChain> dragRuleChainList = tbSysDragChainMapper.selectList(chainQueryWrapper);
if (ObjectUtil.isNotEmpty(dragRuleChainList)) {
for (TbSysDragChain dragRuleChain : dragRuleChainList) {
dragRuleServerUtil.checkDragRuleUpdateLegal(dragRuleChain, dragRuleDTO);
}
}
TbSysDragRule tbSysDragRule = dragRuleConvert.dragRuleDTOToDragRule(dragRuleDTO);
tbSysDragRule.setCrossParam(crossParamStr);
tbSysDragRule.setCustomOptionParam(dragOptionParamsStr);
tbSysDragRuleMapper.updateCrossParamAndOptionParam(tbSysDragRule);
cacheDragRuleDTO.setIfLoadDB(RuleConstant.NUM_1_STR);
cacheDragRuleDTO.setCrossParam(crossParamStr);
cacheDragRuleDTO.setCrossParamList(dragRuleDTO.getCrossParamList());
cacheDragRuleDTO.setCrossParamDescList(dragRuleDTO.getCrossParamDescList());
cacheDragRuleDTO.setCustomOptionParam(dragOptionParamsStr);
cacheDragRuleDTO.setCustomOptionParamList(dragRuleDTO.getCustomOptionParamList());
redisUtil.hset(RuleConstant.DRAG_RULES, dragRuleDTO.getDragRuleId(), JSONObject.toJSONString(cacheDragRuleDTO));
}
@Override
public void deleteDragRule(String id) {
Object dragRuleDTOObj = redisUtil.hget(RuleConstant.DRAG_RULES, id);
if (ObjectUtil.isNull(dragRuleDTOObj)) {
throw new ServiceException(StrUtil.format("通过规则id[{}]未获取到缓存规则信息,删除失败!", id));
}
DragRuleDTO cacheDragRuleDTO = JSONObject.parseObject(dragRuleDTOObj.toString(), DragRuleDTO.class);
QueryWrapper<TbSysDragChain> chainQueryWrapper = new QueryWrapper<>();
chainQueryWrapper.lambda().like(TbSysDragChain::getDragRuleChain, id);
List<TbSysDragChain> dragRuleChainList = tbSysDragChainMapper.selectList(chainQueryWrapper);
if (ObjectUtil.isNotEmpty(dragRuleChainList)) {
String chainInfo = dragRuleChainList.stream().map(dragRuleChain -> "【规则链uri: "
.concat(dragRuleChain.getUri())
.concat(StrUtil.COMMA)
.concat("规则链id为: ")
.concat(dragRuleChain.getDragRuleChainId())
.concat("】")
).collect(Collectors.joining(StrUtil.COMMA));
throw new ServiceException(StrUtil.format("当前拖拽规则[{}]有规则链[{}]引用,不能删除!", id, chainInfo));
}
QueryWrapper<TbSysProductDragChain> productChainQueryWrapper = new QueryWrapper<>();
productChainQueryWrapper.lambda().like(TbSysProductDragChain::getDragRuleChain, id);
List<TbSysProductDragChain> productChainList = productDragChainMapper.selectList(productChainQueryWrapper);
if (ObjectUtil.isNotEmpty(productChainList)) {
String chainInfo = productChainList.stream().map(productChain -> "【产品规则uri: "
.concat(productChain.getUri())
.concat(StrUtil.COMMA)
.concat("产品为productCd: ")
.concat(productChain.getProductCd())
.concat("】")).collect(Collectors.joining());
throw new ServiceException(StrUtil
.format("当前删除规则[{}]被产品规则[{}]引用,请先删除产品规则链", id, chainInfo));
}
tbSysDragRuleMapper.deleteById(id);
cacheDragRuleDTO.setIfLoadDB(RuleConstant.NUM_0_STR);
cacheDragRuleDTO.setCrossParam(null);
cacheDragRuleDTO.setCrossParamList(null);
cacheDragRuleDTO.setCrossParamDescList(null);
cacheDragRuleDTO.setCustomOptionParam(null);
cacheDragRuleDTO.setCustomOptionParamList(null);
redisUtil.hset(RuleConstant.DRAG_RULES, id, JSONObject.toJSONString(cacheDragRuleDTO));
}
@Override
public PageBean<DragRuleDTO> getDragRulePage(DragRuleDTO dragRuleDTO) {
Map<Object, Object> cacheDragRuleObjMap = redisUtil.hmget(RuleConstant.DRAG_RULES);
if (ObjectUtil.isEmpty(cacheDragRuleObjMap)) {
throw new ServiceException("当前缓存中没有拖拽规则信息,请确认是否缓存中没有数据!");
}
Map<String, DragRuleDTO> cacheDragRuleMap = cacheDragRuleObjMap
.entrySet()
.stream()
.filter(entry -> {
DragRuleDTO filterRule = JSONObject.parseObject((String) entry.getValue(), DragRuleDTO.class);
boolean ifRuleIdCheck = true;
if (StrUtil.isNotBlank(dragRuleDTO.getDragRuleId())) {
ifRuleIdCheck = dragRuleDTO.getDragRuleId().equals(filterRule.getDragRuleId());
}
boolean ifRuleDescCheck = true;
if (StrUtil.isNotBlank(dragRuleDTO.getRuleDesc())) {
ifRuleDescCheck = filterRule.getRuleDesc().contains(dragRuleDTO.getRuleDesc());
}
boolean ifLoadDB = true;
if (StrUtil.isNotBlank(dragRuleDTO.getIfLoadDB())) {
ifLoadDB = dragRuleDTO.getIfLoadDB().equals(filterRule.getIfLoadDB());
}
return (ifRuleDescCheck && ifRuleIdCheck && ifLoadDB);
})
.collect(Collectors.toMap(
entry -> (String) entry.getKey(),
entry -> JSONObject.parseObject((String) entry.getValue(), DragRuleDTO.class)
));
QueryWrapper<TbSysDragRule> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(StrUtil.isNotBlank(dragRuleDTO.getDragRuleId()),
TbSysDragRule::getDragRuleId,
dragRuleDTO.getDragRuleId());
queryWrapper.lambda().like(StrUtil.isNotBlank(dragRuleDTO.getRuleDesc()),
TbSysDragRule::getRuleDesc,
dragRuleDTO.getRuleDesc());
List<TbSysDragRule> tbSysDragRuleList = tbSysDragRuleMapper.selectList(queryWrapper);
Map<String, TbSysDragRule> tbSysDragRuleMap = new HashMap<>(RuleConstant.DEFAULT_MAP_SIZE);
if (ObjectUtil.isNotEmpty(tbSysDragRuleList)) {
tbSysDragRuleMap = tbSysDragRuleList.stream().collect(Collectors.toMap(
TbSysDragRule::getDragRuleId,
Function.identity(),
(k1, k2) -> k2
));
}
Map<String, TbSysDragRule> finalTbSysDragRuleMap = tbSysDragRuleMap;
List<DragRuleDTO> displayDragDTOList = cacheDragRuleMap.entrySet().stream().map(entry -> {
DragRuleDTO localDragDTO = entry.getValue();
String ruleDesc = entry.getValue().getRuleDesc();
TbSysDragRule tbSysDragRule = finalTbSysDragRuleMap.get(localDragDTO.getDragRuleId());
if (ObjectUtil.isNotNull(tbSysDragRule)) {
localDragDTO = dragRuleConvert.dragRuleInitToDragRuleDTO(tbSysDragRule, localDragDTO);
localDragDTO.setIfLoadDB(RuleConstant.NUM_1_STR);
localDragDTO.setRuleDesc(ruleDesc);
if (StrUtil.isNotBlank(tbSysDragRule.getCrossParam())) {
List<String> crossParamList = Arrays.asList(StrUtil.split(tbSysDragRule.getCrossParam(), RuleConstant.COMMA));
localDragDTO.setCrossParamList(crossParamList);
Map<String, DragRuleDTO.DragRuleParamsDesc> outParamDescMap = localDragDTO.getOutputParamDescList().stream()
.collect(
Collectors.toMap(DragRuleDTO.DragRuleParamsDesc::getParamName,
Function.identity())
);
List<DragRuleDTO.DragRuleParamsDesc> crossParamDescList = new ArrayList<>();
crossParamList.forEach(paramName -> {
DragRuleDTO.DragRuleParamsDesc dragRuleParamsDesc = outParamDescMap.get(paramName);
if (ObjectUtil.isNotNull(dragRuleParamsDesc)) {
crossParamDescList.add(dragRuleParamsDesc);
}
});
localDragDTO.setCrossParamDescList(crossParamDescList);
}
if (StrUtil.isNotBlank(tbSysDragRule.getCustomOptionParam())) {
String customOptionParam = tbSysDragRule.getCustomOptionParam();
List customOptionParamsList = JSONObject.parseArray(customOptionParam, List.class);
List<List<DragRuleDTO.CustomOptionParam>> typeList = new ArrayList<>();
for (Object customParamList : customOptionParamsList) {
List<JSONObject> jsonParamList = (List<JSONObject>) customParamList;
List<DragRuleDTO.CustomOptionParam> innerParamList = jsonParamList.stream().map(jsonParam -> {
return JSONObject.parseObject(jsonParam.toJSONString(), DragRuleDTO.CustomOptionParam.class);
}).collect(Collectors.toList());
typeList.add(innerParamList);
}
}
}
return localDragDTO;
}).collect(Collectors.toList());
PageBean<DragRuleDTO> pageBean = customPageUtil.setFlowListPage(
displayDragDTOList, PaginationContext.getPageNum(), PaginationContext.getPageSize());
return pageBean;
}
@Override
public void addChain(String dragRuleChain, String uri, MultipartFile file) throws IOException {
List<String> fileChainList = new ArrayList<>();
try (InputStream inputStream = file.getInputStream();) {
BpmnModelInstance modelInstance = Bpmn.readModelFromStream(inputStream);
List<FlowElement> flowElements = (List<FlowElement>) modelInstance.getModelElementsByType(FlowElement.class);
for (FlowElement element : flowElements) {
Class<? extends FlowElement> elementType = element.getClass();
String elementSimpleName = elementType.getSimpleName();
if (!dragRuleServerUtil.checkProcessFileType(elementSimpleName)) {
throw new ServiceException("当前规则链只支持Task节点链,其他类型暂不支持!");
}
}
for (Task task : modelInstance.getModelElementsByType(Task.class)) {
String taskName = task.getName();
fileChainList.add(taskName);
}
} catch (IOException e) {
String errorMessage = LogUtil.getStackTraceInfo(e);
throw new ServiceException(StrUtil.format("解析文件内容异常,异常信息为[{}],请确认!", errorMessage));
}
if (fileChainList.size() == 0) {
throw new ServiceException("规则链文件至少一个规则信息,请确认!");
}
String fileName = file.getOriginalFilename();
String fileBase64Str = Base64.getEncoder().encodeToString(file.getBytes());
Object dragRuleChainObj = redisUtil.hget(CommonDragRuleConstant.DRAG_RULE_CHAIN_FLAG, uri);
if (ObjectUtil.isNull(dragRuleChainObj)) {
throw new ServiceException(StrUtil.format("当前缓存中没有uri[{}]规则链信息,请确认是否缓存中没有数据!", uri));
}
DragChainDTO cacheDragChainDTO = JSONObject.parseObject(dragRuleChainObj.toString(), DragChainDTO.class);
String[] dragRuleChainIds = StrUtil.split(dragRuleChain, RuleConstant.COMMA);
if (dragRuleChainIds.length > 0) {
Set<String> dragRuleChainSet = new HashSet<>();
Collections.addAll(dragRuleChainSet, dragRuleChainIds);
if (dragRuleChainSet.size() != dragRuleChainIds.length) {
throw new ServiceException("当前规则链对应规则存在重复规则,请确认!");
}
QueryWrapper<TbSysDragRule> dragRuleQueryWrapper = new QueryWrapper<>();
dragRuleQueryWrapper.lambda().in(TbSysDragRule::getDragRuleId, dragRuleChainIds);
List<TbSysDragRule> dragRuleList = tbSysDragRuleMapper.selectList(dragRuleQueryWrapper);
if (ObjectUtil.isEmpty(dragRuleList)) {
String idStr = StrUtil.join(RuleConstant.COMMA, dragRuleChainIds);
throw new ServiceException(StrUtil.format("当前保存规则链规则[{}]没有先进行落库保存操作,请先进行规则落库操作!", idStr));
}
if (dragRuleList.size() != dragRuleChainIds.length) {
List<String> dbIdList = dragRuleList.stream().map(
TbSysDragRule::getDragRuleId
).collect(Collectors.toList());
List<String> differIdList = Arrays.stream(dragRuleChainIds)
.filter(id -> !dbIdList.contains(id))
.collect(Collectors.toList());
String idStr = StrUtil.join(RuleConstant.COMMA, differIdList);
throw new ServiceException(StrUtil.format("当前保存规则链部分规则[{}]没有先进行落库保存操作,请先进行规则落库操作!", idStr));
}
if (fileChainList.size() != dragRuleChainIds.length) {
throw new ServiceException("当前规则链长度与规则文件长度不一致,请确认!");
}
for (int i = 0; i < dragRuleChainIds.length; i++) {
if (!dragRuleChainIds[i].equals(fileChainList.get(i))) {
throw new ServiceException("规则链传入规则与文件内规则顺序不一致,请确认!");
}
}
cacheDragChainDTO.setDragRuleChain(dragRuleChain);
}
commonDragRuleUtil.checkDragRuleWithParamLegal(cacheDragChainDTO);
TbSysDragChain tbSysDragChain = dragRuleConvert.dragChainDTOToTbSysDragChain(cacheDragChainDTO);
tbSysDragChain.setDragRuleChain(dragRuleChain);
tbSysDragChain.setChainFileName(fileName);
tbSysDragChain.setChainFile(fileBase64Str);
tbSysDragChainMapper.insert(tbSysDragChain);
cacheDragChainDTO.setIfLoadDB(RuleConstant.NUM_1_STR);
redisUtil.hset(CommonDragRuleConstant.DRAG_RULE_CHAIN_FLAG,
cacheDragChainDTO.getUri(), JSONObject.toJSONString(cacheDragChainDTO));
}
@Override
public void addProductChainInit(String uri, String productCd) throws IOException {
QueryWrapper<TbSysDragChain> dragChainQueryWrapper = new QueryWrapper<>();
dragChainQueryWrapper.lambda().eq(TbSysDragChain::getUri, uri);
TbSysDragChain tbSysDragChain = tbSysDragChainMapper.selectOne(dragChainQueryWrapper);
if (ObjectUtil.isNull(tbSysDragChain)) {
throw new ServiceException(StrUtil.format("当前拖拽规则默认规则未配置,请先前往参数管理=>规则链管理中,配置默认规则链!"));
}
QueryWrapper<TbSysProductDragChain> productChainQueryWrapper = new QueryWrapper<>();
productChainQueryWrapper.lambda().eq(TbSysProductDragChain::getUri, uri)
.eq(TbSysProductDragChain::getProductCd, productCd);
if (productDragChainMapper.selectCount(productChainQueryWrapper) > 0) {
throw new ServiceException(StrUtil.format("当前产品[{}]对应uri[{}]已经存在规则链信息,不能进行新增操作!",
productCd, uri));
}
}
@Override
public void addProductChain(String dragRuleChain, String uri, MultipartFile file, String productCd) throws IOException {
QueryWrapper<TbSysProductDragChain> productChainQueryWrapper = new QueryWrapper<>();
productChainQueryWrapper.lambda().eq(TbSysProductDragChain::getUri, uri)
.eq(TbSysProductDragChain::getProductCd, productCd);
if (productDragChainMapper.selectCount(productChainQueryWrapper) > 0) {
throw new ServiceException(StrUtil.format("当前产品[{}]对应uri[{}]已经存在规则链信息,新增失败!",
productCd, uri));
}
List<String> fileChainList = new ArrayList<>();
try (InputStream inputStream = file.getInputStream();) {
BpmnModelInstance modelInstance = Bpmn.readModelFromStream(inputStream);
List<FlowElement> flowElements = (List<FlowElement>) modelInstance.getModelElementsByType(FlowElement.class);
for (FlowElement element : flowElements) {
Class<? extends FlowElement> elementType = element.getClass();
String elementSimpleName = elementType.getSimpleName();
if (!dragRuleServerUtil.checkProcessFileType(elementSimpleName)) {
throw new ServiceException("当前规则链只支持Task节点链,其他类型暂不支持!");
}
}
for (Task task : modelInstance.getModelElementsByType(Task.class)) {
String taskName = task.getName();
fileChainList.add(taskName);
}
} catch (IOException e) {
String errorMessage = LogUtil.getStackTraceInfo(e);
throw new ServiceException(StrUtil.format("解析文件内容异常,异常信息为[{}],请确认!", errorMessage));
}
if (fileChainList.size() == 0) {
throw new ServiceException("规则链文件至少一个规则信息,请确认!");
}
if (StrUtil.isNotBlank(dragRuleChain)) {
List<String> dragRuleIdList = Arrays.stream(dragRuleChain.split(RuleConstant.COMMA))
.collect(Collectors.toList());
if (dragRuleIdList.size() != dragRuleIdList.stream().distinct().count()) {
throw new ServiceException("当前规则链中规则存在重复,请确认!");
}
QueryWrapper<TbSysDragRule> dragRuleQueryWrapper = new QueryWrapper<>();
dragRuleQueryWrapper.lambda().in(TbSysDragRule::getDragRuleId, dragRuleIdList);
List<TbSysDragRule> dragRuleList = tbSysDragRuleMapper.selectList(dragRuleQueryWrapper);
if (ObjectUtil.isEmpty(dragRuleList)) {
throw new ServiceException("当前规则链规则没有先进行落库保存操作,请先进行规则落库操作!");
} else if (dragRuleList.size() != dragRuleIdList.size()) {
List<String> dragRuleDifferList = dragRuleIdList.stream()
.filter(dragRuleId -> !dragRuleList.stream()
.map(TbSysDragRule::getDragRuleId)
.collect(Collectors.toList())
.contains(dragRuleId))
.collect(Collectors.toList());
String dragDifferStr = String.join(StrUtil.COMMA, dragRuleDifferList);
throw new ServiceException(StrUtil
.format("当前规则链部分规则[{}]没有先进行落库保存操作,请先进行规则落库操作!", dragDifferStr));
}
}
QueryWrapper<TbSysDragChain> dragChainQueryWrapper = new QueryWrapper<>();
dragChainQueryWrapper.lambda().eq(TbSysDragChain::getUri, uri);
TbSysDragChain tbSysDragChain = tbSysDragChainMapper.selectOne(dragChainQueryWrapper);
if (ObjectUtil.isNull(tbSysDragChain)) {
throw new ServiceException(StrUtil.format("当前拖拽规则默认规则未配置,请先前往参数管理=>规则链管理中,配置默认规则链!"));
}
String fileName = file.getOriginalFilename();
String fileBase64Str = Base64.getEncoder().encodeToString(file.getBytes());
TbSysProductDragChain tbSysProductDragChain = new TbSysProductDragChain();
tbSysProductDragChain.setProductChainId(IdUtil.simpleUUID());
tbSysProductDragChain.setProductCd(productCd);
tbSysProductDragChain.setChainFile(fileBase64Str);
tbSysProductDragChain.setChainFileName(fileName);
tbSysProductDragChain.setUri(uri);
tbSysProductDragChain.setDragRuleChain(dragRuleChain);
tbSysProductDragChain.setCtime(new Date());
tbSysProductDragChain.setMtime(new Date());
productDragChainMapper.insert(tbSysProductDragChain);
String productChainKey = uri.concat(StrUtil.COLON).concat(productCd);
redisUtil.hset(CommonDragRuleConstant.DRAG_RULE_PRODUCT_CHAIN_FLAG,
productChainKey, JSONObject.toJSONString(tbSysProductDragChain));
}
@Override
public void updateChain(String dragRuleChain, String uri, MultipartFile file) throws IOException {
List<String> fileChainList = new ArrayList<>();
try (InputStream inputStream = file.getInputStream();) {
BpmnModelInstance modelInstance = Bpmn.readModelFromStream(inputStream);
List<FlowElement> flowElements = (List<FlowElement>) modelInstance.getModelElementsByType(FlowElement.class);
for (FlowElement element : flowElements) {
Class<? extends FlowElement> elementType = element.getClass();
String elementSimpleName = elementType.getSimpleName();
if (!dragRuleServerUtil.checkProcessFileType(elementSimpleName)) {
throw new ServiceException("当前规则链只支持Task节点链,其他类型暂不支持!");
}
}
for (Task task : modelInstance.getModelElementsByType(Task.class)) {
String taskName = task.getName();
fileChainList.add(taskName);
}
} catch (IOException e) {
String errorMessage = LogUtil.getStackTraceInfo(e);
throw new ServiceException(StrUtil.format("解析文件内容异常,异常信息为[{}],请确认!", errorMessage));
}
if (fileChainList.size() == 0) {
throw new ServiceException("规则链文件至少一个规则信息,请确认!");
}
String fileName = file.getOriginalFilename();
String fileBase64Str = Base64.getEncoder().encodeToString(file.getBytes());
Object dragRuleChainObj = redisUtil.hget(CommonDragRuleConstant.DRAG_RULE_CHAIN_FLAG, uri);
if (ObjectUtil.isNull(dragRuleChainObj)) {
throw new ServiceException(StrUtil.format("当前缓存中没有uri[{}]规则链信息,请确认是否缓存中没有数据!", uri));
}
DragChainDTO cacheDragChainDTO = JSONObject.parseObject(dragRuleChainObj.toString(), DragChainDTO.class);
TbSysDragChain dragChain = tbSysDragChainMapper.selectById(cacheDragChainDTO.getDragRuleChainId());
if (ObjectUtil.isNull(dragChain)) {
throw new ServiceException(StrUtil.format("库中未获取到当前拖拽规则链[{}],不能进行修改操作!", cacheDragChainDTO.getDragRuleChainId()));
}
String[] dragRuleChainIds = StrUtil.split(dragRuleChain, RuleConstant.COMMA);
if (dragRuleChainIds.length > 0) {
Set<String> dragRuleChainSet = new HashSet<>();
Collections.addAll(dragRuleChainSet, dragRuleChainIds);
if (dragRuleChainSet.size() != dragRuleChainIds.length) {
throw new ServiceException("当前规则链对应规则存在重复规则,请确认!");
}
QueryWrapper<TbSysDragRule> dragRuleQueryWrapper = new QueryWrapper<>();
dragRuleQueryWrapper.lambda().in(TbSysDragRule::getDragRuleId, dragRuleChainIds);
List<TbSysDragRule> dragRuleList = tbSysDragRuleMapper.selectList(dragRuleQueryWrapper);
if (ObjectUtil.isEmpty(dragRuleList)) {
String idStr = StrUtil.join(RuleConstant.COMMA, dragRuleChainIds);
throw new ServiceException(StrUtil.format("当前保存规则链规则[{}]没有先进行落库保存操作,请先进行规则落库操作!", idStr));
}
if (dragRuleList.size() != dragRuleChainIds.length) {
List<String> dbIdList = dragRuleList.stream().map(
TbSysDragRule::getDragRuleId
).collect(Collectors.toList());
List<String> differIdList = Arrays.stream(dragRuleChainIds)
.filter(id -> !dbIdList.contains(id))
.collect(Collectors.toList());
String idStr = StrUtil.join(RuleConstant.COMMA, differIdList);
throw new ServiceException(StrUtil.format("当前保存规则链部分规则[{}]没有先进行落库保存操作,请先进行规则落库操作!", idStr));
}
if (fileChainList.size() != dragRuleChainIds.length) {
throw new ServiceException("当前规则链长度与规则文件长度不一致,请确认!");
}
for (int i = 0; i < dragRuleChainIds.length; i++) {
if (!dragRuleChainIds[i].equals(fileChainList.get(i))) {
throw new ServiceException(StrUtil.format(
"规则链传入第[{}]个规则与文件内规则顺序不一致,内容分别为[{}]以及[{}]请确认!",
i + 1,
dragRuleChainIds[i],
fileChainList.get(i)));
}
}
cacheDragChainDTO.setDragRuleChain(dragRuleChain);
}
commonDragRuleUtil.checkDragRuleWithParamLegal(cacheDragChainDTO);
TbSysDragChain tbSysDragChain = dragRuleConvert.dragChainDTOToTbSysDragChain(cacheDragChainDTO);
tbSysDragChain.setChainFile(fileBase64Str);
tbSysDragChain.setChainFileName(fileName);
tbSysDragChain.setDragRuleChain(dragRuleChain);
tbSysDragChainMapper.updateById(tbSysDragChain);
redisUtil.hset(CommonDragRuleConstant.DRAG_RULE_CHAIN_FLAG,
cacheDragChainDTO.getUri(), JSONObject.toJSONString(cacheDragChainDTO));
}
@Override
public void updateProductChain(String dragRuleChain, String uri, MultipartFile file, String productCd) throws IOException {
QueryWrapper<TbSysProductDragChain> productChainQueryWrapper = new QueryWrapper<>();
productChainQueryWrapper.lambda().eq(TbSysProductDragChain::getUri, uri)
.eq(TbSysProductDragChain::getProductCd, productCd);
TbSysProductDragChain productChain = productDragChainMapper.selectOne(productChainQueryWrapper);
if (ObjectUtil.isNull(productChain)) {
throw new ServiceException(StrUtil.format("当前产品[{}]对应uri[{}]不存在规则链信息,修改失败!",
productCd, uri));
}
List<String> fileChainList = new ArrayList<>();
try (InputStream inputStream = file.getInputStream();) {
BpmnModelInstance modelInstance = Bpmn.readModelFromStream(inputStream);
List<FlowElement> flowElements = (List<FlowElement>) modelInstance.getModelElementsByType(FlowElement.class);
for (FlowElement element : flowElements) {
Class<? extends FlowElement> elementType = element.getClass();
String elementSimpleName = elementType.getSimpleName();
if (!dragRuleServerUtil.checkProcessFileType(elementSimpleName)) {
throw new ServiceException("当前规则链只支持Task节点链,其他类型暂不支持!");
}
}
for (Task task : modelInstance.getModelElementsByType(Task.class)) {
String taskName = task.getName();
fileChainList.add(taskName);
}
} catch (IOException e) {
String errorMessage = LogUtil.getStackTraceInfo(e);
throw new ServiceException(StrUtil.format("解析文件内容异常,异常信息为[{}],请确认!", errorMessage));
}
if (fileChainList.size() == 0) {
throw new ServiceException("规则链文件至少一个规则信息,请确认!");
}
if (StrUtil.isNotBlank(dragRuleChain)) {
List<String> dragRuleIdList = Arrays.stream(dragRuleChain.split(RuleConstant.COMMA))
.collect(Collectors.toList());
if (dragRuleIdList.size() != dragRuleIdList.stream().distinct().count()) {
throw new ServiceException("当前规则链中规则存在重复,请确认!");
}
QueryWrapper<TbSysDragRule> dragRuleQueryWrapper = new QueryWrapper<>();
dragRuleQueryWrapper.lambda().in(TbSysDragRule::getDragRuleId, dragRuleIdList);
List<TbSysDragRule> dragRuleList = tbSysDragRuleMapper.selectList(dragRuleQueryWrapper);
if (ObjectUtil.isEmpty(dragRuleList) || dragRuleList.size() != dragRuleIdList.size()) {
throw new ServiceException(StrUtil
.format("当前规则链部分规则[{}]没有先进行落库保存操作,请先进行规则落库操作!", dragRuleChain));
}
}
QueryWrapper<TbSysDragChain> dragChainQueryWrapper = new QueryWrapper<>();
dragChainQueryWrapper.lambda().eq(TbSysDragChain::getUri, uri);
TbSysDragChain tbSysDragChain = tbSysDragChainMapper.selectOne(dragChainQueryWrapper);
if (ObjectUtil.isNull(tbSysDragChain)) {
throw new ServiceException(StrUtil.format("当前拖拽规则默认规则未配置,请先前往参数管理=>规则链管理中,配置默认规则链!"));
}
String fileName = file.getOriginalFilename();
String fileBase64Str = Base64.getEncoder().encodeToString(file.getBytes());
productChain.setChainFile(fileBase64Str);
productChain.setChainFileName(fileName);
productChain.setMtime(new Date());
productChain.setDragRuleChain(dragRuleChain);
productDragChainMapper.updateById(productChain);
String updateKey = uri.concat(StrUtil.COLON).concat(productCd);
redisUtil.hset(CommonDragRuleConstant.DRAG_RULE_PRODUCT_CHAIN_FLAG,
updateKey, JSONObject.toJSONString(productChain));
}
@Override
public DragChainDTO getDragChain(String uri) {
Object dragRuleChainObj = redisUtil.hget(CommonDragRuleConstant.DRAG_RULE_CHAIN_FLAG, uri);
if (ObjectUtil.isNull(dragRuleChainObj)) {
throw new ServiceException(StrUtil.format("当前缓存中没有uri[{}]规则链信息,请确认是否缓存中没有数据!", uri));
}
DragChainDTO dragChainDTO = JSONObject.parseObject(dragRuleChainObj.toString(), DragChainDTO.class);
QueryWrapper<TbSysDragChain> chainQueryWrapper = new QueryWrapper<>();
chainQueryWrapper.lambda().eq(TbSysDragChain::getUri, uri);
TbSysDragChain tbSysDragRuleChain = tbSysDragChainMapper.selectOne(chainQueryWrapper);
if (ObjectUtil.isNotNull(tbSysDragRuleChain)) {
dragChainDTO.setDragRuleChainId(tbSysDragRuleChain.getDragRuleChainId());
dragChainDTO.setDragRuleChain(tbSysDragRuleChain.getDragRuleChain());
dragChainDTO.setIfLoadDB(RuleConstant.NUM_1_STR);
}
return dragChainDTO;
}
@Override
public DragChainDTO getDragProductChain(String uri, String productCd) {
Object dragRuleChainObj = redisUtil.hget(CommonDragRuleConstant.DRAG_RULE_CHAIN_FLAG, uri);
if (ObjectUtil.isNull(dragRuleChainObj)) {
throw new ServiceException(StrUtil.format("当前缓存中没有uri[{}]规则链信息,请确认是否缓存中没有数据!", uri));
}
DragChainDTO dragChainDTO = JSONObject.parseObject(dragRuleChainObj.toString(), DragChainDTO.class);
QueryWrapper<TbSysDragChain> chainQueryWrapper = new QueryWrapper<>();
chainQueryWrapper.lambda().eq(TbSysDragChain::getUri, uri);
TbSysDragChain tbSysDragRuleChain = tbSysDragChainMapper.selectOne(chainQueryWrapper);
if (ObjectUtil.isNotNull(tbSysDragRuleChain)) {
dragChainDTO.setDragRuleChainId(tbSysDragRuleChain.getDragRuleChainId());
dragChainDTO.setDragRuleChain(tbSysDragRuleChain.getDragRuleChain());
dragChainDTO.setIfLoadDB(RuleConstant.NUM_1_STR);
}
QueryWrapper<TbSysProductDragChain> productChainQueryWrapper = new QueryWrapper<>();
productChainQueryWrapper.lambda().eq(TbSysProductDragChain::getProductCd, productCd)
.eq(TbSysProductDragChain::getUri, uri);
TbSysProductDragChain productDragChain = productDragChainMapper.selectOne(productChainQueryWrapper);
if (ObjectUtil.isNotNull(productDragChain)) {
dragChainDTO.setProductChainId(productDragChain.getProductChainId());
dragChainDTO.setDragProductRuleChain(productDragChain.getDragRuleChain());
dragChainDTO.setIfLoadDB(RuleConstant.NUM_1_STR);
}
dragChainDTO.setProductCd(productCd);
return dragChainDTO;
}
@Override
public void downloadFile(String uri) throws IOException {
QueryWrapper<TbSysDragChain> chainQueryWrapper = new QueryWrapper<>();
chainQueryWrapper.lambda().eq(TbSysDragChain::getUri, uri);
TbSysDragChain tbSysDragRuleChain = tbSysDragChainMapper.selectOne(chainQueryWrapper);
if (ObjectUtil.isNull(tbSysDragRuleChain) || StrUtil.isBlank(tbSysDragRuleChain.getChainFile())) {
throw new ServiceException(StrUtil.format("通过uri[{}]获取规则链文件信息为空!", uri));
}
String base64FileStr = tbSysDragRuleChain.getChainFile();
String fileName = tbSysDragRuleChain.getChainFileName();
byte[] decodedBytes = Base64.getDecoder().decode(base64FileStr);
response.setContentType(CommonDragRuleConstant.APPLICATION_XML);
response.setHeader(CommonDragRuleConstant.CONTENT_DISPOSITION, "attachment;filename=\"" + fileName + "\"");
try (OutputStream out = response.getOutputStream()) {
out.write(decodedBytes);
} catch (IOException e) {
String errorMessage = LogUtil.getStackTraceInfo(e);
throw new ServiceException(StrUtil.format("下载文件异常,异常信息为[{}]", errorMessage));
} finally {
response.flushBuffer();
}
}
@Override
public void downloadProductFile(String uri, String productCd) throws IOException {
QueryWrapper<TbSysProductDragChain> chainQueryWrapper = new QueryWrapper<>();
chainQueryWrapper.lambda().eq(TbSysProductDragChain::getUri, uri)
.eq(TbSysProductDragChain::getProductCd, productCd);
TbSysProductDragChain productDragChain = productDragChainMapper.selectOne(chainQueryWrapper);
if (ObjectUtil.isNull(productDragChain) || StrUtil.isBlank(productDragChain.getChainFile())) {
throw new ServiceException(StrUtil.format("通过uri[{}]以及产品cd[{}]获取产品规则链文件信息为空!",
uri, productCd));
}
String base64FileStr = productDragChain.getChainFile();
String fileName = productDragChain.getChainFileName();
byte[] decodedBytes = Base64.getDecoder().decode(base64FileStr);
response.setContentType(CommonDragRuleConstant.APPLICATION_XML);
response.setHeader(CommonDragRuleConstant.CONTENT_DISPOSITION, "attachment;filename=\"" + fileName + "\"");
try (OutputStream out = response.getOutputStream()) {
out.write(decodedBytes);
} catch (IOException e) {
String errorMessage = LogUtil.getStackTraceInfo(e);
throw new ServiceException(StrUtil.format("下载文件异常,异常信息为[{}]", errorMessage));
} finally {
response.flushBuffer();
}
}
@Override
public String getXMLStr(String uri) {
QueryWrapper<TbSysDragChain> chainQueryWrapper = new QueryWrapper<>();
chainQueryWrapper.lambda().eq(TbSysDragChain::getUri, uri);
TbSysDragChain tbSysDragRuleChain = tbSysDragChainMapper.selectOne(chainQueryWrapper);
if (ObjectUtil.isNull(tbSysDragRuleChain) || StrUtil.isBlank(tbSysDragRuleChain.getChainFile())) {
throw new ServiceException(StrUtil.format("通过uri[{}]获取规则链文件信息为空!", uri));
}
String base64FileStr = tbSysDragRuleChain.getChainFile();
byte[] decodedBytes = Base64.getDecoder().decode(base64FileStr);
return new String(decodedBytes, StandardCharsets.UTF_8);
}
@Override
public String getProductXMLStr(String uri, String productCd) throws IOException {
QueryWrapper<TbSysProductDragChain> chainQueryWrapper = new QueryWrapper<>();
chainQueryWrapper.lambda().eq(TbSysProductDragChain::getUri, uri)
.eq(TbSysProductDragChain::getProductCd, productCd);
TbSysProductDragChain tbSysDragRuleChain = productDragChainMapper.selectOne(chainQueryWrapper);
if (ObjectUtil.isNull(tbSysDragRuleChain) || StrUtil.isBlank(tbSysDragRuleChain.getChainFile())) {
throw new ServiceException(StrUtil.format("通过uri[{}]以及产品cd[{}]获取规则链文件信息为空!",
uri, productCd));
}
String base64FileStr = tbSysDragRuleChain.getChainFile();
byte[] decodedBytes = Base64.getDecoder().decode(base64FileStr);
return new String(decodedBytes, StandardCharsets.UTF_8);
}
@Override
public PageBean<DragChainDTO> getDragChainPage(DragChainDTO dragChainDTO) {
Map<Object, Object> cacheDragChainObj = redisUtil.hmget(CommonDragRuleConstant.DRAG_RULE_CHAIN_FLAG);
if (ObjectUtil.isEmpty(cacheDragChainObj)) {
throw new ServiceException(StrUtil.format("获取规则链列表[DRAG_RULE_CHAIN_FLAG]缓存信息为空,请确认!"));
}
Map<String, DragChainDTO> cacheDragChainMap = cacheDragChainObj
.entrySet()
.stream()
.filter(entry -> {
DragChainDTO localDragChainDTO = JSONObject.parseObject(entry.getValue().toString(), DragChainDTO.class);
boolean ifControllerNameCheck = true;
if (StrUtil.isNotBlank(dragChainDTO.getControllerName())) {
ifControllerNameCheck = localDragChainDTO.getControllerName().contains(dragChainDTO.getControllerName());
}
boolean ifControllerDescCheck = true;
if (StrUtil.isNotBlank(dragChainDTO.getControllerDesc())) {
ifControllerDescCheck = localDragChainDTO.getControllerDesc().contains(dragChainDTO.getControllerDesc());
}
boolean ifMethodNameCheck = true;
if (StrUtil.isNotBlank(dragChainDTO.getMethodName())) {
ifMethodNameCheck = localDragChainDTO.getMethodName().contains(dragChainDTO.getMethodName());
}
boolean ifMethodDescCheck = true;
if (StrUtil.isNotBlank(dragChainDTO.getMethodDesc())) {
ifMethodDescCheck = localDragChainDTO.getMethodDesc().contains(dragChainDTO.getMethodDesc());
}
boolean ifUriCheck = true;
if (StrUtil.isNotBlank(dragChainDTO.getUri())) {
ifUriCheck = localDragChainDTO.getUri().contains(dragChainDTO.getUri());
}
boolean ifModuleCheck = true;
if (StrUtil.isNotBlank(dragChainDTO.getDragChainModule())) {
ifModuleCheck = localDragChainDTO.getUri().startsWith(dragChainDTO.getDragChainModule());
}
boolean ifIfLoadDBCheck = true;
if (StrUtil.isNotBlank(dragChainDTO.getIfLoadDB())) {
ifIfLoadDBCheck = dragChainDTO.getIfLoadDB().equals(localDragChainDTO.getIfLoadDB());
}
boolean ifDragRuleChainCheck = true;
if (StrUtil.isNotBlank(dragChainDTO.getDragRuleChain())) {
if (StrUtil.isBlank(localDragChainDTO.getDragRuleChain())) {
ifDragRuleChainCheck = false;
} else {
ifDragRuleChainCheck = localDragChainDTO.getDragRuleChain().contains(dragChainDTO.getDragRuleChain());
}
}
return ifControllerNameCheck
&& ifControllerDescCheck
&& ifMethodNameCheck
&& ifMethodDescCheck
&& ifUriCheck
&& ifModuleCheck
&& ifIfLoadDBCheck
&& ifDragRuleChainCheck;
})
.collect(Collectors.toMap(
entry -> entry.getKey().toString(),
entry -> JSONObject.parseObject(entry.getValue().toString(), DragChainDTO.class),
(k1, k2) -> k2
));
QueryWrapper<TbSysDragChain> chainQueryWrapper = new QueryWrapper<>();
chainQueryWrapper.lambda().like(StrUtil.isNotBlank(dragChainDTO.getControllerName()),
TbSysDragChain::getControllerName, dragChainDTO.getControllerName());
chainQueryWrapper.lambda().like(StrUtil.isNotBlank(dragChainDTO.getControllerDesc()),
TbSysDragChain::getControllerDesc, dragChainDTO.getControllerDesc());
chainQueryWrapper.lambda().like(StrUtil.isNotBlank(dragChainDTO.getMethodName()),
TbSysDragChain::getMethodName, dragChainDTO.getMethodName());
chainQueryWrapper.lambda().like(StrUtil.isNotBlank(dragChainDTO.getMethodDesc()),
TbSysDragChain::getMethodDesc, dragChainDTO.getMethodDesc());
chainQueryWrapper.lambda().like(StrUtil.isNotBlank(dragChainDTO.getUri()),
TbSysDragChain::getUri, dragChainDTO.getUri());
chainQueryWrapper.lambda().like(StrUtil.isNotBlank(dragChainDTO.getDragRuleChain()),
TbSysDragChain::getDragRuleChain, dragChainDTO.getDragRuleChain());
chainQueryWrapper.lambda().likeRight(StrUtil.isNotBlank(dragChainDTO.getDragChainModule()),
TbSysDragChain::getUri, dragChainDTO.getDragChainModule());
List<TbSysDragChain> tbSysDragRuleChainList = tbSysDragChainMapper.selectList(chainQueryWrapper);
Map<String, TbSysDragChain> tbSysDragChainMap = new HashMap<>(RuleConstant.DEFAULT_MAP_SIZE);
if (ObjectUtil.isNotEmpty(tbSysDragRuleChainList)) {
tbSysDragChainMap = tbSysDragRuleChainList.stream().collect(Collectors.toMap(
TbSysDragChain::getDragRuleChainId,
Function.identity(),
(k1, k2) -> k2
));
}
Map<String, TbSysDragChain> finalTbSysDragChainMap = tbSysDragChainMap;
List<DragChainDTO> displayDragChainDTOList = cacheDragChainMap
.entrySet()
.stream()
.map(entry -> {
DragChainDTO localDragChainDTO = entry.getValue();
TbSysDragChain tbSysDragChain = finalTbSysDragChainMap.get(localDragChainDTO.getDragRuleChainId());
if (ObjectUtil.isNotNull(tbSysDragChain)) {
localDragChainDTO.setIfLoadDB(RuleConstant.NUM_1_STR);
localDragChainDTO.setDragRuleChain(tbSysDragChain.getDragRuleChain());
}
localDragChainDTO.setDragChainModule(dragRuleServerUtil.modelEnDescToCHDesc(localDragChainDTO.getDragChainModule()));
if (StrUtil.isNotBlank(dragChainDTO.getProductCd())) {
QueryWrapper<TbSysProductDragChain> productDragChainQueryWrapper = new QueryWrapper<>();
productDragChainQueryWrapper.lambda().eq(TbSysProductDragChain::getProductCd, dragChainDTO.getProductCd())
.eq(TbSysProductDragChain::getUri, localDragChainDTO.getUri());
TbSysProductDragChain tbSysProductDragChain = productDragChainMapper.selectOne(productDragChainQueryWrapper);
if (ObjectUtil.isNotNull(tbSysProductDragChain)) {
localDragChainDTO.setDragProductRuleChain(tbSysProductDragChain.getDragRuleChain());
}
}
return localDragChainDTO;
})
.collect(Collectors.toList());
PageBean<DragChainDTO> pageBean = customPageUtil.setFlowListPage(
displayDragChainDTOList, PaginationContext.getPageNum(), PaginationContext.getPageSize());
return pageBean;
}
@Override
public PageBean<DragChainDTO> getDragProductChainPage(DragChainDTO dragChainDTO) {
Page<DragChainDTO> page = new Page<>(PaginationContext.getPageNum(), PaginationContext.getPageSize());
IPage<DragChainDTO> dragChainIPage = productDragChainMapper.selectDragProductPage(page, dragChainDTO);
PageBean<DragChainDTO> pageBean = new PageBean<>(dragChainIPage);
List<DragChainDTO> dragChainDTOList = pageBean.getResult();
List<DragChainDTO> convertResult = dragChainDTOList.stream().map(chainDTO -> {
String dragModuleCHN = dragRuleServerUtil.modelEnDescToCHDesc(chainDTO.getUri());
chainDTO.setDragChainModule(dragModuleCHN);
return chainDTO;
}).collect(Collectors.toList());
pageBean.setResult(convertResult);
return pageBean;
}
@Override
public void deleteChainByUri(String uri) {
QueryWrapper<TbSysProductDragChain> productDragChainQueryWrapper = new QueryWrapper<>();
productDragChainQueryWrapper.lambda().eq(TbSysProductDragChain::getUri, uri);
List<TbSysProductDragChain> productDragChainList = productDragChainMapper.selectList(productDragChainQueryWrapper);
if (ObjectUtil.isNotEmpty(productDragChainList)) {
List<String> productCdList = productDragChainList.stream().map(
TbSysProductDragChain::getProductCd
).collect(Collectors.toList());
QueryWrapper<TbSysProduct> productQueryWrapper = new QueryWrapper<>();
productQueryWrapper.lambda().in(TbSysProduct::getProductCd, productCdList);
List<TbSysProduct> productList = tbSysProductMapper.selectList(productQueryWrapper);
String errorMessage = productList.stream().map(product ->
StrUtil.format("[{},{}]", product.getProductCd(), product.getProductName())
).collect(Collectors.joining());
errorMessage = "当前产品规则链对应".concat(errorMessage).concat("下有配置产品规则,请先删除产品规则再删除当前默认规则链!");
throw new ServiceException(errorMessage);
}
Object dragRuleChainObj = redisUtil.hget(CommonDragRuleConstant.DRAG_RULE_CHAIN_FLAG, uri);
if (ObjectUtil.isNull(dragRuleChainObj)) {
throw new ServiceException(StrUtil.format("当前缓存中没有uri[{}]规则链信息,删除失败!", uri));
}
DragChainDTO dragChainDTO = JSONObject.parseObject(dragRuleChainObj.toString(), DragChainDTO.class);
dragChainDTO.setIfLoadDB(RuleConstant.NUM_0_STR);
dragChainDTO.setDragRuleChain(null);
UpdateWrapper<TbSysDragChain> chainUpdateWrapper = new UpdateWrapper<>();
chainUpdateWrapper.lambda().eq(TbSysDragChain::getUri, uri);
tbSysDragChainMapper.delete(chainUpdateWrapper);
redisUtil.hset(CommonDragRuleConstant.DRAG_RULE_CHAIN_FLAG, uri, JSONObject.toJSONString(dragChainDTO));
}
@Override
public void deleteProductChainByUriAndProductCd(String uri, String productCd) {
String delKey = uri.concat(StrUtil.COLON).concat(productCd);
Object productDragChainObj = redisUtil.hget(CommonDragRuleConstant.DRAG_RULE_PRODUCT_CHAIN_FLAG, delKey);
if (ObjectUtil.isNull(productDragChainObj)) {
throw new ServiceException(StrUtil.format("当前缓存中没有uri[{}]规则链信息,删除失败!", uri));
}
UpdateWrapper<TbSysProductDragChain> chainUpdateWrapper = new UpdateWrapper<>();
chainUpdateWrapper.lambda().eq(TbSysProductDragChain::getUri, uri)
.eq(TbSysProductDragChain::getProductCd, productCd);
productDragChainMapper.delete(chainUpdateWrapper);
redisUtil.hdel(CommonDragRuleConstant.DRAG_RULE_PRODUCT_CHAIN_FLAG, delKey);
}
@Override
public void deleteProductChainByUri(String uri) {
QueryWrapper<TbSysProductDragChain> productDragChainQueryWrapper = new QueryWrapper<>();
productDragChainQueryWrapper.lambda().eq(TbSysProductDragChain::getUri, uri);
List<TbSysProductDragChain> productDragChainList = productDragChainMapper.selectList(productDragChainQueryWrapper);
if (ObjectUtil.isEmpty(productDragChainList)) {
throw new ServiceException(StrUtil.format("通过uri[{}]获取产品规则链信息为空,无删除数据!"));
}
productDragChainMapper.delete(productDragChainQueryWrapper);
List<String> productCdList = productDragChainList.stream().map(
TbSysProductDragChain::getProductCd
).collect(Collectors.toList());
productCdList.forEach(productCd -> {
String delKey = uri.concat(StrUtil.COLON).concat(productCd);
redisUtil.hdel(CommonDragRuleConstant.DRAG_RULE_PRODUCT_CHAIN_FLAG, delKey);
});
}
@Override
public DragRuleChainAllParamDTO checkDragRuleChainAllParamDesc(DragRuleChainAllParamDTO allParamDTO) {
String uri = allParamDTO.getUri();
DragChainDTO cacheDragChainDTO = null;
if (StrUtil.isNotBlank(uri)) {
Object dragRuleChainObj = redisUtil.hget(CommonDragRuleConstant.DRAG_RULE_CHAIN_FLAG, uri);
if (ObjectUtil.isNull(dragRuleChainObj)) {
throw new ServiceException(StrUtil.format("当前缓存中没有uri[{}]规则链信息,请确认是否缓存中没有数据!", uri));
}
cacheDragChainDTO = JSONObject.parseObject(dragRuleChainObj.toString(), DragChainDTO.class);
allParamDTO.setDragRuleChainInParamDescList(cacheDragChainDTO.getInVOParamsDescList());
}
if (StrUtil.isNotBlank(allParamDTO.getDragRuleChain())) {
cacheDragChainDTO.setDragRuleChain(allParamDTO.getDragRuleChain());
DragRuleChainAllParamDescOutVO.DragIllegalRule dragIllegalRule = checkIllegalParam(cacheDragChainDTO, allParamDTO);
allParamDTO.setDragIllegalRule(dragIllegalRule);
}
return allParamDTO;
}
@Override
public String getDragSequenceName(DragRuleSequenceEnum sequenceEnum) {
Double bizIndex = redisUtil.hincr(RuleConstant.DRAG_BIZ_SEQUENCE,
sequenceEnum.getModule(),
SequenceConstant.SEQUENCE_DEFAULT_STEP);
String sequenceStr = StrUtil.toString(bizIndex.intValue());
String subBasicSeqNo = StrUtil.sub(
RuleConstant.DRAG_SEQUENCE_INIT_VAL,
SequenceConstant.FLAG_INT_0,
RuleConstant.NUM_4 - sequenceStr.length());
String finalSequenceTran = sequenceEnum.getModuleAlias()
.concat(subBasicSeqNo)
.concat(sequenceStr)
.concat(RuleConstant.TRAN_SUFFIX);
return finalSequenceTran;
}
public DragRuleChainAllParamDescOutVO.DragIllegalRule checkIllegalParam(DragChainDTO dragChainDTO,
DragRuleChainAllParamDTO allParamDTO) {
DragRuleChainAllParamDescOutVO.DragIllegalRule illegalRule = null;
List<DragRuleDTO> dragRuleList = new ArrayList<>();
String dragRuleChain = dragChainDTO.getDragRuleChain();
List<String> dragRuleChainList = Arrays.asList(dragRuleChain.split(StrUtil.COMMA).clone())
.stream()
.map(String::trim)
.collect(Collectors.toList());
String errorMessage;
if (dragRuleChainList.size() == 1) {
String dragRuleId = dragRuleChainList.get(0);
DragRuleDTO dragRuleDTO = commonDragRuleUtil.getDragRuleDTO(dragRuleId);
if (ObjectUtil.isNull(dragRuleDTO)) {
throw new ServiceException(StrUtil.format("通过规则id[{}]获取规则信息为空,请确认", dragRuleId));
}
JSONObject dragRuleInParam = JSONObject.parseObject(dragRuleDTO.getInputParam());
JSONObject dragChainInParam = JSONObject.parseObject(dragChainDTO.getMethodInParam());
if (ObjectUtil.isEmpty(dragRuleInParam)) {
errorMessage = StrUtil.format("拖拽规则规则链[{}]对应规则方法[{}]获取规则入参参数为空,规则id为[{}]请确认!",
dragChainDTO.getControllerName(),
dragChainDTO.getMethodName(),
dragRuleDTO.getDragRuleId());
throw new ServiceException(errorMessage);
}
if (ObjectUtil.isEmpty(dragChainInParam)) {
errorMessage = StrUtil.format("拖拽规则规则链[{}]切入点[{}]入参参数为空,请确认!",
dragChainDTO.getControllerName(),
dragChainDTO.getMethodName());
throw new ServiceException(errorMessage);
}
dragRuleList.add(dragRuleDTO);
Set<String> dragRuleInParamKeySet = dragRuleInParam.keySet();
Set<String> dragChainInParamKeySet = dragChainInParam.keySet();
Set<String> diffKeySet = dragRuleInParamKeySet.stream()
.filter(element -> !dragChainInParamKeySet.contains(element))
.collect(Collectors.toSet());
if (ObjectUtil.isNotEmpty(diffKeySet)) {
String dragRuleDiffKeyStr = diffKeySet.stream()
.map(String::valueOf)
.collect(Collectors.joining(StrUtil.COMMA));
errorMessage = StrUtil.format("规则链id[{}],规则包含列表第[1]个规则[{}],其入参参数不满足要求!\n缺少入参信息为: [{}]",
dragChainDTO.getDragRuleChainId(),
dragRuleDTO.getDragRuleId(),
dragRuleDiffKeyStr);
illegalRule = new DragRuleChainAllParamDescOutVO.DragIllegalRule();
illegalRule.setDragRuleId(dragRuleDTO.getDragRuleId());
illegalRule.setErrorMessage(errorMessage);
illegalRule.setRuleDesc(dragRuleDTO.getRuleDesc());
illegalRule.setInVOParamsDescList(dragRuleDTO.getInputParamDescList());
List<DragRuleDTO.DragRuleParamsDesc> differParamList = dragRuleDTO.getInputParamDescList().stream()
.filter(element -> diffKeySet.contains(element.getParamName()))
.collect(Collectors.toList());
illegalRule.setIllegalParamDescList(differParamList);
}
} else {
JSONObject dragChainInParam = JSONObject.parseObject(dragChainDTO.getMethodInParam());
if (ObjectUtil.isEmpty(dragChainInParam)) {
errorMessage = StrUtil.format("拖拽规则规则链[{}]控制层方法切入点[{}]入参参数为空,请确认!",
dragChainDTO.getControllerName(),
dragChainDTO.getMethodName());
throw new ServiceException(errorMessage);
}
JSONObject resultInParam = dragChainInParam.clone();
for (int i = 0; i < dragRuleChainList.size(); i++) {
String dragRuleId = dragRuleChainList.get(i);
DragRuleDTO dragRuleDTO = commonDragRuleUtil.getDragRuleDTO(dragRuleId);
if (ObjectUtil.isNull(dragRuleDTO)) {
throw new ServiceException(StrUtil.format("通过规则id[{}]获取规则信息为空,请确认", dragRuleId));
}
dragRuleList.add(dragRuleDTO);
JSONObject dragRuleInParam = JSONObject.parseObject(dragRuleDTO.getInputParam());
Set<String> dragRuleInParamKeySet = dragRuleInParam.keySet();
Set<String> dragChainInParamKeySet = resultInParam.keySet();
Set<String> diffKeySet = dragRuleInParamKeySet.stream()
.filter(element -> !dragChainInParamKeySet.contains(element))
.collect(Collectors.toSet());
if (ObjectUtil.isNotEmpty(diffKeySet)) {
String dragRuleDiffKeyStr = diffKeySet.stream()
.map(String::valueOf)
.collect(Collectors.joining(StrUtil.COMMA));
errorMessage = StrUtil.format("规则链id[{}],规则包含列表第[{}]个规则[{}],其入参参数不满足要求!\n缺少入参信息为: [{}]",
dragChainDTO.getDragRuleChainId(),
i + 1,
dragRuleDTO.getDragRuleId(),
dragRuleDiffKeyStr);
illegalRule = new DragRuleChainAllParamDescOutVO.DragIllegalRule();
illegalRule.setDragRuleId(dragRuleDTO.getDragRuleId());
illegalRule.setErrorMessage(errorMessage);
illegalRule.setRuleDesc(dragRuleDTO.getRuleDesc());
illegalRule.setInVOParamsDescList(dragRuleDTO.getInputParamDescList());
List<DragRuleDTO.DragRuleParamsDesc> differParamList = dragRuleDTO.getInputParamDescList().stream()
.filter(element -> diffKeySet.contains(element.getParamName()))
.collect(Collectors.toList());
illegalRule.setIllegalParamDescList(differParamList);
break;
}
if (StrUtil.isNotBlank(dragRuleDTO.getCrossParam())) {
String[] crossParams = dragRuleDTO.getCrossParam().split(StrUtil.COMMA);
for (int j = 0; j < crossParams.length; j++) {
resultInParam.put(crossParams[j], StrUtil.EMPTY);
}
}
}
}
allParamDTO.setDragRuleList(dragRuleList);
return illegalRule;
}
}