Quellcode durchsuchen

修改map接口,使用ssr技术

JensionDzero vor 1 Jahr
Ursprung
Commit
2d6d8c38fe

+ 61 - 6
ruoyi-demo/src/main/java/com/ruoyi/demo/controller/TopologicalCommonController.java

@@ -1,23 +1,35 @@
 package com.ruoyi.demo.controller;
 
+import cn.hutool.json.JSONString;
+import com.ruoyi.common.annotation.Log;
 import com.ruoyi.common.core.domain.R;
+import com.ruoyi.common.helper.LoginHelper;
+import com.ruoyi.common.utils.JsonUtils;
 import com.ruoyi.demo.constant.RedisContant;
 import com.ruoyi.demo.entity.WdInfo;
 import com.ruoyi.demo.entity.vo.TopologicalWdAceeptVo;
 import com.ruoyi.demo.service.TopologicalCommonService;
 import com.ruoyi.demo.service.WdInfoService;
 import io.swagger.v3.oas.models.security.SecurityScheme;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.geo.Point;
 import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.http.MediaType;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
 
+import java.io.IOException;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeUnit;
 
 @RestController
 @RequestMapping("/poi/common")
+@Slf4j
 public class TopologicalCommonController {
     @Autowired
     TopologicalCommonService topologicalCommonService;
@@ -29,6 +41,11 @@ public class TopologicalCommonController {
     @Autowired
     RedisTemplate redisTemplate;
 
+    /**
+     * SSR
+     */
+    private static Map<Long, SseEmitter> sseCache = new ConcurrentHashMap<>();
+
     @RequestMapping("/prefixSearch")
     public R prefixSearch(String searchText,Integer size){
         List<WdInfo> wdInfos = topologicalCommonService.prefixSearch(searchText,size);
@@ -43,20 +60,58 @@ public class TopologicalCommonController {
         return R.ok(hashMap);
     }
 
-    @RequestMapping("/map")
-    public R map(TopologicalWdAceeptVo topologicalWdAceeptVo){
+    @RequestMapping(path = "/map",produces = {MediaType.TEXT_EVENT_STREAM_VALUE})
+    public SseEmitter   map(TopologicalWdAceeptVo topologicalWdAceeptVo) throws IOException {
         String md5 = topologicalWdAceeptVo.getHash();
+
         //2.查看redis中是否存在有缓存
-        List<WdInfo> wdCount = (List<WdInfo>) redisTemplate.boundHashOps(RedisContant.TOPOLOGICAL_COMMON_MAP).get(md5);
+        List<Point> wdCount = (List<Point>) redisTemplate.boundHashOps(RedisContant.TOPOLOGICAL_COMMON_MAP).get(md5);
         if (wdCount != null) {
-            return R.ok(wdCount);
+            SseEmitter sseEmitter = getSseEmitter(LoginHelper.getUserId());
+            sendFragment(sseEmitter,wdCount);
+            return sseEmitter;
         }
 
-        List<WdInfo> map = topologicalCommonService.map(topologicalWdAceeptVo);
+        List<Point> map = topologicalCommonService.map(topologicalWdAceeptVo);
         redisTemplate.boundHashOps(RedisContant.TOPOLOGICAL_COMMON_MAP).put(md5,map);
         redisTemplate.expire(RedisContant.TOPOLOGICAL_COMMON_MAP,RedisContant.TOPOLOGICAL_COMMON_MAP_TIME, TimeUnit.MINUTES); //30分钟
+        SseEmitter sseEmitter = getSseEmitter(LoginHelper.getUserId());
+        sendFragment(sseEmitter,map);
+        return sseEmitter;
+    }
 
-        return R.ok(map);
+    public SseEmitter getSseEmitter(Long uId){
+        SseEmitter sseEmitter = sseCache.get(uId);
+        if (sseEmitter != null) {
+            return sseEmitter;
+        }
+        // 超时时间设置为3s,用于演示客户端自动重连
+        // 设置前端的重试时间为1s
+        SseEmitter sseEmitter1 = new SseEmitter(30000L);
+        sseCache.put(uId, sseEmitter1);
+        sseEmitter1.onTimeout(() -> {
+            log.info(uId + "超时");
+            sseCache.remove(uId);
+        });
+        //sseEmitter.onCompletion(() -> System.out.println("完成!!!"));
+        return sseEmitter1;
+    }
+
+    public void sendFragment(SseEmitter sseEmitter,List<Point> wdCount){
+        int size = 1000;
+        int pages = wdCount.size()%size == 0 ? wdCount.size()/size:wdCount.size()/size+1;
+        int p1 = 0,p2 = size;
+        for (int i=0;i<pages;i++){
+            if (p2 > wdCount.size())
+                p2 = wdCount.size();
+            try {
+                sseEmitter.send(SseEmitter.event().name("map").data(JsonUtils.toJsonString(wdCount.subList(p1,p2))));
+                p1 = p2;
+                p2+=size;
+            } catch (IOException e) {
+                throw new RuntimeException(e);
+            }
+        }
     }
 
 }

+ 11 - 0
ruoyi-demo/src/main/java/com/ruoyi/demo/entity/bo/PointBo.java

@@ -0,0 +1,11 @@
+package com.ruoyi.demo.entity.bo;
+
+import lombok.Data;
+
+@Data
+public class PointBo {
+    private String wdId;
+    private String wdName;
+    private double lat;
+    private double lng;
+}

+ 5 - 0
ruoyi-demo/src/main/java/com/ruoyi/demo/mapper/WdInfoDao.java

@@ -9,10 +9,15 @@ import com.ruoyi.demo.entity.TopologicalHouseWd;
 import com.ruoyi.demo.entity.WdInfo;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
+import org.springframework.data.geo.Point;
+
+import java.util.List;
 
 @Mapper
 public interface WdInfoDao extends BaseMapper<WdInfo> {
     Page<TopologicalBuildingWd> selectTopologicalBuildWdList(Page<WdInfo> page, @Param(Constants.WRAPPER) QueryWrapper<WdInfo> queryWrapper);
 
     Page<TopologicalHouseWd> selectTopologicalHouseList(Page<WdInfo> page,@Param(Constants.WRAPPER) QueryWrapper<WdInfo> queryWrapper);
+
+    List<Point> map(@Param(Constants.WRAPPER) QueryWrapper<WdInfo> queryWrapper);
 }

+ 2 - 1
ruoyi-demo/src/main/java/com/ruoyi/demo/service/TopologicalCommonService.java

@@ -4,11 +4,12 @@ package com.ruoyi.demo.service;
 
 import com.ruoyi.demo.entity.WdInfo;
 import com.ruoyi.demo.entity.vo.TopologicalWdAceeptVo;
+import org.springframework.data.geo.Point;
 
 import java.util.List;
 
 public interface TopologicalCommonService {
     public List<WdInfo> prefixSearch(String searchText,Integer size);
 
-    List<WdInfo> map(TopologicalWdAceeptVo topologicalWdAceeptVo);
+    List<Point> map(TopologicalWdAceeptVo topologicalWdAceeptVo);
 }

+ 23 - 19
ruoyi-demo/src/main/java/com/ruoyi/demo/service/impl/ChannelAnalyseServiceImpl.java

@@ -406,36 +406,38 @@ public class ChannelAnalyseServiceImpl implements ChannelAnalyseService {
         List<StoreWd> storeWds = storeWdDao.perCapitaConsumpAnalyse(queryWrapper);
 
         //初始化
-        LinkedHashMap<String, Integer> linkedHashMap = new LinkedHashMap<>();
-        linkedHashMap.put("0~20",0);
-        linkedHashMap.put("20~50",0);
-        linkedHashMap.put("50~100",0);
-        linkedHashMap.put("100~200",0);
-        linkedHashMap.put("200以上",0);
+        //初始化
+        String[] arr = new String[]{"0~20","20~50","50~100","100~200","200以上"};
+        ArrayList<Histogram> list = new ArrayList<>();
+        for (String s:arr){
+            list.add(new Histogram(s,0));
+        }
 
         //2.统计各个人均消费的数量
         for (StoreWd storeWd : storeWds) {
             if (storeWd.getPerCapitaConsumption() != null){
                 if (storeWd.getPerCapitaConsumption() >= 0 && storeWd.getPerCapitaConsumption()<20){
-                    linkedHashMap.put("0~20",linkedHashMap.get("0~20")+1);
+                    Histogram histogram = list.get(0);
+                    histogram.setCount(histogram.getCount()+1);
                 }else if(storeWd.getPerCapitaConsumption() >= 20 && storeWd.getPerCapitaConsumption()<50){
-                    linkedHashMap.put("20~50",linkedHashMap.get("20~50")+1);
+                    Histogram histogram = list.get(1);
+                    histogram.setCount(histogram.getCount()+1);
                 }else if(storeWd.getPerCapitaConsumption() >= 50 && storeWd.getPerCapitaConsumption()< 100){
-                    linkedHashMap.put("50~100",linkedHashMap.get("50~100")+1);
+                    Histogram histogram = list.get(2);
+                    histogram.setCount(histogram.getCount()+1);
                 }else if(storeWd.getPerCapitaConsumption() >= 100 && storeWd.getPerCapitaConsumption()< 200){
-                    linkedHashMap.put("100~200",linkedHashMap.get("100~200")+1);
+                    Histogram histogram = list.get(3);
+                    histogram.setCount(histogram.getCount()+1);
                 }else {
-                    linkedHashMap.put("200以上",linkedHashMap.get("200以上")+1);
+                    Histogram histogram = list.get(4);
+                    histogram.setCount(histogram.getCount()+1);
                 }
             }
         }
 
+
+
         //3.分装统计结果
-        ArrayList<Histogram> list = new ArrayList<>();
-        for (String s : linkedHashMap.keySet()) {
-            Histogram histogram1 = new Histogram(s,linkedHashMap.get(s));
-            list.add(histogram1);
-        }
         return list;
     }
 
@@ -620,9 +622,11 @@ public class ChannelAnalyseServiceImpl implements ChannelAnalyseService {
                     tierCode.addAll(list);
                 }
             }
-            queryWrapper.and(wdInfoQueryWrapper -> {
-                wdInfoQueryWrapper.in("addr_code",tierCode);
-            });
+            if(!tierCode.isEmpty()){
+                queryWrapper.and(wdInfoQueryWrapper -> {
+                    wdInfoQueryWrapper.in("addr_code",tierCode);
+                });
+            }
         }
 
 

+ 5 - 3
ruoyi-demo/src/main/java/com/ruoyi/demo/service/impl/ChannelMapServiceImpl.java

@@ -807,9 +807,11 @@ public class ChannelMapServiceImpl implements ChannelMapService {
                     tierCode.addAll(list);
                 }
             }
