Junjie
2026-04-27 10bdc4b6e9701befd1a83bccd2998dcc96cb2c43
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package com.zy.ai.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.ai.domain.autotune.AutoTuneParameterSnapshot;
import com.zy.ai.domain.autotune.AutoTuneSnapshot;
import com.zy.ai.domain.autotune.AutoTuneStationRuntimeItem;
import com.zy.ai.domain.autotune.AutoTuneTaskSnapshot;
import com.zy.ai.service.AutoTuneSnapshotService;
import com.zy.ai.service.FlowTopologySnapshotService;
import com.zy.asrs.domain.vo.StationCycleCapacityVo;
import com.zy.asrs.domain.vo.StationCycleLoopVo;
import com.zy.asrs.entity.BasCrnp;
import com.zy.asrs.entity.BasDualCrnp;
import com.zy.asrs.entity.BasStation;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.asrs.entity.WrkMast;
import com.zy.asrs.service.BasCrnpService;
import com.zy.asrs.service.BasDualCrnpService;
import com.zy.asrs.service.BasStationService;
import com.zy.asrs.service.DeviceConfigService;
import com.zy.asrs.service.StationCycleCapacityService;
import com.zy.asrs.service.WrkMastService;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.SlaveType;
import com.zy.core.enums.WrkStsType;
import com.zy.core.model.protocol.StationProtocol;
import com.zy.core.thread.StationThread;
import com.zy.system.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
@Service("autoTuneSnapshotService")
public class AutoTuneSnapshotServiceImpl implements AutoTuneSnapshotService {
 
    private static final int DEFAULT_CRN_OUT_BATCH_RUNNING_LIMIT = 5;
    private static final int DEFAULT_CONVEYOR_STATION_TASK_LIMIT = 30;
    private static final int DEFAULT_AI_AUTO_TUNE_INTERVAL_MINUTES = 10;
 
    @Autowired
    private WrkMastService wrkMastService;
 
    @Autowired
    private DeviceConfigService deviceConfigService;
 
    @Autowired
    private StationCycleCapacityService stationCycleCapacityService;
 
    @Autowired
    private FlowTopologySnapshotService flowTopologySnapshotService;
 
    @Autowired
    private ConfigService configService;
 
    @Autowired
    private BasStationService basStationService;
 
    @Autowired
    private BasCrnpService basCrnpService;
 
    @Autowired
    private BasDualCrnpService basDualCrnpService;
 
    @Override
    public AutoTuneSnapshot buildSnapshot() {
        List<AutoTuneStationRuntimeItem> stationRuntimeSnapshot = buildStationRuntimeSnapshot();
 
        AutoTuneSnapshot snapshot = new AutoTuneSnapshot();
        snapshot.setTaskSnapshot(buildTaskSnapshot());
        snapshot.setStationRuntimeSnapshot(stationRuntimeSnapshot);
        snapshot.setCycleLoadSnapshot(buildCycleLoadSnapshot());
        snapshot.setFlowTopologySnapshot(flowTopologySnapshotService.buildSnapshot(stationRuntimeSnapshot));
        snapshot.setCurrentParameterSnapshot(buildCurrentParameterSnapshot());
        snapshot.setSnapshotTime(new Date());
        return snapshot;
    }
 
    private AutoTuneTaskSnapshot buildTaskSnapshot() {
        List<WrkMast> activeTasks = loadActiveTasks();
 
        AutoTuneTaskSnapshot snapshot = new AutoTuneTaskSnapshot();
        snapshot.setActiveTaskCount(activeTasks.size());
        snapshot.setByTargetStation(countByTargetStation(activeTasks));
        snapshot.setByBatch(countByBatch(activeTasks));
        snapshot.setByCrn(countByCrn(activeTasks));
        snapshot.setByDualCrn(countByDualCrn(activeTasks));
        snapshot.setByIoType(countByIoType(activeTasks));
        return snapshot;
    }
 
