2 Commity 32fd5d16fb ... 96fcbbb086

Autor SHA1 Wiadomość Data
  吴树波 96fcbbb086 Merge remote-tracking branch 'origin/master' 2 tygodni temu
  吴树波 41f4af880b 发送 2 tygodni temu

+ 233 - 0
fs-common/src/main/java/com/fs/common/core/redis/RedisCacheTenant.java

@@ -0,0 +1,233 @@
+package com.fs.common.core.redis;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.data.redis.core.BoundSetOperations;
+import org.springframework.data.redis.core.HashOperations;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.core.ValueOperations;
+import org.springframework.stereotype.Component;
+
+import java.util.*;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+/**
+ * spring redis 工具类
+ *
+
+ **/
+@SuppressWarnings(value = { "unchecked", "rawtypes" })
+@Component
+public class RedisCacheTenant<T> {
+    @Autowired
+    public RedisTemplate redisTemplate;
+    @Value("${tenant-id}")
+    private Long ymlTenantId;
+    private static final String PARENT_KEY = "tenant:cache:";
+
+    /**
+     * 缓存基本的对象,Integer、String、实体类等
+     *
+     * @param key 缓存的键值
+     * @param value 缓存的值
+     */
+    public void setCacheObject(final String key, final T value)
+    {
+        redisTemplate.opsForValue().set(buildKey(key), value);
+    }
+
+    /**
+     * 缓存基本的对象,Integer、String、实体类等
+     *
+     * @param key 缓存的键值
+     * @param value 缓存的值
+     * @param timeout 时间
+     * @param timeUnit 时间颗粒度
+     */
+    public void setCacheObject(final String key, final T value, final long timeout, final TimeUnit timeUnit)
+    {
+        redisTemplate.opsForValue().set(buildKey(key), value, timeout, timeUnit);
+    }
+
+    /**
+     * 设置有效时间
+     *
+     * @param key Redis键
+     * @param timeout 超时时间
+     * @return true=设置成功;false=设置失败
+     */
+    public boolean expire(final String key, final long timeout)
+    {
+        return expire(buildKey(key), timeout, TimeUnit.SECONDS);
+    }
+
+    /**
+     * 设置有效时间
+     *
+     * @param key Redis键
+     * @param timeout 超时时间
+     * @param unit 时间单位
+     * @return true=设置成功;false=设置失败
+     */
+    public boolean expire(final String key, final long timeout, final TimeUnit unit)
+    {
+        return redisTemplate.expire(buildKey(key), timeout, unit);
+    }
+
+    /**
+     * 获得缓存的基本对象。
+     *
+     * @param key 缓存键值
+     * @return 缓存键值对应的数据
+     */
+    public T getCacheObject(final String key)
+    {
+        ValueOperations<String, T> operation = redisTemplate.opsForValue();
+        return operation.get(buildKey(key));
+    }
+
+    /**
+     * 删除单个对象
+     *
+     * @param key
+     */
+    public boolean deleteObject(final String key)
+    {
+        return redisTemplate.delete(buildKey(key));
+    }
+
+
+    /**
+     * 缓存List数据
+     *
+     * @param key 缓存的键值
+     * @param dataList 待缓存的List数据
+     * @return 缓存的对象
+     */
+    public long setCacheList(final String key, final List<T> dataList)
+    {
+        Long count = redisTemplate.opsForList().rightPushAll(buildKey(key), dataList);
+        return count == null ? 0 : count;
+    }
+
+    /**
+     * 获得缓存的list对象
+     *
+     * @param key 缓存的键值
+     * @return 缓存键值对应的数据
+     */
+    public List<T> getCacheList(final String key)
+    {
+        return redisTemplate.opsForList().range(buildKey(key), 0, -1);
+    }
+    public List<T> getCacheListByPattern(String pattern) {
+        Set<String> keys = redisTemplate.keys(buildKey(pattern));
+        return redisTemplate.opsForValue().multiGet(keys);
+    }
+
+    /**
+     * 缓存Set
+     *
+     * @param key 缓存键值
+     * @param dataSet 缓存的数据
+     * @return 缓存数据的对象
+     */
+    public BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet)
+    {
+        BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(buildKey(key));
+        Iterator<T> it = dataSet.iterator();
+        while (it.hasNext())
+        {
+            setOperation.add(it.next());
+        }
+        return setOperation;
+    }
+
+    /**
+     * 获得缓存的set
+     *
+     * @param key
+     * @return
+     */
+    public Set<T> getCacheSet(final String key)
+    {
+        return redisTemplate.opsForSet().members(buildKey(key));
+    }
+
+    /**
+     * 缓存Map
+     *
+     * @param key
+     * @param dataMap
+     */
+    public void setCacheMap(final String key, final Map<String, T> dataMap)
+    {
+        if (dataMap != null) {
+            redisTemplate.opsForHash().putAll(buildKey(key), dataMap);
+        }
+    }
+
+    /**
+     * 获得缓存的Map
+     *
+     * @param key
+     * @return
+     */
+    public Map<String, T> getCacheMap(final String key)
+    {
+        return redisTemplate.opsForHash().entries(buildKey(key));
+    }
+
+    /**
+     * 往Hash中存入数据
+     *
+     * @param key Redis键
+     * @param hKey Hash键
+     * @param value 值
+     */
+    public void setCacheMapValue(final String key, final String hKey, final T value)
+    {
+        redisTemplate.opsForHash().put(buildKey(key), hKey, value);
+    }
+
+    /**
+     * 获取Hash中的数据
+     *
+     * @param key Redis键
+     * @param hKey Hash键
+     * @return Hash中的对象
+     */
+    public T getCacheMapValue(final String key, final String hKey)
+    {
+        HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
+        return opsForHash.get(buildKey(key), hKey);
+    }
+
+    /**
+     * 获取多个Hash中的数据
+     *
+     * @param key Redis键
+     * @param hKeys Hash键集合
+     * @return Hash对象集合
+     */
+    public List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys)
+    {
+        return redisTemplate.opsForHash().multiGet(buildKey(key), hKeys);
+    }
+
+    /**
+     * 获得缓存的基本对象列表
+     *
+     * @param pattern 字符串前缀
+     * @return 对象列表
+     */
+    public Collection<String> keys(final String pattern)
+    {
+        return redisTemplate.keys(buildKey(pattern));
+    }
+
+    private String buildKey(String key){
+        return PARENT_KEY + ymlTenantId + ":" + key;
+    }
+}

