Browse Source

超时提醒申请人功能

dzx 7 months ago
parent
commit
f64bffb49c

+ 128 - 0
src/main/java/com/xjrsoft/module/job/ProcessTimeoutAlertTask.java

@@ -0,0 +1,128 @@
+package com.xjrsoft.module.job;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import com.xjrsoft.common.mybatis.SqlRunnerAdapter;
+import com.xjrsoft.module.oa.utils.SendMessageUtil;
+import com.xjrsoft.module.workflow.entity.WorkflowAlertRecord;
+import com.xjrsoft.module.workflow.entity.WorkflowSchema;
+import com.xjrsoft.module.workflow.service.IWorkflowAlertRecordService;
+import com.xjrsoft.module.workflow.service.IWorkflowSchemaService;
+import lombok.extern.slf4j.Slf4j;
+import org.camunda.bpm.engine.HistoryService;
+import org.camunda.bpm.engine.history.HistoricProcessInstance;
+import org.camunda.bpm.engine.history.HistoricProcessInstanceQuery;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import java.time.Duration;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.stream.Collectors;
+
+/**
+ * 流程未处理提醒申请人
+ */
+@Component
+@Slf4j
+public class ProcessTimeoutAlertTask {
+
+    @Autowired
+    private HistoryService historyService;
+
+    @Autowired
+    private IWorkflowSchemaService schemaService;
+
+    @Autowired
+    private IWorkflowAlertRecordService alertRecordService;
+    @Scheduled(cron = "0 0 * * * ?")
+    public void execute() {
+        doExecute();
+    }
+
+    public void doExecute(){
+        LocalDateTime now = LocalDateTime.now();
+        try {
+            HistoricProcessInstanceQuery historicProcessInstanceQuery = historyService.createHistoricProcessInstanceQuery().unfinished();
+
+            List<HistoricProcessInstance> historicProcessInstances = historicProcessInstanceQuery.list();
+
+            List<String> processIds = historicProcessInstances.stream().map(HistoricProcessInstance::getId).collect(Collectors.toList());
+
+            if(!processIds.isEmpty()){
+                //获取所有未完成流程的所有节点
+                String uncloseProcessIdStr = processIds.stream()
+                        .map(entity -> "'" + entity + "'")
+                        .collect(Collectors.joining(","));
+                String listWorkflowExtraSql = "select * " +
+                        "from xjr_workflow_extra "
+                        + "where process_id in (" + uncloseProcessIdStr + ") and end_time is null;";
+                List<Map<String, Object>> workflowExtraList = SqlRunnerAdapter.db().selectList(listWorkflowExtraSql);
+
+                JsonParser parser = new JsonParser();
+                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+                Map<String, StringBuilder> message = new HashMap<>();
+                List<WorkflowAlertRecord> recordList = new ArrayList<>();
+                for (Map<String, Object> el : workflowExtraList) {
+                    WorkflowSchema schema = schemaService.getById(el.get("schema_id").toString());
+                    JsonObject jsonContent = parser.parse(schema.getJsonContent()).getAsJsonObject();
+                    JsonObject timeoutRemidConfig = jsonContent.get("processConfig").getAsJsonObject().get("timeoutRemidConfig").getAsJsonObject();
+                    if(!timeoutRemidConfig.get("enabled").getAsBoolean()){
+                        continue;
+                    }
+                    String startUserId = el.get("start_user_id").toString();
+                    List<WorkflowAlertRecord> record = alertRecordService.list(
+                            new QueryWrapper<WorkflowAlertRecord>().lambda()
+                                    .eq(WorkflowAlertRecord::getUserId, Long.parseLong(startUserId))
+                                    .eq(WorkflowAlertRecord::getProcessId, Long.parseLong(el.get("process_id").toString()))
+                                    .eq(WorkflowAlertRecord::getType, 1)
+                                    .orderByDesc(WorkflowAlertRecord::getSendTime)
+                    );
+                    WorkflowAlertRecord alertRecord = record.get(0);
+                    Duration lastBetween = Duration.between(alertRecord.getSendTime(), now);
+                    long lasttime = Math.abs(lastBetween.getSeconds());
+
+                    LocalDateTime startTime = LocalDateTime.parse(el.get("start_time").toString(), formatter);
+                    Duration between = Duration.between(startTime, now);
+                    long abs = Math.abs(between.getSeconds());
+                    int hour = timeoutRemidConfig.get("hour").getAsInt() * 60 * 60;
+                    int interval = timeoutRemidConfig.get("interval").getAsInt() * 60 * 60;
+                    if(abs >= hour && lasttime >= interval){
+                        //对未结束的流程节点进行处理,通知下一个节点需要处理的人
+
+                        if(message.containsKey(startUserId)){
+                            message.get(startUserId).append(",").append(el.get("process_name").toString());
+                        }else {
+                            message.put(startUserId, new StringBuilder(el.get("process_name").toString()));
+                        }
+                        recordList.add(
+                                new WorkflowAlertRecord(){{
+                                    setProcessId(Long.parseLong(el.get("process_id").toString()));
+                                    setUserId(Long.parseLong(startUserId));
+                                    setProcessName(el.get("process_name").toString());
+                                    setType(1);
+                                    setSendTime(now);
+                                }}
+                        );
+                    }
+                }
+                if(!recordList.isEmpty()){
+                    alertRecordService.saveBatch(recordList);
+                }
+
+                CompletableFuture.runAsync(() -> {
+                    SendMessageUtil.sendWorkflowUnapprovedWx(message);
+                });
+            }
+        } catch (Exception e) {
+            log.error(e.getMessage(), "定时提醒流程未处理");
+        }
+    }
+}

