fuliqi
2025-02-14 580aab8d7f186d83409d177f4aed2fe60e01b67f
business/src/main/java/com/ycl/task/FlowableTask.java
@@ -11,6 +11,7 @@
import com.ycl.mapper.ProjectProcessMapper;
import com.ycl.service.ProcessCodingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskInfo;
import org.flowable.task.api.history.HistoricTaskInstance;
@@ -33,6 +34,7 @@
    private ProjectInfoMapper projectInfoMapper;
    @Autowired
    private ProcessCodingMapper processCodingMapper;
    /**
     * 赋码任务
     * 两个逻辑 改项目码、改节点颜色
@@ -40,59 +42,97 @@
    public void expireTask() {
        log.info("开始赋码");
        //当前正在运行的所有任务节点
        List<Task> taskList = taskService.createTaskQuery().list();
        if(CollectionUtils.isEmpty(taskList)) return;
        List<Task> taskList = taskService.createTaskQuery().active().list();
        if (CollectionUtils.isEmpty(taskList)) return;
        List<String> taskIds = taskList.stream().map(TaskInfo::getId).collect(Collectors.toList());
        //需要监控的赋码任务
        List<ProcessCoding> processCodingList = processCodingMapper.selectList(new QueryWrapper<ProcessCoding>().in("task_id",taskIds));
        List<ProcessCoding> processCodingList = processCodingMapper.selectList(new QueryWrapper<ProcessCoding>().in("task_id", taskIds));
        Map<String, ProcessCoding> taskMap = new HashMap<>();
        Map<String, Date> startTaskMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(processCodingList)) {
            //key为taskId value为本体对象
            Map<String, ProcessCoding> taskMap = processCodingList.stream().collect(Collectors.toMap(ProcessCoding::getTaskId, Function.identity()));
            taskMap = processCodingList.stream().collect(Collectors.toMap(ProcessCoding::getTaskId, Function.identity()));
            //拿到开始计时的节点集合 key:taskId value:开始时间
            Map<String, Date> startTaskMap = getStartTaskList(processCodingList);
            //提前准备接收数据的map key:流程实例id value:需要改变的颜色
            Map<String, List<String>> map = new HashMap<>();
            List<ProcessCoding> list = new ArrayList<>();
            map.put(GREEN, new ArrayList<>());
            map.put(RED, new ArrayList<>());
            map.put(YELLOW, new ArrayList<>());
            Date now = new Date();
            //遍历所有代办的节点
            for (Task task : taskList) {
                String taskId = task.getId();
                ProcessCoding processCoding = taskMap.get(taskId);
                if (processCoding == null) {
                    //不需要监控的任务节点直接改为绿色
                    List<String> processInsIds = map.get(GREEN);
                    processInsIds.add(task.getProcessInstanceId());
                    continue;
                }
                //判断是否超时
                Date startTime = startTaskMap.get(processCoding.getStartTaskId());
                Integer yellowTime = processCoding.getYellowTime();
                Integer redTime = processCoding.getRedTime();
            startTaskMap = getStartTaskList(processCodingList);
        }
        //提前准备接收数据的map key:流程实例id value:需要改变的颜色
        Map<String, List<String>> map = new HashMap<>();
        List<ProcessCoding> list = new ArrayList<>();
        map.put(GREEN, new ArrayList<>());
        map.put(RED, new ArrayList<>());
        map.put(YELLOW, new ArrayList<>());
        Date now = new Date();
        //遍历所有代办的节点
        for (Task task : taskList) {
            String taskId = task.getId();
            ProcessCoding processCoding = taskMap.get(taskId);
            if (processCoding == null) {
                //不需要监控的任务节点项目码直接改为绿色
                List<String> processInsIds = map.get(GREEN);
                processInsIds.add(task.getProcessInstanceId());
                continue;
            }
            //判断赋码统一用秒作为单位
            Date startTime = startTaskMap.get(processCoding.getStartTaskId());
            try {
                Long redTime = getTime(processCoding.getRedTime());
                Long yellowTime = getTime(processCoding.getYellowTime());
                Long overtime = getTime(processCoding.getOvertime());
                if (startTime == null) continue;
//                long durationDay = (now.getTime() - startTime.getTime()) / (1000 * 60 * 60 * 24);
                long durationDay = (now.getTime() - startTime.getTime()) / (1000 * 60);
                //节点处理时间
                long durationDay = (now.getTime() - startTime.getTime()) / 1000;
                String status = GREEN; // 默认状态为绿色
                if (redTime != null && durationDay >= redTime) {
                if (redTime != null && redTime !=0 && durationDay >= redTime) {
                    status = RED; // 如果超过红色时间阈值,则状态为红色
                } else if (yellowTime != null && durationDay >= yellowTime) {
                } else if (yellowTime != null && yellowTime !=0 && durationDay >= yellowTime) {
                    status = YELLOW; // 否则,如果超过黄色时间阈值,则状态为黄色
                }
                //处理办理期限
                String overtimeStatus = NORMAL;
                if (overtime != null && overtime !=0 && durationDay >= overtime) {
                    overtimeStatus = OVERTIME; // 如果超过办理期限
                }
                else if (overtime != null && overtime != 0 && durationDay >= (overtime - 12 * 60 * 60)) {
                    overtimeStatus = WILLOVERTIME; // 如果临期(固定超时前12小时为临期)
                }
//                else if (overtime != null && overtime != 0 && durationDay >= (overtime - 60)) {
//                    overtimeStatus = WILLOVERTIME; // 如果临期(固定超时前12小时为临期)
//                }
                List<String> processInsIds = map.get(status);
                processInsIds.add(task.getProcessInstanceId());
                processCoding.setStatus(status);
                processCoding.setOvertimeStatus(overtimeStatus);
                list.add(processCoding);
            } catch (Exception e) {
                log.error(e.getMessage(),"赋码时间格式有误");
            }
            //更新项目码
            map.forEach((key,value)-> updateProjectCoding(value, key));
            //更新节点状态 自定义的mybatis方法
            if(!CollectionUtils.isEmpty(list)) processCodingMapper.updateBatch(list);
        }
        //更新项目码
        map.forEach((key, value) -> updateProjectCoding(value, key));
        //更新节点状态 自定义的mybatis方法
        if (!CollectionUtils.isEmpty(list)) processCodingMapper.updateBatch(list);
        log.info("结束赋码");
    }
    private Long getTime(String timeStr) {
        Long time = null;
        if (StringUtils.isNotBlank(timeStr)) {
            String[] timeArr = timeStr.split("-");
            // 解析天数和小时数
            int days = Integer.parseInt(timeArr[0]);
            int hours = 0;
            if (timeArr.length > 1) {
                hours = Integer.parseInt(timeArr[1]);
            }
            time = (days * 24L + hours) * 3600L;
//            //分-秒
//            time= (days * 60L) + hours;
        }
        return time;
    }
    private Map<String, Date> getStartTaskList(List<ProcessCoding> processCodingList) {
        //查出任务计时起始节点集合
        List<String> startTaskIds = processCodingList.stream().map(ProcessCoding::getStartTaskId).collect(Collectors.toList());