    private List<WrkMast> loadActiveTasks() {
        if (wrkMastService == null) {
            return Collections.emptyList();
        }
        QueryWrapper<WrkMast> wrapper = new QueryWrapper<>();
        wrapper.notIn("wrk_sts", Arrays.asList(
                WrkStsType.COMPLETE_INBOUND.sts,
                WrkStsType.SETTLE_INBOUND.sts,
                WrkStsType.COMPLETE_OUTBOUND.sts,
                WrkStsType.SETTLE_OUTBOUND.sts,
                WrkStsType.COMPLETE_LOC_MOVE.sts,
                WrkStsType.COMPLETE_CRN_MOVE.sts
        ));
        return safeList(wrkMastService.list(wrapper));
    }
 
    private List<AutoTuneStationRuntimeItem> buildStationRuntimeSnapshot() {
        if (deviceConfigService == null) {
            return Collections.emptyList();
        }
        QueryWrapper<DeviceConfig> wrapper = new QueryWrapper<>();
        wrapper.eq("device_type", String.valueOf(SlaveType.Devp));
        wrapper.orderByAsc("device_no");
        List<DeviceConfig> deviceConfigList = safeList(deviceConfigService.list(wrapper));
        Map<Integer, AutoTuneStationRuntimeItem> runtimeMap = new LinkedHashMap<>();
        for (DeviceConfig deviceConfig : deviceConfigList) {
            appendStationRuntime(runtimeMap, deviceConfig);
        }
        return new ArrayList<>(runtimeMap.values());
    }
 
