From 7ffa9c6abafc6d91586bb89e703dd7d2d1d99d09 Mon Sep 17 00:00:00 2001 From: charging-kuafuai Date: Wed, 6 Dec 2023 11:18:15 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A8=E7=BB=93=E6=9E=84=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=EF=BC=9A=20iot=5Fconfig=5Fcenter=20id,parent=5Fid=EF=BC=88-1?= =?UTF-8?q?=E4=B8=BA=E6=A0=B9=E7=9B=AE=E5=BD=95=EF=BC=89,key,value,status?= =?UTF-8?q?=20(-1=E5=88=A0=E9=99=A4,0=E5=A4=B1=E6=95=88,1=E6=9C=89?= =?UTF-8?q?=E6=95=88),app=5Fid,created,update,update=5Fuid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit iot_app_config 应用表 id,app_code,name,status (-1删除,0失效,1有效),created,update,update_uid 配置平台接口 configCreate(parent_id,key,value) configUpdate(id,value) configStatusUpdate(id,status) 微服务创建 appConfigCreate(value) Redis config_appId_key value 后台管理系统保存回溯覆盖 内存缓存(单实例) map(app_id, hashmap(key, Object)) Object:value,childKey_list 微服务 getValue(app_code(业务id),key) Http 应用场景:低数据量高频变动高性能配置化需求 --- .../controller/ConfigController.java | 38 ++++++ .../com/aiassistant/mapper/ConfigMapper.java | 20 +++ .../java/com/aiassistant/model/Config.java | 49 +++++++ .../aiassistant/service/ConfigService.java | 53 ++++++++ .../service/impl/ConfigServiceImpl.java | 63 +++++++++ .../com/aiassistant/utils/ResultModel.java | 122 +++++++++--------- .../aiassistant/utils/ResultPageModel.java | 66 ++-------- 7 files changed, 292 insertions(+), 119 deletions(-) create mode 100644 src/main/java/com/aiassistant/controller/ConfigController.java create mode 100644 src/main/java/com/aiassistant/mapper/ConfigMapper.java create mode 100644 src/main/java/com/aiassistant/model/Config.java create mode 100644 src/main/java/com/aiassistant/service/ConfigService.java create mode 100644 src/main/java/com/aiassistant/service/impl/ConfigServiceImpl.java diff --git a/src/main/java/com/aiassistant/controller/ConfigController.java b/src/main/java/com/aiassistant/controller/ConfigController.java new file mode 100644 index 0000000..a1c010b --- /dev/null +++ b/src/main/java/com/aiassistant/controller/ConfigController.java @@ -0,0 +1,38 @@ +package com.aiassistant.controller; + +import com.aiassistant.model.Config; +import com.aiassistant.service.ConfigService; +import com.aiassistant.utils.ResultModel; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/config") +public class ConfigController { + private final ConfigService configService; + + @Autowired + public ConfigController(ConfigService configService) { + this.configService = configService; + } + + @PostMapping("/add") + public ResultModel addConfig(@RequestBody Config config) { + return configService.addConfig(config); + } + + @PostMapping("/updateValue") + public ResultModel updateConfigValue(@RequestBody Config config) { + return configService.updateConfigValue(config); + } + + @PostMapping("/updateStatus") + public ResultModel updateConfigStatus(@RequestBody Config config) { + return configService.updateConfigStatus(config); + } + + @GetMapping("/getValue") + public ResultModel getConfigValue(@RequestParam String key, @RequestParam String defaultValue) { + return configService.getConfigValue(key, defaultValue); + } +} diff --git a/src/main/java/com/aiassistant/mapper/ConfigMapper.java b/src/main/java/com/aiassistant/mapper/ConfigMapper.java new file mode 100644 index 0000000..e9009b3 --- /dev/null +++ b/src/main/java/com/aiassistant/mapper/ConfigMapper.java @@ -0,0 +1,20 @@ +package com.aiassistant.mapper; + +import com.aiassistant.model.Config; +import org.apache.ibatis.annotations.*; + +@Mapper +public interface ConfigMapper { + @Insert("INSERT INTO config(app_code, key, value, status) VALUES(#{appCode}, #{key}, #{value}, #{status})") + @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") + int insertConfig(Config config); + + @Update("UPDATE config SET value = #{value} WHERE app_code = #{appCode} AND key = #{key}") + int updateConfigValue(Config config); + + @Update("UPDATE config SET status = #{status} WHERE app_code = #{appCode} AND key = #{key}") + int updateConfigStatus(Config config); + + @Select("SELECT value FROM config WHERE app_code = #{appCode} AND key = #{key}") + String selectConfigValue(@Param("appCode") String appCode, @Param("key") String key); +} diff --git a/src/main/java/com/aiassistant/model/Config.java b/src/main/java/com/aiassistant/model/Config.java new file mode 100644 index 0000000..a8da956 --- /dev/null +++ b/src/main/java/com/aiassistant/model/Config.java @@ -0,0 +1,49 @@ +package com.aiassistant.model; + +public class Config { + private int id; + private int parentId; + private String key; + private String value; + private int status; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getParentId() { + return parentId; + } + + public void setParentId(int parentId) { + this.parentId = parentId; + } + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public int getStatus() { + return status; + } + + public void setStatus(int status) { + this.status = status; + } +} diff --git a/src/main/java/com/aiassistant/service/ConfigService.java b/src/main/java/com/aiassistant/service/ConfigService.java new file mode 100644 index 0000000..d360018 --- /dev/null +++ b/src/main/java/com/aiassistant/service/ConfigService.java @@ -0,0 +1,53 @@ +package com.aiassistant.service; + +import com.aiassistant.mapper.ConfigMapper; +import com.aiassistant.model.Config; +import com.aiassistant.utils.ResultModel; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class ConfigService { + @Autowired + private ConfigMapper configMapper; + + public ResultModel addConfig(Config config) { + try { + configMapper.insertConfig(config); + return ResultModel.ofSuccess(); + } catch (Exception e) { + return ResultModel.ofError("Failed to add config", e); + } + } + + public ResultModel updateConfigValue(Config config) { + try { + configMapper.updateConfigValue(config); + return ResultModel.ofSuccess(); + } catch (Exception e) { + return ResultModel.ofError("Failed to update config value", e); + } + } + + public ResultModel updateConfigStatus(Config config) { + try { + configMapper.updateConfigStatus(config); + return ResultModel.ofSuccess(); + } catch (Exception e) { + return ResultModel.ofError("Failed to update config status", e); + } + } + + public ResultModel getConfigValue(String configKey, String defaultValue) { + try { + String configValue = configMapper.getConfigValue(configKey); + if (configValue != null) { + return ResultModel.ofSuccess(configValue); + } else { + return ResultModel.ofSuccess(defaultValue); + } + } catch (Exception e) { + return ResultModel.ofError("Failed to get config value", e); + } + } +} diff --git a/src/main/java/com/aiassistant/service/impl/ConfigServiceImpl.java b/src/main/java/com/aiassistant/service/impl/ConfigServiceImpl.java new file mode 100644 index 0000000..bdc7c21 --- /dev/null +++ b/src/main/java/com/aiassistant/service/impl/ConfigServiceImpl.java @@ -0,0 +1,63 @@ +package com.aiassistant.service.impl; + +import com.aiassistant.mapper.ConfigMapper; +import com.aiassistant.model.Config; +import com.aiassistant.service.ConfigService; +import com.aiassistant.utils.ResultModel; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class ConfigServiceImpl implements ConfigService { + + private final ConfigMapper configMapper; + + @Autowired + public ConfigServiceImpl(ConfigMapper configMapper) { + this.configMapper = configMapper; + } + + @Override + public ResultModel addConfig(Config config) { + try { + configMapper.insertConfig(config); + return ResultModel.ofSuccess(); + } catch (Exception e) { + return ResultModel.ofError("Failed to add config", e); + } + } + + @Override + public ResultModel updateConfigValue(Config config) { + try { + configMapper.updateConfigValue(config); + return ResultModel.ofSuccess(); + } catch (Exception e) { + return ResultModel.ofError("Failed to update config value", e); + } + } + + @Override + public ResultModel updateConfigStatus(Config config) { + try { + configMapper.updateConfigStatus(config); + return ResultModel.ofSuccess(); + } catch (Exception e) { + return ResultModel.ofError("Failed to update config status", e); + } + } + + @Override + public ResultModel getConfigValue(String configKey, String defaultValue) { + try { + String configValue = configMapper.getConfigValue(configKey); + if (configValue != null) { + return ResultModel.ofSuccess(configValue); + } else { + return ResultModel.ofSuccess(defaultValue); + } + } catch (Exception e) { + return ResultModel.ofError("Failed to get config value", e); + } + } +} diff --git a/src/main/java/com/aiassistant/utils/ResultModel.java b/src/main/java/com/aiassistant/utils/ResultModel.java index 5a4970b..2ebba3b 100644 --- a/src/main/java/com/aiassistant/utils/ResultModel.java +++ b/src/main/java/com/aiassistant/utils/ResultModel.java @@ -1,91 +1,85 @@ package com.aiassistant.utils; -import lombok.Data; - -/** - * 控制层-对外返回数据结构 - * - * @param - */ -@Data public class ResultModel { - - private Exception exception = null; - private T data = null; - private String msg; private Integer code; + private String msg; + private T data; + private Exception exception; - public static ResultModel ofResult(Integer code, String msg, T data) { - return ofResult(code, msg, data, null); + public ResultModel(Integer code, String msg, T data) { + this.code = code; + this.msg = msg; + this.data = data; } - public static ResultModel ofResult(Integer code, String msg, T data, Exception exception) { - ResultModel resultModel = new ResultModel(); - resultModel.setCode(code); - resultModel.setData(data); - resultModel.setMsg(msg); - resultModel.setException(exception); - return resultModel; + public ResultModel(Integer code, String msg, T data, Exception exception) { + this.code = code; + this.msg = msg; + this.data = data; + this.exception = exception; } - /** - * 创建成功返回 - * - * @return - */ - public static ResultModel ofSuccess() { + public static ResultModel ofResult(Integer code, String msg, T data) { + return new ResultModel<>(code, msg, data); + } - return ofSuccess(null); + public static ResultModel ofResult(Integer code, String msg, T data, Exception exception) { + return new ResultModel<>(code, msg, data, exception); } - /** - * 创建成功返回-带返回数据 - * - * @param data - * @param - * @return - */ - public static ResultModel ofSuccess(T data) { + public static ResultModel ofSuccess() { + return new ResultModel<>(0, "success", null); + } - return ofSuccess(null, data); + public static ResultModel ofSuccess(T data) { + return new ResultModel<>(0, "success", data); } - /** - * 创建成功返回-带返回信息和返回数据 - * - * @param msg - * @param data - * @param - * @return - */ - public static ResultModel ofSuccess(String msg, T data) { + public static ResultModel ofSuccess(String msg, T data) { + return new ResultModel<>(0, msg, data); + } - return ofResult(0, msg, data); + public static ResultModel ofError() { + return new ResultModel<>(-1, "error", null); } - /** - * 创建失败返回 - * - * @return - */ - public static ResultModel ofError() { + public static ResultModel ofError(String msg) { + return new ResultModel<>(-1, msg, null); + } - return ofError(null); + public static ResultModel ofError(String msg, Exception exception) { + return new ResultModel<>(-1, msg, null, exception); } - /** - * 创建失败返回-带返回信息 - * - * @param msg - * @return - */ - public static ResultModel ofError(String msg) { + public Integer getCode() { + return code; + } - return ofError(msg, null); + public void setCode(Integer code) { + this.code = code; } - public static ResultModel ofError(String msg, Exception exception) { + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + public Exception getException() { + return exception; + } - return ofResult(999, msg, null, exception); + public void setException(Exception exception) { + this.exception = exception; } } diff --git a/src/main/java/com/aiassistant/utils/ResultPageModel.java b/src/main/java/com/aiassistant/utils/ResultPageModel.java index 4a8821a..72f99d4 100644 --- a/src/main/java/com/aiassistant/utils/ResultPageModel.java +++ b/src/main/java/com/aiassistant/utils/ResultPageModel.java @@ -1,79 +1,39 @@ package com.aiassistant.utils; -import java.util.Collections; import java.util.List; -/** - * 控制层-对外返回列表数据结构 - * - * @param - */ public class ResultPageModel { - - private Integer totalRecords = 0; - private Integer pageNo = 1; - private Integer pageSize = 10; - private Integer totalPage = 0; - - private transient Integer firstIndex; + private Integer totalRecords; + private Integer pageNo; + private Integer pageSize; + private Integer totalPage; + private Integer firstIndex; + private List list; public Integer getFirstIndex() { return firstIndex; } - private List list; - - /** - * 创建列表返回 - * - * @param list - * @param - * @return - */ public static ResultPageModel of(List list) { - ResultPageModel resultPageModel = new ResultPageModel<>(1, list.size()); - resultPageModel.setTotalRecords(list.size()); + ResultPageModel resultPageModel = new ResultPageModel<>(); resultPageModel.setList(list); return resultPageModel; } - public ResultPageModel(Integer pageNo, Integer pageSize) { - if (pageNo == null || pageNo < 1) { - pageNo = 1; - } - if (pageSize == null || pageSize < 10) { - pageSize = 10; - } - this.pageNo = pageNo; - this.pageSize = pageSize; - this.list = Collections.emptyList(); - this.firstIndex = (pageNo - 1) * pageSize; - } - public Integer getTotalRecords() { return totalRecords; } - public void setTotalRecords(Integer count) { - this.totalRecords = count; - if (totalRecords != 0) { - if (totalRecords % pageSize == 0) { - totalPage = totalRecords / pageSize; - } else { - totalPage = totalRecords / pageSize + 1; - } - } + public void setTotalRecords(Integer totalRecords) { + this.totalRecords = totalRecords; } public Integer getPageNo() { return pageNo; } - public void setPageNo(Integer pageNumber) { - if (pageNumber < 1) { - pageNumber = 1; - } - this.pageNo = pageNumber; + public void setPageNo(Integer pageNo) { + this.pageNo = pageNo; } public Integer getPageSize() { @@ -81,9 +41,6 @@ public Integer getPageSize() { } public void setPageSize(Integer pageSize) { - if (pageSize < 1) { - pageSize = 1; - } this.pageSize = pageSize; } @@ -102,5 +59,4 @@ public Integer getTotalPage() { public void setTotalPage(Integer totalPage) { this.totalPage = totalPage; } - }