Selaa lähdekoodia

获取全量列表

openHj 1 vuosi sitten
vanhempi
commit
bb6e033e73

+ 16 - 0
hx-customer/src/main/java/com/fjhx/customer/contants/XiaomanContant.java

@@ -0,0 +1,16 @@
+package com.fjhx.customer.contants;
+
+public class XiaomanContant {
+
+    public static final String ALL_CUSTOMER_API_URL = "https://api-sandbox.xiaoman.cn/v1/company/list";
+    /**任务状态redis key**/
+    public static final String TASK_STATUS_KEY = "all_customer:task_status_key";
+    public static final String TASK_STATUS_VALUE = "start_task";
+    /**任务进度说明redis key**/
+    public static final String TASK_STATUS_DESC_KEY = "all_customer:task_status_desc_key";
+
+    /**延迟删除key的时间(秒)-> 5个小时 (一般给跑到一半项目被强行关掉后做最后的处理用)**/
+//    public static final int DELAY_DELETE_KEY_TIME = 18000;
+    public static final int DELAY_DELETE_KEY_TIME = 300;
+
+}

+ 59 - 19
hx-customer/src/main/java/com/fjhx/customer/handle/HandleXiaomanData.java

@@ -1,32 +1,39 @@
 package com.fjhx.customer.handle;
 
 
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.StrUtil;
 import cn.hutool.http.HttpUtil;
 import com.fasterxml.jackson.databind.DeserializationFeature;
 import com.fjhx.customer.entity.xiaoman.dto.XiaomanCustomerSelectDto;
 import com.fjhx.customer.entity.xiaoman.po.XiaomanConfig;
-import com.fjhx.customer.entity.xiaoman.po.XiaomanCustomer;
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fjhx.customer.contants.XiaomanContant;
 import com.fjhx.customer.entity.xiaoman.vo.CustomerInfoVo;
 import com.fjhx.customer.entity.xiaoman.vo.CustomerListApiVo;
+import com.fjhx.customer.entity.xiaoman.vo.XiaomanCustomerVo;
 import com.fjhx.customer.service.xiaoman.XiaomanCustomerService;
 import com.ruoyi.common.utils.spring.SpringUtils;
 import com.fjhx.customer.service.xiaoman.XiaomanConfigService;
