From 23855599412c4d61b38d78f0f3abd3430a48b5b1 Mon Sep 17 00:00:00 2001
From: zhangherong <571457620@qq.com>
Date: 星期三, 25 六月 2025 11:51:38 +0800
Subject: [PATCH] Merge branch 'mdc_hyjs_master'

---
 lxzn-module-flowable/src/main/java/org/jeecg/modules/flowable/service/impl/FlowInstanceServiceImpl.java |  185 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 185 insertions(+), 0 deletions(-)

diff --git a/lxzn-module-flowable/src/main/java/org/jeecg/modules/flowable/service/impl/FlowInstanceServiceImpl.java b/lxzn-module-flowable/src/main/java/org/jeecg/modules/flowable/service/impl/FlowInstanceServiceImpl.java
new file mode 100644
index 0000000..1d0581b
--- /dev/null
+++ b/lxzn-module-flowable/src/main/java/org/jeecg/modules/flowable/service/impl/FlowInstanceServiceImpl.java
@@ -0,0 +1,185 @@
+package org.jeecg.modules.flowable.service.impl;
+
+import cn.hutool.core.util.StrUtil;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.shiro.SecurityUtils;
+import org.flowable.common.engine.api.FlowableObjectNotFoundException;
+import org.flowable.engine.history.HistoricProcessInstance;
+import org.flowable.engine.runtime.ProcessInstance;
+import org.flowable.task.api.Task;
+import org.jeecg.common.api.vo.Result;
+import org.jeecg.common.system.vo.LoginUser;
+import org.jeecg.common.util.SpringContextUtils;
+import org.jeecg.modules.flowable.apithird.business.entity.FlowMyBusiness;
+import org.jeecg.modules.flowable.apithird.business.service.impl.FlowMyBusinessServiceImpl;
+import org.jeecg.modules.flowable.apithird.common.exception.CustomException;
+import org.jeecg.modules.flowable.apithird.entity.SysUser;
+import org.jeecg.modules.flowable.apithird.service.FlowCallBackServiceI;
+import org.jeecg.modules.flowable.apithird.service.IFlowThirdService;
+import org.jeecg.modules.flowable.domain.vo.FlowTaskVo;
+import org.jeecg.modules.flowable.factory.FlowServiceFactory;
+import org.jeecg.modules.flowable.service.IFlowInstanceService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * <p>宸ヤ綔娴佹祦绋嬪疄渚嬬鐞�<p>
+ *
+ */
+@Service
+@Slf4j
+public class FlowInstanceServiceImpl extends FlowServiceFactory implements IFlowInstanceService {
+    @Autowired
+    IFlowThirdService iFlowThirdService;
+    @Autowired
+    FlowMyBusinessServiceImpl flowMyBusinessService;
+
+    @Override
+    public List<Task> queryListByInstanceId(String instanceId) {
+        List<Task> list = taskService.createTaskQuery().processInstanceId(instanceId).active().list();
+        return list;
+    }
+
+    /**
+     * 缁撴潫娴佺▼瀹炰緥
+     *
+     * @param vo
+     */
+    @Override
+    public void stopProcessInstance(FlowTaskVo vo) {
+        String taskId = vo.getTaskId();
+
+    }
+
+    /**
+     * 婵�娲绘垨鎸傝捣娴佺▼瀹炰緥
+     *
+     * @param state      鐘舵��
+     * @param instanceId 娴佺▼瀹炰緥ID
+     */
+    @Override
+    public void updateState(Integer state, String instanceId) {
+
+        // 婵�娲�
+        if (state == 1) {
+            runtimeService.activateProcessInstanceById(instanceId);
+        }
+        // 鎸傝捣
+        if (state == 2) {
+            runtimeService.suspendProcessInstanceById(instanceId);
+        }
+    }
+
+    /**
+     * 鍒犻櫎娴佺▼瀹炰緥ID
+     *
+     * @param instanceId   娴佺▼瀹炰緥ID
+     * @param deleteReason 鍒犻櫎鍘熷洜
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void delete(String instanceId, String deleteReason) {
+        List<Task> task = taskService.createTaskQuery().processInstanceId(instanceId).list();
+        if (CollectionUtils.isEmpty(task)) {
+            throw new CustomException("娴佺▼鏈惎鍔ㄦ垨宸叉墽琛屽畬鎴愶紝鍙栨秷鐢宠澶辫触");
+        }
+        // 鏌ヨ鍘嗗彶鏁版嵁
+        HistoricProcessInstance historicProcessInstance = getHistoricProcessInstanceById(instanceId);
+        if (historicProcessInstance.getEndTime() != null) {
+            historyService.deleteHistoricProcessInstance(historicProcessInstance.getId());
+            return;
+        }
+        // 鍒犻櫎娴佺▼瀹炰緥
+        runtimeService.deleteProcessInstance(instanceId, deleteReason);
+        // 鍒犻櫎鍘嗗彶娴佺▼瀹炰緥
+        historyService.deleteHistoricProcessInstance(instanceId);
+        /*======================鎾ゅ洖鍒犻櫎 鍥炶皟浠ュ強鍏抽敭鏁版嵁淇濆瓨======================*/
+        LambdaQueryWrapper<FlowMyBusiness> flowMyBusinessLambdaQueryWrapper = new LambdaQueryWrapper<>();
+        flowMyBusinessLambdaQueryWrapper.eq(FlowMyBusiness::getProcessInstanceId,instanceId)
+        ;
+        //濡傛灉淇濆瓨鏁版嵁鍓嶆湭璋冪敤蹇呰皟鐨凢lowCommonService.initActBusiness鏂规硶锛屽氨浼氭湁闂
+        FlowMyBusiness business = flowMyBusinessService.getOne(flowMyBusinessLambdaQueryWrapper);
+        //璁剧疆鏁版嵁
+        String doneUsers = business.getDoneUsers();
+        SysUser sysUser = iFlowThirdService.getLoginUser();
+        // 澶勭悊杩囨祦绋嬬殑浜�
+        JSONArray doneUserList = new JSONArray();
+        if (StrUtil.isNotBlank(doneUsers)){
+            doneUserList = JSON.parseArray(doneUsers);
+        }
+        if (!doneUserList.contains(sysUser.getUsername())){
+            doneUserList.add(sysUser.getUsername());
+        }
+            business
+//                    .setActStatus(ActStatus.recall)
+                    .setTaskId("")
+                    .setTaskName("宸叉挙鍥�")
+                    .setPriority("")
+                    .setDoneUsers(doneUserList.toJSONString())
+                    .setTodoUsers("")
+            ;
+        flowMyBusinessService.updateById(business);
+        //spring瀹瑰櫒绫诲悕
+        String serviceImplName = business.getServiceImplName();
+        FlowCallBackServiceI flowCallBackService = (FlowCallBackServiceI) SpringContextUtils.getBean(serviceImplName);
+        // 娴佺▼澶勭悊瀹屽悗锛岃繘琛屽洖璋冧笟鍔″眰
+        if (flowCallBackService!=null)flowCallBackService.afterFlowHandle(business);
+
+    }
+
+    @Override
+    public void deleteByDataId(String dataId, String deleteReason) {
+        LambdaQueryWrapper<FlowMyBusiness> flowMyBusinessLambdaQueryWrapper = new LambdaQueryWrapper<>();
+        flowMyBusinessLambdaQueryWrapper.eq(FlowMyBusiness::getDataId, dataId)
+        ;
+        //濡傛灉淇濆瓨鏁版嵁鍓嶆湭璋冪敤蹇呰皟鐨凢lowCommonService.initActBusiness鏂规硶锛屽氨浼氭湁闂
+        FlowMyBusiness business = flowMyBusinessService.getOne(flowMyBusinessLambdaQueryWrapper);
+        this.delete(business.getProcessInstanceId(),deleteReason);
+    }
+
+    /**
+     * 鏍规嵁瀹炰緥ID鏌ヨ鍘嗗彶瀹炰緥鏁版嵁
+     *
+     * @param processInstanceId
+     * @return
+     */
+    @Override
+    public HistoricProcessInstance getHistoricProcessInstanceById(String processInstanceId) {
+        HistoricProcessInstance historicProcessInstance =
+                historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
+        if (Objects.isNull(historicProcessInstance)) {
+            throw new FlowableObjectNotFoundException("娴佺▼瀹炰緥涓嶅瓨鍦�: " + processInstanceId);
+        }
+        return historicProcessInstance;
+    }
+
+    /**
+     * 鏍规嵁娴佺▼瀹氫箟ID鍚姩娴佺▼瀹炰緥
+     *
+     * @param procDefId 娴佺▼瀹氫箟Id
+     * @param variables 娴佺▼鍙橀噺
+     * @return
+     */
+    @Override
+    public Result startProcessInstanceById(String procDefId, Map<String, Object> variables) {
+
+            // 璁剧疆娴佺▼鍙戣捣浜篒d鍒版祦绋嬩腑
+            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
+            String username = user.getUsername();
+//            identityService.setAuthenticatedUserId(userId.toString());
+            variables.put("initiator",username);
+            variables.put("_FLOWABLE_SKIP_EXPRESSION_ENABLED", true);
+            ProcessInstance processInstance = runtimeService.startProcessInstanceById(procDefId, variables);
+            processInstance.getProcessInstanceId();
+            return Result.OK("娴佺▼鍚姩鎴愬姛");
+    }
+}

--
Gitblit v1.9.3