wangliang 2 роки тому
батько
коміт
0889d71c1c

+ 53 - 0
themis-admin/src/main/java/com/qmth/themis/admin/api/TSNotifyController.java

@@ -1,14 +1,22 @@
 package com.qmth.themis.admin.api;
 
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.google.common.reflect.TypeToken;
+import com.qmth.themis.business.bean.admin.NotifyBean;
 import com.qmth.themis.business.constant.SystemConstant;
 import com.qmth.themis.business.entity.TSBlackList;
 import com.qmth.themis.business.entity.TSNotify;
+import com.qmth.themis.business.enums.UploadFileEnum;
 import com.qmth.themis.business.service.TSNotifyService;
+import com.qmth.themis.business.service.ThemisCacheService;
+import com.qmth.themis.business.util.OssUtil;
 import com.qmth.themis.common.exception.BusinessException;
+import com.qmth.themis.common.util.GsonUtil;
 import com.qmth.themis.common.util.Result;
 import com.qmth.themis.common.util.ResultUtil;
 import io.swagger.annotations.*;
+import org.apache.commons.io.IOUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.transaction.annotation.Transactional;
@@ -19,6 +27,12 @@ import org.springframework.web.bind.annotation.*;
 import javax.annotation.Resource;
 import javax.validation.constraints.Max;
 import javax.validation.constraints.Min;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Objects;
 import java.util.Optional;
 
 /**
@@ -39,6 +53,12 @@ public class TSNotifyController {
     @Resource
     TSNotifyService tsNotifyService;
 
+    @Resource
+    OssUtil ossUtil;
+
+    @Resource
+    ThemisCacheService themisCacheService;
+
     @ApiOperation(value = "系统通知信息查询接口")
     @RequestMapping(value = "/select", method = RequestMethod.POST)
     @ApiResponses({@ApiResponse(code = 200, message = "系统通知信息", response = TSBlackList.class)})
@@ -56,6 +76,7 @@ public class TSNotifyController {
             return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
         }
         tsNotifyService.saveOrUpdate(tsNotify);
+        updateTsNotifyCache();
         return ResultUtil.ok(true);
     }
 
@@ -73,6 +94,38 @@ public class TSNotifyController {
 
         tsNotify.setEnable(enable);
         tsNotifyService.saveOrUpdate(tsNotify);
+        updateTsNotifyCache();
         return ResultUtil.ok(true);
     }
+
+    /**
+     * 刷新缓存信息并写入json文件
+     *
+     * @throws IOException
+     */
+    public void updateTsNotifyCache() {
+        File file = null;
+        try {
+            file = SystemConstant.getFileTempVar(SystemConstant.JSON_PREFIX);
+            List<TSNotify> tsNotifyList = themisCacheService.updateSysNotifyCache();
+            List<NotifyBean> notifyBeanList = GsonUtil.fromJson(GsonUtil.toJson(tsNotifyList), new TypeToken<List<NotifyBean>>() {
+            }.getType());
+            JSONObject jsonObject = new JSONObject();
+            jsonObject.put("notifyList", notifyBeanList);
+            file = ossUtil.download(true, UploadFileEnum.client.name() + "/notifyList.json", file.getPath());
+            IOUtils.write(jsonObject.toJSONString().getBytes(StandardCharsets.UTF_8), new FileOutputStream(file));
+            ossUtil.upload(true, UploadFileEnum.client.name() + "/notifyList.json", file);
+        } catch (Exception e) {
+            e.printStackTrace();
+            if (e instanceof BusinessException) {
+                throw new BusinessException(e.getMessage());
+            } else {
+                throw new RuntimeException(e);
+            }
+        } finally {
+            if (Objects.nonNull(file)) {
+                file.delete();
+            }
+        }
+    }
 }

+ 70 - 0
themis-business/src/main/java/com/qmth/themis/business/bean/admin/NotifyBean.java

@@ -0,0 +1,70 @@
+package com.qmth.themis.business.bean.admin;
+
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
+import com.qmth.themis.business.entity.TSNotify;
+import io.swagger.annotations.ApiModelProperty;
+
+import javax.validation.constraints.NotNull;
+import java.io.Serializable;
+
+/**
+ * @Description: 系统通知bean
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2023/2/1
+ */
+@JsonInclude(JsonInclude.Include.NON_NULL)
+public class NotifyBean implements Serializable {
+
+    @JsonSerialize(using = ToStringSerializer.class)
+    @ApiModelProperty(value = "主键")
+    private Long id;
+
+    @ApiModelProperty(value = "标题")
+    @NotNull(message = "标题不能为空")
+    private String title;
+
+    @ApiModelProperty(value = "内容")
+    @NotNull(message = "内容不能为空")
+    private String content;
+
+    @ApiModelProperty(value = "是否启用,0:停用,1:启用")
+    @NotNull(message = "启用/禁用不能为空")
+    private Boolean enable;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public void setTitle(String title) {
+        this.title = title;
+    }
+
+    public String getContent() {
+        return content;
+    }
+
+    public void setContent(String content) {
+        this.content = content;
+    }
+
+    public Boolean getEnable() {
+        return enable;
+    }
+
+    public void setEnable(Boolean enable) {
+        this.enable = enable;
+    }
+}

+ 11 - 0
themis-business/src/main/java/com/qmth/themis/business/bean/exam/EnvBean.java

