From b246515558588f2a9a1776e05c93f0374551e9aa Mon Sep 17 00:00:00 2001 From: lijiahangmax Date: Tue, 17 Oct 2023 00:31:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AD=97=E5=85=B8=E5=80=BC?= =?UTF-8?q?=E9=80=BB=E8=BE=91.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../redis/core/utils/RedisUtils.java | 14 ++ .../orion-server-module-controller.java.vm | 2 +- .../orion-server-module-service-impl.java.vm | 10 +- .../orion-server-module-service.java.vm | 2 +- .../orion-server-provider-api-impl.java.vm | 8 +- .../orion-server-provider-api.java.vm | 2 +- .../orion-server-test-api-impl-tests.java.vm | 4 +- ...ion-server-test-service-impl-tests.java.vm | 4 +- .../infra/controller/DictKeyController.java | 2 +- .../infra/controller/DictValueController.http | 48 +---- .../infra/controller/DictValueController.java | 18 +- .../define/operator/DictKeyOperatorType.java | 1 + .../operator/DictValueOperatorType.java | 4 + .../request/dict/DictValueUpdateRequest.java | 5 - .../module/infra/service/DictKeyService.java | 2 +- .../infra/service/DictValueService.java | 51 +++--- .../service/impl/DictKeyServiceImpl.java | 42 +++-- .../service/impl/DictValueServiceImpl.java | 170 +++++++++++++----- 18 files changed, 223 insertions(+), 166 deletions(-) diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/core/utils/RedisUtils.java b/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/core/utils/RedisUtils.java index ecb8d003..0235329e 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/core/utils/RedisUtils.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/core/utils/RedisUtils.java @@ -1,12 +1,14 @@ package com.orion.ops.framework.redis.core.utils; import com.orion.lang.define.cache.CacheKeyDefine; +import com.orion.lang.utils.Arrays1; import com.orion.lang.utils.io.Streams; import org.springframework.data.redis.core.Cursor; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ScanOptions; +import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -73,6 +75,18 @@ public class RedisUtils { redisTemplate.delete(key); } + /** + * 删除 key + * + * @param keys keys + */ + public static void delete(String... keys) { + if (Arrays1.isEmpty(keys)) { + return; + } + redisTemplate.delete(Arrays.asList(keys)); + } + /** * 删除 key * diff --git a/orion-ops-launch/src/main/resources/templates/orion-server-module-controller.java.vm b/orion-ops-launch/src/main/resources/templates/orion-server-module-controller.java.vm index d7ccd3b3..46abc566 100644 --- a/orion-ops-launch/src/main/resources/templates/orion-server-module-controller.java.vm +++ b/orion-ops-launch/src/main/resources/templates/orion-server-module-controller.java.vm @@ -114,7 +114,7 @@ public class ${table.controllerName} { @Parameter(name = "idList", description = "idList", required = true) @PreAuthorize("@ss.hasPermission('${package.ModuleName}:${typeHyphen}:delete')") public Integer batchDelete${type}(@RequestParam("idList") List idList) { - return ${typeLower}Service.batchDelete${type}ByIdList(idList); + return ${typeLower}Service.delete${type}ByIdList(idList); } @OperatorLog(${type}OperatorType.EXPORT) diff --git a/orion-ops-launch/src/main/resources/templates/orion-server-module-service-impl.java.vm b/orion-ops-launch/src/main/resources/templates/orion-server-module-service-impl.java.vm index ab6c172c..b1757f4a 100644 --- a/orion-ops-launch/src/main/resources/templates/orion-server-module-service-impl.java.vm +++ b/orion-ops-launch/src/main/resources/templates/orion-server-module-service-impl.java.vm @@ -66,9 +66,9 @@ public class ${table.serviceImplName} implements ${table.serviceName} { @Override public Integer update${type}ById(${type}UpdateRequest request) { - log.info("${type}Service-update${type}ById id: {}, request: {}", request.getId(), JSON.toJSONString(request)); - // 查询 Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING); + log.info("${type}Service-update${type}ById id: {}, request: {}", id, JSON.toJSONString(request)); + // 查询 ${type}DO record = ${typeLower}DAO.selectById(id); Valid.notNull(record, ErrorMessage.DATA_ABSENT); // 转换 @@ -191,10 +191,10 @@ public class ${table.serviceImplName} implements ${table.serviceName} { } @Override - public Integer batchDelete${type}ByIdList(List idList) { - log.info("${type}Service-batchDelete${type}ByIdList idList: {}", idList); + public Integer delete${type}ByIdList(List idList) { + log.info("${type}Service-delete${type}ByIdList idList: {}", idList); int effect = ${typeLower}DAO.deleteBatchIds(idList); - log.info("${type}Service-batchDelete${type}ByIdList effect: {}", effect); + log.info("${type}Service-delete${type}ByIdList effect: {}", effect); #if($cacheMeta.enableCache) // 删除缓存 RedisMaps.delete(${type}CacheKeyDefine.${typeConst}, idList); diff --git a/orion-ops-launch/src/main/resources/templates/orion-server-module-service.java.vm b/orion-ops-launch/src/main/resources/templates/orion-server-module-service.java.vm index d33ebe05..f23e5825 100644 --- a/orion-ops-launch/src/main/resources/templates/orion-server-module-service.java.vm +++ b/orion-ops-launch/src/main/resources/templates/orion-server-module-service.java.vm @@ -106,7 +106,7 @@ public interface ${table.serviceName} { * @param idList idList * @return effect */ - Integer batchDelete${type}ByIdList(List idList); + Integer delete${type}ByIdList(List idList); /** * ${apiComment.deleteAll} diff --git a/orion-ops-launch/src/main/resources/templates/orion-server-provider-api-impl.java.vm b/orion-ops-launch/src/main/resources/templates/orion-server-provider-api-impl.java.vm index 13af9dc0..c8a2594e 100644 --- a/orion-ops-launch/src/main/resources/templates/orion-server-provider-api-impl.java.vm +++ b/orion-ops-launch/src/main/resources/templates/orion-server-provider-api-impl.java.vm @@ -137,12 +137,12 @@ public class ${type}ApiImpl implements ${type}Api { } @Override - public Integer batchDelete${type}ByIdList(List idList) { - log.info("${type}Api.batchDelete${type}ByIdList idList: {}", idList); + public Integer delete${type}ByIdList(List idList) { + log.info("${type}Api.delete${type}ByIdList idList: {}", idList); Valid.notEmpty(idList, ErrorMessage.ID_MISSING); // 删除 - Integer effect = ${typeLower}Service.batchDelete${type}ByIdList(idList); - log.info("${type}Api.batchDelete${type}ByIdList effect: {}", effect); + Integer effect = ${typeLower}Service.delete${type}ByIdList(idList); + log.info("${type}Api.delete${type}ByIdList effect: {}", effect); return effect; } diff --git a/orion-ops-launch/src/main/resources/templates/orion-server-provider-api.java.vm b/orion-ops-launch/src/main/resources/templates/orion-server-provider-api.java.vm index f20bf104..22e1ed4a 100644 --- a/orion-ops-launch/src/main/resources/templates/orion-server-provider-api.java.vm +++ b/orion-ops-launch/src/main/resources/templates/orion-server-provider-api.java.vm @@ -95,7 +95,7 @@ public interface ${type}Api { * @param idList idList * @return effect */ - Integer batchDelete${type}ByIdList(List idList); + Integer delete${type}ByIdList(List idList); /** * ${apiComment.deleteAll} diff --git a/orion-ops-launch/src/main/resources/templates/orion-server-test-api-impl-tests.java.vm b/orion-ops-launch/src/main/resources/templates/orion-server-test-api-impl-tests.java.vm index 12fda65f..e998725c 100644 --- a/orion-ops-launch/src/main/resources/templates/orion-server-test-api-impl-tests.java.vm +++ b/orion-ops-launch/src/main/resources/templates/orion-server-test-api-impl-tests.java.vm @@ -98,8 +98,8 @@ public class ${type}ApiImplTests extends BaseUnitTest { @Test @Order(9) - public void batchDelete${type}ByIdListTest() { - Integer effect = ${typeLower}Api.batchDelete${type}ByIdList(Lists.of(lastId)); + public void delete${type}ByIdListTest() { + Integer effect = ${typeLower}Api.delete${type}ByIdList(Lists.of(lastId)); assertEquals(effect, 0); } diff --git a/orion-ops-launch/src/main/resources/templates/orion-server-test-service-impl-tests.java.vm b/orion-ops-launch/src/main/resources/templates/orion-server-test-service-impl-tests.java.vm index 33e8d541..2abd02c4 100644 --- a/orion-ops-launch/src/main/resources/templates/orion-server-test-service-impl-tests.java.vm +++ b/orion-ops-launch/src/main/resources/templates/orion-server-test-service-impl-tests.java.vm @@ -109,8 +109,8 @@ public class ${type}ServiceImplTests extends BaseUnitTest { @Test @Order(10) - public void batchDelete${type}ByIdListTest() { - Integer effect = ${typeLower}Service.batchDelete${type}ByIdList(Lists.of(lastId)); + public void delete${type}ByIdListTest() { + Integer effect = ${typeLower}Service.delete${type}ByIdList(Lists.of(lastId)); assertEquals(effect, 0); } diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/controller/DictKeyController.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/controller/DictKeyController.java index 1b485d55..09e29872 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/controller/DictKeyController.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/controller/DictKeyController.java @@ -77,7 +77,7 @@ public class DictKeyController { @Parameter(name = "idList", description = "idList", required = true) @PreAuthorize("@ss.hasPermission('infra:dict-key:delete')") public Integer batchDeleteDictKey(@RequestParam("idList") List idList) { - return dictKeyService.batchDeleteDictKeyByIdList(idList); + return dictKeyService.deleteDictKeyByIdList(idList); } } diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/controller/DictValueController.http b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/controller/DictValueController.http index 6ac8173f..5c0bd191 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/controller/DictValueController.http +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/controller/DictValueController.http @@ -31,32 +31,10 @@ Authorization: {{token}} } -### 查询字典配置值 -GET {{baseUrl}}/infra/dict-value/get?id=1 -Authorization: {{token}} - - -### 批量查询字典配置值 -GET {{baseUrl}}/infra/dict-value/batch-get?idList=1,2,3 -Authorization: {{token}} - - ### 查询全部字典配置值 -POST {{baseUrl}}/infra/dict-value/list -Content-Type: application/json +POST {{baseUrl}}/infra/dict-value/list?key= Authorization: {{token}} -{ - "id": "", - "keyId": "", - "key": "", - "label": "", - "value": "", - "desc": "", - "extra": "", - "sort": "" -} - ### 分页查询字典配置值 POST {{baseUrl}}/infra/dict-value/query @@ -66,14 +44,10 @@ Authorization: {{token}} { "page": 1, "limit": 10, - "id": "", "keyId": "", - "key": "", "label": "", "value": "", - "desc": "", - "extra": "", - "sort": "" + "desc": "" } @@ -87,21 +61,3 @@ DELETE {{baseUrl}}/infra/dict-value/batch-delete?idList=1,2,3 Authorization: {{token}} -### 导出字典配置值 -POST {{baseUrl}}/infra/dict-value/export -Content-Type: application/json -Authorization: {{token}} - -{ - "id": "", - "keyId": "", - "key": "", - "label": "", - "value": "", - "desc": "", - "extra": "", - "sort": "" -} - - -### diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/controller/DictValueController.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/controller/DictValueController.java index cad710b8..eda973bf 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/controller/DictValueController.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/controller/DictValueController.java @@ -59,20 +59,10 @@ public class DictValueController { } @IgnoreLog(IgnoreLogMode.RET) - @GetMapping("/get") + @GetMapping("/list") @Operation(summary = "查询字典配置值") - @Parameter(name = "id", description = "id", required = true) - @PreAuthorize("@ss.hasPermission('infra:dict-value:query')") - public DictValueVO getDictValue(@RequestParam("id") Long id) { - return dictValueService.getDictValueById(id); - } - - @IgnoreLog(IgnoreLogMode.RET) - @PostMapping("/list") - @Operation(summary = "查询全部字典配置值") - @PreAuthorize("@ss.hasPermission('infra:dict-value:query')") - public List getDictValueList(@Validated @RequestBody DictValueQueryRequest request) { - return dictValueService.getDictValueList(request); + public List getDictValueList(@RequestParam("key") String key) { + return dictValueService.getDictValueList(key); } @IgnoreLog(IgnoreLogMode.RET) @@ -98,7 +88,7 @@ public class DictValueController { @Parameter(name = "idList", description = "idList", required = true) @PreAuthorize("@ss.hasPermission('infra:dict-value:delete')") public Integer batchDeleteDictValue(@RequestParam("idList") List idList) { - return dictValueService.batchDeleteDictValueByIdList(idList); + return dictValueService.deleteDictValueByIdList(idList); } } diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/define/operator/DictKeyOperatorType.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/define/operator/DictKeyOperatorType.java index 07edea53..c065788b 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/define/operator/DictKeyOperatorType.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/define/operator/DictKeyOperatorType.java @@ -25,6 +25,7 @@ public class DictKeyOperatorType extends InitializingOperatorTypes { @Override public OperatorType[] types() { return new OperatorType[]{ + // todo 添加参数 new OperatorType(L, CREATE, "创建字典配置项"), new OperatorType(M, UPDATE, "更新字典配置项"), new OperatorType(H, DELETE, "删除字典配置项"), diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/define/operator/DictValueOperatorType.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/define/operator/DictValueOperatorType.java index 600e96f1..e6355c68 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/define/operator/DictValueOperatorType.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/define/operator/DictValueOperatorType.java @@ -20,11 +20,15 @@ public class DictValueOperatorType extends InitializingOperatorTypes { public static final String UPDATE = "dict-value:update"; + // todo 实现 + public static final String ROLLBACK = "dict-value:rollback"; + public static final String DELETE = "dict-value:delete"; @Override public OperatorType[] types() { return new OperatorType[]{ + // todo 添加参数 new OperatorType(L, CREATE, "创建字典配置值"), new OperatorType(M, UPDATE, "更新字典配置值"), new OperatorType(H, DELETE, "删除字典配置值"), diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/entity/request/dict/DictValueUpdateRequest.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/entity/request/dict/DictValueUpdateRequest.java index 73aaf02d..47930b04 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/entity/request/dict/DictValueUpdateRequest.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/entity/request/dict/DictValueUpdateRequest.java @@ -33,11 +33,6 @@ public class DictValueUpdateRequest implements Serializable { @Schema(description = "配置项id") private Long keyId; - @NotBlank - @Size(max = 32) - @Schema(description = "配置项") - private String key; - @NotBlank @Size(max = 32) @Schema(description = "配置名称") diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/DictKeyService.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/DictKeyService.java index ed960f55..aa9a86a3 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/DictKeyService.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/DictKeyService.java @@ -52,6 +52,6 @@ public interface DictKeyService { * @param idList idList * @return effect */ - Integer batchDeleteDictKeyByIdList(List idList); + Integer deleteDictKeyByIdList(List idList); } diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/DictValueService.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/DictValueService.java index aaf74c81..7d5b9ca3 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/DictValueService.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/DictValueService.java @@ -33,28 +33,13 @@ public interface DictValueService { */ Integer updateDictValueById(DictValueUpdateRequest request); - /** - * 查询字典配置值 - * - * @param id id - * @return row - */ - DictValueVO getDictValueById(Long id); - /** * 查询全部字典配置值 * - * @param request request + * @param key key * @return rows */ - List getDictValueList(DictValueQueryRequest request); - - /** - * 通过缓存查询字典配置值 - * - * @return rows - */ - List getDictValueListByCache(); + List getDictValueList(String key); /** * 分页查询字典配置值 @@ -65,7 +50,17 @@ public interface DictValueService { DataGrid getDictValuePage(DictValueQueryRequest request); /** - * 删除字典配置值 + * 通过 keyId 更新 keyName + * + * @param keyId keyId + * @param beforeKey beforeKey + * @param newKey newKey + * @return effect + */ + Integer updateKeyNameByKeyId(Long keyId, String beforeKey, String newKey); + + /** + * 通过 id 删除字典配置值 * * @param id id * @return effect @@ -73,11 +68,27 @@ public interface DictValueService { Integer deleteDictValueById(Long id); /** - * 批量删除字典配置值 + * 通过 id 批量删除字典配置值 * * @param idList idList * @return effect */ - Integer batchDeleteDictValueByIdList(List idList); + Integer deleteDictValueByIdList(List idList); + + /** + * 通过 keyId 删除字典配置值 + * + * @param keyId keyId + * @return effect + */ + Integer deleteDictValueByKeyId(Long keyId); + + /** + * 通过 keyId 批量删除字典配置值 + * + * @param keyIdList keyIdList + * @return effect + */ + Integer deleteDictValueByKeyIdList(List keyIdList); } diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DictKeyServiceImpl.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DictKeyServiceImpl.java index 72b26247..1b09196b 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DictKeyServiceImpl.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DictKeyServiceImpl.java @@ -2,6 +2,7 @@ package com.orion.ops.module.infra.service.impl; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.orion.lang.utils.Objects1; import com.orion.ops.framework.common.constant.Const; import com.orion.ops.framework.common.constant.ErrorMessage; import com.orion.ops.framework.common.utils.Valid; @@ -15,8 +16,10 @@ import com.orion.ops.module.infra.entity.request.dict.DictKeyCreateRequest; import com.orion.ops.module.infra.entity.request.dict.DictKeyUpdateRequest; import com.orion.ops.module.infra.entity.vo.DictKeyVO; import com.orion.ops.module.infra.service.DictKeyService; +import com.orion.ops.module.infra.service.DictValueService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.Comparator; @@ -37,9 +40,12 @@ public class DictKeyServiceImpl implements DictKeyService { @Resource private DictKeyDAO dictKeyDAO; + @Resource + private DictValueService dictValueService; + @Override public Long createDictKey(DictKeyCreateRequest request) { - log.info("DictKeyService-createDictKey request: {}", JSON.toJSONString(request)); + log.info("DictKeyService-createDictKey request: {}" , JSON.toJSONString(request)); // 转换 DictKeyDO record = DictKeyConvert.MAPPER.to(request); // 查询数据是否冲突 @@ -47,17 +53,18 @@ public class DictKeyServiceImpl implements DictKeyService { // 插入 int effect = dictKeyDAO.insert(record); Long id = record.getId(); - log.info("DictKeyService-createDictKey id: {}, effect: {}", id, effect); + log.info("DictKeyService-createDictKey id: {}, effect: {}" , id, effect); // 删除缓存 RedisMaps.delete(DictCacheKeyDefine.DICT_KEY); return id; } @Override + @Transactional(rollbackFor = Exception.class) public Integer updateDictKeyById(DictKeyUpdateRequest request) { - log.info("DictKeyService-updateDictKeyById id: {}, request: {}", request.getId(), JSON.toJSONString(request)); - // 查询 Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING); + log.info("DictKeyService-updateDictKeyById id: {}, request: {}" , id, JSON.toJSONString(request)); + // 查询 DictKeyDO record = dictKeyDAO.selectById(id); Valid.notNull(record, ErrorMessage.DATA_ABSENT); // 转换 @@ -66,12 +73,13 @@ public class DictKeyServiceImpl implements DictKeyService { this.checkDictKeyPresent(updateRecord); // 更新 int effect = dictKeyDAO.updateById(updateRecord); - log.info("DictKeyService-updateDictKeyById effect: {}", effect); + // 如果修改了 key 则需要修改 dictValue.key + if (!Objects1.eq(record.getKey(), request.getKey())) { + dictValueService.updateKeyNameByKeyId(id, record.getKey(), request.getKey()); + } // 删除缓存 RedisMaps.delete(DictCacheKeyDefine.DICT_KEY); - - // 修改 value 的 key - + log.info("DictKeyService-updateDictKeyById effect: {}" , effect); return effect; } @@ -96,29 +104,33 @@ public class DictKeyServiceImpl implements DictKeyService { return list.stream() .filter(s -> !s.getId().equals(Const.NONE_ID)) .map(DictKeyConvert.MAPPER::to) - .sorted(Comparator.comparing(DictKeyVO::getId).reversed()) + .sorted(Comparator.comparing(DictKeyVO::getKey)) .collect(Collectors.toList()); } @Override public Integer deleteDictKeyById(Long id) { - log.info("DictKeyService-deleteDictKeyById id: {}", id); + log.info("DictKeyService-deleteDictKeyById id: {}" , id); // 检查数据是否存在 DictKeyDO record = dictKeyDAO.selectById(id); Valid.notNull(record, ErrorMessage.DATA_ABSENT); - // 删除 + // 删除配置项 int effect = dictKeyDAO.deleteById(id); - log.info("DictKeyService-deleteDictKeyById id: {}, effect: {}", id, effect); + // 删除配置值 + dictValueService.deleteDictValueByKeyId(id); // 删除缓存 RedisMaps.delete(DictCacheKeyDefine.DICT_KEY, id); + log.info("DictKeyService-deleteDictKeyById id: {}, effect: {}" , id, effect); return effect; } @Override - public Integer batchDeleteDictKeyByIdList(List idList) { - log.info("DictKeyService-batchDeleteDictKeyByIdList idList: {}", idList); + public Integer deleteDictKeyByIdList(List idList) { + log.info("DictKeyService-deleteDictKeyByIdList idList: {}" , idList); int effect = dictKeyDAO.deleteBatchIds(idList); - log.info("DictKeyService-batchDeleteDictKeyByIdList effect: {}", effect); + // 删除配置值 + dictValueService.deleteDictValueByKeyIdList(idList); + log.info("DictKeyService-deleteDictKeyByIdList effect: {}" , effect); // 删除缓存 RedisMaps.delete(DictCacheKeyDefine.DICT_KEY, idList); return effect; diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DictValueServiceImpl.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DictValueServiceImpl.java index 35f11d50..19cb28c3 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DictValueServiceImpl.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DictValueServiceImpl.java @@ -3,9 +3,11 @@ package com.orion.ops.module.infra.service.impl; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.orion.lang.define.wrapper.DataGrid; +import com.orion.lang.utils.collect.Lists; import com.orion.ops.framework.common.constant.Const; import com.orion.ops.framework.common.constant.ErrorMessage; import com.orion.ops.framework.common.utils.Valid; +import com.orion.ops.framework.mybatis.core.query.Conditions; import com.orion.ops.framework.redis.core.utils.RedisMaps; import com.orion.ops.module.infra.convert.DictValueConvert; import com.orion.ops.module.infra.dao.DictKeyDAO; @@ -17,12 +19,16 @@ import com.orion.ops.module.infra.entity.dto.DictValueCacheDTO; import com.orion.ops.module.infra.entity.request.dict.DictValueCreateRequest; import com.orion.ops.module.infra.entity.request.dict.DictValueQueryRequest; import com.orion.ops.module.infra.entity.request.dict.DictValueUpdateRequest; +import com.orion.ops.module.infra.entity.request.history.HistoryValueCreateRequest; import com.orion.ops.module.infra.entity.vo.DictValueVO; +import com.orion.ops.module.infra.enums.HistoryValueTypeEnum; import com.orion.ops.module.infra.service.DictValueService; +import com.orion.ops.module.infra.service.HistoryValueService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; @@ -43,69 +49,77 @@ public class DictValueServiceImpl implements DictValueService { @Resource private DictValueDAO dictValueDAO; + @Resource + private HistoryValueService historyValueService; + @Override public Long createDictValue(DictValueCreateRequest request) { - log.info("DictValueService-createDictValue request: {}", JSON.toJSONString(request)); + log.info("DictValueService-createDictValue request: {}" , JSON.toJSONString(request)); // 转换 DictValueDO record = DictValueConvert.MAPPER.to(request); - // 查询 key 是否存在 - DictKeyDO key = dictKeyDAO.selectById(request.getKeyId()); - Valid.notNull(key, ErrorMessage.CONFIG_ABSENT); - String keyName = record.getKey(); + // 查询 dictKey 是否存在 + DictKeyDO dictKey = dictKeyDAO.selectById(request.getKeyId()); + String key = Valid.notNull(dictKey, ErrorMessage.CONFIG_ABSENT).getKey(); // 查询数据是否冲突 this.checkDictValuePresent(record); // 插入 + record.setKey(key); int effect = dictValueDAO.insert(record); Long id = record.getId(); - log.info("DictValueService-createDictValue id: {}, effect: {}", id, effect); + log.info("DictValueService-createDictValue id: {}, effect: {}" , id, effect); // 删除缓存 - RedisMaps.delete(DictCacheKeyDefine.DICT_VALUE); + RedisMaps.delete(DictCacheKeyDefine.DICT_VALUE.format(key)); return id; } @Override public Integer updateDictValueById(DictValueUpdateRequest request) { - log.info("DictValueService-updateDictValueById id: {}, request: {}", request.getId(), JSON.toJSONString(request)); + log.info("DictValueService-updateDictValueById id: {}, request: {}" , request.getId(), JSON.toJSONString(request)); // 查询 Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING); DictValueDO record = dictValueDAO.selectById(id); Valid.notNull(record, ErrorMessage.DATA_ABSENT); + // 查询 dictKey 是否存在 + DictKeyDO dictKey = dictKeyDAO.selectById(request.getKeyId()); + String key = Valid.notNull(dictKey, ErrorMessage.CONFIG_ABSENT).getKey(); // 转换 DictValueDO updateRecord = DictValueConvert.MAPPER.to(request); // 查询数据是否冲突 this.checkDictValuePresent(updateRecord); // 更新 + updateRecord.setKey(key); int effect = dictValueDAO.updateById(updateRecord); - log.info("DictValueService-updateDictValueById effect: {}", effect); + log.info("DictValueService-updateDictValueById effect: {}" , effect); // 删除缓存 - RedisMaps.delete(DictCacheKeyDefine.DICT_VALUE); + RedisMaps.delete(DictCacheKeyDefine.DICT_VALUE.format(key)); + // 检查值是否发生改变 + String beforeValue = record.getValue(); + String afterValue = request.getValue(); + if (!beforeValue.equals(afterValue)) { + // 记录历史值 + HistoryValueCreateRequest historyRequest = new HistoryValueCreateRequest(); + historyRequest.setRelId(id); + historyRequest.setType(HistoryValueTypeEnum.DICT.name()); + historyRequest.setBeforeValue(beforeValue); + historyRequest.setAfterValue(afterValue); + historyValueService.createHistoryValue(historyRequest); + } return effect; } @Override - public DictValueVO getDictValueById(Long id) { - // 查询 - DictValueDO record = dictValueDAO.selectById(id); - Valid.notNull(record, ErrorMessage.DATA_ABSENT); - // 转换 - return DictValueConvert.MAPPER.to(record); - } - - @Override - public List getDictValueList(DictValueQueryRequest request) { - // 条件 - LambdaQueryWrapper wrapper = this.buildQueryWrapper(request); - // 查询 - return dictValueDAO.of(wrapper).list(DictValueConvert.MAPPER::to); - } - - @Override - public List getDictValueListByCache() { + @SuppressWarnings("unchecked") + public List getDictValueList(String key) { // 查询缓存 - List list = RedisMaps.valuesJson(DictCacheKeyDefine.DICT_VALUE); + String cacheKey = DictCacheKeyDefine.DICT_VALUE.format(key); + List list = RedisMaps.valuesJson(cacheKey, (Class) DictCacheKeyDefine.DICT_VALUE.getType()); if (list.isEmpty()) { // 查询数据库 - list = dictValueDAO.of().list(DictValueConvert.MAPPER::toCache); + list = dictValueDAO.of() + .createWrapper() + .eq(DictValueDO::getKey, key) + .then() + .list(DictValueConvert.MAPPER::toCache); // 添加默认值 防止穿透 if (list.isEmpty()) { list.add(DictValueCacheDTO.builder() @@ -113,13 +127,14 @@ public class DictValueServiceImpl implements DictValueService { .build()); } // 设置缓存 - RedisMaps.putAllJson(DictCacheKeyDefine.DICT_VALUE.getKey(), s -> s.getId().toString(), list); - RedisMaps.setExpire(DictCacheKeyDefine.DICT_VALUE); + RedisMaps.putAllJson(cacheKey, s -> s.getId().toString(), list); + RedisMaps.setExpire(cacheKey, DictCacheKeyDefine.DICT_VALUE); } // 删除默认值 return list.stream() .filter(s -> !s.getId().equals(Const.NONE_ID)) .map(DictValueConvert.MAPPER::to) + .sorted(Comparator.comparing(DictValueVO::getSort)) .collect(Collectors.toList()); } @@ -134,26 +149,84 @@ public class DictValueServiceImpl implements DictValueService { } @Override - public Integer deleteDictValueById(Long id) { - log.info("DictValueService-deleteDictValueById id: {}", id); - // 检查数据是否存在 - DictValueDO record = dictValueDAO.selectById(id); - Valid.notNull(record, ErrorMessage.DATA_ABSENT); - // 删除 - int effect = dictValueDAO.deleteById(id); - log.info("DictValueService-deleteDictValueById id: {}, effect: {}", id, effect); + public Integer updateKeyNameByKeyId(Long keyId, String beforeKey, String newKey) { + // 修改数据库 + DictValueDO updateRecord = new DictValueDO(); + updateRecord.setKey(newKey); + LambdaQueryWrapper wrapper = dictValueDAO.lambda() + .eq(DictValueDO::getKeyId, beforeKey); + int effect = dictValueDAO.update(updateRecord, wrapper); // 删除缓存 - RedisMaps.delete(DictCacheKeyDefine.DICT_VALUE, id); + String beforeCacheKey = DictCacheKeyDefine.DICT_VALUE.format(beforeKey); + String newCacheKey = DictCacheKeyDefine.DICT_VALUE.format(newKey); + RedisMaps.delete(beforeCacheKey, newCacheKey); return effect; } @Override - public Integer batchDeleteDictValueByIdList(List idList) { - log.info("DictValueService-batchDeleteDictValueByIdList idList: {}", idList); + public Integer deleteDictValueById(Long id) { + log.info("DictValueService-deleteDictValueById id: {}" , id); + // 检查数据是否存在 + DictValueDO record = dictValueDAO.selectById(id); + Valid.notNull(record, ErrorMessage.DATA_ABSENT); + // 删除 + return this.deleteDictValue(Lists.singleton(id), Lists.singleton(record)); + } + + @Override + public Integer deleteDictValueByIdList(List idList) { + log.info("DictValueService-deleteDictValueByIdList idList: {}" , idList); + // 查询数据 + List records = dictValueDAO.selectBatchIds(idList); + // 删除 + return this.deleteDictValue(idList, records); + } + + @Override + public Integer deleteDictValueByKeyId(Long keyId) { + log.info("DictValueService-deleteDictValueByKeyId keyId: {}" , keyId); + // 查询数据 + List records = dictValueDAO.selectList(Conditions.eq(DictValueDO::getKeyId, keyId)); + // 删除 + List idList = records.stream() + .map(DictValueDO::getId) + .collect(Collectors.toList()); + return this.deleteDictValue(idList, records); + } + + @Override + public Integer deleteDictValueByKeyIdList(List keyIdList) { + log.info("DictValueService-deleteDictValueByKeyIdList keyIdList: {}" , keyIdList); + // 查询数据 + List records = dictValueDAO.selectList(Conditions.in(DictValueDO::getKeyId, keyIdList)); + // 删除 + List idList = records.stream() + .map(DictValueDO::getId) + .collect(Collectors.toList()); + return this.deleteDictValue(idList, records); + } + + /** + * 删除字典配置项 + * + * @param idList idList + * @param records records + * @return effect + */ + private int deleteDictValue(List idList, List records) { + if (records.isEmpty()) { + return 0; + } + // 删除 int effect = dictValueDAO.deleteBatchIds(idList); - log.info("DictValueService-batchDeleteDictValueByIdList effect: {}", effect); + log.info("DictValueService-deleteDictValue effect: {}" , effect); // 删除缓存 - RedisMaps.delete(DictCacheKeyDefine.DICT_VALUE, idList); + List keyList = records.stream() + .map(DictValueDO::getKey) + .distinct() + .map(DictCacheKeyDefine.DICT_VALUE::format) + .collect(Collectors.toList()); + RedisMaps.delete(keyList); return effect; } @@ -184,9 +257,10 @@ public class DictValueServiceImpl implements DictValueService { private LambdaQueryWrapper buildQueryWrapper(DictValueQueryRequest request) { return dictValueDAO.wrapper() .eq(DictValueDO::getKeyId, request.getKeyId()) - .eq(DictValueDO::getLabel, request.getLabel()) - .eq(DictValueDO::getValue, request.getValue()) - .eq(DictValueDO::getDesc, request.getDesc()); + .like(DictValueDO::getLabel, request.getLabel()) + .like(DictValueDO::getValue, request.getValue()) + .like(DictValueDO::getDesc, request.getDesc()) + .orderByDesc(DictValueDO::getId); } }