|
@@ -1,9 +1,12 @@
|
|
|
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.service.ThemisCacheService;
|
|
|
import com.qmth.themis.business.util.OssUtil;
|
|
@@ -12,9 +15,11 @@ 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.*;
|
|
@@ -22,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.nio.charset.StandardCharsets;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
import java.util.Optional;
|
|
|
|
|
|
/**
|
|
@@ -67,7 +78,7 @@ public class SysConfigController {
|
|
|
sysConfig.setEditor(1);
|
|
|
sysConfig.setEnable(true);
|
|
|
sysConfigService.saveOrUpdate(sysConfig);
|
|
|
- SystemConstant.updateSysconfig(sysConfig);
|
|
|
+ updateSysconfig(sysConfig);
|
|
|
return ResultUtil.ok(true);
|
|
|
}
|
|
|
|
|
@@ -82,7 +93,50 @@ public class SysConfigController {
|
|
|
|
|
|
sysConfig.setEnable(enable);
|
|
|
sysConfigService.saveOrUpdate(sysConfig);
|
|
|
- SystemConstant.updateSysconfig(sysConfig);
|
|
|
+ updateSysconfig(sysConfig);
|
|
|
return ResultUtil.ok(true);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 刷新系统参数并写入json文件
|
|
|
+ */
|
|
|
+ public void updateSysconfig(SysConfig sysConfig) {
|
|
|
+ File file = null;
|
|
|
+ try {
|
|
|
+ themisCacheService.updateSysConfigCache(sysConfig.getConfigKey());
|
|
|
+ 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 (Objects.nonNull(sysConfigBean.getEnable()) && 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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|