#
Junjie
10 小时以前 aa710969e00e9d7e56a276066a239f74d5c49310
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
package com.zy.core.thread.support;
 
import com.alibaba.fastjson.JSONObject;
import com.core.common.Cools;
import com.core.common.SpringUtils;
import com.zy.asrs.entity.BasStationErrLog;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.asrs.entity.WrkMast;
import com.zy.asrs.service.BasStationErrLogService;
import com.zy.asrs.service.WrkMastService;
import com.zy.common.utils.RedisUtil;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.model.protocol.StationProtocol;
 
import java.util.Date;
import java.util.List;
 
public final class StationErrLogSupport {
 
    private StationErrLogSupport() {
    }
 
    public static void sync(DeviceConfig deviceConfig, RedisUtil redisUtil, List<StationProtocol> statusList) {
        if (redisUtil == null || statusList == null || statusList.isEmpty()) {
            return;
        }
        try {
            BasStationErrLogService errLogService = SpringUtils.getBean(BasStationErrLogService.class);
            if (errLogService == null) {
                return;
            }
            WrkMastService wrkMastService = null;
            try {
                wrkMastService = SpringUtils.getBean(WrkMastService.class);
            } catch (Exception ignore) {
            }
            for (StationProtocol stationProtocol : statusList) {
                syncOne(deviceConfig, redisUtil, errLogService, wrkMastService, stationProtocol);
            }
        } catch (Exception ignore) {
        }
    }
 
    private static void syncOne(DeviceConfig deviceConfig,
                                RedisUtil redisUtil,
                                BasStationErrLogService errLogService,
                                WrkMastService wrkMastService,
                                StationProtocol stationProtocol) {
        if (stationProtocol == null || stationProtocol.getStationId() == null) {
            return;
        }
        String errFlagKey = RedisKeyType.DEVICE_ERR_ACTIVE_STATION.key + stationProtocol.getStationId();
        Object active = redisUtil.get(errFlagKey);
        Date now = new Date();
        if (hasError(stationProtocol)) {
            if (active != null) {
                return;
            }
            BasStationErrLog log = new BasStationErrLog();
            Integer wrkNo = stationProtocol.getTaskNo();
            if (wrkNo != null && wrkNo > 0) {
                log.setWrkNo(wrkNo);
            }
            log.setStartTime(now);
            log.setStationId(stationProtocol.getStationId());
            log.setStaNo(stationProtocol.getTargetStaNo());
            log.setBarcode(stationProtocol.getBarcode());
            log.setErrCode(normalizeErrCode(stationProtocol.getError()));
            log.setError(resolveError(stationProtocol));
            log.setStatus(1);
            log.setCreateTime(now);
            log.setSystemStatus(buildSystemStatus(deviceConfig, stationProtocol));
            fillWorkContext(log, wrkMastService, wrkNo);
            errLogService.save(log);
            if (log.getId() != null) {
                redisUtil.set(errFlagKey, log.getId(), 60 * 60 * 24);
            }
            return;
        }
 
        if (active == null) {
            return;
        }
        BasStationErrLog update = new BasStationErrLog();
        update.setId(Long.valueOf(String.valueOf(active)));
        update.setEndTime(now);
        update.setStatus(2);
        update.setUpdateTime(now);
        errLogService.updateById(update);
        redisUtil.del(errFlagKey);
    }
 
    private static void fillWorkContext(BasStationErrLog log, WrkMastService wrkMastService, Integer wrkNo) {
        if (wrkMastService == null || wrkNo == null || wrkNo <= 0) {
            return;
        }
        WrkMast wrkMast = wrkMastService.selectByWorkNo(wrkNo);
        if (wrkMast == null) {
            return;
        }
        log.setWrkSts(wrkMast.getWrkSts());
        log.setIoType(wrkMast.getIoType());
        log.setLocNo(wrkMast.getLocNo());
        log.setSourceStaNo(wrkMast.getSourceStaNo());
        log.setSourceLocNo(wrkMast.getSourceLocNo());
        if (log.getStaNo() == null) {
            log.setStaNo(wrkMast.getStaNo());
        }
        if (Cools.isEmpty(log.getBarcode())) {
            log.setBarcode(wrkMast.getBarcode());
        }
    }
 
    private static boolean hasError(StationProtocol stationProtocol) {
        return normalizeErrCode(stationProtocol.getError()) != null || !Cools.isEmpty(stationProtocol.getErrorMsg());
    }
 
    private static Integer normalizeErrCode(Integer errCode) {
        return errCode != null && errCode > 0 ? errCode : null;
    }
 
    private static String resolveError(StationProtocol stationProtocol) {
        if (!Cools.isEmpty(stationProtocol.getErrorMsg())) {
            return stationProtocol.getErrorMsg();
        }
        Integer errCode = normalizeErrCode(stationProtocol.getError());
        return errCode == null ? null : "站点报警";
    }
 
    private static String buildSystemStatus(DeviceConfig deviceConfig, StationProtocol stationProtocol) {
        JSONObject snapshot = new JSONObject();
        if (deviceConfig != null) {
            snapshot.put("deviceNo", deviceConfig.getDeviceNo());
        }
        snapshot.put("stationProtocol", stationProtocol);
        return snapshot.toJSONString();
    }
}