package com.zy.asrs.utils; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.core.common.SnowflakeIdWorker; import com.zy.asrs.domain.NotifyDto; import com.zy.asrs.domain.enums.NotifyMsgType; import com.zy.common.utils.RedisUtil; import com.zy.core.enums.RedisKeyType; import com.zy.core.enums.SlaveType; import com.zy.system.entity.Config; import com.zy.system.service.ConfigService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; import java.util.Set; @Component public class NotifyUtils { @Autowired private RedisUtil redisUtil; @Autowired private SnowflakeIdWorker snowflakeIdWorker; @Autowired private ConfigService configService; public synchronized boolean notify(String deviceType, Integer device, String taskNo, NotifyMsgType msgType) { SlaveType type = SlaveType.findInstance(deviceType); if (type == null) { return false; } return append(type, device, taskNo, msgType); } public synchronized List takeKeys(String deviceType, Integer device) { String key = getKey(deviceType, device); if(key == null){ return null; } Set keys = redisUtil.keys(key + "*"); if (keys == null) { return null; } List list = new ArrayList<>(); for (Object object : keys) { list.add(object.toString()); } return list; } public String getKey(String deviceType, Integer device) { SlaveType type = SlaveType.findInstance(deviceType); if (type == null) { return null; } String key = null; switch (type) { case Shuttle: key = RedisKeyType.QUEUE_SHUTTLE.key + device; break; case ForkLift: key = RedisKeyType.QUEUE_FORK_LIFT.key + device; break; default: return null; } return key; } private boolean append(SlaveType deviceType, Integer device, String taskNo, NotifyMsgType msgType) { String key = null; switch (deviceType) { case Shuttle: key = RedisKeyType.QUEUE_SHUTTLE.key + device; break; case ForkLift: key = RedisKeyType.QUEUE_FORK_LIFT.key + device; break; default: return false; } NotifyDto dto = new NotifyDto(); dto.setId(snowflakeIdWorker.nextId()); dto.setDeviceType(String.valueOf(deviceType)); dto.setDevice(device); dto.setMsgType(msgType.flag); dto.setContent(msgType.desc); dto.setTaskNo(taskNo); //重试次数 Config notifyFailTimesConfig = configService.selectOne(new EntityWrapper().eq("code", "notifyFailTimes")); if (notifyFailTimesConfig != null) { dto.setFailTimes(Integer.parseInt(notifyFailTimesConfig.getValue())); } //重试间隔 Config notifyRetryTimeConfig = configService.selectOne(new EntityWrapper().eq("code", "notifyRetryTime")); if (notifyRetryTimeConfig != null) { dto.setRetryTime(Integer.parseInt(notifyRetryTimeConfig.getValue())); } redisUtil.set(key + "_" + dto.getId(), dto); return true; } }