Browse Source

更新小满数据

1018653686@qq.com 1 year ago
parent
commit
7ce7f6c94e

+ 4 - 1
hx-customer/src/main/java/com/fjhx/customer/contants/XiaomanContant.java

@@ -6,10 +6,13 @@ public class XiaomanContant {
     /**客户数据查询**/
     public static final String CUSTOMER_DETAIL_API_URL = "https://api-sandbox.xiaoman.cn/v1/company/info";
     /** 客户更新列表**/
-    public static final String UPDATE_CUSTOMER_API_URL = "https://api-sandbox.xiaoman.cn/v1/company/updates";
+    public static final String GET_UPDATE_CUSTOMER_API_URL = "https://api-sandbox.xiaoman.cn/v1/company/updates";
     /**客户状态,客户分组,公海分组,客户来源下拉框数据**/
     public static final String SELECTOR_API_URL = "https://api-sandbox.xiaoman.cn/v1/company/fields/selector";
 
+    /** 客户数据更新**/
+    public static final String UPDATE_INFO_API_URL = "https://api-sandbox.xiaoman.cn/v1/company/updates";
+
     /**任务状态redis key**/
     public static final String TASK_STATUS_KEY = "all_customer:task_status_key";
     public static final String TASK_STATUS_VALUE = "start_task";

+ 3 - 0
hx-customer/src/main/java/com/fjhx/customer/entity/xiaoman/vo/XiaomanUpdateInfoVO.java

@@ -37,6 +37,9 @@ public class XiaomanUpdateInfoVO {
     private String remark;
     //客户联系人
     private List<Customer> customers;
+    //客户公海分组ID,默认为0
+    @JSONField(name = "pool_id")
+    private Integer poolId;
 
     @Data
     public static class Customer {

+ 63 - 10
hx-customer/src/main/java/com/fjhx/customer/handle/HandleXiaomanData.java

@@ -1,13 +1,18 @@
 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;
 import com.alibaba.fastjson2.JSONObject;
 import com.baomidou.dynamic.datasource.annotation.DSTransactional;
@@ -20,9 +25,7 @@ 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.entity.xiaoman.vo.*;
 import com.fjhx.customer.service.xiaoman.XiaomanCustomerService;
 import com.fjhx.tenant.entity.dict.dto.DictTenantDataSelectDto;
 import com.fjhx.tenant.entity.dict.po.DictTenantData;
@@ -30,6 +33,7 @@ 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;
@@ -40,6 +44,7 @@ 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;
@@ -184,7 +189,7 @@ public class HandleXiaomanData {
                 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.UPDATE_CUSTOMER_API_URL, token, params);
+                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);
@@ -209,10 +214,10 @@ public class HandleXiaomanData {
     @DSTransactional
     public static void getSelectorData() {
         XiaomanConfig config = xiaomanConfigService.getConfig();
-        SecurityUtils.setTenantId(TENANT_ID);
         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);
@@ -220,7 +225,7 @@ public class HandleXiaomanData {
             DictTenantDataSelectDto dictTenantDataSelectDto = new DictTenantDataSelectDto();
             dictTenantDataSelectDto.setDictCode(v);
             List<DictTenantDataVo> dictTenantDataServiceList = dictTenantDataService.getList(dictTenantDataSelectDto);
-            Map<String, String> collect = dictTenantDataServiceList.stream().collect(Collectors.toMap(DictTenantDataVo::getDictValue, DictTenantDataVo::getDictKey));
+            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);
@@ -231,25 +236,59 @@ public class HandleXiaomanData {
         });
         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, String> collect) {
+
+    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(IdUtil.getSnowflakeNextIdStr());
+                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()){
@@ -257,20 +296,25 @@ public class HandleXiaomanData {
         }
     }
 
-    private static void dealCustomerStatusDict(JSONArray data, Map<String, String> collect) {
+    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(IdUtil.getSnowflakeNextIdStr());
+                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()){
@@ -316,4 +360,13 @@ public class HandleXiaomanData {
     }
 
 
+    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);
+        }
+    }
+
 }