deason 1 year ago
parent
commit
9915bef881

+ 23 - 0
src/main/java/com/qmth/exam/reserve/bean/org/OrgInfo.java

@@ -0,0 +1,23 @@
+package com.qmth.exam.reserve.bean.org;
+
+import com.qmth.exam.reserve.bean.IModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+public class OrgInfo implements IModel {
+
+    private static final long serialVersionUID = -7196168241899887928L;
+
+    @ApiModelProperty("机构ID")
+    private Long orgId;
+
+    @ApiModelProperty("机构名称")
+    private String orgName;
+
+    @ApiModelProperty("是否启用")
+    private Boolean enable;
+
+}

+ 12 - 0
src/main/java/com/qmth/exam/reserve/cache/CacheConstants.java

@@ -5,6 +5,18 @@ package com.qmth.exam.reserve.cache;
  */
 public interface CacheConstants {
 
+    /**
+     * 某个学校缓存
+     * $cache:org:{orgId}
+     */
+    String CACHE_ORG = "$cache:org:%s";
+
+    /**
+     * 当前默认的学校缓存
+     * $cache:current_org
+     */
+    String CACHE_CURRENT_ORG = "$cache:current_org";
+
     /**
      * 当前启用的预约任务缓存
      * $cache:current_apply_task:{orgId}

+ 18 - 0
src/main/java/com/qmth/exam/reserve/cache/impl/ApplyTaskCacheService.java

@@ -56,6 +56,12 @@ public class ApplyTaskCacheService implements CacheConstants {
         return value;
     }
 
+    public void clearCurrentApplyTaskCache(Long orgId) {
+        String cacheKey = String.format(CACHE_CURRENT_APPLY_TASK, orgId);
+        redisClient.delete(cacheKey);
+        log.warn("清理缓存!cacheKey:{}", cacheKey);
+    }
+
     /**
      * 获取某考生的“允许预约时段次数”
      */
@@ -73,6 +79,12 @@ public class ApplyTaskCacheService implements CacheConstants {
         return value;
     }
 
+    public void clearStudentApplyNumberCache(Long studentId) {
+        String cacheKey = String.format(CACHE_STUDENT_APPLY_NUMBER, studentId);
+        redisClient.delete(cacheKey);
+        log.warn("清理缓存!cacheKey:{}", cacheKey);
+    }
+
     /**
      * 获取某考点的“可预约总量”
      */
@@ -90,6 +102,12 @@ public class ApplyTaskCacheService implements CacheConstants {
         return value;
     }
 
+    public void clearApplyTotalCountCache(Long examSiteId) {
+        String cacheKey = String.format(CACHE_APPLY_TOTAL, examSiteId);
+        redisClient.delete(cacheKey);
+        log.warn("清理缓存!cacheKey:{}", cacheKey);
+    }
+
     /**
      * 获取某考点某时段的“已预约数量”
      */

+ 77 - 0
src/main/java/com/qmth/exam/reserve/cache/impl/OrgCacheService.java

@@ -0,0 +1,77 @@
+package com.qmth.exam.reserve.cache.impl;
+
+import com.qmth.exam.reserve.bean.org.OrgInfo;
+import com.qmth.exam.reserve.cache.CacheConstants;
+import com.qmth.exam.reserve.cache.RedisClient;
+import com.qmth.exam.reserve.service.OrgService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.concurrent.TimeUnit;
+
+@Component
+public class OrgCacheService implements CacheConstants {
+
+    private static final Logger log = LoggerFactory.getLogger(OrgCacheService.class);
+
+    @Autowired
+    private RedisClient redisClient;
+
+    @Autowired
+    private OrgService orgService;
+
+    /**
+     * 获取某个学校缓存
+     */
+    public OrgInfo getOrgById(Long orgId) {
+        String cacheKey = String.format(CACHE_ORG, orgId);
+        OrgInfo value = redisClient.get(cacheKey, OrgInfo.class);
+        if (value != null) {
+            return value;
+        }
+
+        value = orgService.findInfoById(orgId);
+        if (value == null) {
+            return null;
+        }
+
+        redisClient.set(cacheKey, value, 3, TimeUnit.HOURS);
+        log.info("{} = id:{} name:{}", cacheKey, value.getOrgId(), value.getOrgName());
+
+        return value;
+    }
+
+    public void clearOrgByIdCache(Long orgId) {
+        String cacheKey = String.format(CACHE_ORG, orgId);
+        redisClient.delete(cacheKey);
+        log.warn("清理缓存!cacheKey:{}", cacheKey);
+    }
+
+    /**
+     * 获取当前默认的学校缓存
+     */
+    public OrgInfo currentOrg() {
+        OrgInfo value = redisClient.get(CACHE_CURRENT_ORG, OrgInfo.class);
+        if (value != null) {
+            return value;
+        }
+
+        value = orgService.currentOrg();
+        if (value == null) {
+            return null;
+        }
+
+        redisClient.set(CACHE_CURRENT_ORG, value);
+        log.info("{} = id:{} name:{}", CACHE_CURRENT_ORG, value.getOrgId(), value.getOrgName());
+
+        return value;
+    }
+
+    public void clearCurrentOrgCache() {
+        redisClient.delete(CACHE_CURRENT_ORG);
+        log.warn("清理缓存!cacheKey:{}", CACHE_CURRENT_ORG);
+    }
+
+}

+ 8 - 13
src/main/java/com/qmth/exam/reserve/controller/admin/SystemPropertyController.java

@@ -1,17 +1,16 @@
 package com.qmth.exam.reserve.controller.admin;
 
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.qmth.boot.api.annotation.Aac;
 import com.qmth.boot.api.constant.ApiConstant;
 import com.qmth.boot.core.fss.store.FileStore;
 import com.qmth.exam.reserve.bean.Constants;
 import com.qmth.exam.reserve.bean.applytask.CurrentApplyTaskVO;
+import com.qmth.exam.reserve.bean.org.OrgInfo;
 import com.qmth.exam.reserve.bean.systemproperty.SystemPropertyBean;
 import com.qmth.exam.reserve.cache.impl.ApplyTaskCacheService;
+import com.qmth.exam.reserve.cache.impl.OrgCacheService;
 import com.qmth.exam.reserve.controller.BaseController;
-import com.qmth.exam.reserve.entity.OrgEntity;
 import com.qmth.exam.reserve.entity.SystemPropertyEntity;
-import com.qmth.exam.reserve.service.OrgService;
 import com.qmth.exam.reserve.service.SystemPropertyService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -36,7 +35,7 @@ public class SystemPropertyController extends BaseController {
     private FileStore fileStore;
 
     @Autowired
-    private OrgService orgService;
+    private OrgCacheService orgCacheService;
 
     @Autowired
     private ApplyTaskCacheService applyTaskCacheService;
@@ -50,19 +49,15 @@ public class SystemPropertyController extends BaseController {
         SystemPropertyBean properties = new SystemPropertyBean();
         properties.setFileUrlPrefix(fileStore.getServer());
 
-        OrgEntity org;
+        OrgInfo org;
         if (orgId != null) {
-            org = orgService.getById(orgId);
+            org = orgCacheService.getOrgById(orgId);
         } else {
-            // 未明确指定学校,默认取一个学校
-            LambdaQueryWrapper<OrgEntity> wrapper = new LambdaQueryWrapper<>();
-            wrapper.select(OrgEntity::getId, OrgEntity::getName);
-            wrapper.last("LIMIT 1");
-            org = orgService.getOne(wrapper);
+            org = orgCacheService.currentOrg();
         }
         if (org != null) {
-            properties.setOrgTitle(org.getName());
-            orgId = org.getId();
+            properties.setOrgTitle(org.getOrgName());
+            orgId = org.getOrgId();
         }
 
         CurrentApplyTaskVO curApplyTask = applyTaskCacheService.currentApplyTask(orgId);

+ 5 - 0
src/main/java/com/qmth/exam/reserve/service/OrgService.java

@@ -1,8 +1,13 @@
 package com.qmth.exam.reserve.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.qmth.exam.reserve.bean.org.OrgInfo;
 import com.qmth.exam.reserve.entity.OrgEntity;
 
 public interface OrgService extends IService<OrgEntity> {
 
+    OrgInfo currentOrg();
+
+    OrgInfo findInfoById(Long orgId);
+
 }

+ 35 - 0
src/main/java/com/qmth/exam/reserve/service/impl/OrgServiceImpl.java

@@ -1,6 +1,8 @@
 package com.qmth.exam.reserve.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.qmth.exam.reserve.bean.org.OrgInfo;
 import com.qmth.exam.reserve.dao.OrgDao;
 import com.qmth.exam.reserve.entity.OrgEntity;
 import com.qmth.exam.reserve.service.OrgService;
@@ -13,5 +15,38 @@ public class OrgServiceImpl extends ServiceImpl<OrgDao, OrgEntity> implements Or
 
     private static final Logger log = LoggerFactory.getLogger(OrgServiceImpl.class);
 
+    @Override
+    public OrgInfo currentOrg() {
+        // 未明确指定学校,默认取一个学校
+        LambdaQueryWrapper<OrgEntity> wrapper = new LambdaQueryWrapper<>();
+        wrapper.select(OrgEntity::getId, OrgEntity::getName, OrgEntity::getEnable);
+        wrapper.eq(OrgEntity::getEnable, Boolean.TRUE);
+        wrapper.last("LIMIT 1");
+
+        OrgEntity org = this.getOne(wrapper);
+        if (org == null) {
+            return null;
+        }
+
+        OrgInfo vo = new OrgInfo();
+        vo.setOrgId(org.getId());
+        vo.setOrgName(org.getName());
+        vo.setEnable(org.getEnable());
+        return vo;
+    }
+
+    @Override
+    public OrgInfo findInfoById(Long orgId) {
+        OrgEntity org = this.getById(orgId);
+        if (org == null) {
+            return null;
+        }
+
+        OrgInfo vo = new OrgInfo();
+        vo.setOrgId(org.getId());
+        vo.setEnable(org.getEnable());
+        vo.setOrgName(org.getName());
+        return vo;
+    }
 
 }