创建仓库
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package vvpkassistant.FunctionConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import vvpkassistant.Data.ResponseData;
|
||||
import vvpkassistant.Data.ResponseInfo;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("config")
|
||||
public class FunctionConfigController {
|
||||
|
||||
@Autowired
|
||||
private FunctionConfigMapper configMapper;
|
||||
|
||||
// 获取所有配置
|
||||
@GetMapping("getAllConfig")
|
||||
public ResponseData<Object> getAllConfig() {
|
||||
return ResponseData.success(FunctionConfigHolder.CONFIGS);
|
||||
}
|
||||
|
||||
// 更新配置项内容
|
||||
@PostMapping("updateConfigValue")
|
||||
public ResponseData<Object> updateConfigValue(@RequestBody FunctionConfigModel model) {
|
||||
// 1. 更新数据库
|
||||
configMapper.updateById(model);
|
||||
// 2. 更新内存
|
||||
FunctionConfigHolder.CONFIGS.removeIf(c -> model.getFunctionName().equals(c.getFunctionName()));
|
||||
FunctionConfigHolder.CONFIGS.add(model);
|
||||
return ResponseData.success("");
|
||||
}
|
||||
|
||||
@PostMapping("add")
|
||||
public ResponseData<Object> addNewConfig(@RequestBody FunctionConfigModel newModel) {
|
||||
String name = newModel.getFunctionName();
|
||||
boolean isDuplicate = FunctionConfigHolder.CONFIGS.stream()
|
||||
.anyMatch(config -> name.equals(config.getFunctionName()));
|
||||
if (isDuplicate) {
|
||||
return ResponseData.error(ResponseInfo.ERROR,"配置名称重复");
|
||||
}else {
|
||||
configMapper.insert(newModel);
|
||||
FunctionConfigHolder.CONFIGS.add(newModel);
|
||||
return ResponseData.success("");
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("deleteConfigById")
|
||||
public ResponseData<Object> deleteConfigById(@RequestBody FunctionConfigModel model) {
|
||||
int i = configMapper.deleteById(model);
|
||||
if (i == 1) {
|
||||
FunctionConfigHolder.CONFIGS.removeIf(c -> model.getId().equals(c.getId()));
|
||||
return ResponseData.success("");
|
||||
}else {
|
||||
return ResponseData.error(ResponseInfo.ERROR,null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package vvpkassistant.FunctionConfig;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class FunctionConfigHolder {
|
||||
// 线程安全的全局配置容器
|
||||
static final List<FunctionConfigModel> CONFIGS = new CopyOnWriteArrayList<>();
|
||||
|
||||
@Autowired
|
||||
private FunctionConfigMapper configMapper;
|
||||
|
||||
/**
|
||||
* 启动时加载所有配置到内存
|
||||
*/
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
List<FunctionConfigModel> dbConfigs = configMapper.selectList(null);
|
||||
CONFIGS.clear();
|
||||
CONFIGS.addAll(dbConfigs);
|
||||
System.out.println("已加载 "+CONFIGS.size()+" 条功能配置");
|
||||
}
|
||||
|
||||
/**
|
||||
* 按功能名获取配置值
|
||||
*/
|
||||
public static String getValue(String functionName) {
|
||||
return CONFIGS.stream()
|
||||
.filter(c -> functionName.equals(c.getFunctionName()))
|
||||
.findFirst()
|
||||
.map(FunctionConfigModel::getFunctionValue)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package vvpkassistant.FunctionConfig;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface FunctionConfigMapper extends BaseMapper<FunctionConfigModel> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package vvpkassistant.FunctionConfig;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@TableName("function_configuration")
|
||||
public class FunctionConfigModel {
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Integer id; // 主键
|
||||
private String functionName; // 功能名称
|
||||
private String functionValue; // 设置的值
|
||||
}
|
||||
Reference in New Issue
Block a user