Selaa lähdekoodia

会员管理代码

wwh 1 viikko sitten
vanhempi
commit
38280a7ae7

+ 17 - 0
baqing-shop/src/main/java/com/ruoyi/web/modules/store/mapper/BizShopStatsMapper.java

@@ -0,0 +1,17 @@
1
+package com.ruoyi.web.modules.store.mapper;
2
+
3
+/**
4
+ * 店铺统计 biz_shop_stats
5
+ */
6
+public interface BizShopStatsMapper
7
+{
8
+    /**
9
+     * 为「有评价但尚无统计行」的未删除店铺插入 stats 并写入评分
10
+     */
11
+    int insertStatsForShopsWithReviews();
12
+
13
+    /**
14
+     * 按未删除店铺重算 biz_shop_stats.rating(无有效评价则为 NULL)
15
+     */
16
+    int updateAllShopRatings();
17
+}

+ 14 - 0
baqing-shop/src/main/java/com/ruoyi/web/modules/store/service/IShopRatingRefreshService.java

@@ -0,0 +1,14 @@
1
+package com.ruoyi.web.modules.store.service;
2
+
3
+/**
4
+ * 店铺综合评分刷新
5
+ */
6
+public interface IShopRatingRefreshService
7
+{
8
+    /**
9
+     * 重算未删除店铺的综合评分(有效评价平均分)
10
+     *
11
+     * @return 受影响行数(插入 + 更新)
12
+     */
13
+    int refreshAllShopRatings();
14
+}

+ 23 - 0
baqing-shop/src/main/java/com/ruoyi/web/modules/store/service/impl/ShopRatingRefreshServiceImpl.java

@@ -0,0 +1,23 @@
1
+package com.ruoyi.web.modules.store.service.impl;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.stereotype.Service;
5
+import org.springframework.transaction.annotation.Transactional;
6
+import com.ruoyi.web.modules.store.mapper.BizShopStatsMapper;
7
+import com.ruoyi.web.modules.store.service.IShopRatingRefreshService;
8
+
9
+@Service
10
+public class ShopRatingRefreshServiceImpl implements IShopRatingRefreshService
11
+{
12
+    @Autowired
13
+    private BizShopStatsMapper shopStatsMapper;
14
+
15
+    @Override
16
+    @Transactional(rollbackFor = Exception.class)
17
+    public int refreshAllShopRatings()
18
+    {
19
+        int inserted = shopStatsMapper.insertStatsForShopsWithReviews();
20
+        int updated = shopStatsMapper.updateAllShopRatings();
21
+        return inserted + updated;
22
+    }
23
+}

+ 28 - 0
baqing-shop/src/main/java/com/ruoyi/web/modules/store/task/ShopRatingRefreshTask.java

@@ -0,0 +1,28 @@
1
+package com.ruoyi.web.modules.store.task;
2
+
3
+import org.slf4j.Logger;
4
+import org.slf4j.LoggerFactory;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.scheduling.annotation.Scheduled;
7
+import org.springframework.stereotype.Component;
8
+import com.ruoyi.web.modules.store.service.IShopRatingRefreshService;
9
+
10
+/**
11
+ * 店铺综合评分日终统计:评分 = 有效评价星级之和 / 评价条数
12
+ */
13
+@Component
14
+public class ShopRatingRefreshTask
15
+{
16
+    private static final Logger LOGGER = LoggerFactory.getLogger(ShopRatingRefreshTask.class);
17
+
18
+    @Autowired
19
+    private IShopRatingRefreshService shopRatingRefreshService;
20
+
21
+    @Scheduled(cron = "0 55 22 * * ?")
22
+    public void refreshShopRatings()
23
+    {
24
+        LOGGER.info("ShopRatingRefreshTask start");
25
+        int affected = shopRatingRefreshService.refreshAllShopRatings();
26
+        LOGGER.info("ShopRatingRefreshTask done, affectedRows={}", affected);
27
+    }
28
+}

+ 35 - 0
baqing-shop/src/main/resources/mapper/store/BizShopStatsMapper.xml

@@ -0,0 +1,35 @@
1
+<?xml version="1.0" encoding="UTF-8" ?>
2
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3
+<mapper namespace="com.ruoyi.web.modules.store.mapper.BizShopStatsMapper">
4
+
5
+    <insert id="insertStatsForShopsWithReviews">
6
+        insert into biz_shop_stats (shop_id, rating, fans_count, update_time)
7
+        select r.shop_id,
8
+               round(sum(r.score) / count(1), 2),
9
+               0,
10
+               now()
11
+        from biz_goods_review r
12
+        inner join biz_shop s on r.shop_id = s.shop_id and s.del_flag = '0'
13
+        left join biz_shop_stats st on st.shop_id = r.shop_id
14
+        where r.del_flag = '0'
15
+          and r.score is not null
16
+          and st.shop_id is null
17
+        group by r.shop_id
18
+    </insert>
19
+
20
+    <update id="updateAllShopRatings">
21
+        update biz_shop_stats st
22
+        inner join biz_shop s on st.shop_id = s.shop_id and s.del_flag = '0'
23
+        left join (
24
+            select shop_id,
25
+                   round(sum(score) / count(1), 2) as avg_rating
26
+            from biz_goods_review
27
+            where del_flag = '0'
28
+              and score is not null
29
+            group by shop_id
30
+        ) r on st.shop_id = r.shop_id
31
+        set st.rating = r.avg_rating,
32
+            st.update_time = now()
33
+    </update>
34
+
35
+</mapper>

+ 35 - 0
baqing-shop/src/test/java/com/ruoyi/web/modules/store/service/ShopRatingRefreshServiceImplTest.java

@@ -0,0 +1,35 @@
1
+package com.ruoyi.web.modules.store.service;
2
+
3
+import static org.junit.jupiter.api.Assertions.assertEquals;
4
+import static org.mockito.Mockito.verify;
5
+import static org.mockito.Mockito.when;
6
+import org.junit.jupiter.api.Test;
7
+import org.junit.jupiter.api.extension.ExtendWith;
8
+import org.mockito.InjectMocks;
9
+import org.mockito.Mock;
10
+import org.mockito.junit.jupiter.MockitoExtension;
11
+import com.ruoyi.web.modules.store.mapper.BizShopStatsMapper;
12
+import com.ruoyi.web.modules.store.service.impl.ShopRatingRefreshServiceImpl;
13
+
14
+@ExtendWith(MockitoExtension.class)
15
+class ShopRatingRefreshServiceImplTest
16
+{
17
+    @Mock
18
+    private BizShopStatsMapper shopStatsMapper;
19
+
20
+    @InjectMocks
21
+    private ShopRatingRefreshServiceImpl shopRatingRefreshService;
22
+
23
+    @Test
24
+    void refreshAllShopRatings_insertsThenUpdates()
25
+    {
26
+        when(shopStatsMapper.insertStatsForShopsWithReviews()).thenReturn(1);
27
+        when(shopStatsMapper.updateAllShopRatings()).thenReturn(5);
28
+
29
+        int affected = shopRatingRefreshService.refreshAllShopRatings();
30
+
31
+        assertEquals(6, affected);
32
+        verify(shopStatsMapper).insertStatsForShopsWithReviews();
33
+        verify(shopStatsMapper).updateAllShopRatings();
34
+    }
35
+}