@@ -20,6 +20,9 @@ public class EnvBean {
 	@ApiModelProperty("黑名单资源文件下载地址")
 	private String denyList;
 
+    @ApiModelProperty("系统通知下载地址")
+    private String notifyList;
+
 	public VersionBean getVersion() {
 		return version;
 	}
@@ -51,4 +54,12 @@ public class EnvBean {
 	public void setDenyList(String denyList) {
 		this.denyList = denyList;
 	}
+
+    public String getNotifyList() {
+        return notifyList;
+    }
+
+    public void setNotifyList(String notifyList) {
+        this.notifyList = notifyList;
+    }
 }

+ 2 - 0
themis-business/src/main/java/com/qmth/themis/business/constant/SystemConstant.java

@@ -197,6 +197,8 @@ public class SystemConstant {
     public static final String EXAM_AUDIO_CACHE = "exam:audio:cache";
     public static final String ATTACHMENT_CACHE = "attachment:cache";
     public static final String BLACK_LIST_CACHE = "blacklist:cache";
+    public static final String SYS_NOTIFY_CACHE = "sys:notify:cache";
+
     public volatile static Searcher SEARCHER = null;
     public static final String ONLINE_WARN_INTERVAL = "online.warn.interval";
     public static final String ONLINE_WARN_SCALESIZE = "online.warn.scaleSize";

+ 19 - 0
themis-business/src/main/java/com/qmth/themis/business/service/ThemisCacheService.java

@@ -265,4 +265,23 @@ public interface ThemisCacheService {
      * 删除黑名单缓存
      */
     public void removeBlackListCache();
+
+    /**
+     * 系统通知缓存
+     *
+     * @return
+     */
+    public List<TSNotify> sysNotifyCache();
+
+    /**
+     * 修改系统通知缓存
+     *
+     * @return
+     */
+    public List<TSNotify> updateSysNotifyCache();
+
+    /**
+     * 删除系统通知缓存
+     */
+    public void removeSysNotifyCache();
 }

+ 31 - 0
themis-business/src/main/java/com/qmth/themis/business/service/impl/ThemisCacheServiceImpl.java

@@ -516,4 +516,35 @@ public class ThemisCacheServiceImpl implements ThemisCacheService {
     public void removeBlackListCache() {
 
     }
+
+    /**
+     * 系统通知缓存
+     *
+     * @return
+     */
+    @Override
+    @Cacheable(value = SystemConstant.SYS_NOTIFY_CACHE, unless = "#result == null")
+    public List<TSNotify> sysNotifyCache() {
+        return tsNotifyService.list(new QueryWrapper<TSNotify>().lambda().eq(TSNotify::getEnable, true));
+    }
+
+    /**
+     * 修改系统通知缓存
+     *
+     * @return
+     */
+    @Override
+    @CachePut(value = SystemConstant.SYS_NOTIFY_CACHE, condition = "#result != null")
+    public List<TSNotify> updateSysNotifyCache() {
+        return tsNotifyService.list(new QueryWrapper<TSNotify>().lambda().eq(TSNotify::getEnable, true));
+    }
+
+    /**
+     * 删除系统通知缓存
+     */
+    @Override
+    @CacheEvict(value = SystemConstant.SYS_NOTIFY_CACHE)
+    public void removeSysNotifyCache() {
+
+    }
 }

+ 5 - 2
themis-exam/src/main/java/com/qmth/themis/exam/api/SysController.java

@@ -10,8 +10,8 @@ import com.qmth.themis.business.entity.SysConfig;
 import com.qmth.themis.business.entity.TBClientVersion;
 import com.qmth.themis.business.entity.TBOrg;
 import com.qmth.themis.business.enums.UploadFileEnum;
-import com.qmth.themis.business.service.ThemisCacheService;
 import com.qmth.themis.business.service.TBClientVersionService;
+import com.qmth.themis.business.service.ThemisCacheService;
 import com.qmth.themis.common.exception.BusinessException;
 import com.qmth.themis.common.util.Result;
 import com.qmth.themis.common.util.ResultUtil;
@@ -66,16 +66,19 @@ public class SysController {
             env.setVersion(v);
         }
         boolean oss = dictionaryConfig.sysDomain().isOss();
-        String url = null;
+        String url = null, notifyUrl = null;
         SysConfig sysConfig = themisCacheService.addSysConfigCache(SystemConstant.EXAM_FILE_HOST);
         Optional.ofNullable(sysConfig).orElseThrow(() -> new BusinessException("未配置exam文件host"));
 
         if (oss) {
             url = dictionaryConfig.aliYunOssDomain().getPublicUrl() + File.separator + UploadFileEnum.client.name() + "/denyList.json";
+            notifyUrl = dictionaryConfig.aliYunOssDomain().getPublicUrl() + File.separator + UploadFileEnum.client.name() + "/notifyList.json";
         } else {
             url = "http://" + sysConfig.getConfigValue() + File.separator + UploadFileEnum.client.name() + "/denyList.json";
+            notifyUrl = "http://" + sysConfig.getConfigValue() + File.separator + UploadFileEnum.client.name() + "/notifyList.json";
         }
         env.setDenyList(url);
+        env.setNotifyList(notifyUrl);
         return ResultUtil.ok(env);
     }
 }