|
@@ -4,13 +4,9 @@ package com.fjhx.customer.handle;
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.date.DateField;
|
|
|
-import cn.hutool.core.date.DateUnit;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
-import cn.hutool.core.lang.generator.UUIDGenerator;
|
|
|
-import cn.hutool.core.util.IdUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
-import cn.hutool.http.Header;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
import com.alibaba.fastjson2.JSONArray;
|
|
@@ -18,7 +14,6 @@ import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
|
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
-import com.fjhx.common.aspect.DataSourceAspect;
|
|
|
import com.fjhx.common.constant.SourceConstant;
|
|
|
import com.fjhx.customer.entity.xiaoman.dto.XiaomanCustomerSelectDto;
|
|
|
import com.fjhx.customer.entity.xiaoman.po.XiaomanConfig;
|
|
@@ -31,12 +26,9 @@ import com.fjhx.tenant.entity.dict.dto.DictTenantDataSelectDto;
|
|
|
import com.fjhx.tenant.entity.dict.po.DictTenantData;
|
|
|
import com.fjhx.tenant.entity.dict.vo.DictTenantDataVo;
|
|
|
import com.fjhx.tenant.service.dict.DictTenantDataService;
|
|
|
-import com.ruoyi.common.core.domain.entity.SysDictData;
|
|
|
import com.ruoyi.common.utils.SecurityUtils;
|
|
|
-import com.ruoyi.common.utils.StringUtils;
|
|
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
|
|
import com.fjhx.customer.service.xiaoman.XiaomanConfigService;
|
|
|
-import com.ruoyi.system.service.ISysDictDataService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
|
|
@@ -44,10 +36,8 @@ import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.FileReader;
|
|
|
import java.io.IOException;
|
|
|
-import java.nio.charset.Charset;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
@@ -57,8 +47,7 @@ public class HandleXiaomanData {
|
|
|
private static XiaomanCustomerService xiaomanCustomerService = SpringUtils.getBean(XiaomanCustomerService.class);
|
|
|
private static StringRedisTemplate redisTemplate = SpringUtils.getBean(StringRedisTemplate.class);
|
|
|
private static DictTenantDataService dictTenantDataService = SpringUtils.getBean(DictTenantDataService.class);
|
|
|
- private static final int PAGE_SIZE = 20;
|
|
|
- public static final String TENANT_ID = "ehsd";
|
|
|
+
|
|
|
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 = "";
|
|
@@ -121,252 +110,8 @@ public class HandleXiaomanData {
|
|
|
/////////////////////////////////////小满api接口////////////////////////////////////////////////////
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
- public static void initAllList(){
|
|
|
- redisTemplate.delete(XiaomanContant.TASK_STATUS_KEY);
|
|
|
- redisTemplate.opsForValue().set(XiaomanContant.TASK_STATUS_DESC_KEY, "全量更新出现异常,已停止");
|
|
|
- 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.offsetHour(DateUtil.date(), 1).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.GET_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 final Map<String, String> SELECTOR_KEY = new HashMap(){{
|
|
|
- put("trail_status", "customer_status");
|
|
|
- put("origin", "customer_source");
|
|
|
- }};
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取下拉框字典数据
|
|
|
- * @author hj
|
|
|
- * @date 2024/4/7 10:51
|
|
|
- */
|
|
|
- @DSTransactional
|
|
|
- public static void getSelectorData() {
|
|
|
- XiaomanConfig config = xiaomanConfigService.getConfig();
|
|
|
- String token = config.getAccessToken();
|
|
|
- Map<String, Object> params = new HashMap<>();
|
|
|
- DynamicDataSourceContextHolder.push(SourceConstant.BASE);
|
|
|
- SecurityUtils.setTenantId(TENANT_ID);
|
|
|
- SELECTOR_KEY.forEach((k, v) -> {
|
|
|
- params.put("field", k);
|
|
|
- String str = getData(XiaomanContant.SELECTOR_API_URL, token, params);
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(str);
|
|
|
- DictTenantDataSelectDto dictTenantDataSelectDto = new DictTenantDataSelectDto();
|
|
|
- dictTenantDataSelectDto.setDictCode(v);
|
|
|
- List<DictTenantDataVo> dictTenantDataServiceList = dictTenantDataService.getList(dictTenantDataSelectDto);
|
|
|
- Map<String, DictTenantData> collect = dictTenantDataServiceList.stream().collect(Collectors.toMap(DictTenantDataVo::getDictValue, o->BeanUtil.copyProperties(o, DictTenantData.class)));
|
|
|
- JSONArray data = jsonObject.getJSONArray("data");
|
|
|
- if("trail_status".equals(k)){
|
|
|
- dealCustomerStatusDict(data, collect);
|
|
|
- } else if("origin".equals(k)){
|
|
|
- dealCustomerSourceDict(data, collect);
|
|
|
- }
|
|
|
- log.info("获取小满下拉框字典数据:{}", jsonObject);
|
|
|
- });
|
|
|
- SecurityUtils.clearTenantId();
|
|
|
- DynamicDataSourceContextHolder.poll();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public static void updateXiaomanData(XiaomanUpdateInfoVO xiaomanUpdateInfoVO){
|
|
|
- if (ObjectUtil.isNull(xiaomanUpdateInfoVO)){
|
|
|
- throw new RuntimeException("数据为空");
|
|
|
- }
|
|
|
- if (StrUtil.isBlank(xiaomanUpdateInfoVO.getName())){
|
|
|
- throw new RuntimeException("客户名称为空");
|
|
|
- }
|
|
|
- xiaomanUpdateInfoVO.setPoolId(0);
|
|
|
- List<XiaomanUpdateInfoVO.Customer> customers = xiaomanUpdateInfoVO.getCustomers();
|
|
|
- if (CollectionUtil.isEmpty(customers)){
|
|
|
- throw new RuntimeException("客户联系人为空");
|
|
|
- }
|
|
|
- for (XiaomanUpdateInfoVO.Customer customer : customers) {
|
|
|
- if (StrUtil.isBlank(customer.getName())){
|
|
|
- throw new RuntimeException("客户联系人名称存在为空");
|
|
|
- }
|
|
|
- if (StrUtil.isBlank(customer.getEmail())){
|
|
|
- throw new RuntimeException("客户联系人email存在为空");
|
|
|
- }
|
|
|
- customer.setMainCustomerFlag("0");
|
|
|
- }
|
|
|
- xiaomanUpdateInfoVO.setCustomers(customers);
|
|
|
-
|
|
|
-
|
|
|
- String body = JSON.toJSONString(xiaomanUpdateInfoVO);
|
|
|
- XiaomanConfig config = xiaomanConfigService.getConfig();
|
|
|
- String token = config.getAccessToken();
|
|
|
- updateInfoData(token, body);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private static void dealCustomerSourceDict(JSONArray data, Map<String, DictTenantData> collect) {
|
|
|
- List<DictTenantData> list = new ArrayList<>();
|
|
|
- for (int i = 0; i < data.size(); i++) {
|
|
|
- JSONObject jsonObject = data.getJSONObject(i);
|
|
|
- String label = jsonObject.getString("origin_name");
|
|
|
- String key = jsonObject.getString("origin_id");
|
|
|
- boolean result = collect.containsKey(label);
|
|
|
- if(!result){
|
|
|
- DictTenantData dictTenantData = new DictTenantData();
|
|
|
- dictTenantData.setDictCode("customer_source");
|
|
|
- dictTenantData.setDictValue(label);
|
|
|
- dictTenantData.setDictKey(key);
|
|
|
- dictTenantData.setTenantId(TENANT_ID);
|
|
|
- dictTenantData.setSort(100);
|
|
|
- list.add(dictTenantData);
|
|
|
- }else {
|
|
|
- DictTenantData dictTenantData = collect.get(label);
|
|
|
- dictTenantData.setDictKey(key);
|
|
|
- dictTenantDataService.updateById(dictTenantData);
|
|
|
- }
|
|
|
- }
|
|
|
- if (!list.isEmpty()){
|
|
|
- dictTenantDataService.saveBatch(list);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private static void dealCustomerStatusDict(JSONArray data, Map<String, DictTenantData> collect) {
|
|
|
- List<DictTenantData> list = new ArrayList<>();
|
|
|
- for (int i = 0; i < data.size(); i++) {
|
|
|
- JSONObject jsonObject = data.getJSONObject(i);
|
|
|
- String label = jsonObject.getString("status_name");
|
|
|
- String key = jsonObject.getString("status_id");
|
|
|
- boolean result = collect.containsKey(label);
|
|
|
- if(!result){
|
|
|
- DictTenantData dictTenantData = new DictTenantData();
|
|
|
- dictTenantData.setDictCode("customer_status");
|
|
|
- dictTenantData.setDictValue(label);
|
|
|
- dictTenantData.setDictKey(key);
|
|
|
- dictTenantData.setTenantId(TENANT_ID);
|
|
|
- dictTenantData.setSort(100);
|
|
|
- list.add(dictTenantData);
|
|
|
- }else {
|
|
|
- DictTenantData dictTenantData = collect.get(label);
|
|
|
- dictTenantData.setDictKey(key);
|
|
|
- dictTenantDataService.updateById(dictTenantData);
|
|
|
- }
|
|
|
- }
|
|
|
- if (!list.isEmpty()){
|
|
|
- dictTenantDataService.saveBatch(list);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private static Map<String, Object> initPageParams(int page) {
|
|
|
- Map<String, Object> params = new HashMap<>();
|
|
|
- params.put("start_index", page);
|
|
|
- params.put("count", PAGE_SIZE);
|
|
|
- return params;
|
|
|
- }
|
|
|
-
|
|
|
- private 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;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public static void updateInfoData(String token, String body){
|
|
|
- String res = HttpUtil.createPost(XiaomanContant.UPDATE_INFO_API_URL).header("Authorization", "Bearer " + token).body(body).execute().body();
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
- String error = jsonObject.getString("error");
|
|
|
- if (StrUtil.isNotBlank(error)) {
|
|
|
- throw new RuntimeException("获取小满数据异常:" + error);
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
}
|