perf: 基础设置功能健壮性提升
现在所有基础设置配置项数据以后端枚举类中的配置为准
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package com.adc.da.sys.constant;
|
||||
|
||||
/**
|
||||
* @author ThinkBook
|
||||
*/
|
||||
|
||||
public enum TsConfigKeyEnum {
|
||||
|
||||
/**
|
||||
* 水印前缀
|
||||
*/
|
||||
WATERMARK_PREFIX
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.adc.da.sys.controller;
|
||||
|
||||
import com.adc.da.eo.ConfigEntity;
|
||||
import com.adc.da.sys.service.iservice.IConfigService;
|
||||
import com.adc.da.sys.vo.ConfigVo;
|
||||
import com.adc.da.util.http.ResponseMessage;
|
||||
import com.adc.da.util.http.Result;
|
||||
import io.swagger.annotations.Api;
|
||||
@@ -11,7 +11,6 @@ import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Caihaohan
|
||||
@@ -31,12 +30,7 @@ public class ConfigController {
|
||||
@ApiOperation(value = "获取一个基本设置项")
|
||||
@GetMapping("/{key}")
|
||||
public ResponseMessage findConfig(@NotNull @PathVariable String key) {
|
||||
try {
|
||||
return Result.success(iConfigService.query().eq("config_key", key).one());
|
||||
} catch (Exception e) {
|
||||
log.error("获取一个基本设置项失败", e);
|
||||
return Result.error("获取失败");
|
||||
}
|
||||
return iConfigService.findConfig(key);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,12 +40,11 @@ public class ConfigController {
|
||||
@GetMapping("/all")
|
||||
public ResponseMessage findAllConfig() {
|
||||
try {
|
||||
return Result.success(iConfigService.list());
|
||||
return Result.success(iConfigService.findAllConfig());
|
||||
} catch (Exception e) {
|
||||
log.error("获取所有基本设置项", e);
|
||||
return Result.error("获取失败");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,28 +52,7 @@ public class ConfigController {
|
||||
*/
|
||||
@ApiOperation(value = "保存/更新 基本设置项")
|
||||
@PostMapping("/save")
|
||||
public ResponseMessage saveConfig(@RequestBody ConfigEntity configEntity) {
|
||||
try {
|
||||
iConfigService.saveOrUpdate(configEntity);
|
||||
return Result.success("保存成功");
|
||||
} catch (Exception e) {
|
||||
log.error("保存基本设置项失败", e);
|
||||
return Result.error("保存失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存所有基本设置项
|
||||
*/
|
||||
@ApiOperation(value = "批量 保存/更新 基本设置项")
|
||||
@PostMapping("/saveAll")
|
||||
public ResponseMessage saveAllConfig(@RequestBody List<ConfigEntity> configEntityList) {
|
||||
try {
|
||||
iConfigService.saveOrUpdateBatch(configEntityList);
|
||||
return Result.success("保存成功");
|
||||
} catch (Exception e) {
|
||||
log.error("保存所有基本设置项失败", e);
|
||||
return Result.error("保存失败");
|
||||
}
|
||||
public ResponseMessage saveConfig(@RequestBody ConfigVo configVo) {
|
||||
return iConfigService.saveOrUpdateConfig(configVo);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,98 @@
|
||||
package com.adc.da.sys.service;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.adc.da.sys.constant.TsConfigKeyEnum;
|
||||
import com.adc.da.sys.vo.ConfigVo;
|
||||
import com.adc.da.util.http.ResponseMessage;
|
||||
import com.adc.da.util.http.Result;
|
||||
import com.adc.da.util.utils.UUID;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.adc.da.eo.ConfigEntity;
|
||||
import com.adc.da.sys.service.iservice.IConfigService;
|
||||
import com.adc.da.sys.dao.mysql.ConfigDao;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* @author ThinkBook
|
||||
* @description 针对表【ts_config】的数据库操作Service实现
|
||||
* @createDate 2023-05-04 09:45:30
|
||||
*/
|
||||
* @author ThinkBook
|
||||
* @description 针对表【ts_config】的数据库操作Service实现
|
||||
* @createDate 2023-05-04 09:45:30
|
||||
*/
|
||||
@Service
|
||||
public class ConfigServiceImpl extends ServiceImpl<ConfigDao, ConfigEntity>
|
||||
implements IConfigService {
|
||||
implements IConfigService {
|
||||
|
||||
@Override
|
||||
public ResponseMessage saveOrUpdateConfig(ConfigVo configVo) {
|
||||
try {
|
||||
// 先查询
|
||||
ConfigEntity configEntity = new ConfigEntity();
|
||||
BeanUtil.copyProperties(configVo, configEntity);
|
||||
|
||||
//判断Key是否是后端预设好的
|
||||
if (isKeyValid(configEntity.getConfigKey())) {
|
||||
List<ConfigEntity> configEntityList = query().eq("config_key", configEntity.getConfigKey()).list();
|
||||
if (!configEntityList.isEmpty()){
|
||||
ConfigEntity existDbConfig = configEntityList.get(0);
|
||||
configEntity.setId(existDbConfig.getId());
|
||||
updateById(configEntity);
|
||||
} else {
|
||||
configEntity.setId(UUID.randomUUID10());
|
||||
save(configEntity);
|
||||
}
|
||||
return Result.success("保存成功");
|
||||
}
|
||||
return Result.error("配置项Key不存在");
|
||||
} catch (Exception e) {
|
||||
log.error("保存基本设置项失败", e);
|
||||
return Result.error("保存失败");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ConfigVo> findAllConfig() {
|
||||
List<ConfigVo> result = new ArrayList<>();
|
||||
for (TsConfigKeyEnum configKey : TsConfigKeyEnum.values()) {
|
||||
List<ConfigEntity> configEntityList = query().eq("config_key", configKey.toString()).list();
|
||||
if (!configEntityList.isEmpty()){
|
||||
ConfigVo configVo = new ConfigVo();
|
||||
BeanUtil.copyProperties(configEntityList.get(0), configVo);
|
||||
result.add(configVo);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseMessage findConfig(String key) {
|
||||
if (isKeyValid(key)) {
|
||||
ConfigEntity configEntity = query().eq("config_key", key).one();
|
||||
ConfigVo configVo = new ConfigVo();
|
||||
BeanUtil.copyProperties(configEntity, configVo);
|
||||
return Result.success(configVo);
|
||||
}
|
||||
return Result.error("配置项Key不存在");
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断传入的key是否是后端预设好的key
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
private boolean isKeyValid(String key) {
|
||||
boolean isValid = false;
|
||||
|
||||
for (TsConfigKeyEnum configKey : TsConfigKeyEnum.values()) {
|
||||
if (configKey.toString().equals(key)) {
|
||||
isValid = true;
|
||||
}
|
||||
}
|
||||
return isValid;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.adc.da.sys.service.iservice;
|
||||
|
||||
import com.adc.da.eo.ConfigEntity;
|
||||
import com.adc.da.sys.vo.ConfigVo;
|
||||
import com.adc.da.util.http.ResponseMessage;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
@@ -13,4 +14,26 @@ import java.util.List;
|
||||
*/
|
||||
public interface IConfigService extends IService<ConfigEntity> {
|
||||
|
||||
/**
|
||||
* 新增或更新配置项
|
||||
*
|
||||
* @param configVo
|
||||
* @return
|
||||
*/
|
||||
ResponseMessage saveOrUpdateConfig(ConfigVo configVo);
|
||||
|
||||
/**
|
||||
* 获取所有配置项
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<ConfigVo> findAllConfig();
|
||||
|
||||
/**
|
||||
* 返回一个配置项
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
ResponseMessage findConfig(String key);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.adc.da.sys.vo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author CaiHaohan
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ConfigVo {
|
||||
|
||||
/**
|
||||
* 配置项key
|
||||
*/
|
||||
private String configKey;
|
||||
|
||||
/**
|
||||
* 配置项value
|
||||
*/
|
||||
private String configValue;
|
||||
}
|
||||
Reference in New Issue
Block a user