liulijun
6 天以前 4cf43750c41e1833143b557ecbbf6844656f49d6
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// WCS系统任务异常监控服务
import http from '../api/http';
 
// WCS任务异常监控服务类
class WcsTaskMonitorService {
  constructor() {
    this.checkTaskStatusTimer = null;
    this.apiTaskData = []; // 存储通过API获取的任务数据
    this.apiRefreshTimer = null; // API数据刷新定时器
    this.apiRefreshInterval = 5000; // API数据刷新间隔,5秒
  }
 
  // 初始化WCS任务异常监控服务
  init(app) {
    // 设置定时器,每5秒检查一次
    this.checkTaskStatusTimer = setInterval(() => {
      this.checkTaskExceptions();
    }, 5000);
 
    // 设置API数据刷新定时器,每5秒刷新一次任务数据
    this.apiRefreshTimer = setInterval(() => {
      this.refreshTaskDataFromApi();
    }, this.apiRefreshInterval);
 
    // 初始刷新一次任务数据
    this.refreshTaskDataFromApi();
 
    // 保存定时器引用,方便后续清理
    if (app && app.config) {
      app.config.globalProperties.$global.wcsTaskMonitorTimer = this.checkTaskStatusTimer;
      app.config.globalProperties.$global.wcsTaskMonitorApiTimer = this.apiRefreshTimer;
    }
 
    // 方便手动测试,将check方法挂载到window对象
    window.testWcsTaskMonitor = () => {
      this.checkTaskExceptions();
    };
  }
 
  // 从API刷新任务数据
  async refreshTaskDataFromApi() {
    try {
      // 调用WCS专门的API获取异常任务数据
      const apiData = await http.post('http://localhost:9291/api/Task/GetTasksWithException', {});
 
      if (apiData && apiData.data) {
        this.apiTaskData = apiData.data;
      } else {
        this.apiTaskData = [];
      }
    } catch (error) {
      console.error('从API获取异常任务数据失败:', error);
      this.apiTaskData = [];
    }
  }
 
  // 检查任务异常
  checkTaskExceptions() {
    // 直接使用从API获取的异常任务数据
    const exceptionTasks = this.apiTaskData || [];
 
    // 去重,确保每个任务只处理一次
    const uniqueExceptionTasks = [];
    const taskNumSet = new Set();
    
    exceptionTasks.forEach(task => {
      if (task && task.taskNum && !taskNumSet.has(task.taskNum)) {
        uniqueExceptionTasks.push(task);
        taskNumSet.add(task.taskNum);
      }
    });
 
    // 获取全局对象
    const globalObj = window.$global || {};
 
    // 处理消息删除逻辑,重置对应任务的异常标记
    const deletedMessages = [...(globalObj.messageDeleted || [])];
 
    if (deletedMessages.length > 0) {
      // 遍历被删除的消息ID,重置对应任务的异常标记
      deletedMessages.forEach(deletedId => {
        // 直接遍历所有已处理的异常任务
        // 因为消息可能已经被从messageList中删除
        for (const taskNum of this.processedExceptions) {
          // 这里不依赖messageList,直接重置所有可能的任务
          // 确保删除后能重新发送消息
          this.processedExceptions.delete(taskNum);
        }
      });
      
      // 清空已处理的删除消息列表,避免重复处理
      globalObj.messageDeleted = [];
    }
 
    // 处理异常任务
    uniqueExceptionTasks.forEach(task => {
      // 不管是否已经处理过,只要任务在异常列表中,就发送消息
      // 这样确保删除后能重新发送消息
      this.sendTaskExceptionMessage(task);
      // 不需要添加到processedExceptions,因为我们不再使用它来阻止重发
    });
 
    // 清除已解决的任务异常消息
    this.clearResolvedTaskExceptionMessages(uniqueExceptionTasks);
  }
 
  // 清除已解决的任务异常消息
  clearResolvedTaskExceptionMessages(currentExceptionTasks) {
    // 获取全局对象
    const globalObj = window.$global || {};
    
    // 获取全局消息列表
    const globalMessageList = [...(globalObj.messageList || [])];
    
    // 找出需要清除的任务异常消息
    const taskExceptionMessages = globalMessageList.filter(msg => msg.businessType === 'wcs_task_exception');
    
    // 构建当前异常任务集合
    const currentExceptionTaskNums = new Set();
    currentExceptionTasks.forEach(task => {
      if (task && task.taskNum) {
        currentExceptionTaskNums.add(task.taskNum);
      }
    });
    
    // 遍历所有任务异常消息
    taskExceptionMessages.forEach(msg => {
      const taskNum = msg.taskNum;
      // 检查任务是否不在当前异常任务列表中(说明异常已解决)
      if (taskNum && !currentExceptionTaskNums.has(taskNum)) {
        // 任务异常已解决,清除该消息
        if (globalObj.messageList && Array.isArray(globalObj.messageList)) {
          const index = globalObj.messageList.findIndex(m => m.id === msg.id);
          if (index !== -1) {
            globalObj.messageList.splice(index, 1);
          }
        }
      }
    });
  }
 
  // 发送任务异常消息
  sendTaskExceptionMessage(task) {
    // 创建唯一的消息ID
    const messageId = Date.now() + Math.random().toString(36).substr(2, 9);
    
    // 获取异常描述,确保不为空
    const exceptionDescription = task.ExceptionMessage || '未知异常';
 
    // 创建异常消息
    const exceptionMessage = {
      id: messageId,
      title: '任务异常警告',
      message: `任务号 ${task.taskNum} 出现异常:${exceptionDescription},请及时处理!`,
      type: 'error',
      businessType: 'wcs_task_exception',
      taskNum: task.taskNum,
      createTime: new Date().toLocaleString(),
      relatedData: {
        TaskNum: task.taskNum,
        ExceptionMessage: exceptionDescription,
        CreateDate: task.createDate
      }
    };
 
    // 获取全局对象
    const globalObj = window.$global || {};
 
    // 检查是否已经存在相同的任务异常警告
    const globalMessageList = globalObj.messageList || [];
 
    const hasExistingWarning = globalMessageList.some(msg =>
      msg.businessType === 'wcs_task_exception' && msg.taskNum === task.taskNum
    );
 
    if (hasExistingWarning) return;
 
    // 发送消息到全局消息列表
    try {
      if (globalObj.messageList) {
        globalObj.messageList.push(exceptionMessage);
      }
    } catch (error) {
      // 出错时使用浏览器原生alert作为最终备选
      try {
        alert(`任务异常警告: 任务号 ${task.taskNum} 出现异常:${exceptionDescription},请及时处理!`);
      } catch (e) {
        // 忽略所有错误
      }
    }
  }
 
  // 手动检查任务异常(用于外部调用)
  manualCheckTaskExceptions() {
    this.checkTaskExceptions();
  }
 
  // 清理资源
  cleanup() {
    if (this.checkTaskStatusTimer) {
      clearInterval(this.checkTaskStatusTimer);
      this.checkTaskStatusTimer = null;
    }
    
    if (this.apiRefreshTimer) {
      clearInterval(this.apiRefreshTimer);
      this.apiRefreshTimer = null;
    }
    
    this.apiTaskData = [];
    delete window.testWcsTaskMonitor;
  }
}
 
// 创建单例实例
const wcsTaskMonitorService = new WcsTaskMonitorService();
 
export default wcsTaskMonitorService;