+import org.springframework.data.redis.core.StringRedisTemplate;
 
 
 import java.io.BufferedReader;
 import java.io.FileReader;
 import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
 import java.util.*;
 import java.util.stream.Collectors;
 
 public class HandleXiaomanData {
-    private static final String ALL_CUSTOMER_API_URL = "https://api-sandbox.xiaoman.cn/v1/company/list";
     private static XiaomanConfigService xiaomanConfigService = SpringUtils.getBean(XiaomanConfigService.class);
     private static XiaomanCustomerService xiaomanCustomerService = SpringUtils.getBean(XiaomanCustomerService.class);
+    private static StringRedisTemplate redisTemplate = SpringUtils.getBean(StringRedisTemplate.class);
 
-    private static final int PAGE_SIZE = 20;
+    private static final int PAGE_SIZE = 1000;
 
     public static void main(String[] args) {
         String filePath = "D:\\java_conding\\erhong\\hx-customer\\src\\main\\java\\com\\fjhx\\customer\\handle\\bbb.json";
@@ -78,27 +85,60 @@ public class HandleXiaomanData {
     ///////////////////////////////////////////////////////////////////////////////////////////////////
 
     public static void initAllList(){
-        XiaomanConfig config = xiaomanConfigService.getConfig();
-        String token = config.getAccessToken();
-        Set<Long> set = xiaomanCustomerService.getList(new XiaomanCustomerSelectDto()).stream().map(XiaomanCustomer::getCompanyId).collect(Collectors.toSet());
-        int pageIndex = 1;
-        int totalPage = 1;
-        do {
-            String str = getData(ALL_CUSTOMER_API_URL, token, pageIndex);
-            CustomerListApiVo customerListApiVo = handleAllCustomer(str,set);
-            int totalItem = customerListApiVo.getTotalItem();
-            totalPage = (totalItem / PAGE_SIZE) + (totalItem % PAGE_SIZE > 0 ? 1 : 0);
-            pageIndex++;
-        } while (pageIndex <= totalPage);
+        boolean result = setNxWithExpiration(XiaomanContant.TASK_STATUS_KEY, XiaomanContant.TASK_STATUS_VALUE, XiaomanContant.DELAY_DELETE_KEY_TIME);
+        if (!result) {
+            String mes = redisTemplate.opsForValue().get(XiaomanContant.TASK_STATUS_DESC_KEY);
+            throw new RuntimeException(StrUtil.isBlank(mes)? "全量更新正在进行中" : mes);
+        }
+        redisTemplate.opsForValue().set(XiaomanContant.TASK_STATUS_DESC_KEY, "全量更新正在进行中");
+        try {
+            XiaomanConfig config = xiaomanConfigService.getConfig();
+            String token = config.getAccessToken();
+            List<XiaomanCustomerVo> list = xiaomanCustomerService.getList(new XiaomanCustomerSelectDto());
+            Set<Long> collect = list.stream().map(XiaomanCustomerVo::getCustomerId).collect(Collectors.toSet());
+            int pageIndex = 1;
+            int totalPage;
+            do {
+                String str = getData(XiaomanContant.ALL_CUSTOMER_API_URL, token, pageIndex);
+                CustomerListApiVo customerListApiVo = handleAllCustomer(str, collect);
+                int totalItem = customerListApiVo.getTotalItem();
+                totalPage = (totalItem / PAGE_SIZE) + (totalItem % PAGE_SIZE > 0 ? 1 : 0);
+                pageIndex++;
+            } while (pageIndex <= totalPage);
+        } catch (Exception e) {
+            e.printStackTrace();
+            redisTemplate.delete(XiaomanContant.TASK_STATUS_KEY);
+            redisTemplate.opsForValue().set(XiaomanContant.TASK_STATUS_DESC_KEY, "全量更新出现异常,已停止");
+            return;
+        }
+        redisTemplate.delete(XiaomanContant.TASK_STATUS_KEY);
+        redisTemplate.opsForValue().set(XiaomanContant.TASK_STATUS_DESC_KEY, "全量更新完成,完成时间:" + DateUtil.now());
     }
 
-
-
     public static String getData(String url, String token, int page) {
         Map<String, Object> params = new HashMap<>();
-        params.put("page", page);
-        params.put("pageSize", PAGE_SIZE);
+        params.put("start_index", page);
+        params.put("count", PAGE_SIZE);
         String res = HttpUtil.createGet(url).header("Authorization", "Bearer " + token).form(params).execute().body();
         return res;
     }
+
+    /**
+     * 设置key-value并设置过期时间
+     * @author hj
+     * @date 2024/4/6 21:14
+     * @param key
+     * @param value
+     * @param seconds
+     * @return boolean
+     */
+    public static boolean setNxWithExpiration(String key, String value, long seconds) {
+        Boolean result = redisTemplate.opsForValue().setIfAbsent(key, value);
+        if (result != null && result) {
+            // 设置过期时间
+            redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
+            return true;
+        }
+        return false;
+    }
 }

+ 6 - 3
hx-customer/src/main/java/com/fjhx/customer/initializers/XiaomanInitializers.java

@@ -29,15 +29,18 @@ public class XiaomanInitializers {
         //刷新token
         refreshToken();
         //获取全量数据
-        HandleXiaomanData.initAllList();
+        try {
+            HandleXiaomanData.initAllList();
+        } catch (Exception e) {
+            log.error("获取小满全量数据异常", e);
+        }
+
         //更新更新数据
 
         //获取数据字典
     }
 
 
-
-
     @Scheduled(cron = "0 0/30 * * * ? ")
         //每分钟执行一次
 //    @Scheduled(cron = "0 0/1 *  * * ? ")

+ 3 - 3
hx-customer/src/main/java/com/fjhx/customer/service/xiaoman/impl/XiaomanCustomerServiceImpl.java

@@ -4,7 +4,7 @@ import com.fjhx.customer.entity.xiaoman.po.XiaomanCustomer;
 import com.fjhx.customer.entity.xiaoman.vo.CustomerApiVo;
 import com.fjhx.customer.entity.xiaoman.vo.CustomerListApiVo;
 import com.fjhx.customer.mapper.xiaoman.XiaomanCustomerMapper;
-import com.fjhx.customer.service.xiaoman.XiaomanCustomerInfoJsonService;
+//import com.fjhx.customer.service.xiaoman.XiaomanCustomerInfoJsonService;
 import com.fjhx.customer.service.xiaoman.XiaomanCustomerService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.springframework.stereotype.Service;
@@ -33,8 +33,8 @@ import static com.ruoyi.common.utils.wrapper.IWrapper.getWrapper;
 @Service
 public class XiaomanCustomerServiceImpl extends ServiceImpl<XiaomanCustomerMapper, XiaomanCustomer> implements XiaomanCustomerService {
 
-    @Resource
-    private XiaomanCustomerInfoJsonService xiaomanCustomerInfoJsonService;
+//    @Resource
+//    private XiaomanCustomerInfoJsonService xiaomanCustomerInfoJsonService;
 
     @Override
     public List<XiaomanCustomerVo> getList(XiaomanCustomerSelectDto dto) {