openHj 1 жил өмнө
parent
commit
77ef817552

+ 2 - 0
hx-admin/src/main/java/com/fjhx/admin/BytesailingApplication.java

@@ -3,11 +3,13 @@ package com.fjhx.admin;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.scheduling.annotation.EnableAsync;
 import org.springframework.scheduling.annotation.EnableScheduling;
 
 @Slf4j
 @SpringBootApplication(scanBasePackages = "com.fjhx.**")
 @EnableScheduling
+@EnableAsync
 public class BytesailingApplication {
 
     public static void main(String[] args) {

+ 2 - 0
hx-customer/src/main/java/com/fjhx/customer/contants/XiaomanContant.java

@@ -24,5 +24,7 @@ public class XiaomanContant {
     public static final int DELAY_DELETE_KEY_TIME = 300;
 
 
+    public static final int PAGE_SIZE = 20;
+    public static final String TENANT_ID = "ehsd";
 
 }

+ 1 - 256
hx-customer/src/main/java/com/fjhx/customer/handle/HandleXiaomanData.java

@@ -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);
-        }
-    }
 
 }

+ 5 - 2
hx-customer/src/main/java/com/fjhx/customer/initializers/XiaomanInitializers.java

@@ -6,6 +6,7 @@ import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import com.fjhx.customer.entity.xiaoman.po.XiaomanConfig;
 import com.fjhx.customer.handle.HandleXiaomanData;
+import com.fjhx.customer.service.xiaoman.XiaomanApiService;
 import com.fjhx.customer.service.xiaoman.XiaomanConfigService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.scheduling.annotation.Scheduled;
