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 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(); } }