소스 검색

完善“获取品牌地理位置标签分布”->返回10条数据降序排序,并为其添加redis缓存机制

云殇忆 1 년 전
부모
커밋
2c187cb34f

+ 3 - 0
benyun-core/src/main/java/com/benyun/core/constant/RedisContant.java

@@ -33,4 +33,7 @@ public class RedisContant {
 
     public static String CHANNEL_ANALYSE_CITYTIER = "channel_analyse_cityiter";
     public static int CHANNEL_ANALYSE_CITYTIER_TIME = 30;
+
+    public static String BRAND_GEOLABEL = "brand_geolabel";
+    public static int BRAND_GEOLABEL_TIME = 30;
 }

+ 15 - 0
benyun-core/src/main/java/com/benyun/core/controller/BrandController.java

@@ -1,17 +1,21 @@
 package com.benyun.core.controller;
 
+import com.benyun.core.constant.RedisContant;
 import com.benyun.core.entity.bo.BrandGeoLabelBo;
 import com.benyun.core.entity.bo.BrandSearch;
 import com.benyun.core.entity.vo.ListQueryBody;
 import com.benyun.core.service.BrandService;
+import com.benyun.core.utils.HashUtil;
 import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.R;
 import lombok.RequiredArgsConstructor;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 @Validated
 @RequiredArgsConstructor
@@ -20,6 +24,8 @@ import java.util.List;
 public class BrandController extends BaseController {
     @Autowired
     private BrandService brandService;
+    @Autowired
+    RedisTemplate redisTemplate;
 
 //    获取品牌信息列表
     @GetMapping("/search")
@@ -101,7 +107,16 @@ public class BrandController extends BaseController {
 //    获取品牌地理位置标签分布
     @GetMapping("/geolabel")
     public R geolabel(String brandId){
+        String md5 = HashUtil.hash(brandId,"MD5");
+        List<BrandGeoLabelBo> bos = (List<BrandGeoLabelBo>) redisTemplate.boundHashOps(RedisContant.BRAND_GEOLABEL).get(md5);
+        if (bos != null)
+            return R.ok(bos);
+
         List<BrandGeoLabelBo> brandGeoLabelBos = brandService.searchGeoLabel(brandId);
+
+        redisTemplate.boundHashOps(RedisContant.BRAND_GEOLABEL).put(md5,brandGeoLabelBos);
+        redisTemplate.expire(RedisContant.BRAND_GEOLABEL, RedisContant.BRAND_GEOLABEL_TIME, TimeUnit.MINUTES);
+
         return R.ok(brandGeoLabelBos);
     }
 

+ 36 - 7
benyun-core/src/main/java/com/benyun/core/service/impl/BrandServiceImpl.java

@@ -359,7 +359,7 @@ public class BrandServiceImpl implements BrandService {
         if (storeWds.size() == 0)
             return bos;
 
-        // 查询标签
+        // 查询门店标签
         int count = storeWds.size(); // 总数,用于做比率
         QueryWrapper<WdTopologicalInfo> infoQueryWrapper = new QueryWrapper<>();
         List<String> wdIds = new ArrayList<>();
@@ -385,14 +385,37 @@ public class BrandServiceImpl implements BrandService {
                 typeMap.put(split[0],Integer.parseInt(split[1]));
             }
         }
-        Set<String> keySet = typeMap.keySet();
+//        Set<String> keySet = typeMap.keySet();
+//        QueryWrapper<TypeBy> typeByQueryWrapper = new QueryWrapper<>();
+//        typeByQueryWrapper.in("type_code_by",keySet).and(typeByQueryWrapper1 -> {
+//            typeByQueryWrapper1.ne("mid_category","");
+//        }).and(typeByQueryWrapper1 -> {
+//            typeByQueryWrapper1.ne("sub_category","");
+//        });
+
+        // 排序
+//        Map<String,Integer> sortMap = new LinkedHashMap<>();
+//        typeMap.entrySet()
+//            .stream().sorted(Map.Entry.comparingByValue())
+//            .forEachOrdered(x -> sortMap.put(x.getKey(),x.getValue()));
+        List<Map.Entry<String,Integer>> sortMap = new ArrayList<>(typeMap.entrySet());
+        Collections.sort(sortMap, new Comparator<Map.Entry<String, Integer>>() {
+            @Override
+            public int compare(Map.Entry<String, Integer> t1, Map.Entry<String, Integer> t2) {
+                return t2.getValue().compareTo(t1.getValue()); // 降序
+            }
+        });
+        // 获取前10项
+        List<String> searchList = new ArrayList<>();
+        for (int i=0;i<sortMap.size();i++){
+            // System.out.println(sortMap.get(i).getKey()+" "+sortMap.get(i).getValue());
+            searchList.add(sortMap.get(i).getKey());
+            if (searchList.size() >= 10)
+                break;
+        }
         // 查询标签
         QueryWrapper<TypeBy> typeByQueryWrapper = new QueryWrapper<>();
-        typeByQueryWrapper.in("type_code_by",keySet).and(typeByQueryWrapper1 -> {
-            typeByQueryWrapper1.ne("mid_category","");
-        }).and(typeByQueryWrapper1 -> {
-            typeByQueryWrapper1.ne("sub_category","");
-        });
+        typeByQueryWrapper.in("type_code_by",searchList);
         List<TypeBy> typeByList = typeByDao.selectList(typeByQueryWrapper);
         // 赋值
         for (TypeBy typeBy : typeByList){
@@ -410,6 +433,12 @@ public class BrandServiceImpl implements BrandService {
             bo.setRatio((ratio*100) + "%");
             bos.add(bo);
         }
+        bos.sort(new Comparator<BrandGeoLabelBo>() {
+            @Override
+            public int compare(BrandGeoLabelBo t1, BrandGeoLabelBo t2) {
+                return t2.getTotal().compareTo(t1.getTotal());
+            }
+        });
         return bos;
     }
 }