@@ -21,6 +22,8 @@ public class XiaomanInitializers {
 
     @Resource
     private XiaomanConfigService xiaomanConfigService;
+    @Resource
+    private XiaomanApiService xiaomanApiService;
 
     @PostConstruct
     public void dataInitializer() {
@@ -30,7 +33,7 @@ public class XiaomanInitializers {
         refreshToken();
         //获取全量数据
         try {
-            HandleXiaomanData.getSelectorData();
+            xiaomanApiService.getSelectorData();
         } catch (Exception e) {
             log.error("获取小满全量数据异常", e);
         }
@@ -72,7 +75,7 @@ public class XiaomanInitializers {
     void getSelectorData(){
         //获取全量数据
         try {
-            HandleXiaomanData.getSelectorData();
+            xiaomanApiService.getSelectorData();
         } catch (Exception e) {
             log.error("获取小满下拉框字典数据异常", e);
         }

+ 26 - 0
hx-customer/src/main/java/com/fjhx/customer/service/xiaoman/XiaomanApiService.java

@@ -0,0 +1,26 @@
+package com.fjhx.customer.service.xiaoman;
+
+import com.baomidou.dynamic.datasource.annotation.DSTransactional;
+import com.fjhx.customer.entity.xiaoman.vo.XiaomanUpdateInfoVO;
+
+public interface XiaomanApiService {
+
+    /**
+     * 初始化所有客户列表
+     * @author hj
+     * @date 2024/4/8 22:20
+     */
+    void initAllList();
+
+
+    boolean setNxWithExpiration(String key, String value, long expireTime);
+
+    String getCustomerDetail(Long companyId);
+
+    void updateList();
+
+    @DSTransactional
+    void getSelectorData();
+
+    void updateXiaomanData(XiaomanUpdateInfoVO xiaomanUpdateInfoVO);
+}

+ 321 - 0
hx-customer/src/main/java/com/fjhx/customer/service/xiaoman/impl/XiaomanApiServiceImpl.java

@@ -0,0 +1,321 @@
+package com.fjhx.customer.service.xiaoman.impl;
+
+import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.date.DateField;
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.core.util.StrUtil;
+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;
+import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
+import com.fjhx.common.constant.SourceConstant;
+import com.fjhx.customer.contants.XiaomanContant;
+import com.fjhx.customer.entity.xiaoman.dto.XiaomanCustomerSelectDto;
+import com.fjhx.customer.entity.xiaoman.po.XiaomanConfig;
+import com.fjhx.customer.entity.xiaoman.vo.CustomerListApiVo;
+import com.fjhx.customer.entity.xiaoman.vo.XiaomanCustomerVo;
+import com.fjhx.customer.entity.xiaoman.vo.XiaomanUpdateInfoVO;
+import com.fjhx.customer.handle.HandleXiaomanData;
+import com.fjhx.customer.service.xiaoman.XiaomanApiService;
+import com.fjhx.customer.service.xiaoman.XiaomanConfigService;
+import com.fjhx.customer.service.xiaoman.XiaomanCustomerService;
+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.utils.SecurityUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.data.redis.core.StringRedisTemplate;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.*;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+@Service
+@Slf4j
+public class XiaomanApiServiceImpl implements XiaomanApiService {
+    @Resource
+    XiaomanConfigService xiaomanConfigService;
+    @Resource
+    StringRedisTemplate stringRedisTemplate;
+    @Resource
+    XiaomanCustomerService xiaomanCustomerService;
+    @Resource
+    DictTenantDataService dictTenantDataService;
+
+    @Override
+    @DSTransactional
+    @Async
+    public void initAllList() {
+        stringRedisTemplate.delete(XiaomanContant.TASK_STATUS_KEY);
+        stringRedisTemplate.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 = stringRedisTemplate.opsForValue().get(XiaomanContant.TASK_STATUS_DESC_KEY);
+            throw new RuntimeException(StrUtil.isBlank(mes)? "全量更新正在进行中" : mes);
+        }
+        stringRedisTemplate.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 = HandleXiaomanData.handleAllCustomer(str, collect);
+                int totalItem = customerListApiVo.getTotalItem();
+                totalPage = (totalItem / XiaomanContant.PAGE_SIZE) + (totalItem % XiaomanContant.PAGE_SIZE > 0 ? 1 : 0);
+                pageIndex++;
+            } while (pageIndex <= totalPage);
+        } catch (Exception e) {
+            e.printStackTrace();
+            stringRedisTemplate.delete(XiaomanContant.TASK_STATUS_KEY);
+            stringRedisTemplate.opsForValue().set(XiaomanContant.TASK_STATUS_DESC_KEY, "全量更新出现异常,已停止");
+            return;
+        }
+        stringRedisTemplate.delete(XiaomanContant.TASK_STATUS_KEY);
+        stringRedisTemplate.opsForValue().set(XiaomanContant.TASK_STATUS_DESC_KEY, "全量更新完成,完成时间:" + DateUtil.now());
+    }
+
+    /**
+     * 设置key-value并设置过期时间
+     * @author hj
+     * @date 2024/4/6 21:14
+     * @param key
+     * @param value
+     * @param seconds
+     * @return boolean
+     */
+    @Override
+    public boolean setNxWithExpiration(String key, String value, long seconds) {
+        Boolean result = stringRedisTemplate.opsForValue().setIfAbsent(key, value);
+        if (result != null && result) {
+            // 设置过期时间
+            stringRedisTemplate.expire(key, seconds, TimeUnit.SECONDS);
+            return true;
+        }
+        return false;
+    }
+
+
+    /**
+     * 获取客户详情
+     * @author hj
+     * @date 2024/4/6 21:38
+     * @param companyId
+     */
+    @Override
+    public 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
+     */
+    @Override
+    @Async
+    public 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 = HandleXiaomanData.handleAllCustomer(str, collect);
+                int totalItem = customerListApiVo.getTotalItem();
+                totalPage = (totalItem / XiaomanContant.PAGE_SIZE) + (totalItem % XiaomanContant.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
+    @Override
+    public void getSelectorData() {
+        XiaomanConfig config = xiaomanConfigService.getConfig();
+        String token = config.getAccessToken();
+        Map<String, Object> params = new HashMap<>();
+        DynamicDataSourceContextHolder.push(SourceConstant.BASE);
+        SecurityUtils.setTenantId(XiaomanContant.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();
+    }
+
+
+
+
+
+
+
+    private static Map<String, Object> initPageParams(int page) {
+        Map<String, Object> params = new HashMap<>();
+        params.put("start_index", page);
+        params.put("count", XiaomanContant.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;
+    }
+
+
+    @Override
+    public 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);
+    }
+
+
+
+
+
+    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);
+        }
+    }
+
+
+
+
+    private 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(XiaomanContant.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 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(XiaomanContant.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);
+        }
+    }
+}

