소스 검색

Merge remote-tracking branch 'origin/master'

Aslee 3 년 전
부모
커밋
e7b3813035

+ 23 - 0
pom.xml

@@ -50,6 +50,14 @@
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-openfeign</artifactId>
+        </dependency>
         <!-- mysql -->
         <dependency>
             <groupId>mysql</groupId>
@@ -117,6 +125,21 @@
             <artifactId>rocketmq-spring-boot-starter</artifactId>
             <version>2.2.0</version>
         </dependency>
+        <!-- redis依赖包 -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-redis</artifactId>
+            <exclusions>
+                <exclusion>
+                    <groupId>io.lettuce</groupId>
+                    <artifactId>lettuce-core</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <dependency>
+            <groupId>redis.clients</groupId>
+            <artifactId>jedis</artifactId>
+        </dependency>
 
         <!-- knife4j:swagger增强-->
         <!-- https://doc.xiaominfo.com/knife4j/documentation/get_start.html -->

+ 4 - 0
src/main/java/com/caimei365/tools/ToolsApplication.java

@@ -2,11 +2,15 @@ package com.caimei365.tools;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
+import org.springframework.cloud.openfeign.EnableFeignClients;
 
 /**
  * @author Charles
  */
+@EnableDiscoveryClient
 @SpringBootApplication
+@EnableFeignClients(basePackages = {"com.caimei365.tools.feign"})
 public class ToolsApplication {
 
     public static void main(String[] args) {

+ 21 - 0
src/main/java/com/caimei365/tools/feign/CommodityFeign.java

@@ -0,0 +1,21 @@
+package com.caimei365.tools.feign;
+
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2021/12/30
+ */
+@FeignClient("CAIMEI365-CLOUD-COMMODITY")
+public interface CommodityFeign {
+    /**
+     * 更新商品搜索索引
+     * @param productId 商品id
+     */
+    @PostMapping("/commodity/search/index/update/product")
+    String updateProductIndex(@RequestParam Integer productId);
+}

+ 8 - 0
src/main/java/com/caimei365/tools/mapper/BaseMapper.java

@@ -75,4 +75,12 @@ public interface BaseMapper {
      * @param count  增加条数
      */
     void updateSmsSendCount(int markId, int count);
+
+    /**
+     * 统计前一天商品访问量
+     * @param productId 商品Id
+     * @param views     访问量
+     * @param viewTime 访问日期(天)
+     */
+    void insertProductViews(Integer productId, Integer views, Date viewTime);
 }

+ 80 - 0
src/main/java/com/caimei365/tools/task/StatisticsTask.java

@@ -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);
+        }
+    }
+}

+ 4 - 0
src/main/resources/mapper/BaseMapper.xml

@@ -90,6 +90,10 @@
         INSERT INTO cm_short_link (markId, shortLink, jumpLink, createTime)
         VALUES (#{markId}, #{shortLink}, #{url},NOW())
     </insert>
+    <insert id="insertProductViews">
+        INSERT INTO cm_product_views_record (productId, views, viewTime)
+        VALUES (#{productId}, #{views}, #{viewTime})
+    </insert>
     <update id="updateSmsSendCount">
         UPDATE cm_sms_statistics SET sendNum = (sendNum + #{count}) WHERE markId = #{markId}
     </update>