+ 44 - 0
src/main/java/com/xjrsoft/module/workflow/entity/WorkflowAlertRecord.java

@@ -0,0 +1,44 @@
+package com.xjrsoft.module.workflow.entity;
+
+import com.baomidou.mybatisplus.annotation.TableName;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.time.LocalDateTime;
+
+/**
+ * <p>
+ *  流程提醒记录表
+ * </p>
+ *
+ * @author tzx
+ * @since 2022-10-11
+ */
+@TableName("xjr_workflow_alert_record")
+@ApiModel(value = "WorkflowAlertRecord对象", description = "")
+@Data
+public class WorkflowAlertRecord implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @ApiModelProperty("id")
+    private Long id;
+
+    @ApiModelProperty("提醒时间")
+    private LocalDateTime sendTime;
+
+    @ApiModelProperty("被提醒人id")
+    private Long userId;
+
+    @ApiModelProperty("流程名称")
+    private String processName;
+
+    @ApiModelProperty("进程id")
+    private Long processId;
+
+    @ApiModelProperty("提醒类别(1:超时提醒...)")
+    private Integer type;
+
+}

+ 20 - 0
src/main/java/com/xjrsoft/module/workflow/mapper/WorkflowAlertRecordMapper.java

@@ -0,0 +1,20 @@
+package com.xjrsoft.module.workflow.mapper;
+
+import com.github.yulichang.base.MPJBaseMapper;
+import com.xjrsoft.module.workflow.entity.WorkflowAlertRecord;
+import com.xjrsoft.module.workflow.entity.WorkflowExtra;
+import org.apache.ibatis.annotations.Delete;
+import org.apache.ibatis.annotations.Mapper;
+
+/**
+ * <p>
+ *  Mapper 接口
+ * </p>
+ *
+ * @author tzx
+ * @since 2022-10-11
+ */
+@Mapper
+public interface WorkflowAlertRecordMapper extends MPJBaseMapper<WorkflowAlertRecord> {
+
+}

+ 17 - 0
src/main/java/com/xjrsoft/module/workflow/service/IWorkflowAlertRecordService.java

@@ -0,0 +1,17 @@
+package com.xjrsoft.module.workflow.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.xjrsoft.module.workflow.entity.WorkflowAlertRecord;
+import com.xjrsoft.module.workflow.entity.WorkflowExtra;
+
+/**
+ * <p>
+ *  服务类
+ * </p>
+ *
+ * @author tzx
+ * @since 2022-10-11
+ */
+public interface IWorkflowAlertRecordService extends IService<WorkflowAlertRecord> {
+
+}

+ 20 - 0
src/main/java/com/xjrsoft/module/workflow/service/impl/WorkflowAlertRecordServiceImpl.java

@@ -0,0 +1,20 @@
+package com.xjrsoft.module.workflow.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.xjrsoft.module.workflow.entity.WorkflowAlertRecord;
+import com.xjrsoft.module.workflow.mapper.WorkflowAlertRecordMapper;
+import com.xjrsoft.module.workflow.service.IWorkflowAlertRecordService;
+import org.springframework.stereotype.Service;
+
+/**
+ * <p>
+ *  服务实现类
+ * </p>
+ *
+ * @author tzx
+ * @since 2022-10-11
+ */
+@Service
+public class WorkflowAlertRecordServiceImpl extends ServiceImpl<WorkflowAlertRecordMapper, WorkflowAlertRecord> implements IWorkflowAlertRecordService {
+
+}