123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- package com.fjhx.customer.handle;
- import cn.hutool.core.date.DateField;
- import cn.hutool.core.date.DateUnit;
- import cn.hutool.core.date.DateUtil;
- import cn.hutool.core.util.StrUtil;
- import cn.hutool.http.HttpUtil;
- import com.alibaba.fastjson2.JSONObject;
- import com.fasterxml.jackson.databind.DeserializationFeature;
- import com.fjhx.customer.entity.xiaoman.dto.XiaomanCustomerSelectDto;
- import com.fjhx.customer.entity.xiaoman.po.XiaomanConfig;
- 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 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 = 1000;
- 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();
- }
- }
- /**
- * 处理-小满用户
- * @param res
- * @param allCustomer
- * @return
- */
- 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;
- }
- /**
- * 反序列化
- * @param res
- * @param typeReference
- * @param <T>
- * @return
- */
- 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(){
- 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::getCompanyId).collect(Collectors.toSet());
- int pageIndex = 1;
- int totalPage;
- do {
- String str = getData(XiaomanContant.ALL_CUSTOMER_API_URL, token, initPageParams(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());
- }
- /**
- * 获取客户详情
- * @author hj
- * @date 2024/4/6 21:38
- * @param companyId
- */
- public static String getCustomerDetail(Long companyId){
- XiaomanConfig config = xiaomanConfigService.getConfig();
- String token = config.getAccessToken();
- Map<String, Object> params = new HashMap<>();
- params.put("company_id", companyId);
- params.put("format", "1");
- return getData(XiaomanContant.CUSTOMER_DETAIL_API_URL, token, params);
- }
-
- /**
- * 客户更新列表
- * @author hj
- * @date 2024/4/6 21:54
- */
- public static void updateList(){
- try {
- XiaomanConfig config = xiaomanConfigService.getConfig();
- String token = config.getAccessToken();
- List<XiaomanCustomerVo> list = xiaomanCustomerService.getList(new XiaomanCustomerSelectDto());
- Set<Long> collect = list.stream().map(XiaomanCustomerVo::getCompanyId).collect(Collectors.toSet());
- int pageIndex = 1;
- int totalPage;
- do {
- Map<String, Object> params = initPageParams(pageIndex);
- String nowStr = DateUtil.date().toString("yyyy-MM-dd HH:00:00");
- params.put("start_time", DateUtil.parse(nowStr).offset(DateField.HOUR, -8).toString("yyyy-MM-dd HH:00:00"));
- params.put("end_time", nowStr);
- String str = getData(XiaomanContant.UPDATE_CUSTOMER_API_URL, token, params);
- 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();
- }
- }
-
-
- private static Map<String, Object> initPageParams(int page) {
- Map<String, Object> params = new HashMap<>();
- params.put("page", page);
- params.put("pageSize", PAGE_SIZE);
- return params;
- }
- public static String getData(String url, String token, Map<String, Object> params) {
- String res = HttpUtil.createGet(url).header("Authorization", "Bearer " + token).form(params).execute().body();
- JSONObject jsonObject = JSONObject.parseObject(res);
- String error = jsonObject.getString("error");
- if (StrUtil.isNotBlank(error)) {
- throw new RuntimeException("获取小满数据异常:" + error);
- }
- 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;
- }
- }
|