From a6f4cdb932fa27512b9965ebe93e48abd3a3b43d Mon Sep 17 00:00:00 2001 From: GYJ <1157756119@qq.com> Date: Mon, 10 Feb 2025 13:28:34 +0800 Subject: [PATCH] =?UTF-8?q?sys=20param=20=E5=88=A0=E9=99=A4=20id?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/czg/SystemApplication.java | 2 + .../czg/controller/SysParamController.java | 38 +++++++++- cash-common/cash-common-redis/pom.xml | 4 + .../main/java/com/czg/config/RedisConfig.java | 75 +++++++++++++------ .../czg/service/system/dto/SysParamsDTO.java | 4 +- .../czg/service/system/entity/SysParams.java | 17 ++--- .../system/service/SysParamsService.java | 28 ++++++- .../service/impl/SysParamsServiceImpl.java | 64 ++++++++++++++-- 8 files changed, 184 insertions(+), 48 deletions(-) diff --git a/cash-api/system-server/src/main/java/com/czg/SystemApplication.java b/cash-api/system-server/src/main/java/com/czg/SystemApplication.java index 05932657..93542877 100644 --- a/cash-api/system-server/src/main/java/com/czg/SystemApplication.java +++ b/cash-api/system-server/src/main/java/com/czg/SystemApplication.java @@ -3,11 +3,13 @@ package com.czg; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /** * @author ww */ +@EnableCaching @SpringBootApplication @EnableDiscoveryClient @MapperScan("com.czg.service.system.mapper") diff --git a/cash-api/system-server/src/main/java/com/czg/controller/SysParamController.java b/cash-api/system-server/src/main/java/com/czg/controller/SysParamController.java index 61a72bc3..fd234dd3 100644 --- a/cash-api/system-server/src/main/java/com/czg/controller/SysParamController.java +++ b/cash-api/system-server/src/main/java/com/czg/controller/SysParamController.java @@ -9,6 +9,8 @@ import jakarta.annotation.Resource; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import java.util.List; + /** * @author GYJoker */ @@ -26,18 +28,48 @@ public class SysParamController { * @return 新增结果 */ @PostMapping - public CzgResult insertParams(@RequestBody @Validated({InsertGroup.class}) SysParamsDTO paramsDTO) { + public CzgResult insertParams(@RequestBody @Validated({InsertGroup.class}) SysParamsDTO paramsDTO) { return sysParamsService.insertParams(paramsDTO); } /** * 修改参数 * @param paramsDTO 参数 - * @return 修改结果 + * @return 修改结果 */ @PutMapping - public CzgResult updateParams(@RequestBody @Validated({UpdateGroup.class}) SysParamsDTO paramsDTO) { + public CzgResult updateParams(@RequestBody @Validated({UpdateGroup.class}) SysParamsDTO paramsDTO) { return sysParamsService.updateParams(paramsDTO); } + /** + * 删除参数 + * @param code 参数code + * @return 删除结果 + */ + @DeleteMapping("/{code}") + public CzgResult deleteParams(@PathVariable String code) { + return sysParamsService.deleteParams(code); + } + + /** + * 根据参数编码获取参数 + * @param code 参数编码 + * @return 参数 + */ + @GetMapping("/code/{code}") + public CzgResult deleteCode(@PathVariable String code) { + return sysParamsService.getParamsByCode(code); + } + + /** + * 根据参数类型获取参数 + * @param type 参数类型 + * @return 参数列表 + */ + @GetMapping("/type/{type}") + public CzgResult> deleteType(@PathVariable String type) { + return sysParamsService.getParamsByType(Integer.valueOf(type)); + } + } diff --git a/cash-common/cash-common-redis/pom.xml b/cash-common/cash-common-redis/pom.xml index eb378626..49a0e325 100644 --- a/cash-common/cash-common-redis/pom.xml +++ b/cash-common/cash-common-redis/pom.xml @@ -19,5 +19,9 @@ org.springframework.data spring-data-redis + + com.alibaba.fastjson2 + fastjson2 + diff --git a/cash-common/cash-common-redis/src/main/java/com/czg/config/RedisConfig.java b/cash-common/cash-common-redis/src/main/java/com/czg/config/RedisConfig.java index bfd0ab37..5b4c0dbe 100644 --- a/cash-common/cash-common-redis/src/main/java/com/czg/config/RedisConfig.java +++ b/cash-common/cash-common-redis/src/main/java/com/czg/config/RedisConfig.java @@ -1,41 +1,74 @@ package com.czg.config; +import jakarta.annotation.Resource; +import org.springframework.cache.CacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.cache.RedisCacheConfiguration; +import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; -import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.*; +import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.StringRedisSerializer; +import java.time.Duration; + /** * @author GYJoker */ @Configuration public class RedisConfig { - /** - * redisTemplate相关配置 - * [@Role(BeanDefinition.ROLE_INFRASTRUCTURE)] 表明这个bean是完全后台模式,不需要被代理。 - * - * @param factory Redis连接工厂类 - * @return 返回配置项对象 - */ + @Resource + private RedisConnectionFactory factory; + @Bean - public RedisTemplate redisTemplate(RedisConnectionFactory factory) { + public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { + RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(30)) + // 设置缓存过期时间为30分钟 + .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())) + .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())) + .disableCachingNullValues(); - RedisTemplate template = new RedisTemplate<>(); - // 配置连接工厂 - template.setConnectionFactory(factory); + return RedisCacheManager.builder(redisConnectionFactory) + .cacheDefaults(cacheConfiguration) + .build(); + } + @Bean + public RedisTemplate redisTemplate() { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setHashKeySerializer(new StringRedisSerializer()); + redisTemplate.setHashValueSerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(new StringRedisSerializer()); + redisTemplate.setConnectionFactory(factory); + return redisTemplate; + } - // 使用StringRedisSerializer来序列化和反序列化redis的key值 - template.setKeySerializer(new StringRedisSerializer()); - // 设置hash key 和value序列化模式 - template.setHashKeySerializer(new StringRedisSerializer()); - // template.setHashValueSerializer(jacksonSerial); - template.setHashValueSerializer(new StringRedisSerializer()); - template.setValueSerializer(new StringRedisSerializer()); - template.afterPropertiesSet(); + @Bean + public HashOperations hashOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForHash(); + } - return template; + @Bean + public ValueOperations valueOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForValue(); + } + + @Bean + public ListOperations listOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForList(); + } + + @Bean + public SetOperations setOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForSet(); + } + + @Bean + public ZSetOperations zSetOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForZSet(); } } diff --git a/cash-service/system-service/src/main/java/com/czg/service/system/dto/SysParamsDTO.java b/cash-service/system-service/src/main/java/com/czg/service/system/dto/SysParamsDTO.java index cb86e8cb..04ff8088 100644 --- a/cash-service/system-service/src/main/java/com/czg/service/system/dto/SysParamsDTO.java +++ b/cash-service/system-service/src/main/java/com/czg/service/system/dto/SysParamsDTO.java @@ -5,14 +5,14 @@ import com.czg.group.UpdateGroup; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import lombok.Data; +import lombok.experimental.Accessors; /** * @author GYJoker */ @Data +@Accessors(chain = true) public class SysParamsDTO { - @NotNull(message = "id不能为空", groups = {UpdateGroup.class}) - private Long id; @NotBlank(message = "参数编码不能为空", groups = {UpdateGroup.class, InsertGroup.class}) private String paramCode; diff --git a/cash-service/system-service/src/main/java/com/czg/service/system/entity/SysParams.java b/cash-service/system-service/src/main/java/com/czg/service/system/entity/SysParams.java index 8a8ca926..c75bbe0a 100644 --- a/cash-service/system-service/src/main/java/com/czg/service/system/entity/SysParams.java +++ b/cash-service/system-service/src/main/java/com/czg/service/system/entity/SysParams.java @@ -2,19 +2,17 @@ package com.czg.service.system.entity; import com.mybatisflex.annotation.Column; import com.mybatisflex.annotation.Id; -import com.mybatisflex.annotation.KeyType; import com.mybatisflex.annotation.Table; -import java.io.Serializable; -import java.time.LocalDateTime; - -import java.io.Serial; - import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; +import java.io.Serial; +import java.io.Serializable; +import java.time.LocalDateTime; + /** * 实体类。 * @@ -32,15 +30,10 @@ public class SysParams implements Serializable { @Serial private static final long serialVersionUID = 1L; - /** - * ID - */ - @Id(keyType = KeyType.Auto) - private Long id; - /** * 参数编码 */ + @Id private String paramCode; /** diff --git a/cash-service/system-service/src/main/java/com/czg/service/system/service/SysParamsService.java b/cash-service/system-service/src/main/java/com/czg/service/system/service/SysParamsService.java index a650a926..38e92148 100644 --- a/cash-service/system-service/src/main/java/com/czg/service/system/service/SysParamsService.java +++ b/cash-service/system-service/src/main/java/com/czg/service/system/service/SysParamsService.java @@ -5,6 +5,8 @@ import com.czg.service.system.dto.SysParamsDTO; import com.mybatisflex.core.service.IService; import com.czg.service.system.entity.SysParams; +import java.util.List; + /** * 服务层。 * @@ -18,12 +20,34 @@ public interface SysParamsService extends IService { * @param paramsDTO 参数 * @return 新增结果 */ - CzgResult insertParams(SysParamsDTO paramsDTO); + CzgResult insertParams(SysParamsDTO paramsDTO); /** * 修改参数 * @param paramsDTO 参数 * @return 修改结果 */ - CzgResult updateParams(SysParamsDTO paramsDTO); + CzgResult updateParams(SysParamsDTO paramsDTO); + + /** + * 删除参数 + * @param code 参数code + * @return 删除结果 + */ + CzgResult deleteParams(String code); + + /** + * 根据参数编码获取参数 + * @param code 参数编码 + * @return 参数 + */ + CzgResult getParamsByCode(String code); + + /** + * 根据参数类型获取参数 + * @param type 参数类型 + * @return 参数列表 + */ + CzgResult> getParamsByType(Integer type); + } diff --git a/cash-service/system-service/src/main/java/com/czg/service/system/service/impl/SysParamsServiceImpl.java b/cash-service/system-service/src/main/java/com/czg/service/system/service/impl/SysParamsServiceImpl.java index b3de3197..d1874e62 100644 --- a/cash-service/system-service/src/main/java/com/czg/service/system/service/impl/SysParamsServiceImpl.java +++ b/cash-service/system-service/src/main/java/com/czg/service/system/service/impl/SysParamsServiceImpl.java @@ -1,5 +1,6 @@ package com.czg.service.system.service.impl; +import cn.hutool.core.bean.BeanUtil; import com.czg.resp.CzgResult; import com.czg.sa.StpKit; import com.czg.service.system.dto.SysParamsDTO; @@ -8,8 +9,14 @@ import com.czg.service.system.mapper.SysParamsMapper; import com.czg.service.system.service.SysParamsService; import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.spring.service.impl.ServiceImpl; +import org.springframework.cache.annotation.CacheConfig; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.List; + /** * 服务层实现。 * @@ -17,10 +24,11 @@ import org.springframework.stereotype.Service; * @since 2025-02-07 */ @Service +@CacheConfig(cacheNames = "params") public class SysParamsServiceImpl extends ServiceImpl implements SysParamsService{ @Override - public CzgResult insertParams(SysParamsDTO paramsDTO) { + public CzgResult insertParams(SysParamsDTO paramsDTO) { // 查询 paramCode 是否存在 SysParams sysParams = getOne(new QueryWrapper().eq(SysParams::getParamCode, paramsDTO.getParamCode())); if (sysParams != null) { @@ -39,14 +47,15 @@ public class SysParamsServiceImpl extends ServiceImpl updateParams(SysParamsDTO paramsDTO) { + @CacheEvict(key = "#paramsDTO.paramCode") + public CzgResult updateParams(SysParamsDTO paramsDTO) { // 查询 paramCode 是否存在 SysParams sysParams = getOne(new QueryWrapper().eq(SysParams::getParamCode, paramsDTO.getParamCode()) - .ne(SysParams::getId, paramsDTO.getId())); + .ne(SysParams::getParamCode, paramsDTO.getParamCode())); if (sysParams != null) { return CzgResult.failure("参数编码已存在"); } @@ -54,14 +63,53 @@ public class SysParamsServiceImpl extends ServiceImpl deleteParams(String code) { + SysParams sysParams = getById(code); + if (sysParams == null) { + return CzgResult.failure("参数不存在"); + } + + removeById(code); + return CzgResult.success(true); + } + + @Override + @Cacheable(key = "#code") + public CzgResult getParamsByCode(String code) { + SysParams sysParams = getOne(new QueryWrapper().eq(SysParams::getParamCode, code)); + + if (sysParams == null) { + return CzgResult.failure("参数不存在"); + } + + SysParamsDTO sysParamsDTO = BeanUtil.toBean(sysParams, SysParamsDTO.class); + + return CzgResult.success(sysParamsDTO); + } + + @Override + public CzgResult> getParamsByType(Integer type) { + List sysParamsList = list(new QueryWrapper().eq(SysParams::getParamType, type)); + List sysParamsDTOList = new ArrayList<>(); + for (SysParams sysParams : sysParamsList) { + sysParamsDTOList.add(BeanUtil.toBean(sysParams, SysParamsDTO.class)); + } + return CzgResult.success(sysParamsDTOList); } }