HandleXiaomanData.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.fjhx.customer.handle;
  2. import cn.hutool.http.HttpUtil;
  3. import com.fasterxml.jackson.databind.DeserializationFeature;
  4. import com.fjhx.customer.entity.xiaoman.dto.XiaomanCustomerSelectDto;
  5. import com.fjhx.customer.entity.xiaoman.po.XiaomanConfig;
  6. import com.fjhx.customer.entity.xiaoman.po.XiaomanCustomer;
  7. import com.fasterxml.jackson.core.type.TypeReference;
  8. import com.fasterxml.jackson.databind.ObjectMapper;
  9. import com.fjhx.customer.entity.xiaoman.vo.CustomerInfoVo;
  10. import com.fjhx.customer.entity.xiaoman.vo.CustomerListApiVo;
  11. import com.fjhx.customer.service.xiaoman.XiaomanCustomerService;
  12. import com.ruoyi.common.utils.spring.SpringUtils;
  13. import com.fjhx.customer.service.xiaoman.XiaomanConfigService;
  14. import java.io.BufferedReader;
  15. import java.io.FileReader;
  16. import java.io.IOException;
  17. import java.util.*;
  18. import java.util.stream.Collectors;
  19. public class HandleXiaomanData {
  20. private static final String ALL_CUSTOMER_API_URL = "https://api-sandbox.xiaoman.cn/v1/company/list";
  21. private static XiaomanConfigService xiaomanConfigService = SpringUtils.getBean(XiaomanConfigService.class);
  22. private static XiaomanCustomerService xiaomanCustomerService = SpringUtils.getBean(XiaomanCustomerService.class);
  23. private static final int PAGE_SIZE = 20;
  24. public static void main(String[] args) {
  25. String filePath = "D:\\java_conding\\erhong\\hx-customer\\src\\main\\java\\com\\fjhx\\customer\\handle\\bbb.json";
  26. String jsonData = "";
  27. try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
  28. String line;
  29. while ((line = br.readLine()) != null) {
  30. jsonData += line;
  31. }
  32. handleDate(jsonData, new TypeReference<R<CustomerInfoVo>>() { });
  33. // handleAllCustomer(jsonData);
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. public static CustomerListApiVo handleAllCustomer(String res,Set<Long> allCustomer){
  39. //反序列化对象
  40. CustomerListApiVo customerListApiVo = handleDate(res, new TypeReference<R<CustomerListApiVo>>() { });
  41. //判断列表是否为空
  42. if (!customerListApiVo.getList().isEmpty()){
  43. xiaomanCustomerService.handleSaveOrUpdate(customerListApiVo.getList(),allCustomer);
  44. }
  45. return customerListApiVo;
  46. }
  47. public static <T> T handleDate(String res, TypeReference<R<T>> typeReference) {
  48. ObjectMapper objectMapper = new ObjectMapper();
  49. try {
  50. objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  51. R<T> result = objectMapper.readValue(res, typeReference);
  52. if (result.isOk()) {
  53. return result.getData();
  54. }
  55. return null;
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. return null;
  59. }
  60. }
  61. ///////////////////////////////////////////////////////////////////////////////////////////////////
  62. /////////////////////////////////////小满api接口////////////////////////////////////////////////////
  63. ///////////////////////////////////////////////////////////////////////////////////////////////////
  64. public static void initAllList(){
  65. XiaomanConfig config = xiaomanConfigService.getConfig();
  66. String token = config.getAccessToken();
  67. Set<Long> set = xiaomanCustomerService.getList(new XiaomanCustomerSelectDto()).stream().map(XiaomanCustomer::getCompanyId).collect(Collectors.toSet());
  68. int pageIndex = 1;
  69. int totalPage = 1;
  70. do {
  71. String str = getData(ALL_CUSTOMER_API_URL, token, pageIndex);
  72. CustomerListApiVo customerListApiVo = handleAllCustomer(str,set);
  73. int totalItem = customerListApiVo.getTotalItem();
  74. totalPage = (totalItem / PAGE_SIZE) + (totalItem % PAGE_SIZE > 0 ? 1 : 0);
  75. pageIndex++;
  76. } while (pageIndex <= totalPage);
  77. }
  78. public static String getData(String url, String token, int page) {
  79. Map<String, Object> params = new HashMap<>();
  80. params.put("page", page);
  81. params.put("pageSize", PAGE_SIZE);
  82. String res = HttpUtil.createGet(url).header("Authorization", "Bearer " + token).form(params).execute().body();
  83. return res;
  84. }
  85. }