wlzboy
1 天以前 08f95b2f159b56fa3bd4f4b348855989de8aa456
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.ruoyi.system.utils;
 
import com.ruoyi.system.domain.enums.TaskStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
/**
 * 新系统状态向旧系统状态转换工具类
 * 
 * @author ruoyi
 * @date 2024-01-16
 */
public class TaskStatusPushConverter {
    
    private static final Logger log = LoggerFactory.getLogger(TaskStatusPushConverter.class);
    
    /**
     * 将新系统TaskStatus转换为旧系统状态码
     * 
     * @param taskStatus 新系统任务状态枚举
     * @return 旧系统状态码,如果不需要同步则返回null
     */
    public static Integer convertToLegacyStatus(TaskStatus taskStatus) {
        if (taskStatus == null) {
            log.warn("新系统状态为空");
            return null;
        }
        
        switch (taskStatus) {
            case DEPARTING:      // 出发中
                return 4;
            case IN_PROGRESS:    // 任务中
                return 6;
            case RETURNING:      // 返程中
                return 7;
            case COMPLETED:      // 已完成
                return 8;
            case CANCELLED:      // 已取消
                return 10;
            case PENDING:        // 待处理 - 不同步
            case ARRIVED:        // 已到达 - 不同步
            default:
                log.debug("新系统状态不需要同步到旧系统: {}", taskStatus.getInfo());
                return null;
        }
    }
    
    /**
     * 判断是否需要更新旧系统状态
     * 增加防止状态倒退机制:当新系统状态落后于旧系统时,不进行推送
     * 
     * @param newStatusCode 新系统要推送的状态码
     * @param oldStatusCode 旧系统当前状态码
     * @return 是否需要更新
     */
    public static boolean shouldUpdateLegacyStatus(Integer newStatusCode, Integer oldStatusCode) {
        if (newStatusCode == null || oldStatusCode == null) {
            log.warn("状态码为空,跳过更新,新状态码: {}, 旧状态码: {}", newStatusCode, oldStatusCode);
            return false;
        }
        
        // 防止状态倒退:旧系统状态 >= 新系统要推送的状态时,不更新
        // 原因:可能是旧系统已经处理了更新的状态,而新系统由于时间差还没有同步到
        if (oldStatusCode >= newStatusCode) {
            log.info("旧系统状态({}) >= 新系统目标状态({}),跳过推送,防止状态倒退," +
                "旧系统: {} ({}), 新系统目标: {} ({})", 
                oldStatusCode, newStatusCode,
                oldStatusCode, getLegacyStatusDescription(oldStatusCode),
                newStatusCode, getLegacyStatusDescription(newStatusCode));
            return false;
        }
        
        // 特殊状态检查:已完成或已取消的任务不应该被更新为中间状态
        if (oldStatusCode >= 8) {
            // 旧系统已经是完成态(8,9,10),不应该被任何中间状态覆盖
            if (newStatusCode < 8) {
                log.warn("旧系统已是终态({})(:{}),拒绝推送中间状态({})(:{})", 
                    oldStatusCode, getLegacyStatusDescription(oldStatusCode),
                    newStatusCode, getLegacyStatusDescription(newStatusCode));
                return false;
            }
        }
        
        log.debug("允许推送状态,旧系统: {} ({}), 新系统目标: {} ({})",
            oldStatusCode, getLegacyStatusDescription(oldStatusCode),
            newStatusCode, getLegacyStatusDescription(newStatusCode));
        
        return true;
    }
    
    /**
     * 获取旧系统状态描述
     * 
     * @param statusCode 旧系统状态码
     * @return 状态描述
     */
    public static String getLegacyStatusDescription(Integer statusCode) {
        if (statusCode == null) {
            return "未知";
        }
        
        switch (statusCode) {
            case 0: return "新调度单(未下发)";
            case 1: return "完全未确认";
            case 2: return "部分已确认";
            case 3: return "未出车";
            case 4: return "已出车(去接患者途中)";
            case 5: return "已出车(等待患者)";
            case 6: return "已出车(服务中)";
            case 7: return "已送达(回程中)";
            case 8: return "已返回";
            case 9: return "跑空单,已返回";
            case 10: return "取消";
            case 11: return "审核中";
            case 12: return "审核通过";
            case 13: return "审核不通过";
            case 14: return "驻点";
            default: return "未知状态(" + statusCode + ")";
        }
    }
}