HandleXiaomanData.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package com.fjhx.customer.handle;
  2. import cn.hutool.core.date.DateField;
  3. import cn.hutool.core.date.DateUnit;
  4. import cn.hutool.core.date.DateUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import cn.hutool.http.HttpUtil;
  7. import com.alibaba.fastjson2.JSONObject;
  8. import com.fasterxml.jackson.databind.DeserializationFeature;
  9. import com.fjhx.customer.entity.xiaoman.dto.XiaomanCustomerSelectDto;
  10. import com.fjhx.customer.entity.xiaoman.po.XiaomanConfig;
  11. import com.fasterxml.jackson.core.type.TypeReference;
  12. import com.fasterxml.jackson.databind.ObjectMapper;
  13. import com.fjhx.customer.contants.XiaomanContant;
  14. import com.fjhx.customer.entity.xiaoman.vo.CustomerInfoVo;
  15. import com.fjhx.customer.entity.xiaoman.vo.CustomerListApiVo;
  16. import com.fjhx.customer.entity.xiaoman.vo.XiaomanCustomerVo;
  17. import com.fjhx.customer.service.xiaoman.XiaomanCustomerService;
  18. import com.ruoyi.common.utils.spring.SpringUtils;
  19. import com.fjhx.customer.service.xiaoman.XiaomanConfigService;
  20. import org.springframework.data.redis.core.StringRedisTemplate;
  21. import java.io.BufferedReader;
  22. import java.io.FileReader;
  23. import java.io.IOException;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. import java.util.concurrent.TimeUnit;
  27. import java.util.*;
  28. import java.util.stream.Collectors;
  29. public class HandleXiaomanData {
  30. private static XiaomanConfigService xiaomanConfigService = SpringUtils.getBean(XiaomanConfigService.class);
  31. private static XiaomanCustomerService xiaomanCustomerService = SpringUtils.getBean(XiaomanCustomerService.class);
  32. private static StringRedisTemplate redisTemplate = SpringUtils.getBean(StringRedisTemplate.class);
  33. private static final int PAGE_SIZE = 1000;
  34. public static void main(String[] args) {
  35. String filePath = "D:\\java_conding\\erhong\\hx-customer\\src\\main\\java\\com\\fjhx\\customer\\handle\\bbb.json";
  36. String jsonData = "";
  37. try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
  38. String line;
  39. while ((line = br.readLine()) != null) {
  40. jsonData += line;
  41. }
  42. handleDate(jsonData, new TypeReference<R<CustomerInfoVo>>() { });
  43. // handleAllCustomer(jsonData);
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. /**
  49. * 处理-小满用户
  50. * @param res
  51. * @param allCustomer
  52. * @return
  53. */
  54. public static CustomerListApiVo handleAllCustomer(String res,Set<Long> allCustomer){
  55. //反序列化对象
  56. CustomerListApiVo customerListApiVo = handleDate(res, new TypeReference<R<CustomerListApiVo>>() { });
  57. //判断列表是否为空
  58. if (!customerListApiVo.getList().isEmpty()){
  59. //保存或修改
  60. xiaomanCustomerService.handleSaveOrUpdate(customerListApiVo.getList(),allCustomer);
  61. }
  62. return customerListApiVo;
  63. }
  64. /**
  65. * 反序列化
  66. * @param res
  67. * @param typeReference
  68. * @param <T>
  69. * @return
  70. */
  71. public static <T> T handleDate(String res, TypeReference<R<T>> typeReference) {
  72. ObjectMapper objectMapper = new ObjectMapper();
  73. try {
  74. objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  75. R<T> result = objectMapper.readValue(res, typeReference);
  76. if (result.isOk()) {
  77. return result.getData();
  78. }
  79. return null;
  80. } catch (Exception e) {
  81. e.printStackTrace();
  82. return null;
  83. }
  84. }
  85. ///////////////////////////////////////////////////////////////////////////////////////////////////
  86. /////////////////////////////////////小满api接口////////////////////////////////////////////////////
  87. ///////////////////////////////////////////////////////////////////////////////////////////////////
  88. public static void initAllList(){
  89. boolean result = setNxWithExpiration(XiaomanContant.TASK_STATUS_KEY, XiaomanContant.TASK_STATUS_VALUE, XiaomanContant.DELAY_DELETE_KEY_TIME);
  90. if (!result) {
  91. String mes = redisTemplate.opsForValue().get(XiaomanContant.TASK_STATUS_DESC_KEY);
  92. throw new RuntimeException(StrUtil.isBlank(mes)? "全量更新正在进行中" : mes);
  93. }
  94. redisTemplate.opsForValue().set(XiaomanContant.TASK_STATUS_DESC_KEY, "全量更新正在进行中");
  95. try {
  96. XiaomanConfig config = xiaomanConfigService.getConfig();
  97. String token = config.getAccessToken();
  98. List<XiaomanCustomerVo> list = xiaomanCustomerService.getList(new XiaomanCustomerSelectDto());
  99. Set<Long> collect = list.stream().map(XiaomanCustomerVo::getCompanyId).collect(Collectors.toSet());
  100. int pageIndex = 1;
  101. int totalPage;
  102. do {
  103. String str = getData(XiaomanContant.ALL_CUSTOMER_API_URL, token, initPageParams(pageIndex));
  104. CustomerListApiVo customerListApiVo = handleAllCustomer(str, collect);
  105. int totalItem = customerListApiVo.getTotalItem();
  106. totalPage = (totalItem / PAGE_SIZE) + (totalItem % PAGE_SIZE > 0 ? 1 : 0);
  107. pageIndex++;
  108. } while (pageIndex <= totalPage);
  109. } catch (Exception e) {
  110. e.printStackTrace();
  111. redisTemplate.delete(XiaomanContant.TASK_STATUS_KEY);
  112. redisTemplate.opsForValue().set(XiaomanContant.TASK_STATUS_DESC_KEY, "全量更新出现异常,已停止");
  113. return;
  114. }
  115. redisTemplate.delete(XiaomanContant.TASK_STATUS_KEY);
  116. redisTemplate.opsForValue().set(XiaomanContant.TASK_STATUS_DESC_KEY, "全量更新完成,完成时间:" + DateUtil.now());
  117. }
  118. /**
  119. * 获取客户详情
  120. * @author hj
  121. * @date 2024/4/6 21:38
  122. * @param companyId
  123. */
  124. public static String getCustomerDetail(Long companyId){
  125. XiaomanConfig config = xiaomanConfigService.getConfig();
  126. String token = config.getAccessToken();
  127. Map<String, Object> params = new HashMap<>();
  128. params.put("company_id", companyId);
  129. params.put("format", "1");
  130. return getData(XiaomanContant.CUSTOMER_DETAIL_API_URL, token, params);
  131. }
  132. /**
  133. * 客户更新列表
  134. * @author hj
  135. * @date 2024/4/6 21:54
  136. */
  137. public static void updateList(){
  138. try {
  139. XiaomanConfig config = xiaomanConfigService.getConfig();
  140. String token = config.getAccessToken();
  141. List<XiaomanCustomerVo> list = xiaomanCustomerService.getList(new XiaomanCustomerSelectDto());
  142. Set<Long> collect = list.stream().map(XiaomanCustomerVo::getCompanyId).collect(Collectors.toSet());
  143. int pageIndex = 1;
  144. int totalPage;
  145. do {
  146. Map<String, Object> params = initPageParams(pageIndex);
  147. String nowStr = DateUtil.date().toString("yyyy-MM-dd HH:00:00");
  148. params.put("start_time", DateUtil.parse(nowStr).offset(DateField.HOUR, -8).toString("yyyy-MM-dd HH:00:00"));
  149. params.put("end_time", nowStr);
  150. String str = getData(XiaomanContant.UPDATE_CUSTOMER_API_URL, token, params);
  151. CustomerListApiVo customerListApiVo = handleAllCustomer(str, collect);
  152. int totalItem = customerListApiVo.getTotalItem();
  153. totalPage = (totalItem / PAGE_SIZE) + (totalItem % PAGE_SIZE > 0 ? 1 : 0);
  154. pageIndex++;
  155. } while (pageIndex <= totalPage);
  156. } catch (Exception e) {
  157. e.printStackTrace();
  158. }
  159. }
  160. private static Map<String, Object> initPageParams(int page) {
  161. Map<String, Object> params = new HashMap<>();
  162. params.put("page", page);
  163. params.put("pageSize", PAGE_SIZE);
  164. return params;
  165. }
  166. public static String getData(String url, String token, Map<String, Object> params) {
  167. String res = HttpUtil.createGet(url).header("Authorization", "Bearer " + token).form(params).execute().body();
  168. JSONObject jsonObject = JSONObject.parseObject(res);
  169. String error = jsonObject.getString("error");
  170. if (StrUtil.isNotBlank(error)) {
  171. throw new RuntimeException("获取小满数据异常:" + error);
  172. }
  173. return res;
  174. }
  175. /**
  176. * 设置key-value并设置过期时间
  177. * @author hj
  178. * @date 2024/4/6 21:14
  179. * @param key
  180. * @param value
  181. * @param seconds
  182. * @return boolean
  183. */
  184. public static boolean setNxWithExpiration(String key, String value, long seconds) {
  185. Boolean result = redisTemplate.opsForValue().setIfAbsent(key, value);
  186. if (result != null && result) {
  187. // 设置过期时间
  188. redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
  189. return true;
  190. }
  191. return false;
  192. }
  193. }