diff --git a/im-admin/ruoyi-admin/src/main/java/org/dromara/ImAdminApplication.java b/im-admin/ruoyi-admin/src/main/java/org/dromara/ImAdminApplication.java index 1135d3b..6b8c265 100644 --- a/im-admin/ruoyi-admin/src/main/java/org/dromara/ImAdminApplication.java +++ b/im-admin/ruoyi-admin/src/main/java/org/dromara/ImAdminApplication.java @@ -3,6 +3,7 @@ package org.dromara; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup; +import org.springframework.scheduling.annotation.EnableScheduling; /** * 启动程序 @@ -11,6 +12,7 @@ import org.springframework.boot.context.metrics.buffering.BufferingApplicationSt */ @SpringBootApplication +@EnableScheduling public class ImAdminApplication { public static void main(String[] args) { diff --git a/im-admin/ruoyi-im/src/main/java/org/dromara/im/constant/ImConstant.java b/im-admin/ruoyi-im/src/main/java/org/dromara/im/constant/ImConstant.java index ab1054b..b4da0c4 100644 --- a/im-admin/ruoyi-im/src/main/java/org/dromara/im/constant/ImConstant.java +++ b/im-admin/ruoyi-im/src/main/java/org/dromara/im/constant/ImConstant.java @@ -12,8 +12,5 @@ public class ImConstant { */ public final static String DS_IM_PLATFORM = "platform"; - /** - * 代理token缓存key前缀 - */ - public static final String AGENT_TOKEN_KEY = "agent_token:"; + } diff --git a/im-admin/ruoyi-im/src/main/java/org/dromara/im/constant/ImRedisKey.java b/im-admin/ruoyi-im/src/main/java/org/dromara/im/constant/ImRedisKey.java index 7787672..a3eea9e 100644 --- a/im-admin/ruoyi-im/src/main/java/org/dromara/im/constant/ImRedisKey.java +++ b/im-admin/ruoyi-im/src/main/java/org/dromara/im/constant/ImRedisKey.java @@ -21,6 +21,16 @@ public class ImRedisKey { */ public static final String IM_QUEUE_GROUP_UNBAN = "im:queue:group:unban"; + /** + * 代理token缓存key前缀 + */ + public static final String AGENT_TOKEN_KEY = "agent_token:"; + + /** + * 代理套餐消息提示(1代表今日已经提示) + */ + public static final String EXPIRATION_MSG = "expiration_msg:"; + diff --git a/im-admin/ruoyi-im/src/main/java/org/dromara/im/controller/ImUserController.java b/im-admin/ruoyi-im/src/main/java/org/dromara/im/controller/ImUserController.java index 82ace5d..c98cdf7 100644 --- a/im-admin/ruoyi-im/src/main/java/org/dromara/im/controller/ImUserController.java +++ b/im-admin/ruoyi-im/src/main/java/org/dromara/im/controller/ImUserController.java @@ -155,10 +155,6 @@ public class ImUserController extends BaseController { @SaCheckPermission("im:user:listCustomer") @GetMapping("/listCustomer") public TableDataInfo listCustomer(ImUserBo bo, PageQuery pageQuery) { - Set test = WebSocketUtils.getOnlineUserIds(); - System.out.println(LoginHelper.getUserId()); - System.out.println("1111111"); - System.out.println(test); return userService.queryPageListCustomer(bo, pageQuery); } diff --git a/im-admin/ruoyi-im/src/main/java/org/dromara/im/service/IImAgentService.java b/im-admin/ruoyi-im/src/main/java/org/dromara/im/service/IImAgentService.java index 86453c2..135b25a 100644 --- a/im-admin/ruoyi-im/src/main/java/org/dromara/im/service/IImAgentService.java +++ b/im-admin/ruoyi-im/src/main/java/org/dromara/im/service/IImAgentService.java @@ -88,4 +88,11 @@ public interface IImAgentService { * @return 返回与令牌匹配的ImAgent对象,如果没有找到匹配的代理则可能返回null */ ImAgent getAgentByToken(String token); + + /** + * 发送套餐过期消息的方法 + * @param sysId 系统ID,用于标识需要发送过期消息的目标系统 + * @return 如果套餐即将到期(七天内),返回到对应提示 每天只提示一次 + */ + String checkIsExpiredTask(Long sysId); } diff --git a/im-admin/ruoyi-im/src/main/java/org/dromara/im/service/impl/ImAgentServiceImpl.java b/im-admin/ruoyi-im/src/main/java/org/dromara/im/service/impl/ImAgentServiceImpl.java index 9f54922..c1f5f8a 100644 --- a/im-admin/ruoyi-im/src/main/java/org/dromara/im/service/impl/ImAgentServiceImpl.java +++ b/im-admin/ruoyi-im/src/main/java/org/dromara/im/service/impl/ImAgentServiceImpl.java @@ -1,8 +1,9 @@ package org.dromara.im.service.impl; +import cn.hutool.core.date.DateUnit; +import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ObjectUtil; import com.baomidou.dynamic.datasource.annotation.DS; -import org.dromara.common.core.constant.GlobalConstants; import org.dromara.common.core.utils.MapstructUtils; import org.dromara.common.core.utils.StringUtils; import org.dromara.common.mybatis.core.page.TableDataInfo; @@ -23,13 +24,11 @@ import org.dromara.im.mapper.ImAgentMapper; import org.dromara.im.service.IImAgentService; import java.time.Duration; -import java.util.HashMap; -import java.util.List; -import java.util.Collection; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; -import static org.dromara.im.constant.ImConstant.AGENT_TOKEN_KEY; +import static org.dromara.im.constant.ImRedisKey.AGENT_TOKEN_KEY; +import static org.dromara.im.constant.ImRedisKey.EXPIRATION_MSG; /** * 代理关联Service业务层处理 @@ -44,8 +43,6 @@ public class ImAgentServiceImpl implements IImAgentService { private final ImAgentMapper baseMapper; - private final ImRedisMQTemplate redisMQTemplate; - /** * 查询代理关联 * @@ -158,7 +155,7 @@ public class ImAgentServiceImpl implements IImAgentService { String cacheKey = AGENT_TOKEN_KEY + userId; // 先从缓存中获取 - String cachedToken = (String) redisMQTemplate.opsForValue().get(cacheKey); + String cachedToken = RedisUtils.getCacheObject(cacheKey); if (cachedToken != null && ObjectUtil.isNotEmpty(cachedToken)) { return cachedToken; @@ -173,7 +170,7 @@ public class ImAgentServiceImpl implements IImAgentService { if(ObjectUtil.isNotEmpty(uniqueToken)){ token = uniqueToken; // 将token存入缓存,过期时间1小时 - redisMQTemplate.opsForValue().set(cacheKey, token, Duration.ofHours(1)); + RedisUtils.setCacheObject(cacheKey, token, Duration.ofHours(1)); } } @@ -197,4 +194,53 @@ public class ImAgentServiceImpl implements IImAgentService { lqw.eq(ImAgent::getUniqueToken, token); return baseMapper.selectOne(lqw); } + + @Override + public String checkIsExpiredTask(Long sysId) { + LambdaQueryWrapper lqw = Wrappers.lambdaQuery(); + lqw.eq(ImAgent::getSysId, sysId); + + ImAgent agent = baseMapper.selectOne(lqw); + + if(agent == null){ + return ""; + } + + Date expirationDate = agent.getPackageExpireTime(); + + // 判断当前代理套餐是否即将到期 + if (expirationDate == null) { + return ""; + } + + //获取当前时间,判断两个日期的差值 + Date now = new Date(); + long diff = expirationDate.getTime() - now.getTime(); + // 将毫秒转换为天数 + long days = diff / (1000 * 60 * 60 * 24); + + String formatTime = DateUtil.format(expirationDate, "yyyy-MM-dd"); + + //redis的key expirationMsg+id为1,过期时间为明天零点-当前时间,代表今天已经提示过了 + long expireTime = DateUtil.between(DateUtil.beginOfDay(DateUtil.tomorrow()), now, DateUnit.SECOND); + + if(RedisUtils.hasKey(EXPIRATION_MSG + sysId)) { + return ""; + } + + // 如果大于等于0小于等于7天则返回即将到期提示 + if (days >= 0 && days <= 7) { + RedisUtils.setCacheObject(EXPIRATION_MSG + sysId, "1", Duration.ofSeconds(expireTime)); + return "您的代理套餐将于 "+ formatTime + " 到期,请及时续费!"; + } + + // 如果小于0则返回已过期提示 + if (days < 0) { + RedisUtils.setCacheObject(EXPIRATION_MSG + sysId, "1", Duration.ofSeconds(expireTime)); + return "您的代理套餐已于 " + formatTime + " 到期,请及时续费!"; + } + + return ""; + + } } diff --git a/im-admin/ruoyi-im/src/main/java/org/dromara/im/task/ExpiredMsgSendTask.java b/im-admin/ruoyi-im/src/main/java/org/dromara/im/task/ExpiredMsgSendTask.java new file mode 100644 index 0000000..51ef333 --- /dev/null +++ b/im-admin/ruoyi-im/src/main/java/org/dromara/im/task/ExpiredMsgSendTask.java @@ -0,0 +1,55 @@ +package org.dromara.im.task; + +import cn.hutool.core.util.ObjectUtil; +import lombok.RequiredArgsConstructor; +import org.dromara.common.websocket.dto.WebSocketMessageDto; +import org.dromara.common.websocket.utils.WebSocketUtils; +import org.dromara.im.service.IImAgentService; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.util.Date; +import java.util.List; +import java.util.Set; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +@Component +@RequiredArgsConstructor +public class ExpiredMsgSendTask { + + + private final IImAgentService imAgentService; + + private final ScheduledExecutorService scheduledExecutorService; + + /** + * 每30分钟执行一次 + * cron表达式: 0 0/30 * * * ? + */ + @Scheduled(cron = "0 0/30 * * * ?") +// @Scheduled(cron = "* * * * * ?") +// @Scheduled(cron = "*/15 * * * * ?") + public void executeEvery30Minutes() { + // 在这里编写您需要每30分钟执行的操作 +// System.out.println("定时任务执行时间: " + new Date()); + + Set list = WebSocketUtils.getOnlineUserIds(); + + if(!list.isEmpty()) { + for (Long userId : list) { + //判断是否到期,到期7天内发送到期时间提醒 + String expiredMsg = imAgentService.checkIsExpiredTask(userId); + if (expiredMsg != null && ObjectUtil.isNotEmpty(expiredMsg)) { + scheduledExecutorService.schedule(() -> { + WebSocketMessageDto dto = new WebSocketMessageDto(); + dto.setMessage(expiredMsg); + dto.setSessionKeys(List.of(userId)); + WebSocketUtils.publishMessage(dto); + }, 5, TimeUnit.SECONDS); + } + } + } + } + +} diff --git a/im-admin/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/IImAgentAdminService.java b/im-admin/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/IImAgentAdminService.java index 23a119e..8ee3979 100644 --- a/im-admin/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/IImAgentAdminService.java +++ b/im-admin/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/IImAgentAdminService.java @@ -77,7 +77,7 @@ public interface IImAgentAdminService { /** * 发送套餐过期消息的方法 * @param sysId 系统ID,用于标识需要发送过期消息的目标系统 - * @return 如果套餐即将到期(三天内),返回到对应提示 + * @return 如果套餐即将到期(七天内),返回到对应提示 */ String checkIsExpired(Long sysId);