123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package com.fjhx.customer.handle;
- 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.entity.xiaoman.vo.CustomerInfoVo;
- import com.fjhx.customer.entity.xiaoman.vo.CustomerListApiVo;
- import com.fjhx.customer.service.xiaoman.XiaomanCustomerService;
- import com.ruoyi.common.utils.spring.SpringUtils;
- import com.fjhx.customer.service.xiaoman.XiaomanConfigService;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- 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 final int PAGE_SIZE = 20;
- public static void main(String[] args) {
- String filePath = "D:\\java_conding\\erhong\\hx-customer\\src\\main\\java\\com\\fjhx\\customer\\handle\\bbb.json";
- String jsonData = "";
- try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
- String line;
- while ((line = br.readLine()) != null) {
- jsonData += line;
- }
- handleDate(jsonData, new TypeReference<R<CustomerInfoVo>>() { });
- // handleAllCustomer(jsonData);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static CustomerListApiVo handleAllCustomer(String res,Set<Long> allCustomer){
- //反序列化对象
- CustomerListApiVo customerListApiVo = handleDate(res, new TypeReference<R<CustomerListApiVo>>() { });
- //判断列表是否为空
- if (!customerListApiVo.getList().isEmpty()){
- xiaomanCustomerService.handleSaveOrUpdate(customerListApiVo.getList(),allCustomer);
- }
- return customerListApiVo;
- }
- public static <T> T handleDate(String res, TypeReference<R<T>> typeReference) {
- ObjectMapper objectMapper = new ObjectMapper();
- try {
- objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
- R<T> result = objectMapper.readValue(res, typeReference);
- if (result.isOk()) {
- return result.getData();
- }
- return null;
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////小满api接口////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- 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);
- }
- 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);
- String res = HttpUtil.createGet(url).header("Authorization", "Bearer " + token).form(params).execute().body();
- return res;
- }
- }
|