Browse Source

新增系统参数修改

wangliang 2 years ago
parent
commit
f41453c7d6

+ 59 - 0
themis-admin/src/main/java/com/qmth/themis/admin/api/SysConfigController.java

@@ -1,18 +1,24 @@
 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.constant.SystemConstant;
 import com.qmth.themis.business.dto.response.SysConfigBean;
 import com.qmth.themis.business.entity.SysConfig;
+import com.qmth.themis.business.enums.UploadFileEnum;
 import com.qmth.themis.business.service.SysConfigService;
+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;
+import org.springframework.util.CollectionUtils;
 import org.springframework.validation.BindingResult;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
@@ -20,6 +26,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.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
 import java.util.Optional;
 
 /**
@@ -39,6 +51,9 @@ public class SysConfigController {
     @Resource
     SysConfigService sysConfigService;
 
+    @Resource
+    OssUtil ossUtil;
+
     @ApiOperation(value = "系统参数查询接口")
     @RequestMapping(value = "/select", method = RequestMethod.POST)
     @ApiResponses({@ApiResponse(code = 200, message = "系统参数信息", response = SysConfigBean.class)})
@@ -58,6 +73,7 @@ public class SysConfigController {
         SysConfig sysConfig = GsonUtil.fromJson(GsonUtil.toJson(sysConfigBean), SysConfig.class);
         sysConfig.setEditor(1);
         sysConfigService.saveOrUpdate(sysConfig);
+        updateSysconfig(sysConfig);
         return ResultUtil.ok(true);
     }
 
@@ -75,6 +91,49 @@ public class SysConfigController {
 
         sysConfig.setEnable(enable);
         sysConfigService.saveOrUpdate(sysConfig);
+        updateSysconfig(sysConfig);
         return ResultUtil.ok(true);
     }
+
+    /**
+     * 刷新系统参数并写入json文件
+     */
+    public void updateSysconfig(SysConfig sysConfig) {
+        File file = null;
+        try {
+            file = SystemConstant.getFileTempVar(SystemConstant.JSON_PREFIX);
+            JSONObject jsonObject = JSONObject
+                    .parseObject(new String(ossUtil.download(true, UploadFileEnum.client.name() + "/" + SystemConstant.EXAM_CONFIG_FILE_NAME), StandardCharsets.UTF_8));
+            List<SysConfigBean> sysConfigBeanList = null;
+            if (Objects.isNull(jsonObject) || jsonObject.size() == 0) {
+                jsonObject = new JSONObject();
+                sysConfigBeanList = new ArrayList<>();
+            } else {
+                sysConfigBeanList = (List<SysConfigBean>) jsonObject.get(SystemConstant.SYS_CONFIG_LIST);
+                sysConfigBeanList = CollectionUtils.isEmpty(sysConfigBeanList) ? new ArrayList<>() : sysConfigBeanList;
+                sysConfigBeanList = GsonUtil.fromJson(GsonUtil.toJson(sysConfigBeanList), new TypeToken<List<SysConfigBean>>() {
+                }.getType());
+            }
+            SysConfigBean sysConfigBean = GsonUtil.fromJson(GsonUtil.toJson(sysConfig), SysConfigBean.class);
+            sysConfigBeanList.removeIf(e -> Objects.equals(e.getConfigKey(), sysConfigBean.getConfigKey()));
+            if (sysConfigBean.getEnable()) {
+                sysConfigBeanList.add(sysConfigBean);
+            }
+            jsonObject.put(SystemConstant.SYS_CONFIG_LIST, sysConfigBeanList);
+
+            IOUtils.write(jsonObject.toJSONString().getBytes(StandardCharsets.UTF_8), new FileOutputStream(file));
+            ossUtil.upload(true, UploadFileEnum.client.name() + "/" + SystemConstant.EXAM_CONFIG_FILE_NAME, file);
+        } catch (Exception e) {
+            log.error(SystemConstant.LOG_ERROR, e);
+            if (e instanceof BusinessException) {
+                throw new BusinessException(e.getMessage());
+            } else {
+                throw new RuntimeException(e);
+            }
+        } finally {
+            if (Objects.nonNull(file)) {
+                file.delete();
+            }
+        }
+    }
 }

+ 5 - 2
themis-admin/src/main/java/com/qmth/themis/admin/api/TSNotifyController.java

@@ -107,9 +107,12 @@ public class TSNotifyController {
             List<TSNotify> tsNotifyList = themisCacheService.updateSysNotifyCache();
             List<NotifyBean> notifyBeanList = GsonUtil.fromJson(GsonUtil.toJson(tsNotifyList), new TypeToken<List<NotifyBean>>() {
             }.getType());
-            JSONObject jsonObject = new JSONObject();
+            JSONObject jsonObject = JSONObject
+                    .parseObject(new String(ossUtil.download(true, UploadFileEnum.client.name() + "/" + SystemConstant.EXAM_CONFIG_FILE_NAME), StandardCharsets.UTF_8));
+            if (Objects.isNull(jsonObject) || jsonObject.size() == 0) {
+                jsonObject = new JSONObject();
+            }
             jsonObject.put(SystemConstant.NOTIFY_LIST, notifyBeanList);
-            file = ossUtil.download(true, UploadFileEnum.client.name() + "/" + SystemConstant.EXAM_CONFIG_FILE_NAME, file.getPath());
             IOUtils.write(jsonObject.toJSONString().getBytes(StandardCharsets.UTF_8), new FileOutputStream(file));
             ossUtil.upload(true, UploadFileEnum.client.name() + "/" + SystemConstant.EXAM_CONFIG_FILE_NAME, file);
         } catch (Exception e) {

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

@@ -110,6 +110,7 @@ public class SystemConstant {
     public static final String DENY_LIST_FILE_NAME = "denyList.json";
     public static final String EXAM_CONFIG_FILE_NAME = "examConfig.json";
     public static final String NOTIFY_LIST = "notifyList";
+    public static final String SYS_CONFIG_LIST = "sysconfigList";
 
     public static final String DATE_TIME_FORMAT = "%02d";