From 1ba2c7d0a7de1fc525a2abfc0dc3b6d00995a5b4 Mon Sep 17 00:00:00 2001 From: khalil Date: Fri, 21 Mar 2025 10:32:45 +0800 Subject: [PATCH] =?UTF-8?q?bravo-=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scheduler/task/luckyBag/BravoTask.java | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 accompany-scheduler/accompany-scheduler-service/src/main/java/com/accompany/scheduler/task/luckyBag/BravoTask.java diff --git a/accompany-scheduler/accompany-scheduler-service/src/main/java/com/accompany/scheduler/task/luckyBag/BravoTask.java b/accompany-scheduler/accompany-scheduler-service/src/main/java/com/accompany/scheduler/task/luckyBag/BravoTask.java new file mode 100644 index 000000000..608e77df2 --- /dev/null +++ b/accompany-scheduler/accompany-scheduler-service/src/main/java/com/accompany/scheduler/task/luckyBag/BravoTask.java @@ -0,0 +1,106 @@ +package com.accompany.scheduler.task.luckyBag; + +import com.accompany.business.message.BravoMessage; +import com.accompany.business.message.Lucky24Message; +import com.accompany.business.service.gift.BravoMessageService; +import com.accompany.business.service.lucky.BravoRecordService; +import com.accompany.common.redis.RedisKey; +import com.accompany.common.utils.DateTimeUtil; +import com.accompany.core.model.PartitionInfo; +import com.accompany.core.service.common.JedisService; +import com.accompany.core.service.partition.PartitionInfoService; +import com.alibaba.fastjson.JSON; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.time.Duration; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ThreadPoolExecutor; + +@Component +@Slf4j +public class BravoTask { + + @Autowired + private PartitionInfoService partitionInfoService; + @Autowired + private BravoRecordService service; + @Resource(name = "bizExecutor") + private ThreadPoolExecutor bizExecutor; + + @Autowired + private JedisService jedisService; + @Autowired + private BravoMessageService bravoMessageService; + + /** + * 重新消费队列的消息 + */ + @Scheduled(cron = "0 */5 * * * ?") + public void retryBravoQueue() { + log.info("retryBravoQueue start ..."); + Map map = jedisService.hgetAll(RedisKey.lucky_24_status.getKey()); + if (map == null || map.isEmpty()) { + return; + } + long curTime = System.currentTimeMillis(); + long gapTime = 1000 * 60 * 10; // 十分钟内没被消费 + + map.entrySet().parallelStream().forEach(entry -> { + try { + String messId = entry.getKey(); + String val = entry.getValue(); + BravoMessage giftMessage = JSON.parseObject(val, BravoMessage.class); + if (curTime - giftMessage.getCreateTime() > gapTime) { + bravoMessageService.handleGiftMessage(giftMessage); + } + } catch (Exception e) { + log.error("retryBravoQueue error", e); + } + }); + log.info("retryBravoQueue end ..."); + } + + @Scheduled(cron = "0 2 * * * ? ") + public void bravoRecordStat() { + Date now = new Date(); + List partitionInfoList = partitionInfoService.listAll(); + for (PartitionInfo partitionInfo : partitionInfoList) { + ZonedDateTime zdt = DateTimeUtil.convertWithZoneId(now, partitionInfo.getZoneId()); + ZonedDateTime hourAgo = zdt.minusHours(1L); + log.info("[BravoRecordStat] zdt {} hourAgo {}, zdtDay {} hourAgoDay {}", + zdt, hourAgo, zdt.getDayOfYear(), hourAgo.getDayOfWeek()); + if (zdt.getDayOfYear() == hourAgo.getDayOfYear()){ + continue; + } + bizExecutor.execute(() -> { + // 获取当天的第一秒 + ZonedDateTime startOfDay = hourAgo.withHour(0) + .withMinute(0) + .withSecond(0) + .withNano(0); + Date systemStartTime = DateTimeUtil.converLocalDateTimeToDate(startOfDay.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime()); + Date startTime = DateTimeUtil.converLocalDateTimeToDate(startOfDay.toLocalDateTime()); + + long zoneIdHour = Duration.between(systemStartTime.toInstant(), startTime.toInstant()).toHours(); + + // 获取当天的最后一秒 + ZonedDateTime endOfDay = hourAgo.withHour(23) + .withMinute(59) + .withSecond(59) + .withNano(999999999); + Date systemEndTime = DateTimeUtil.converLocalDateTimeToDate(endOfDay.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime()); + + service.statDate(partitionInfo.getId(), systemStartTime, systemEndTime, zoneIdHour); + }); + } + } + +}