|
@@ -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;
|
|
|
+ }
|
|
|
}
|