+ 2 - 1
fs-ipad-task/src/main/java/com/fs/app/service/IpadSendServer.java

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.fs.common.core.redis.RedisCache;
 import com.fs.common.core.redis.RedisCache;
+import com.fs.common.core.redis.RedisCacheTenant;
 import com.fs.common.exception.base.BaseException;
 import com.fs.common.exception.base.BaseException;
 import com.fs.common.utils.StringUtils;
 import com.fs.common.utils.StringUtils;
 import com.fs.common.utils.date.DateUtil;
 import com.fs.common.utils.date.DateUtil;
@@ -57,7 +58,7 @@ public class IpadSendServer {
     private final QwExternalContactMapper qwExternalContactMapper;
     private final QwExternalContactMapper qwExternalContactMapper;
     private final IFsCourseWatchLogService watchLogService;
     private final IFsCourseWatchLogService watchLogService;
     private final IQwUserVideoService qwUserVideoService;
     private final IQwUserVideoService qwUserVideoService;
-    private final RedisCache redisCache;
+    private final RedisCacheTenant<Long> redisCache;
     private final ICompanyMiniappService companyMiniappService;
     private final ICompanyMiniappService companyMiniappService;
     private final IFsCoursePlaySourceConfigService playSourceConfigService;
     private final IFsCoursePlaySourceConfigService playSourceConfigService;
     private final FsUserMapper fsUserMapper;
     private final FsUserMapper fsUserMapper;

+ 3 - 2
fs-ipad-task/src/main/java/com/fs/app/task/SendMsg.java

@@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.fs.app.service.IpadSendServer;
 import com.fs.app.service.IpadSendServer;
 import com.fs.common.annotation.TenantDataScope;
 import com.fs.common.annotation.TenantDataScope;
 import com.fs.common.core.redis.RedisCacheT;
 import com.fs.common.core.redis.RedisCacheT;
+import com.fs.common.core.redis.RedisCacheTenant;
 import com.fs.common.utils.DateUtils;
 import com.fs.common.utils.DateUtils;
 import com.fs.common.utils.PubFun;
 import com.fs.common.utils.PubFun;
 import com.fs.course.config.CourseConfig;
 import com.fs.course.config.CourseConfig;
@@ -59,7 +60,7 @@ public class SendMsg {
     private final SysConfigMapper sysConfigMapper;
     private final SysConfigMapper sysConfigMapper;
     private final IQwSopLogsService qwSopLogsService;
     private final IQwSopLogsService qwSopLogsService;
     private final QwIpadServerMapper qwIpadServerMapper;
     private final QwIpadServerMapper qwIpadServerMapper;
-    private final RedisCacheT<Long> redisCache;
+    private final RedisCacheTenant<Long> redisCache;
     private final AsyncSopTestService asyncSopTestService;
     private final AsyncSopTestService asyncSopTestService;
     private final IFsCoursePlaySourceConfigService fsCoursePlaySourceConfigService;
     private final IFsCoursePlaySourceConfigService fsCoursePlaySourceConfigService;
     private final QwPushCountMapper qwPushCountMapper;
     private final QwPushCountMapper qwPushCountMapper;
@@ -100,7 +101,7 @@ public class SendMsg {
     @Qualifier("customThreadPool")
     @Qualifier("customThreadPool")
     private ThreadPoolTaskExecutor customThreadPool;
     private ThreadPoolTaskExecutor customThreadPool;
 
 
-    public SendMsg(QwUserMapper qwUserMapper, QwSopLogsMapper qwSopLogsMapper, IpadSendServer sendServer, SysConfigMapper sysConfigMapper, IQwSopLogsService qwSopLogsService, QwIpadServerMapper qwIpadServerMapper, RedisCacheT<Long> redisCache, AsyncSopTestService asyncSopTestService, IFsCoursePlaySourceConfigService fsCoursePlaySourceConfigService, QwPushCountMapper qwPushCountMapper, QwRestrictionPushRecordMapper qwRestrictionPushRecordMapper, TenantDataSourceAspect tenantDataSourceAspect) {
+    public SendMsg(QwUserMapper qwUserMapper, QwSopLogsMapper qwSopLogsMapper, IpadSendServer sendServer, SysConfigMapper sysConfigMapper, IQwSopLogsService qwSopLogsService, QwIpadServerMapper qwIpadServerMapper, RedisCacheTenant<Long> redisCache, AsyncSopTestService asyncSopTestService, IFsCoursePlaySourceConfigService fsCoursePlaySourceConfigService, QwPushCountMapper qwPushCountMapper, QwRestrictionPushRecordMapper qwRestrictionPushRecordMapper, TenantDataSourceAspect tenantDataSourceAspect) {
         this.qwUserMapper = qwUserMapper;
         this.qwUserMapper = qwUserMapper;
         this.qwSopLogsMapper = qwSopLogsMapper;
         this.qwSopLogsMapper = qwSopLogsMapper;
         this.sendServer = sendServer;
         this.sendServer = sendServer;

+ 5 - 0
fs-ipad-task/src/main/java/com/fs/framework/aspectj/TenantDataSourceAspect.java

@@ -4,9 +4,11 @@ import com.alibaba.druid.pool.DruidDataSource;
 import com.fs.common.annotation.TenantDataScope;
 import com.fs.common.annotation.TenantDataScope;
 import com.fs.common.core.redis.RedisCacheT;
 import com.fs.common.core.redis.RedisCacheT;
 import com.fs.common.enums.TenantIdType;
 import com.fs.common.enums.TenantIdType;
+import com.fs.common.exception.CustomException;
 import com.fs.common.utils.StringUtils;
 import com.fs.common.utils.StringUtils;
 import com.fs.framework.datasource.DynamicDataSource;
 import com.fs.framework.datasource.DynamicDataSource;
 import com.fs.framework.datasource.DynamicDataSourceContextHolder;
 import com.fs.framework.datasource.DynamicDataSourceContextHolder;
+import com.fs.huifuPay.sdk.opps.core.exception.BasePayException;
 import com.fs.tenant.domain.TenantInfo;
 import com.fs.tenant.domain.TenantInfo;
 import com.fs.tenant.mapper.TenantInfoMapper;
 import com.fs.tenant.mapper.TenantInfoMapper;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
@@ -88,6 +90,9 @@ public class TenantDataSourceAspect {
         TenantInfo tenantInfo = redis.getCacheObject(TENANT_KEY + id);
         TenantInfo tenantInfo = redis.getCacheObject(TENANT_KEY + id);
         if(tenantInfo == null){
         if(tenantInfo == null){
             tenantInfo = tenantInfoMapper.selectById(id);
             tenantInfo = tenantInfoMapper.selectById(id);
+            if(tenantInfo == null){
+                throw new CustomException("租户不存在请检查");
+            }
             redis.setCacheObject(TENANT_KEY + id, tenantInfo);
             redis.setCacheObject(TENANT_KEY + id, tenantInfo);
         }
         }
         // 用租户主键作为唯一标识
         // 用租户主键作为唯一标识

+ 2 - 2
fs-service/src/main/java/com/fs/ipad/IpadSendUtils.java

@@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.fs.common.core.redis.RedisCache;
 import com.fs.common.core.redis.RedisCache;
 import com.fs.common.core.redis.RedisCacheT;
 import com.fs.common.core.redis.RedisCacheT;
+import com.fs.common.core.redis.RedisCacheTenant;
 import com.fs.common.exception.base.BaseException;
 import com.fs.common.exception.base.BaseException;
 import com.fs.common.utils.PubFun;
 import com.fs.common.utils.PubFun;
 import com.fs.common.utils.StringUtils;
 import com.fs.common.utils.StringUtils;
@@ -50,13 +51,12 @@ import java.util.stream.Collectors;
 public class IpadSendUtils {
 public class IpadSendUtils {
 
 
     private final WxWorkServiceNew wxWorkService;
     private final WxWorkServiceNew wxWorkService;
-    private final RedisCacheT<String> redisCache;
+    private final RedisCacheTenant<String> redisCache;
     private final QwGroupChatMapper qwGroupChatMapper;
     private final QwGroupChatMapper qwGroupChatMapper;
     private final QwSopMapper qwSopMapper;
     private final QwSopMapper qwSopMapper;
     private final QwExternalContactMapper qwExternalContactMapper;
     private final QwExternalContactMapper qwExternalContactMapper;
     private final SopUserLogsMapper sopUserLogsMapper;
     private final SopUserLogsMapper sopUserLogsMapper;
     private final QwUserMapper qwUserMapper;
     private final QwUserMapper qwUserMapper;
-    private final RedisCache redisCacheUrl;
     private final String FILE_KEY = "ipad:upload:";
     private final String FILE_KEY = "ipad:upload:";
     private final String USER_KEY = "ipad:user:";
     private final String USER_KEY = "ipad:user:";
 
 

+ 2 - 1
fs-service/src/main/java/com/fs/wxwork/service/WxWorkServiceNew.java

@@ -2,6 +2,7 @@ package com.fs.wxwork.service;
 
 
 import com.alibaba.fastjson.TypeReference;
 import com.alibaba.fastjson.TypeReference;
 import com.fs.common.core.redis.RedisCache;
 import com.fs.common.core.redis.RedisCache;
+import com.fs.common.core.redis.RedisCacheTenant;
 import com.fs.common.exception.CustomException;
 import com.fs.common.exception.CustomException;
 import com.fs.ipad.param.WxGetSessionRoomListParam;
 import com.fs.ipad.param.WxGetSessionRoomListParam;
 import com.fs.ipad.param.WxRoomUserListParam;
 import com.fs.ipad.param.WxRoomUserListParam;
@@ -29,7 +30,7 @@ public class WxWorkServiceNew {
     /**
     /**
      * 基础地址
      * 基础地址
      */
      */
-    private final RedisCache redisCache;
+    private final RedisCacheTenant<String> redisCache;
     private final IQwIpadServerService qwIpadServerService;
     private final IQwIpadServerService qwIpadServerService;
 
 
     public String getUrl(Long serverId) {
     public String getUrl(Long serverId) {