Преглед на файлове

feat:国家字母排序

zhangwei преди 2 седмици
родител
ревизия
f7a9ac93ed

+ 5 - 0
edu-travel-service/edu-travel-service-country/src/main/java/edu/travel/country/service/BaseCountryServeService.java

@@ -8,8 +8,10 @@ import edu.travel.dto.BaseCountryServeStatusDto;
 import edu.travel.rpc.RPCBaseResponse;
 import edu.travel.vo.BaseCountryServeStatusVo;
 import edu.travel.vo.BaseCountryServeVo;
+import edu.travel.vo.ServiceCountryVo;
 
 import java.util.List;
+import java.util.Map;
 
 public interface BaseCountryServeService extends IService<BaseCountryServe> {
 
@@ -22,4 +24,7 @@ public interface BaseCountryServeService extends IService<BaseCountryServe> {
 
     //更新国家状态
     RPCBaseResponse<BaseCountryServeStatusVo> updateServeStatus(BaseCountryServeStatusDto entity);
+
+    Map<String, List<ServiceCountryVo>> grouped();
+
 }

+ 49 - 4
edu-travel-service/edu-travel-service-country/src/main/java/edu/travel/country/service/impl/BaseCountryServeServiceImpl.java

@@ -1,8 +1,14 @@
 package edu.travel.country.service.impl;
 
+import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.json.JSONArray;
+import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import edu.travel.cache.util.RedisKey;
 import edu.travel.country.entity.BaseCountry;
 import edu.travel.country.entity.BaseCountryServe;
 import edu.travel.country.mapper.BaseCountryMapper;
@@ -14,19 +20,24 @@ import edu.travel.rpc.RPCBaseResponse;
 import edu.travel.service.SysServiceImpl;
 import edu.travel.vo.BaseCountryServeStatusVo;
 import edu.travel.vo.BaseCountryServeVo;
+import edu.travel.vo.ServiceCountryVo;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.annotation.Cacheable;
+import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.stereotype.Service;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
+import java.util.stream.Collectors;
+
+import static edu.travel.rpc.RPCBaseResponse.success;
 
 @Service
 public class BaseCountryServeServiceImpl extends SysServiceImpl<BaseCountryServeMapper, BaseCountryServe> implements BaseCountryServeService {
     @Autowired
     private BaseCountryMapper baseCountryMapper;
+    @Autowired
+    private StringRedisTemplate stringRedisTemplate;
 
     //分页连表
     @Override
@@ -76,6 +87,40 @@ public class BaseCountryServeServiceImpl extends SysServiceImpl<BaseCountryServe
         return new RPCBaseResponse<>(200, "SUCCESS", baseCountryServeStatusVo);
     }
 
+    @Override
+//    @Cacheable(value = "countryCache",keyGenerator = "DefaultCacheableKeyGenerator")
+    public Map<String, List<ServiceCountryVo>> grouped() {
+        //缓存获取
+        String s = stringRedisTemplate.opsForValue().get(RedisKey.COUNTRY_SORT);
+        if(!ObjectUtil.isEmpty(s)){
+            JSONObject objects = JSONUtil.parseObj(s);
+            Map<String, List<ServiceCountryVo>> result = new HashMap<>();
+
+            objects.forEach((key, value) -> {
+                JSONArray jsonArray = (JSONArray) value;
+                List<ServiceCountryVo> countries = jsonArray.toList(ServiceCountryVo.class);
+                result.put(key, countries);
+            });
+            return result;
+        }
+        List<BaseCountryServe> list = list();
+        List<ServiceCountryVo> serviceCountryVos = BeanUtil.copyToList(list, ServiceCountryVo.class);
+        TreeMap<String, List<ServiceCountryVo>> collect = serviceCountryVos.stream()
+                .filter(country -> country != null && !ObjectUtil.isEmpty(country))
+                .collect(Collectors.groupingBy(
+                        country -> {
+                            String firstChar = country.getCountryNameEn().substring(0, 1);
+                            return firstChar.toUpperCase();
+                        },
+                        TreeMap::new,
+                        Collectors.toList()
+                ));
+        //放入缓存
+        String jsonStr = JSONUtil.toJsonStr(collect);
+        stringRedisTemplate.opsForValue().set(RedisKey.COUNTRY_SORT,jsonStr);
+        return collect;
+    }
+
 
     /**
      * 服务国家树