-            queryWrapper.and(wdInfoQueryWrapper -> {
-                wdInfoQueryWrapper.in("addr_code",tierCode);
-            });
+            if(!tierCode.isEmpty()){
+                queryWrapper.and(wdInfoQueryWrapper -> {
+                    wdInfoQueryWrapper.in("addr_code",tierCode);
+                });
+            }
         }
 
         //搜索字段

+ 7 - 7
ruoyi-demo/src/main/java/com/ruoyi/demo/service/impl/TopologicalCommonServiceImpl.java

@@ -18,11 +18,10 @@ import org.springframework.data.redis.connection.RedisGeoCommands;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
 
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
 
 @Service
 @Transactional
@@ -36,6 +35,7 @@ public class TopologicalCommonServiceImpl implements TopologicalCommonService {
     @Autowired
     RedisTemplate redisTemplate;
 
+
     /**
      * 点位评估中心点查询
      * @param searchText
@@ -63,7 +63,7 @@ public class TopologicalCommonServiceImpl implements TopologicalCommonService {
      * @return
      */
     @Override
-    public List<WdInfo> map(TopologicalWdAceeptVo topologicalWdAceeptVo) {
+    public List<Point> map(TopologicalWdAceeptVo topologicalWdAceeptVo) {
         List<String> aroundWdId = new ArrayList<>();
 
         //1.根据经纬度 找到 周边网点Id 及 距离
@@ -73,9 +73,9 @@ public class TopologicalCommonServiceImpl implements TopologicalCommonService {
 
         //2.查询符合的周边网点信息
         QueryWrapper<WdInfo> queryWrapper = new QueryWrapper<>();
-        queryWrapper.select("wd_id","wd_name","addr_info","addr_code","lat","lng","type_code_by","type_name_by","wd_type_code")
+        queryWrapper.select("wd_id","wd_name","lat","lng")
                 .in("wd_id",aroundWdId);
-        List<WdInfo> wdInfos = wdInfoDao.selectList(queryWrapper);
+        List<Point> wdInfos = wdInfoDao.map(queryWrapper);
 
         return wdInfos;
     }

+ 17 - 20
ruoyi-demo/src/main/java/com/ruoyi/demo/service/impl/TopologicalStoreWdServiceImpl.java

@@ -257,39 +257,36 @@ public class TopologicalStoreWdServiceImpl implements TopologicalStoreWdService
         });
         List<StoreWd> storeWds = storeWdDao.perCapitaConsumpAnalyse(queryWrapper);
 
-        //统计初始化
-        HashMap<String, Integer> hashMap = new HashMap<>();
-        hashMap.put("0~20",0);
-        hashMap.put("20~50",0);
-        hashMap.put("50~100",0);
-        hashMap.put("100~200",0);
-        hashMap.put("200以上",0);
+        //初始化
+        String[] arr = new String[]{"0~20","20~50","50~100","100~200","200以上"};
+        ArrayList<Histogram> list = new ArrayList<>();
+        for (String s:arr){
+            list.add(new Histogram(s,0));
+        }
 
-        //3.统计
+        //2.统计各个人均消费的数量
         for (StoreWd storeWd : storeWds) {
             if (storeWd.getPerCapitaConsumption() != null){
                 if (storeWd.getPerCapitaConsumption() >= 0 && storeWd.getPerCapitaConsumption()<20){
-                    hashMap.put("0~20",hashMap.get("0~20")+1);
+                    Histogram histogram = list.get(0);
+                    histogram.setCount(histogram.getCount()+1);
                 }else if(storeWd.getPerCapitaConsumption() >= 20 && storeWd.getPerCapitaConsumption()<50){
-                    hashMap.put("20~50",hashMap.get("20~50")+1);
+                    Histogram histogram = list.get(1);
+                    histogram.setCount(histogram.getCount()+1);
                 }else if(storeWd.getPerCapitaConsumption() >= 50 && storeWd.getPerCapitaConsumption()< 100){
-                    hashMap.put("50~100",hashMap.get("50~100")+1);
+                    Histogram histogram = list.get(2);
+                    histogram.setCount(histogram.getCount()+1);
                 }else if(storeWd.getPerCapitaConsumption() >= 100 && storeWd.getPerCapitaConsumption()< 200){
-                    hashMap.put("100~200",hashMap.get("100~200")+1);
+                    Histogram histogram = list.get(3);
+                    histogram.setCount(histogram.getCount()+1);
                 }else {
-                    hashMap.put("200以上",hashMap.get("200以上")+1);
+                    Histogram histogram = list.get(4);
+                    histogram.setCount(histogram.getCount()+1);
                 }
             }
         }
 
         //4.组装返回结果
-        ArrayList<Histogram> list = new ArrayList<>();
-        for (String s : hashMap.keySet()) {
-            Histogram histogram = new Histogram();
-            histogram.setName(s);
-            histogram.setCount(hashMap.get(s));
-            list.add(histogram);
-        }
         return list;
     }
 

+ 4 - 0
ruoyi-demo/src/main/resources/mapper/demo/WdInfoDaoMapper.xml

@@ -9,5 +9,9 @@
     <select id="selectTopologicalHouseList" resultType="com.ruoyi.demo.entity.TopologicalHouseWd">
         select ddt_wd_info.wd_id as around_wd_id,wd_name as around_wd_name,wd_type_code as aroundWdTypeCodee,addr_code,addr_info,rent,households,people_count,property_type,lat,lng from ddt_wd_info left join ddt_house_wd on ddt_wd_info.wd_id = ddt_house_wd.wd_id ${ew.customSqlSegment}
     </select>
+
+    <select id="map" resultType="com.ruoyi.demo.entity.bo.PointBo">
+        select ${ew.sqlSelect} from ddt_wd_info ${ew.customSqlSegment}
+    </select>
 </mapper>