+ 7 - 3
hx-customer/src/main/java/com/fjhx/customer/service/xiaoman/impl/XiaomanCustomerServiceImpl.java

@@ -6,6 +6,7 @@ import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.json.JsonMapper;
 import com.fjhx.common.constant.SourceConstant;
+import com.fjhx.customer.contants.XiaomanContant;
 import com.fjhx.customer.entity.customer.po.Customer;
 import com.fjhx.customer.entity.customer.po.CustomerUser;
 import com.fjhx.customer.entity.xiaoman.po.XiaomanCustomer;
@@ -17,6 +18,7 @@ import com.fjhx.customer.handle.R;
 import com.fjhx.customer.mapper.xiaoman.XiaomanCustomerMapper;
 import com.fjhx.customer.service.customer.CustomerService;
 import com.fjhx.customer.service.customer.CustomerUserService;
+import com.fjhx.customer.service.xiaoman.XiaomanApiService;
 import com.fjhx.customer.service.xiaoman.XiaomanCustomerService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.fjhx.tenant.entity.dict.dto.DictTenantDataSelectDto;
@@ -63,6 +65,8 @@ public class XiaomanCustomerServiceImpl extends ServiceImpl<XiaomanCustomerMappe
 
     @Resource
     private SysUserServiceImpl sysUserService;
+    @Resource
+    private XiaomanApiService xiaomanApiService;
 
     @Override
     public List<XiaomanCustomerVo> getList(XiaomanCustomerSelectDto dto) {
@@ -110,7 +114,7 @@ public class XiaomanCustomerServiceImpl extends ServiceImpl<XiaomanCustomerMappe
                 if (allCustomer.contains(customer.getCompanyId())){
 
                     //TODO 获取详情
-                    String customerDetailJson = HandleXiaomanData.getCustomerDetail(customer.getCompanyId());
+                    String customerDetailJson = xiaomanApiService.getCustomerDetail(customer.getCompanyId());
                     XiaomanCustomerInfoJson xiaomanCustomerInfoJson = new XiaomanCustomerInfoJson();
                     xiaomanCustomerInfoJson.setCompanyId(customer.getCompanyId());
                     xiaomanCustomerInfoJson.setJson(customerDetailJson);
@@ -124,7 +128,7 @@ public class XiaomanCustomerServiceImpl extends ServiceImpl<XiaomanCustomerMappe
                 }else {
 
                     //TODO 获取详情
-                    String customerDetailJson = HandleXiaomanData.getCustomerDetail(customer.getCompanyId());
+                    String customerDetailJson = xiaomanApiService.getCustomerDetail(customer.getCompanyId());
                     XiaomanCustomerInfoJson xiaomanCustomerInfoJson = new XiaomanCustomerInfoJson();
                     xiaomanCustomerInfoJson.setCompanyId(customer.getCompanyId());
                     xiaomanCustomerInfoJson.setJson(customerDetailJson);
@@ -152,7 +156,7 @@ public class XiaomanCustomerServiceImpl extends ServiceImpl<XiaomanCustomerMappe
      * @param vo
      */
     public Long customerConversion(CustomerInfoVo vo) {
-        SecurityUtils.setTenantId(HandleXiaomanData.TENANT_ID);
+        SecurityUtils.setTenantId(XiaomanContant.TENANT_ID);
         List<Customer> list = customerService.lambdaQuery()
                 .eq(StringUtils.isNotEmpty(vo.getName()), Customer::getName, vo.getName())
                 .or()