package com.iailab.module.bpm.controller.admin.task; import cn.hutool.core.collection.CollUtil; import com.iailab.framework.common.pojo.CommonResult; import com.iailab.framework.common.pojo.PageResult; import com.iailab.framework.common.util.number.NumberUtils; import com.iailab.module.bpm.controller.admin.task.vo.instance.*; import com.iailab.module.bpm.convert.task.BpmProcessInstanceConvert; import com.iailab.module.bpm.dal.dataobject.definition.BpmCategoryDO; import com.iailab.module.bpm.dal.dataobject.definition.BpmProcessDefinitionInfoDO; import com.iailab.module.bpm.service.definition.BpmCategoryService; import com.iailab.module.bpm.service.definition.BpmProcessDefinitionService; import com.iailab.module.bpm.service.task.BpmProcessInstanceService; import com.iailab.module.bpm.service.task.BpmTaskService; import com.iailab.module.system.api.dept.DeptApi; import com.iailab.module.system.api.dept.dto.DeptRespDTO; import com.iailab.module.system.api.user.AdminUserApi; import com.iailab.module.system.api.user.dto.AdminUserRespDTO; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import org.flowable.engine.history.HistoricProcessInstance; import org.flowable.engine.repository.ProcessDefinition; import org.flowable.task.api.Task; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.validation.Valid; import java.util.List; import java.util.Map; import static com.iailab.framework.common.pojo.CommonResult.success; import static com.iailab.framework.common.util.collection.CollectionUtils.convertList; import static com.iailab.framework.common.util.collection.CollectionUtils.convertSet; import static com.iailab.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; @Tag(name = "管理后台 - 流程实例") // 流程实例,通过流程定义创建的一次“申请” @RestController @RequestMapping("/bpm/process-instance") @Validated public class BpmProcessInstanceController { @Resource private BpmProcessInstanceService processInstanceService; @Resource private BpmTaskService taskService; @Resource private BpmProcessDefinitionService processDefinitionService; @Resource private BpmCategoryService categoryService; @Resource private AdminUserApi adminUserApi; @Resource private DeptApi deptApi; @GetMapping("/my-page") @Operation(summary = "获得我的实例分页列表", description = "在【我的流程】菜单中,进行调用") @PreAuthorize("@ss.hasPermission('bpm:process-instance:query')") public CommonResult> getProcessInstanceMyPage( @Valid BpmProcessInstancePageReqVO pageReqVO) { PageResult pageResult = processInstanceService.getProcessInstancePage( getLoginUserId(), pageReqVO); if (CollUtil.isEmpty(pageResult.getList())) { return success(PageResult.empty(pageResult.getTotal())); } // 拼接返回 Map> taskMap = taskService.getTaskMapByProcessInstanceIds( convertList(pageResult.getList(), HistoricProcessInstance::getId)); Map processDefinitionMap = processDefinitionService.getProcessDefinitionMap( convertSet(pageResult.getList(), HistoricProcessInstance::getProcessDefinitionId)); Map categoryMap = categoryService.getCategoryMap( convertSet(processDefinitionMap.values(), ProcessDefinition::getCategory)); return success(BpmProcessInstanceConvert.INSTANCE.buildProcessInstancePage(pageResult, processDefinitionMap, categoryMap, taskMap, null, null)); } @GetMapping("/manager-page") @Operation(summary = "获得管理流程实例的分页列表", description = "在【流程实例】菜单中,进行调用") @PreAuthorize("@ss.hasPermission('bpm:process-instance:manager-query')") public CommonResult> getProcessInstanceManagerPage( @Valid BpmProcessInstancePageReqVO pageReqVO) { PageResult pageResult = processInstanceService.getProcessInstancePage( null, pageReqVO); if (CollUtil.isEmpty(pageResult.getList())) { return success(PageResult.empty(pageResult.getTotal())); } // 拼接返回 Map> taskMap = taskService.getTaskMapByProcessInstanceIds( convertList(pageResult.getList(), HistoricProcessInstance::getId)); Map processDefinitionMap = processDefinitionService.getProcessDefinitionMap( convertSet(pageResult.getList(), HistoricProcessInstance::getProcessDefinitionId)); Map categoryMap = categoryService.getCategoryMap( convertSet(processDefinitionMap.values(), ProcessDefinition::getCategory)); // 发起人信息 Map userMap = adminUserApi.getUserMap( convertSet(pageResult.getList(), processInstance -> NumberUtils.parseLong(processInstance.getStartUserId()))); Map deptMap = deptApi.getDeptMap( convertSet(userMap.values(), AdminUserRespDTO::getDeptId)); return success(BpmProcessInstanceConvert.INSTANCE.buildProcessInstancePage(pageResult, processDefinitionMap, categoryMap, taskMap, userMap, deptMap)); } @PostMapping("/create") @Operation(summary = "新建流程实例") @PreAuthorize("@ss.hasPermission('bpm:process-instance:query')") public CommonResult createProcessInstance(@Valid @RequestBody BpmProcessInstanceCreateReqVO createReqVO) { return success(processInstanceService.createProcessInstance(getLoginUserId(), createReqVO)); } @GetMapping("/get") @Operation(summary = "获得指定流程实例", description = "在【流程详细】界面中,进行调用") @Parameter(name = "id", description = "流程实例的编号", required = true) @PreAuthorize("@ss.hasPermission('bpm:process-instance:query')") public CommonResult getProcessInstance(@RequestParam("id") String id) { HistoricProcessInstance processInstance = processInstanceService.getHistoricProcessInstance(id); if (processInstance == null) { return success(null); } // 拼接返回 ProcessDefinition processDefinition = processDefinitionService.getProcessDefinition( processInstance.getProcessDefinitionId()); BpmProcessDefinitionInfoDO processDefinitionInfo = processDefinitionService.getProcessDefinitionInfo( processInstance.getProcessDefinitionId()); AdminUserRespDTO startUser = adminUserApi.getUser(NumberUtils.parseLong(processInstance.getStartUserId())).getCheckedData(); DeptRespDTO dept = null; if (startUser != null && startUser.getDeptId() != null) { dept = deptApi.getDept(startUser.getDeptId()).getCheckedData(); } return success(BpmProcessInstanceConvert.INSTANCE.buildProcessInstance(processInstance, processDefinition, processDefinitionInfo, startUser, dept)); } @DeleteMapping("/cancel-by-start-user") @Operation(summary = "用户取消流程实例", description = "取消发起的流程") @PreAuthorize("@ss.hasPermission('bpm:process-instance:cancel')") public CommonResult cancelProcessInstanceByStartUser( @Valid @RequestBody BpmProcessInstanceCancelReqVO cancelReqVO) { processInstanceService.cancelProcessInstanceByStartUser(getLoginUserId(), cancelReqVO); return success(true); } @DeleteMapping("/cancel-by-admin") @Operation(summary = "管理员取消流程实例", description = "管理员撤回流程") @PreAuthorize("@ss.hasPermission('bpm:process-instance:cancel-by-admin')") public CommonResult cancelProcessInstanceByManager( @Valid @RequestBody BpmProcessInstanceCancelReqVO cancelReqVO) { processInstanceService.cancelProcessInstanceByAdmin(getLoginUserId(), cancelReqVO); return success(true); } @GetMapping("/get-approval-detail") @Operation(summary = "获得审批详情") @Parameter(name = "id", description = "流程实例的编号", required = true) @PreAuthorize("@ss.hasPermission('bpm:process-instance:query')") public CommonResult getApprovalDetail(@Valid BpmApprovalDetailReqVO reqVO) { return success(processInstanceService.getApprovalDetail(getLoginUserId(), reqVO)); } @GetMapping("/get-bpmn-model-view") @Operation(summary = "获取流程实例的 BPMN 模型视图", description = "在【流程详细】界面中,进行调用") @Parameter(name = "id", description = "流程实例的编号", required = true) public CommonResult getProcessInstanceBpmnModelView(@RequestParam(value = "id") String id) { return success(processInstanceService.getProcessInstanceBpmnModelView(id)); } }