Kaynağa Gözat

Merge remote-tracking branch 'origin/xiaoman'

yzc 1 yıl önce
ebeveyn
işleme
31ab3ff396

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

@@ -27,4 +27,6 @@ public class XiaomanContant {
     public static final int PAGE_SIZE = 500;
     public static final String TENANT_ID = "ehsd";
 
+    /**小满redis缓存key**/
+    public static final String TOKEN_KEY = "xiaoman_token_key";
 }

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

@@ -29,21 +29,24 @@ public class XiaomanInitializers {
     public void dataInitializer() {
         // 初始化数据的代码
         log.info("XiaomanInitializers dataInitializer");
+        //缓存token
+        xiaomanConfigService.cacheToken();
         //刷新token
         refreshToken();
         //获取全量数据
         try {
-//            xiaomanApiService.initAllList();
+            xiaomanApiService.updateList(8);
         } catch (Exception e) {
             log.error("获取小满全量数据异常", e);
         }
+        log.info("XiaomanInitializers finish dataInitializer");
     }
 
 
     @Scheduled(cron = "0 0 0/8 * * ? ")
     void refreshToken(){
         //如果过期时间是空的,就获取新的token
-        XiaomanConfig config = xiaomanConfigService.getConfig();
+        XiaomanConfig config = xiaomanConfigService.getCache();
 
         Date expireTime = config.getExpireTime();
         if(ObjectUtil.isNull(expireTime) || StrUtil.isBlank(config.getRefreshToken())) {

+ 9 - 2
hx-customer/src/main/java/com/fjhx/customer/service/xiaoman/XiaomanConfigService.java

@@ -23,11 +23,18 @@ public interface XiaomanConfigService extends BaseService<XiaomanConfig> {
 
     void refreshToken(XiaomanConfig config);
 
-    XiaomanConfig getConfig();
-
     Page<XiaomanConfigVo> getPage(XiaomanConfigSelectDto dto);
 
     XiaomanConfigVo detail(Long id);
 
     void edit(XiaomanConfigDto xiaomanConfigDto);
+
+    /**
+     * 缓存token
+     * @author hj
+     * @date 2024/4/11 17:29
+     */
+    void cacheToken();
+
+    XiaomanConfig getCache();
 }

+ 10 - 7
hx-customer/src/main/java/com/fjhx/customer/service/xiaoman/impl/XiaomanApiServiceImpl.java

@@ -65,13 +65,15 @@ public class XiaomanApiServiceImpl implements XiaomanApiService {
         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 {
+                XiaomanConfig config = xiaomanConfigService.getCache();
+                String token = config.getAccessToken();
+
                 String str = getData(XiaomanContant.ALL_CUSTOMER_API_URL, token, initPageParams(pageIndex));
                 CustomerListApiVo customerListApiVo = HandleXiaomanData.handleAllCustomer(str, collect,true);
                 int totalItem = customerListApiVo.getTotalItem();
@@ -117,7 +119,7 @@ public class XiaomanApiServiceImpl implements XiaomanApiService {
      */
     @Override
     public String getCustomerDetail(Long companyId){
-        XiaomanConfig config = xiaomanConfigService.getConfig();
+        XiaomanConfig config = xiaomanConfigService.getCache();
         String token = config.getAccessToken();
         Map<String, Object> params = new HashMap<>();
         params.put("company_id", companyId);
@@ -139,13 +141,14 @@ public class XiaomanApiServiceImpl implements XiaomanApiService {
             //先更新一下数据字典
             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 {
+                XiaomanConfig config = xiaomanConfigService.getCache();
+                String token = config.getAccessToken();
+
                 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, -hours).toString("yyyy-MM-dd HH:00:00"));
@@ -176,7 +179,7 @@ public class XiaomanApiServiceImpl implements XiaomanApiService {
     @DSTransactional
     @Override
     public void getSelectorData() {
-        XiaomanConfig config = xiaomanConfigService.getConfig();
+        XiaomanConfig config = xiaomanConfigService.getCache();
         String token = config.getAccessToken();
         Map<String, Object> params = new HashMap<>();
         DynamicDataSourceContextHolder.push(SourceConstant.BASE);
@@ -252,7 +255,7 @@ public class XiaomanApiServiceImpl implements XiaomanApiService {
 
 
         String body = JSON.toJSONString(xiaomanUpdateInfoVO);
-        XiaomanConfig config = xiaomanConfigService.getConfig();
+        XiaomanConfig config = xiaomanConfigService.getCache();
         String token = config.getAccessToken();
         updateInfoData(token, body);
     }

+ 31 - 12
hx-customer/src/main/java/com/fjhx/customer/service/xiaoman/impl/XiaomanConfigServiceImpl.java

@@ -12,6 +12,7 @@ import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONObject;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.fjhx.customer.contants.XiaomanContant;
 import com.fjhx.customer.entity.xiaoman.dto.XiaomanConfigDto;
 import com.fjhx.customer.entity.xiaoman.dto.XiaomanConfigSelectDto;
 import com.fjhx.customer.entity.xiaoman.dto.XiaomanCustomerDto;
@@ -22,10 +23,12 @@ import com.fjhx.customer.entity.xiaoman.vo.XiaomanConfigVo;
 import com.fjhx.customer.entity.xiaoman.vo.XiaomanCustomerVo;
 import com.fjhx.customer.mapper.xiaoman.XiaomanConfigMapper;
 import com.fjhx.customer.service.xiaoman.XiaomanConfigService;
+import com.ruoyi.common.core.redis.RedisCache;
 import com.ruoyi.common.utils.wrapper.IWrapper;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
+import javax.annotation.Resource;
 import java.nio.charset.Charset;
 import java.util.HashMap;
 import java.util.List;
@@ -42,6 +45,9 @@ import static com.ruoyi.common.utils.wrapper.IWrapper.getWrapper;
 @Service
 @Slf4j
 public class XiaomanConfigServiceImpl extends ServiceImpl<XiaomanConfigMapper, XiaomanConfig> implements XiaomanConfigService {
+    @Resource
+    RedisCache redisCache;
+
 
     @Override
     public void getToken(XiaomanConfig config) {
@@ -66,6 +72,7 @@ public class XiaomanConfigServiceImpl extends ServiceImpl<XiaomanConfigMapper, X
         jsonObject.put("refresh_token", config.getRefreshToken());
         XiaoManTokenVO token = getToken(config, jsonObject.toJSONString(), config.getRefreshTokenUrl());
         updateConfig(config, token);
+        cacheToken();
     }
 
     /**
@@ -84,6 +91,7 @@ public class XiaomanConfigServiceImpl extends ServiceImpl<XiaomanConfigMapper, X
         String body = jsonObject.toJSONString();
         XiaoManTokenVO token = getToken(config, body, config.getTokenUrl());
         updateConfig(config, token);
+        cacheToken();
     }
 
     private void updateConfig(XiaomanConfig config, XiaoManTokenVO token) {
@@ -114,17 +122,6 @@ public class XiaomanConfigServiceImpl extends ServiceImpl<XiaomanConfigMapper, X
     }
 
 
-    @Override
-    public XiaomanConfig getConfig() {
-        List<XiaomanConfig> list = this.list();
-        if (CollectionUtil.isEmpty(list)) {
-            throw new RuntimeException("小满配置表数据为空");
-        } else if (list.size() > 1) {
-            throw new RuntimeException("小满配置表数据大于1条");
-        }
-        return list.get(0);
-    }
-
 
     @Override
     public Page<XiaomanConfigVo> getPage(XiaomanConfigSelectDto dto) {
@@ -146,6 +143,28 @@ public class XiaomanConfigServiceImpl extends ServiceImpl<XiaomanConfigMapper, X
                 .set(XiaomanConfig::getPassword, StrUtil.isNotBlank(xiaomanConfigDto.getPassword())? DigestUtil.sha256Hex(xiaomanConfigDto.getPassword()) : DigestUtil.sha256Hex("#"))
                 .eq(XiaomanConfig::getId, xiaomanConfigDto.getId())
                 .update();
-        this.getToken(this.getConfig());
+        this.getToken(this.getCache());
+    }
+
+
+    @Override
+    public void cacheToken() {
+        redisCache.setCacheObject(XiaomanContant.TOKEN_KEY, this.getConfig());
+    }
+
+    @Override
+    public XiaomanConfig getCache() {
+        return redisCache.getCacheObject(XiaomanContant.TOKEN_KEY);
+    }
+
+
+    private XiaomanConfig getConfig() {
+        List<XiaomanConfig> list = this.list();
+        if (CollectionUtil.isEmpty(list)) {
+            throw new RuntimeException("小满配置表数据为空");
+        } else if (list.size() > 1) {
+            throw new RuntimeException("小满配置表数据大于1条");
+        }
+        return list.get(0);
     }
 }

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

@@ -157,7 +157,7 @@ public class XiaomanCustomerServiceImpl extends ServiceImpl<XiaomanCustomerMappe
                     System.out.println("2");
                 }
             }catch (Exception e){
-               e.printStackTrace();
+                e.printStackTrace();
             }
         }
     }