Bläddra i källkod

update 优化

ct 1 dag sedan
förälder
incheckning
6554741d62

+ 36 - 17
fs-admin/src/main/java/com/fs/api/controller/IndexStatisticsController.java

@@ -73,15 +73,15 @@ public class IndexStatisticsController {
             userType = 0;
         }
         if(medicalMallConfig.isStatics()|| (param.getCompanyId() == null && param.getDeptId() == null) || (param.getCompanyId() == null && param.getDeptId() == 1)){
-            analysisPreviewDTO = redisCache.getCacheObject(String.format("%s:%d:%d",DATA_OVERVIEW_DEALER_ANALYSISPREVIEW,type,userType));
+            analysisPreviewDTO = redisCache.getCacheObject(String.format("%s:%d:%d",DATA_OVERVIEW_DEALER_ANALYSISPREVIEW,type,userType), AnalysisPreviewDTO.class);
         }else if(param.getCompanyId() != null){
-            analysisPreviewDTO = redisCache.getCacheObject(String.format("%s:%d:%d:%d",DATA_OVERVIEW_DEALER_ANALYSISPREVIEW,type,userType,param.getCompanyId()));
+            analysisPreviewDTO = redisCache.getCacheObject(String.format("%s:%d:%d:%d",DATA_OVERVIEW_DEALER_ANALYSISPREVIEW,type,userType,param.getCompanyId()), AnalysisPreviewDTO.class);
         }else{
             Company company = new Company();
             company.setDeptId(param.getDeptId());
             Long[] companyIds = companyService.selectCompanyList(company).stream().map(Company::getCompanyId).toArray(Long[]::new);
             for(Long companyId : companyIds){
-                AnalysisPreviewDTO clildDTO = redisCache.getCacheObject(String.format("%s:%d:%d:%d",DATA_OVERVIEW_DEALER_ANALYSISPREVIEW,type,userType,companyId));
+                AnalysisPreviewDTO clildDTO = redisCache.getCacheObject(String.format("%s:%d:%d:%d",DATA_OVERVIEW_DEALER_ANALYSISPREVIEW,type,userType,companyId), AnalysisPreviewDTO.class);
                 if (clildDTO != null) {
                     //将除了completedRate、correctRate和watchRate之外的数据相加,并计算completedRate、correctRate和watchRate
                     analysisPreviewDTO.setWatchUserCount(
@@ -147,15 +147,18 @@ public class IndexStatisticsController {
     public R rechargeComsumption(StatisticsDeptCompanyParam param){
         ConsumptionBalanceDataDTO consumptionBalanceDataDTO = new ConsumptionBalanceDataDTO();
         if(medicalMallConfig.isStatics() || (param.getCompanyId() == null && param.getDeptId() == null) || (param.getCompanyId() == null && param.getDeptId() == 1)) {
-            consumptionBalanceDataDTO = redisCache.getCacheObject(StatisticsRedisConstant.DATA_OVERVIEW_DEALER_BALANCE);
+            consumptionBalanceDataDTO = redisCache.getCacheObject(StatisticsRedisConstant.DATA_OVERVIEW_DEALER_BALANCE, ConsumptionBalanceDataDTO.class);
         }else if(param.getCompanyId() != null){
-            consumptionBalanceDataDTO = redisCache.getCacheObject(String.format("%s:%d",DATA_OVERVIEW_DEALER_BALANCE,param.getCompanyId()));
+            consumptionBalanceDataDTO = redisCache.getCacheObject(String.format("%s:%d",DATA_OVERVIEW_DEALER_BALANCE,param.getCompanyId()), ConsumptionBalanceDataDTO.class);
         }else{
             Company company = new Company();
             company.setDeptId(param.getDeptId());
             Long[] companyIds = companyService.selectCompanyList(company).stream().map(Company::getCompanyId).toArray(Long[]::new);
             for(Long companyId : companyIds){
-                ConsumptionBalanceDataDTO clildDTO = redisCache.getCacheObject(String.format("%s:%d",DATA_OVERVIEW_DEALER_BALANCE,companyId));
+                ConsumptionBalanceDataDTO clildDTO = redisCache.getCacheObject(String.format("%s:%d",DATA_OVERVIEW_DEALER_BALANCE,companyId), ConsumptionBalanceDataDTO.class);
+                if (clildDTO == null) {
+                    continue;
+                }
                 consumptionBalanceDataDTO.setBalance(
                     (consumptionBalanceDataDTO.getBalance() == null ? BigDecimal.ZERO : consumptionBalanceDataDTO.getBalance())
                     .add(clildDTO.getBalance() == null ? BigDecimal.ZERO : clildDTO.getBalance())
@@ -170,7 +173,7 @@ public class IndexStatisticsController {
                 );
             }
         }
-        BigDecimal redPacketCompanyMoney = redisCache.getCacheObject("redpacket_money");
+        BigDecimal redPacketCompanyMoney = redisCache.getCacheObject("redpacket_money", BigDecimal.class);
         if(ObjectUtils.isNull(redPacketCompanyMoney)){
             redPacketCompanyMoney = BigDecimal.ZERO;
         }
@@ -189,7 +192,7 @@ public class IndexStatisticsController {
     public R getTrafficLog(StatisticsDeptCompanyParam  param){
         TrafficLogDTO result = new TrafficLogDTO();
         if(!medicalMallConfig.isStatics() || (param.getCompanyId() == null && param.getDeptId() == null) || (param.getCompanyId() == null && param.getDeptId() == 1)) {
-            result = redisCache.getCacheObject(DATA_OVERVIEW_TRAFFIC_LOG);
+            result = redisCache.getCacheObject(DATA_OVERVIEW_TRAFFIC_LOG, TrafficLogDTO.class);
             if (!medicalMallConfig.isStatics()) {
                 SysConfig sysConfig = sysConfigService.selectConfigByConfigKey("redPacket.Traffic.config");
                 if(result == null || sysConfig == null) {
@@ -621,9 +624,15 @@ public class IndexStatisticsController {
     public R dealerAggregated(StatisticsDeptCompanyParam param){
         DealerAggregatedDTO result = new DealerAggregatedDTO();
         if (!medicalMallConfig.isStatics() || (param.getCompanyId() == null && param.getDeptId() == null) || (param.getCompanyId() == null && param.getDeptId() == 1)) {
-            result = redisCache.getCacheObject(StatisticsRedisConstant.DATA_OVERVIEW_DEALER_AGGREGATED);
+            result = redisCache.getCacheObject(StatisticsRedisConstant.DATA_OVERVIEW_DEALER_AGGREGATED, DealerAggregatedDTO.class);
+            if (result == null) {
+                result = new DealerAggregatedDTO();
+            }
         }else if (param.getCompanyId() != null) {
-            result = redisCache.getCacheObject(String.format("%s:%d", DATA_OVERVIEW_DEALER_AGGREGATED, param.getCompanyId()));
+            result = redisCache.getCacheObject(String.format("%s:%d", DATA_OVERVIEW_DEALER_AGGREGATED, param.getCompanyId()), DealerAggregatedDTO.class);
+            if (result == null) {
+                result = new DealerAggregatedDTO();
+            }
         //没选中销售公司,部门不为总公司
         }else{
             //Long padMaxNum = deptLimiteService.selectById(param.getDeptId()).getMaxPadNum();
@@ -631,7 +640,7 @@ public class IndexStatisticsController {
             company.setDeptId(param.getDeptId());
             Long[] companyIds = companyService.selectCompanyList(company).stream().map(Company::getCompanyId).toArray(Long[]::new);
             for(Long companyId : companyIds) {
-                DealerAggregatedDTO dealerAggregatedDTO = redisCache.getCacheObject(String.format("%s:%d", DATA_OVERVIEW_DEALER_AGGREGATED, companyId));
+                DealerAggregatedDTO dealerAggregatedDTO = redisCache.getCacheObject(String.format("%s:%d", DATA_OVERVIEW_DEALER_AGGREGATED, companyId), DealerAggregatedDTO.class);
                 // 添加空值检查
                 if (dealerAggregatedDTO != null) {
                     result.setDealderCount((result.getDealderCount() == null ? 0 : result.getDealderCount())
@@ -738,9 +747,11 @@ public class IndexStatisticsController {
     @GetMapping("/thisMonthOrderCount")
     public R thisMonthOrderCount(StatisticsDeptCompanyParam  param){
         if (!medicalMallConfig.isStatics() || (param.getCompanyId() == null && param.getDeptId() == null) || (param.getCompanyId() == null && param.getDeptId() == 1)){
-            return redisCache.getCacheObject(StatisticsRedisConstant.THIS_MONTH_ORDER_COUNT);
+            R cached = redisCache.getCacheObject(StatisticsRedisConstant.THIS_MONTH_ORDER_COUNT, R.class);
+            return cached != null ? cached : R.ok();
         }else if(param.getCompanyId() != null){
-            return redisCache.getCacheObject(String.format("%s:%d", THIS_MONTH_ORDER_COUNT, param.getCompanyId()));
+            R cached = redisCache.getCacheObject(String.format("%s:%d", THIS_MONTH_ORDER_COUNT, param.getCompanyId()), R.class);
+            return cached != null ? cached : R.ok();
         }else{
             Company company = new Company();
             company.setDeptId(param.getDeptId());
@@ -749,7 +760,10 @@ public class IndexStatisticsController {
             List<Integer> orderCountList = new ArrayList<>();
             List<Integer> payPriceList = new ArrayList<>();
             for(Long companyId : companyIds) {
-                R result = redisCache.getCacheObject(String.format("%s:%d", THIS_MONTH_ORDER_COUNT, companyId));
+                R result = redisCache.getCacheObject(String.format("%s:%d", THIS_MONTH_ORDER_COUNT, companyId), R.class);
+                if (result == null) {
+                    continue;
+                }
                 Object datas = result.get("datas");
                 Object orderCount = result.get("orderCount");
                 Object payPrice = result.get("payPrice");
@@ -769,9 +783,11 @@ public class IndexStatisticsController {
     @GetMapping("/thisMonthRecvCount")
     public R thisMonthRecvCount(StatisticsDeptCompanyParam  param){
         if (!medicalMallConfig.isStatics() || (param.getCompanyId() == null && param.getDeptId() == null) || (param.getCompanyId() == null && param.getDeptId() == 1)){
-            return redisCache.getCacheObject(StatisticsRedisConstant.THIS_MONTH_RECV_COUNT);
+            R cached = redisCache.getCacheObject(StatisticsRedisConstant.THIS_MONTH_RECV_COUNT, R.class);
+            return cached != null ? cached : R.ok();
         }else if(param.getCompanyId() != null){
-            return redisCache.getCacheObject(String.format("%s:%d", THIS_MONTH_RECV_COUNT, param.getCompanyId()));
+            R cached = redisCache.getCacheObject(String.format("%s:%d", THIS_MONTH_RECV_COUNT, param.getCompanyId()), R.class);
+            return cached != null ? cached : R.ok();
         }else{
             Company company = new Company();
             company.setDeptId(param.getDeptId());
@@ -780,7 +796,10 @@ public class IndexStatisticsController {
             List<Integer> orderCountList = new ArrayList<>();
             List<Float> payMoneyList = new ArrayList<>();
             for(Long companyId : companyIds) {
-               R result = redisCache.getCacheObject(String.format("%s:%d", THIS_MONTH_RECV_COUNT, companyId));
+               R result = redisCache.getCacheObject(String.format("%s:%d", THIS_MONTH_RECV_COUNT, companyId), R.class);
+               if (result == null) {
+                   continue;
+               }
                Object datas = result.get("datas");
                Object orderCount = result.get("orderCount");
                Object payMoney = result.get("payMoney");

+ 40 - 0
fs-common/src/main/java/com/fs/common/core/redis/RedisCache.java

@@ -1,9 +1,11 @@
 package com.fs.common.core.redis;
 
+import java.math.BigDecimal;
 import java.util.*;
 import java.util.concurrent.TimeUnit;
 
 import com.alibaba.fastjson2.JSON;
+import com.fs.common.core.domain.R;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.BoundSetOperations;
 import org.springframework.data.redis.core.HashOperations;
@@ -87,6 +89,7 @@ public class RedisCache
     /**
      * 按目标类型读取缓存(兼容关闭 Fastjson AutoType 后 Redis 返回 JSONObject 的情况)。
      */
+    @SuppressWarnings("unchecked")
     public <T> T getCacheObject(final String key, final Class<T> clazz)
     {
         Object value = redisTemplate.opsForValue().get(key);
@@ -96,6 +99,27 @@ public class RedisCache
         if (clazz.isInstance(value)) {
             return clazz.cast(value);
         }
+        // R 继承 HashMap,优先 putAll
+        if (R.class.equals(clazz) && value instanceof Map) {
+            R r = new R();
+            r.putAll((Map<? extends String, ?>) value);
+            return (T) r;
+        }
+        if (clazz == Long.class && value instanceof Number) {
+            return (T) Long.valueOf(((Number) value).longValue());
+        }
+        if (clazz == Integer.class && value instanceof Number) {
+            return (T) Integer.valueOf(((Number) value).intValue());
+        }
+        if (clazz == BigDecimal.class) {
+            if (value instanceof BigDecimal) {
+                return (T) value;
+            }
+            return (T) new BigDecimal(String.valueOf(value));
+        }
+        if (clazz == String.class) {
+            return (T) String.valueOf(value);
+        }
         try {
             return JSON.parseObject(JSON.toJSONString(value), clazz);
         } catch (Exception e) {
@@ -103,6 +127,22 @@ public class RedisCache
         }
     }
 
+    /**
+     * 读取 setCacheObject 存入的 List
+     */
+    public <T> List<T> getCacheObjectList(final String key, final Class<T> elementClass)
+    {
+        Object value = redisTemplate.opsForValue().get(key);
+        if (value == null) {
+            return null;
+        }
+        try {
+            return JSON.parseArray(JSON.toJSONString(value), elementClass);
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
     /**
      * 删除单个对象
      *

+ 45 - 15
fs-company/src/main/java/com/fs/company/controller/company/IndexStatisticsController.java

@@ -56,7 +56,9 @@ public class IndexStatisticsController {
         if(type == 5) {
             analysisPreviewDTO = statisticsCompanyService.analysisPreview(param);
         } else {
-            analysisPreviewDTO = redisCache.getCacheObject(String.format("%s:%d:%d:%d",DATA_OVERVIEW_DEALER_ANALYSISPREVIEW,type,userType,param.getCompanyId()));
+            analysisPreviewDTO = redisCache.getCacheObject(
+                    String.format("%s:%d:%d:%d",DATA_OVERVIEW_DEALER_ANALYSISPREVIEW,type,userType,param.getCompanyId()),
+                    AnalysisPreviewDTO.class);
         }
 
         return R.ok().put("data",analysisPreviewDTO);
@@ -71,7 +73,9 @@ public class IndexStatisticsController {
         LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
         Long companyId = loginUser.getCompany().getCompanyId();
 
-        ConsumptionBalanceDataDTO consumptionBalanceDataDTO = redisCache.getCacheObject(String.format("%s:%d",StatisticsRedisConstant.DATA_OVERVIEW_DEALER_BALANCE,companyId));
+        ConsumptionBalanceDataDTO consumptionBalanceDataDTO = redisCache.getCacheObject(
+                String.format("%s:%d",StatisticsRedisConstant.DATA_OVERVIEW_DEALER_BALANCE,companyId),
+                ConsumptionBalanceDataDTO.class);
         return R.ok().put("data", consumptionBalanceDataDTO);
     }
 
@@ -83,7 +87,9 @@ public class IndexStatisticsController {
     public R getTrafficLog(){
         LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
         Long companyId = loginUser.getCompany().getCompanyId();
-        TrafficLogDTO trafficLogDTO = redisCache.getCacheObject(String.format("%s:%d",DATA_OVERVIEW_TRAFFIC_LOG,companyId));
+        TrafficLogDTO trafficLogDTO = redisCache.getCacheObject(
+                String.format("%s:%d",DATA_OVERVIEW_TRAFFIC_LOG,companyId),
+                TrafficLogDTO.class);
         return R.ok().put("data",trafficLogDTO);
     }
 
@@ -109,7 +115,10 @@ public class IndexStatisticsController {
             deaMemberTopTenDTOS = statisticsCompanyService.watchEndPlayTrend(param);
         } else {
             String key = String.format("%s:%d:%d:%d", DATA_OVERVIEW_DEALER_CHARTS, type,userType,param.getCompanyId());
-            deaMemberTopTenDTOS = redisCache.getCacheObject(key);
+            deaMemberTopTenDTOS = redisCache.getCacheObjectList(key, WatchEndPlayTrendDTO.class);
+            if (deaMemberTopTenDTOS == null) {
+                deaMemberTopTenDTOS = new ArrayList<>();
+            }
         }
 
         return R.ok().put("data", deaMemberTopTenDTOS);
@@ -137,7 +146,12 @@ public class IndexStatisticsController {
         if(type == 5) {
             deaMemberTopTenDTOS = statisticsCompanyService.deaMemberTopTen(param);
         } else {
-            deaMemberTopTenDTOS = redisCache.getCacheObject(String.format("%s:%d:%d:%d:%d", CHARTS_MEMBER_TOP_TEN_WATCH, type, statisticalType,userType,param.getCompanyId()));
+            deaMemberTopTenDTOS = redisCache.getCacheObjectList(
+                    String.format("%s:%d:%d:%d:%d", CHARTS_MEMBER_TOP_TEN_WATCH, type, statisticalType,userType,param.getCompanyId()),
+                    DeaMemberTopTenDTO.class);
+            if (deaMemberTopTenDTOS == null) {
+                deaMemberTopTenDTOS = new ArrayList<>();
+            }
         }
         return R.ok().put("data", deaMemberTopTenDTOS);
     }
@@ -154,7 +168,9 @@ public class IndexStatisticsController {
         Long companyId = loginUser.getCompany().getCompanyId();
         param.setCompanyId(companyId);
 
-        List<RewardMoneyTopTenDTO> rewardMoneyTopTenDTOS = redisCache.getCacheObject( String.format("%s:%d:%d:%d:%d", CHARTS_REWARD_MONEY_TOP_TEN, type,dataType,userType,param.getCompanyId()));
+        List<RewardMoneyTopTenDTO> rewardMoneyTopTenDTOS = redisCache.getCacheObjectList(
+                String.format("%s:%d:%d:%d:%d", CHARTS_REWARD_MONEY_TOP_TEN, type,dataType,userType,param.getCompanyId()),
+                RewardMoneyTopTenDTO.class);
         return R.ok().put("data", rewardMoneyTopTenDTOS);
     }
 
@@ -169,7 +185,9 @@ public class IndexStatisticsController {
         Long companyId = loginUser.getCompany().getCompanyId();
         param.setCompanyId(companyId);
 
-        List<RewardMoneyTrendDTO> rewardMoneyTrendDTOS = redisCache.getCacheObject( String.format("%s:%d:%d:%d", CHARTS_REWARD_MONEY_TREND, type,userType,param.getCompanyId()));
+        List<RewardMoneyTrendDTO> rewardMoneyTrendDTOS = redisCache.getCacheObjectList(
+                String.format("%s:%d:%d:%d", CHARTS_REWARD_MONEY_TREND, type,userType,param.getCompanyId()),
+                RewardMoneyTrendDTO.class);
         return R.ok().put("data", rewardMoneyTrendDTOS);
     }
 
@@ -186,7 +204,9 @@ public class IndexStatisticsController {
         Long companyId = loginUser.getCompany().getCompanyId();
         param.setCompanyId(companyId);
 
-        List<CourseStatsDTO> courseStatsDTOS = redisCache.getCacheObject(String.format("%s:%d:%d:%d:%s:%d", CHARTS_WATCH_TOP_TEN, type,statisticalType,userType,sort,param.getCompanyId()));
+        List<CourseStatsDTO> courseStatsDTOS = redisCache.getCacheObjectList(
+                String.format("%s:%d:%d:%d:%s:%d", CHARTS_WATCH_TOP_TEN, type,statisticalType,userType,sort,param.getCompanyId()),
+                CourseStatsDTO.class);
         return R.ok().put("data", courseStatsDTOS);
     }
 
@@ -198,7 +218,9 @@ public class IndexStatisticsController {
         LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
         Long companyId = loginUser.getCompany().getCompanyId();
 
-        DealerAggregatedDTO dealerAggregatedDTO = redisCache.getCacheObject(String.format("%s:%d",StatisticsRedisConstant.DATA_OVERVIEW_DEALER_AGGREGATED,companyId));
+        DealerAggregatedDTO dealerAggregatedDTO = redisCache.getCacheObject(
+                String.format("%s:%d",StatisticsRedisConstant.DATA_OVERVIEW_DEALER_AGGREGATED,companyId),
+                DealerAggregatedDTO.class);
 
         return R.ok().put("data",dealerAggregatedDTO);
     }
@@ -211,7 +233,9 @@ public class IndexStatisticsController {
         LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
         Long companyId = loginUser.getCompany().getCompanyId();
 
-        Long smsBalance = redisCache.getCacheObject(String.format("%s:%d",StatisticsRedisConstant.DATA_OVERVIEW_DEALER_SMS_BALANCE,companyId));
+        Long smsBalance = redisCache.getCacheObject(
+                String.format("%s:%d",StatisticsRedisConstant.DATA_OVERVIEW_DEALER_SMS_BALANCE,companyId),
+                Long.class);
 
         return R.ok().put("data", smsBalance);
     }
@@ -225,7 +249,9 @@ public class IndexStatisticsController {
         LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
         Long companyId = loginUser.getCompany().getCompanyId();
 
-        AuthorizationInfoDTO authorizationInfoDTO = redisCache.getCacheObject(String.format("%s:%d",StatisticsRedisConstant.DATA_OVERVIEW_DEALER_AUTHORIZATION_INFO,companyId));
+        AuthorizationInfoDTO authorizationInfoDTO = redisCache.getCacheObject(
+                String.format("%s:%d",StatisticsRedisConstant.DATA_OVERVIEW_DEALER_AUTHORIZATION_INFO,companyId),
+                AuthorizationInfoDTO.class);
 
         return R.ok().put("data", authorizationInfoDTO);
     }
@@ -240,8 +266,10 @@ public class IndexStatisticsController {
         LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
         Long companyId = loginUser.getCompany().getCompanyId();
 
-        R result = redisCache.getCacheObject(String.format("%s:%d",StatisticsRedisConstant.THIS_MONTH_ORDER_COUNT,companyId));
-        return result;
+        R result = redisCache.getCacheObject(
+                String.format("%s:%d",StatisticsRedisConstant.THIS_MONTH_ORDER_COUNT,companyId),
+                R.class);
+        return result != null ? result : R.ok();
     }
 
     /**
@@ -253,8 +281,10 @@ public class IndexStatisticsController {
         LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
         Long companyId = loginUser.getCompany().getCompanyId();
 
-        R result = redisCache.getCacheObject(String.format("%s:%d",StatisticsRedisConstant.THIS_MONTH_RECV_COUNT,companyId));
-        return result;
+        R result = redisCache.getCacheObject(
+                String.format("%s:%d",StatisticsRedisConstant.THIS_MONTH_RECV_COUNT,companyId),
+                R.class);
+        return result != null ? result : R.ok();
     }
 
     /**