package com.zy.asrs.utils; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.core.common.SnowflakeIdWorker; import com.zy.asrs.domain.dto.NotifyCustomDataDto; import com.zy.asrs.domain.dto.NotifyDto; import com.zy.asrs.domain.enums.NotifyMsgType; import com.zy.asrs.domain.enums.RedisKeyType; import com.zy.common.utils.RedisUtil; 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 notifyType, Integer device, String taskNo, String superTaskNo, NotifyMsgType msgType) { return append(notifyType, device, taskNo, superTaskNo, msgType, null, false, null); } public synchronized boolean notify(String notifyType, Integer device, String taskNo, String superTaskNo, NotifyMsgType msgType, String data, Boolean sendCustomData, NotifyCustomDataDto customData) { return append(notifyType, device, taskNo, superTaskNo, msgType, data, sendCustomData, customData); } public synchronized List takeKeys(String notifyType, Integer device) { String key = getKey(notifyType, 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 notifyType, Integer device) { String key = null; if (notifyType.equals("task")) { key = RedisKeyType.QUEUE_TASK.key + device; } else { return null; } return key; } private boolean append(String notifyType, Integer device, String taskNo, String superTaskNo, NotifyMsgType msgType, String data, Boolean sendCustomData, NotifyCustomDataDto customData) { String key = getKey(notifyType, device); if (key == null) { return false; } NotifyDto dto = new NotifyDto(); dto.setId(snowflakeIdWorker.nextId()); dto.setNotifyType(notifyType); dto.setDevice(device); dto.setMsgType(msgType.flag); dto.setMsgDesc(msgType.desc); dto.setData(data); dto.setTaskNo(taskNo); dto.setSuperTaskNo(superTaskNo); dto.setSendCustomData(sendCustomData); dto.setCustomData(customData); //重试次数 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; } }