houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.bpm.controller.admin.definition;
H 2
3 import cn.hutool.core.collection.CollUtil;
4 import com.iailab.framework.common.pojo.CommonResult;
5 import com.iailab.framework.common.pojo.PageResult;
6 import com.iailab.framework.common.util.collection.CollectionUtils;
7 import com.iailab.framework.common.util.io.IoUtils;
8 import com.iailab.framework.common.util.json.JsonUtils;
9 import com.iailab.framework.common.util.object.BeanUtils;
10 import com.iailab.module.bpm.controller.admin.definition.vo.model.*;
11 import com.iailab.module.bpm.convert.definition.BpmModelConvert;
12 import com.iailab.module.bpm.dal.dataobject.definition.BpmCategoryDO;
13 import com.iailab.module.bpm.dal.dataobject.definition.BpmFormDO;
14 import com.iailab.module.bpm.service.definition.BpmCategoryService;
15 import com.iailab.module.bpm.service.definition.BpmFormService;
16 import com.iailab.module.bpm.service.definition.BpmModelService;
17 import com.iailab.module.bpm.service.definition.BpmProcessDefinitionService;
18 import com.iailab.module.bpm.service.definition.dto.BpmModelMetaInfoRespDTO;
19 import io.swagger.v3.oas.annotations.Operation;
20 import io.swagger.v3.oas.annotations.Parameter;
21 import io.swagger.v3.oas.annotations.tags.Tag;
22 import org.flowable.engine.repository.Deployment;
23 import org.flowable.engine.repository.Model;
24 import org.flowable.engine.repository.ProcessDefinition;
25 import org.springframework.security.access.prepost.PreAuthorize;
26 import org.springframework.validation.annotation.Validated;
27 import org.springframework.web.bind.annotation.*;
28
29 import javax.annotation.Resource;
30 import javax.validation.Valid;
31 import java.io.IOException;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Set;
36
37 import static com.iailab.framework.common.pojo.CommonResult.success;
38 import static com.iailab.framework.common.util.collection.CollectionUtils.convertMap;
39 import static com.iailab.framework.common.util.collection.CollectionUtils.convertSet;
40
41 @Tag(name = "管理后台 - 流程模型")
42 @RestController
43 @RequestMapping("/bpm/model")
44 @Validated
45 public class BpmModelController {
46
47     @Resource
48     private BpmModelService modelService;
49     @Resource
50     private BpmFormService formService;
51     @Resource
52     private BpmCategoryService categoryService;
53     @Resource
54     private BpmProcessDefinitionService processDefinitionService;
55
56     @GetMapping("/page")
57     @Operation(summary = "获得模型分页")
58     public CommonResult<PageResult<BpmModelRespVO>> getModelPage(BpmModelPageReqVO pageVO) {
59         PageResult<Model> pageResult = modelService.getModelPage(pageVO);
60         if (CollUtil.isEmpty(pageResult.getList())) {
61             return success(PageResult.empty(pageResult.getTotal()));
62         }
63
64         // 拼接数据
65         // 获得 Form 表单
66         Set<Long> formIds = convertSet(pageResult.getList(), model -> {
67             BpmModelMetaInfoRespDTO metaInfo = JsonUtils.parseObject(model.getMetaInfo(), BpmModelMetaInfoRespDTO.class);
68             return metaInfo != null ? metaInfo.getFormId() : null;
69         });
70         Map<Long, BpmFormDO> formMap = formService.getFormMap(formIds);
71         // 获得 Category Map
72         Map<String, BpmCategoryDO> categoryMap = categoryService.getCategoryMap(
73                 convertSet(pageResult.getList(), Model::getCategory));
74         // 获得 Deployment Map
75         Set<String> deploymentIds = new HashSet<>();
76         pageResult.getList().forEach(model -> CollectionUtils.addIfNotNull(deploymentIds, model.getDeploymentId()));
77         Map<String, Deployment> deploymentMap = processDefinitionService.getDeploymentMap(deploymentIds);
78         // 获得 ProcessDefinition Map
79         List<ProcessDefinition> processDefinitions = processDefinitionService.getProcessDefinitionListByDeploymentIds(deploymentIds);
80         Map<String, ProcessDefinition> processDefinitionMap = convertMap(processDefinitions, ProcessDefinition::getDeploymentId);
81         return success(BpmModelConvert.INSTANCE.buildModelPage(pageResult, formMap, categoryMap, deploymentMap, processDefinitionMap));
82     }
83
84     @GetMapping("/get")
85     @Operation(summary = "获得模型")
86     @Parameter(name = "id", description = "编号", required = true, example = "1024")
87     @PreAuthorize("@ss.hasPermission('bpm:model:query')")
88     public CommonResult<BpmModelRespVO> getModel(@RequestParam("id") String id) {
89         Model model = modelService.getModel(id);
90         if (model == null) {
91             return null;
92         }
93         byte[] bpmnBytes = modelService.getModelBpmnXML(id);
94         return success(BpmModelConvert.INSTANCE.buildModel(model, bpmnBytes));
95     }
96
97     @PostMapping("/create")
98     @Operation(summary = "新建模型")
99     @PreAuthorize("@ss.hasPermission('bpm:model:create')")
100     public CommonResult<String> createModel(@Valid @RequestBody BpmModelCreateReqVO createRetVO) {
101         return success(modelService.createModel(createRetVO, null));
102     }
103
104     @PutMapping("/update")
105     @Operation(summary = "修改模型")
106     @PreAuthorize("@ss.hasPermission('bpm:model:update')")
107     public CommonResult<Boolean> updateModel(@Valid @RequestBody BpmModelUpdateReqVO modelVO) {
108         modelService.updateModel(modelVO);
109         return success(true);
110     }
111
112     @PostMapping("/import")
113     @Operation(summary = "导入模型")
114     @PreAuthorize("@ss.hasPermission('bpm:model:import')")
115     public CommonResult<String> importModel(@Valid BpmModeImportReqVO importReqVO) throws IOException {
116         BpmModelCreateReqVO createReqVO = BeanUtils.toBean(importReqVO, BpmModelCreateReqVO.class);
117         // 读取文件
118         String bpmnXml = IoUtils.readUtf8(importReqVO.getBpmnFile().getInputStream(), false);
119         return success(modelService.createModel(createReqVO, bpmnXml));
120     }
121
122     @PostMapping("/deploy")
123     @Operation(summary = "部署模型")
124     @Parameter(name = "id", description = "编号", required = true, example = "1024")
125     @PreAuthorize("@ss.hasPermission('bpm:model:deploy')")
126     public CommonResult<Boolean> deployModel(@RequestParam("id") String id) {
127         modelService.deployModel(id);
128         return success(true);
129     }
130
131     @PutMapping("/update-state")
132     @Operation(summary = "修改模型的状态", description = "实际更新的部署的流程定义的状态")
133     @PreAuthorize("@ss.hasPermission('bpm:model:update')")
134     public CommonResult<Boolean> updateModelState(@Valid @RequestBody BpmModelUpdateStateReqVO reqVO) {
135         modelService.updateModelState(reqVO.getId(), reqVO.getState());
136         return success(true);
137     }
138
139     @DeleteMapping("/delete")
140     @Operation(summary = "删除模型")
141     @Parameter(name = "id", description = "编号", required = true, example = "1024")
142     @PreAuthorize("@ss.hasPermission('bpm:model:delete')")
143     public CommonResult<Boolean> deleteModel(@RequestParam("id") String id) {
144         modelService.deleteModel(id);
145         return success(true);
146     }
147
148 }