|
@@ -0,0 +1,337 @@
|
|
|
+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;
|
|
|
+
|
|
|
+// @DSTransactional
|
|
|
+ @Override
|
|
|
+ @Async
|
|
|
+ public void initAllList() {
|
|
|
+ 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 {
|
|
|
+ getSelectorData();
|
|
|
+
|
|
|
+ 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 {
|
|
|
+ //先更新一下数据字典
|
|
|
+ getSelectorData();
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void dealCustomerTagDict(String label,String key) {
|
|
|
+
|
|
|
+ DictTenantData dictTenantData = new DictTenantData();
|
|
|
+ dictTenantData.setDictCode("customer_tag");
|
|
|
+ dictTenantData.setDictValue(label);
|
|
|
+ dictTenantData.setDictKey(key);
|
|
|
+ dictTenantData.setTenantId(XiaomanContant.TENANT_ID);
|
|
|
+ dictTenantData.setSort(100);
|
|
|
+ dictTenantDataService.save(dictTenantData);
|
|
|
+
|
|
|
+ }
|
|
|
+}
|