    private void appendStationRuntime(Map<Integer, AutoTuneStationRuntimeItem> runtimeMap, DeviceConfig deviceConfig) {
        if (deviceConfig == null || deviceConfig.getDeviceNo() == null) {
            return;
        }
        StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, deviceConfig.getDeviceNo());
        if (stationThread == null || stationThread.getStatusMap() == null) {
            return;
        }
        for (StationProtocol protocol : stationThread.getStatusMap().values()) {
            AutoTuneStationRuntimeItem runtimeItem = toRuntimeItem(protocol);
            if (runtimeItem != null) {
                runtimeMap.put(runtimeItem.getStationId(), runtimeItem);
            }
        }
    }
 
    private AutoTuneStationRuntimeItem toRuntimeItem(StationProtocol protocol) {
        if (protocol == null || protocol.getStationId() == null) {
            return null;
        }
        AutoTuneStationRuntimeItem item = new AutoTuneStationRuntimeItem();
        item.setStationId(protocol.getStationId());
        item.setAutoing(protocol.isAutoing() ? 1 : 0);
        item.setLoading(protocol.isLoading() ? 1 : 0);
        item.setTaskNo(protocol.getTaskNo() == null ? 0 : protocol.getTaskNo());
        item.setIoMode(protocol.getIoMode() == null ? null : String.valueOf(protocol.getIoMode()));
        return item;
    }
 
    private Map<String, Object> buildCycleLoadSnapshot() {
        if (stationCycleCapacityService == null) {
            return new LinkedHashMap<>();
        }
        return toCycleLoadMap(stationCycleCapacityService.getLatestSnapshot());
    }
 
    private Map<String, Object> toCycleLoadMap(StationCycleCapacityVo cycleCapacityVo) {
        Map<String, Object> result = new LinkedHashMap<>();
        if (cycleCapacityVo == null) {
            return result;
        }
        result.put("loopList", toLoopMapList(cycleCapacityVo.getLoopList()));
        result.put("loopCount", cycleCapacityVo.getLoopCount());
        result.put("totalStationCount", cycleCapacityVo.getTotalStationCount());
        result.put("taskStationCount", cycleCapacityVo.getTaskStationCount());
        result.put("manualStationCount", cycleCapacityVo.getManualStationCount());
        result.put("occupiedStationCount", cycleCapacityVo.getOccupiedStationCount());
        result.put("currentLoad", cycleCapacityVo.getCurrentLoad());
        result.put("refreshTime", cycleCapacityVo.getRefreshTime());
        return result;
    }
 
    private List<Map<String, Object>> toLoopMapList(List<StationCycleLoopVo> loopList) {
        List<Map<String, Object>> result = new ArrayList<>();
        for (StationCycleLoopVo loopVo : safeList(loopList)) {
            if (loopVo == null) {
                continue;
            }
            Map<String, Object> loopMap = new LinkedHashMap<>();
            loopMap.put("loopNo", loopVo.getLoopNo());
            loopMap.put("stationIdList", loopVo.getStationIdList());
            loopMap.put("workNoList", loopVo.getWorkNoList());
            loopMap.put("stationCount", loopVo.getStationCount());
            loopMap.put("taskCount", loopVo.getTaskCount());
            loopMap.put("manualStationCount", loopVo.getManualStationCount());
            loopMap.put("occupiedStationCount", loopVo.getOccupiedStationCount());
            loopMap.put("currentLoad", loopVo.getCurrentLoad());
            result.add(loopMap);
        }
        return result;
    }
 
    private AutoTuneParameterSnapshot buildCurrentParameterSnapshot() {
        AutoTuneParameterSnapshot snapshot = new AutoTuneParameterSnapshot();
        snapshot.setCrnOutBatchRunningLimit(readConfigInt(
                "crnOutBatchRunningLimit",
                DEFAULT_CRN_OUT_BATCH_RUNNING_LIMIT
        ));
        snapshot.setConveyorStationTaskLimit(readConfigInt(
                "conveyorStationTaskLimit",
                DEFAULT_CONVEYOR_STATION_TASK_LIMIT
        ));
        snapshot.setAiAutoTuneIntervalMinutes(readConfigInt(
                "aiAutoTuneIntervalMinutes",
                DEFAULT_AI_AUTO_TUNE_INTERVAL_MINUTES
        ));
        List<BasCrnp> crnList = loadCrnList();
        List<BasDualCrnp> dualCrnList = loadDualCrnList();
        snapshot.setStationOutTaskLimits(loadStationOutTaskLimits());
        snapshot.setCrnMaxOutTask(buildCrnMaxOutTask(crnList));
        snapshot.setCrnMaxInTask(buildCrnMaxInTask(crnList));
        snapshot.setDualCrnMaxOutTask(buildDualCrnMaxOutTask(dualCrnList));
        snapshot.setDualCrnMaxInTask(buildDualCrnMaxInTask(dualCrnList));
        return snapshot;
    }
 
    private int readConfigInt(String code, int defaultValue) {
        if (configService == null) {
            return defaultValue;
        }
        String value = configService.getConfigValue(code, String.valueOf(defaultValue));
        if (value == null || value.trim().isEmpty()) {
            return defaultValue;
        }
        try {
            return Integer.parseInt(value.trim());
        } catch (NumberFormatException exception) {
            return defaultValue;
        }
    }
 
    private Map<String, Integer> loadStationOutTaskLimits() {
        Map<String, Integer> result = new LinkedHashMap<>();
        if (basStationService == null) {
            return result;
        }
        QueryWrapper<BasStation> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("station_id");
        return buildStationOutTaskLimitMap(basStationService.list(wrapper));
    }
 
    Map<String, Integer> buildStationOutTaskLimitMap(List<BasStation> stationList) {
        Map<String, Integer> result = new LinkedHashMap<>();
        for (BasStation station : safeList(stationList)) {
            if (station != null && station.getStationId() != null) {
                result.put(String.valueOf(station.getStationId()), station.getOutTaskLimit());
            }
        }
        return result;
    }
 
    private Map<String, Integer> buildCrnMaxOutTask(List<BasCrnp> crnList) {
        Map<String, Integer> result = new LinkedHashMap<>();
        for (BasCrnp crn : safeList(crnList)) {
            if (crn == null || crn.getCrnNo() == null) {
                continue;
            }
            result.put(String.valueOf(crn.getCrnNo()), defaultInt(crn.getMaxOutTask()));
        }
        return result;
    }
 
    private Map<String, Integer> buildCrnMaxInTask(List<BasCrnp> crnList) {
        Map<String, Integer> result = new LinkedHashMap<>();
        for (BasCrnp crn : safeList(crnList)) {
            if (crn == null || crn.getCrnNo() == null) {
                continue;
            }
            result.put(String.valueOf(crn.getCrnNo()), defaultInt(crn.getMaxInTask()));
        }
        return result;
    }
 
    private Map<String, Integer> buildDualCrnMaxOutTask(List<BasDualCrnp> dualCrnList) {
        Map<String, Integer> result = new LinkedHashMap<>();
        for (BasDualCrnp dualCrn : safeList(dualCrnList)) {
            if (dualCrn == null || dualCrn.getCrnNo() == null) {
                continue;
            }
            result.put(String.valueOf(dualCrn.getCrnNo()), defaultInt(dualCrn.getMaxOutTask()));
        }
        return result;
    }
 
    private Map<String, Integer> buildDualCrnMaxInTask(List<BasDualCrnp> dualCrnList) {
        Map<String, Integer> result = new LinkedHashMap<>();
        for (BasDualCrnp dualCrn : safeList(dualCrnList)) {
            if (dualCrn == null || dualCrn.getCrnNo() == null) {
                continue;
            }
            result.put(String.valueOf(dualCrn.getCrnNo()), defaultInt(dualCrn.getMaxInTask()));
        }
        return result;
    }
 
    private List<BasCrnp> loadCrnList() {
        if (basCrnpService == null) {
            return Collections.emptyList();
        }
        QueryWrapper<BasCrnp> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("crn_no");
        return safeList(basCrnpService.list(wrapper));
    }
 
    private List<BasDualCrnp> loadDualCrnList() {
        if (basDualCrnpService == null) {
            return Collections.emptyList();
        }
        QueryWrapper<BasDualCrnp> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("crn_no");
        return safeList(basDualCrnpService.list(wrapper));
    }
 
    private Map<String, Integer> countByTargetStation(List<WrkMast> activeTasks) {
        Map<String, Integer> result = new LinkedHashMap<>();
        for (WrkMast task : activeTasks) {
            increment(result, task == null ? null : task.getStaNo());
        }
        return result;
    }
 
    private Map<String, Integer> countByBatch(List<WrkMast> activeTasks) {
        Map<String, Integer> result = new LinkedHashMap<>();
        for (WrkMast task : activeTasks) {
            String batch = task == null ? null : task.getBatch();
            if (batch != null && !batch.trim().isEmpty()) {
                increment(result, batch.trim());
            }
        }
        return result;
    }
 
    private Map<String, Integer> countByCrn(List<WrkMast> activeTasks) {
        Map<String, Integer> result = new LinkedHashMap<>();
        for (WrkMast task : activeTasks) {
            increment(result, task == null ? null : task.getCrnNo());
        }
        return result;
    }
 
    private Map<String, Integer> countByDualCrn(List<WrkMast> activeTasks) {
        Map<String, Integer> result = new LinkedHashMap<>();
        for (WrkMast task : activeTasks) {
            increment(result, task == null ? null : task.getDualCrnNo());
        }
        return result;
    }
 
    private Map<String, Integer> countByIoType(List<WrkMast> activeTasks) {
        Map<String, Integer> result = new LinkedHashMap<>();
        for (WrkMast task : activeTasks) {
            increment(result, task == null ? null : task.getIoType());
        }
        return result;
    }
 
    private void increment(Map<String, Integer> result, Integer key) {
        if (key == null) {
            return;
        }
        increment(result, String.valueOf(key));
    }
 
    private void increment(Map<String, Integer> result, String key) {
        if (key == null || key.trim().isEmpty()) {
            return;
        }
        String normalizedKey = key.trim();
        result.put(normalizedKey, result.getOrDefault(normalizedKey, 0) + 1);
    }
 
    private int defaultInt(Integer value) {
        return value == null ? 0 : value;
    }
 
    private <T> List<T> safeList(List<T> value) {
        return value == null ? Collections.emptyList() : value;
    }
}