|
@@ -0,0 +1,80 @@
|
|
|
|
+package com.caimei365.tools.task;
|
|
|
|
+
|
|
|
|
+import com.caimei365.tools.feign.CommodityFeign;
|
|
|
|
+import com.caimei365.tools.mapper.BaseMapper;
|
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
|
+import org.springframework.data.redis.core.RedisCallback;
|
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
+import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
+import redis.clients.jedis.commands.JedisCommands;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.Calendar;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Description
|
|
|
|
+ *
|
|
|
|
+ * @author : Charles
|
|
|
|
+ * @date : 2021/12/29
|
|
|
|
+ */
|
|
|
|
+@Slf4j
|
|
|
|
+@Configuration
|
|
|
|
+@EnableScheduling
|
|
|
|
+@RequiredArgsConstructor
|
|
|
|
+public class StatisticsTask {
|
|
|
|
+ @Resource
|
|
|
|
+ private BaseMapper baseMapper;
|
|
|
|
+ @Resource
|
|
|
|
+ private RedisTemplate<String,String> redisTemplate;
|
|
|
|
+ @Resource
|
|
|
|
+ private CommodityFeign commodityFeign;
|
|
|
|
+ /**
|
|
|
|
+ * 每天凌晨3点执行一次统计前一天商品访问量
|
|
|
|
+ * <p>
|
|
|
|
+ * cron表达式语法:秒 分 小时 日 月 周
|
|
|
|
+ * 年可省略
|
|
|
|
+ * * 表示所有值。
|
|
|
|
+ * ? 表示不指定值。
|
|
|
|
+ */
|
|
|
|
+ @Scheduled(cron = "0 0 3 * * ?")
|
|
|
|
+ public void countProductViews() {
|
|
|
|
+ try {
|
|
|
|
+ // 获取昨天日期字符串
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
+ calendar.setTime(new Date());
|
|
|
|
+ calendar.add(Calendar.DAY_OF_MONTH, -1);
|
|
|
|
+ Date viewTime = calendar.getTime();
|
|
|
|
+ String dateStr = new SimpleDateFormat("yyyyMMdd").format(viewTime);
|
|
|
|
+ log.info(">>>>>>>>>>>【定时任务】统计前一天商品详情访问量,统计日期(前一天):" + dateStr);
|
|
|
|
+ // 获取Redis的Key: @Statistics(prefix = "statistics_details", field = "productId")
|
|
|
|
+ String redisKey = "statistics_details:productId:" + dateStr;
|
|
|
|
+ Map<String, String> result = redisTemplate.execute(
|
|
|
|
+ (RedisCallback<Map<String, String>>) connection -> (
|
|
|
|
+ (JedisCommands) connection.getNativeConnection()
|
|
|
|
+ ).hgetAll(redisKey)
|
|
|
|
+ );
|
|
|
|
+ if (null != result && !result.isEmpty()){
|
|
|
|
+ for (Map.Entry<String, String> entry : result.entrySet()) {
|
|
|
|
+ if (null != entry && StringUtils.isNotEmpty(entry.getKey()) && StringUtils.isNotEmpty(entry.getValue())) {
|
|
|
|
+ Integer productId = Integer.parseInt(entry.getKey());
|
|
|
|
+ Integer views = Integer.parseInt(entry.getValue());
|
|
|
|
+ // 保存当前商品前一天商品详情访问量
|
|
|
|
+ baseMapper.insertProductViews(productId, views, viewTime);
|
|
|
|
+ // 更新当前商品阿里云索引
|
|
|
|
+ commodityFeign.updateProductIndex(productId);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ log.info(">>>>>>>>>>>【定时任务】统计前一天商品详情访问量,统计成功:" + result);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error(">>>>>>>>>>>【定时任务】统计前一天商品详情访问量失败:",e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|