潘志宝
9 天以前 9d5be382e52f9ac57199d5ef75cc23f925a4cdb0
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.bpm.convert.definition;
H 2
3 import cn.hutool.core.util.ArrayUtil;
4 import com.iailab.framework.common.util.date.DateUtils;
5 import com.iailab.framework.common.util.json.JsonUtils;
6 import com.iailab.framework.common.util.object.BeanUtils;
bb2880 7 import com.iailab.module.bpm.controller.admin.base.user.UserSimpleBaseVO;
H 8 import com.iailab.module.bpm.controller.admin.definition.vo.model.BpmModelMetaInfoVO;
e7c126 9 import com.iailab.module.bpm.controller.admin.definition.vo.model.BpmModelRespVO;
bb2880 10 import com.iailab.module.bpm.controller.admin.definition.vo.model.BpmModelSaveReqVO;
e7c126 11 import com.iailab.module.bpm.controller.admin.definition.vo.process.BpmProcessDefinitionRespVO;
H 12 import com.iailab.module.bpm.dal.dataobject.definition.BpmCategoryDO;
13 import com.iailab.module.bpm.dal.dataobject.definition.BpmFormDO;
bb2880 14 import com.iailab.module.bpm.framework.flowable.core.util.BpmnModelUtils;
H 15 import com.iailab.module.system.api.user.dto.AdminUserRespDTO;
e7c126 16 import org.flowable.common.engine.impl.db.SuspensionState;
H 17 import org.flowable.engine.repository.Deployment;
18 import org.flowable.engine.repository.Model;
19 import org.flowable.engine.repository.ProcessDefinition;
20 import org.mapstruct.Mapper;
21 import org.mapstruct.factory.Mappers;
22
bb2880 23 import java.util.Collections;
H 24 import java.util.Comparator;
e7c126 25 import java.util.List;
H 26 import java.util.Map;
bb2880 27
H 28 import static com.iailab.framework.common.util.collection.CollectionUtils.convertList;
e7c126 29
H 30 /**
31  * 流程模型 Convert
32  *
bb2880 33  * @author hou
e7c126 34  */
H 35 @Mapper
36 public interface BpmModelConvert {
37
38     BpmModelConvert INSTANCE = Mappers.getMapper(BpmModelConvert.class);
39
bb2880 40     default List<BpmModelRespVO> buildModelList(List<Model> list,
H 41                                                 Map<Long, BpmFormDO> formMap,
42                                                 Map<String, BpmCategoryDO> categoryMap,
43                                                 Map<String, Deployment> deploymentMap,
44                                                 Map<String, ProcessDefinition> processDefinitionMap,
45                                                 Map<Long, AdminUserRespDTO> userMap) {
46         List<BpmModelRespVO> result = convertList(list, model -> {
47             BpmModelMetaInfoVO metaInfo = parseMetaInfo(model);
e7c126 48             BpmFormDO form = metaInfo != null ? formMap.get(metaInfo.getFormId()) : null;
H 49             BpmCategoryDO category = categoryMap.get(model.getCategory());
50             Deployment deployment = model.getDeploymentId() != null ? deploymentMap.get(model.getDeploymentId()) : null;
bb2880 51             ProcessDefinition processDefinition = model.getDeploymentId() != null ?
H 52                     processDefinitionMap.get(model.getDeploymentId()) : null;
53             List<AdminUserRespDTO> startUsers = metaInfo != null ? convertList(metaInfo.getStartUserIds(), userMap::get) : null;
54             return buildModel0(model, metaInfo, form, category, deployment, processDefinition, startUsers);
e7c126 55         });
bb2880 56         // 排序
H 57         result.sort(Comparator.comparing(BpmModelMetaInfoVO::getSort));
58         return result;
e7c126 59     }
H 60
bb2880 61     default BpmModelRespVO buildModel(Model model, byte[] bpmnBytes) {
H 62         BpmModelMetaInfoVO metaInfo = parseMetaInfo(model);
63         BpmModelRespVO modelVO = buildModel0(model, metaInfo, null, null, null, null, null);
e7c126 64         if (ArrayUtil.isNotEmpty(bpmnBytes)) {
bb2880 65             modelVO.setBpmnXml(BpmnModelUtils.getBpmnXml(bpmnBytes));
e7c126 66         }
H 67         return modelVO;
68     }
69
70     default BpmModelRespVO buildModel0(Model model,
bb2880 71                                        BpmModelMetaInfoVO metaInfo, BpmFormDO form, BpmCategoryDO category,
H 72                                        Deployment deployment, ProcessDefinition processDefinition,
73                                        List<AdminUserRespDTO> startUsers) {
e7c126 74         BpmModelRespVO modelRespVO = new BpmModelRespVO().setId(model.getId()).setName(model.getName())
H 75                 .setKey(model.getKey()).setCategory(model.getCategory())
76                 .setCreateTime(DateUtils.of(model.getCreateTime()));
77         // Form
bb2880 78         BeanUtils.copyProperties(metaInfo, modelRespVO);
e7c126 79         if (form != null) {
bb2880 80             modelRespVO.setFormName(form.getName());
e7c126 81         }
H 82         // Category
83         if (category != null) {
84             modelRespVO.setCategoryName(category.getName());
85         }
86         // ProcessDefinition
87         if (processDefinition != null) {
88             modelRespVO.setProcessDefinition(BeanUtils.toBean(processDefinition, BpmProcessDefinitionRespVO.class));
89             modelRespVO.getProcessDefinition().setSuspensionState(processDefinition.isSuspended() ?
90                     SuspensionState.SUSPENDED.getStateCode() : SuspensionState.ACTIVE.getStateCode());
91             if (deployment != null) {
92                 modelRespVO.getProcessDefinition().setDeploymentTime(DateUtils.of(deployment.getDeploymentTime()));
93             }
94         }
bb2880 95         // User
H 96         modelRespVO.setStartUsers(BeanUtils.toBean(startUsers, UserSimpleBaseVO.class));
e7c126 97         return modelRespVO;
H 98     }
99
bb2880 100     default void copyToModel(Model model, BpmModelSaveReqVO reqVO) {
H 101         model.setName(reqVO.getName());
102         model.setKey(reqVO.getKey());
103         model.setCategory(reqVO.getCategory());
104         model.setMetaInfo(JsonUtils.toJsonString(BeanUtils.toBean(reqVO, BpmModelMetaInfoVO.class)));
e7c126 105     }
H 106
bb2880 107     default BpmModelMetaInfoVO parseMetaInfo(Model model) {
H 108         BpmModelMetaInfoVO vo = JsonUtils.parseObject(model.getMetaInfo(), BpmModelMetaInfoVO.class);
109         if (vo == null) {
110             return null;
e7c126 111         }
bb2880 112         if (vo.getManagerUserIds() == null) {
H 113             vo.setManagerUserIds(Collections.emptyList());
e7c126 114         }
bb2880 115         if (vo.getStartUserIds() == null) {
H 116             vo.setStartUserIds(Collections.emptyList());
e7c126 117         }
bb2880 118         // 如果为空,兜底处理,使用 createTime 创建时间
H 119         if (vo.getSort() == null) {
120             vo.setSort(model.getCreateTime().getTime());
e7c126 121         }
bb2880 122         return vo;
e7c126 123     }
H 124
125 }