什么是租户
在多个系统同时使用同一套数据库的流程引擎时,这些系统就是租户
举个例子:我现在有一个通用的流程引擎平台,现在 A,B 两个系统接入平台,那么 A 系统就只能看到租户为 A 的流程,B 只能看到租户为 B 的流程
优点
提高了流程模型的复用性
,比如我两个系统都用这一个模型,那我直接根据租户
,生成两个系统各自的流程定义
即可,而不需要两个模型
设置租户
该方式是在流程模型发布生成流程定义时候设置
repositoryService.createDeployment()
.name(modelData.getName())
.addBytes(processName, bpmnBytes)
// 设置租户
.tenantId(tenantId)
.deploy();
更换租户
/**
* 更换流程定义租户
* @param deploymentId 部署ID
* @param tenantId 新租户
* @return
*/
@PutMapping(value = "/changeProcessDefinedTenant/{deploymentId}/{tenantId}")
public String changeProcessDefinedTenant(@PathVariable(value = "deploymentId") String deploymentId, @PathVariable(value = "tenantId") String tenantId) {
repositoryService.changeDeploymentTenantId(deploymentId, tenantId);
return JsonUtil.toJSON(ErrorMsg.UPDATE_SUCCESS);
}
查询租户的定义与实例数据
tenantId
即租户
// 租户流程定义列表查询
rrepositoryService.createProcessDefinitionQuery().processDefinitionTenantId(tenantId).list()
// 租户流程实例列表查询
runtimeService.createProcessInstanceQuery().processInstanceTenantId(tenantId).list()
如 Key 为 a 的流程定义,就是由同一个模型生成的
批量查询租户列表
租户毕竟是系统,如果我系统使用的是公共的一套用户,这时一个用户就有对应多个系统的情况,那么查就要一次查多个租户的数据
可惜,官方并没有给出租户相关数据的 IN 查询,只能自己写了,下面以流程实例的查询为例,附带分页
/**
* 流程实例列表查询
* @param tenantList 租户列表
* @param page 页面
* @param limit 每页条数
* @return
*/
@PostMapping(value = "/getFlowinfo/{page}/{limit}")
public String getFlowinfo(@RequestBody List<String> tenantList, @PathVariable(name="page") Integer page, @PathVariable(name="limit") Integer limit) {
// 把租户列表转化为逗号分隔的字符串
String tenantIds = "\"" + Joiner.on("\",\"").join(tenantList) + "\"";
Integer startIndex = (page - 1) * limit;
String sql = "SELECT * FROM " + managementService.getTableName(ProcessInstance.class) +
" WHERE ID_ = PROC_INST_ID_ " +
"AND TENANT_ID_ IN ("+ tenantIds +") " +
"ORDER BY START_TIME_ DESC ";
// 获取数据总条数
Integer total = runtimeService.createNativeProcessInstanceQuery().sql(sql).list().size();
// 拼接分页
sql = sql + "LIMIT "+ startIndex +","+ limit;
// 获取当前页数据
List<ProcessInstance> processInstanceList = runtimeService.createNativeProcessInstanceQuery().sql(sql).list();
List<ProcessInstanceVo> list = new ArrayList<>();
processInstanceList.forEach(processInstance -> list.add(new ProcessInstanceVo(processInstance)));
// 分页数据组装,返回前台
PageEntity pageEntity = new PageEntity(page, limit, total);
pageEntity.setData(list);
return JsonUtil.toJSON(ErrorMsg.SUCCESS.setNewData(pageEntity));
}
ProcessInstanceVo
/**
* ProcessInstanceVo
* @author: linjinp
* @create: 2020-01-02 14:15
**/
@Data
public class ProcessInstanceVo {
private String id;
private String processDefinitionKey;
private String processDefinitionId;
private String businessKey;
private String processDefinitionName;
private Date startTime;
private String deploymentId;
private String description;
private String name;
private Integer processDefinitionVersion;
private Map<String, Object> processVariables;
private String startUserId;
private String callbackId;
private String callbackType;
private String tenantId;
private String localizedDescription;
private String localizedName;
private String processInstanceId;
private String activityId;
private String parentId;
private String rootProcessInstanceId;
private String superExecutionId;
private Boolean isSuspended;
private Boolean isEnded;
public ProcessInstanceVo(){}
public ProcessInstanceVo(ProcessInstance processInstance){
id = processInstance.getId();
processDefinitionKey = processInstance.getProcessDefinitionKey();
processDefinitionId = processInstance.getProcessDefinitionId();
businessKey = processInstance.getBusinessKey();
processDefinitionName = processInstance.getProcessDefinitionName();
startTime = processInstance.getStartTime();
deploymentId = processInstance.getDeploymentId();
description = processInstance.getDescription();
name = processInstance.getName();
processDefinitionVersion = processInstance.getProcessDefinitionVersion();
processVariables = processInstance.getProcessVariables();
startUserId = processInstance.getStartUserId();
callbackId = processInstance.getCallbackId();
callbackType = processInstance.getCallbackType();
tenantId = processInstance.getTenantId();
localizedDescription = processInstance.getLocalizedDescription();
localizedName = processInstance.getLocalizedName();
processInstanceId = processInstance.getProcessInstanceId();
activityId = processInstance.getActivityId();
parentId = processInstance.getParentId();
rootProcessInstanceId = processInstance.getRootProcessInstanceId();
superExecutionId = processInstance.getSuperExecutionId();
isSuspended = processInstance.isSuspended();
isEnded = processInstance.isEnded();
}
}
转载:https://blog.csdn.net/qq_37143673/article/details/103985374
查看评论