diff --git a/maku-boot-system/src/main/java/net/maku/system/cache/SysParamsCache.java b/maku-boot-system/src/main/java/net/maku/system/cache/SysParamsCache.java new file mode 100644 index 0000000..97508a5 --- /dev/null +++ b/maku-boot-system/src/main/java/net/maku/system/cache/SysParamsCache.java @@ -0,0 +1,78 @@ +package net.maku.system.cache; + +import cn.hutool.core.collection.CollUtil; +import lombok.AllArgsConstructor; +import net.maku.framework.common.cache.RedisCache; +import net.maku.system.entity.SysParamsEntity; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * 参数管理 Cache + * + * @author 阿沐 babamu@126.com + */ +@Service +@AllArgsConstructor +public class SysParamsCache { + private final RedisCache redisCache; + + /** + * 参数管理 KEY + */ + private final String SYSTEM_PARAMS_KEY = "system:params"; + + /** + * 保存参数列表到缓存 + * + * @param list 参数列表 + */ + public void saveList(List list) { + if (CollUtil.isEmpty(list)) { + return; + } + + // list 转成 map + Map map = list.stream().collect(Collectors.toMap(SysParamsEntity::getParamKey, SysParamsEntity::getParamValue)); + + redisCache.hMSet(SYSTEM_PARAMS_KEY, map, RedisCache.NOT_EXPIRE); + } + + /** + * 删除缓存中的全部参数列表 + */ + public void delList() { + redisCache.delete(SYSTEM_PARAMS_KEY); + } + + /** + * 根据参数键,获取参数值 + * + * @param paramKey 参数键 + */ + public String get(String paramKey) { + return (String) redisCache.hGet(SYSTEM_PARAMS_KEY, paramKey); + } + + /** + * 根据参数键,获取参数值 + * + * @param paramKey 参数键 + * @param paramValue 参数值 + */ + public void save(String paramKey, String paramValue) { + redisCache.hSet(SYSTEM_PARAMS_KEY, paramKey, paramValue); + } + + /** + * 根据参数键,删除参数值 + * + * @param paramKey 参数键 + */ + public void del(String... paramKey) { + redisCache.hDel(SYSTEM_PARAMS_KEY, paramKey); + } +} diff --git a/maku-boot-system/src/main/java/net/maku/system/controller/SysParamsController.java b/maku-boot-system/src/main/java/net/maku/system/controller/SysParamsController.java new file mode 100644 index 0000000..7c7adc1 --- /dev/null +++ b/maku-boot-system/src/main/java/net/maku/system/controller/SysParamsController.java @@ -0,0 +1,75 @@ +package net.maku.system.controller; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.AllArgsConstructor; +import net.maku.framework.common.page.PageResult; +import net.maku.framework.common.utils.Result; +import net.maku.system.convert.SysParamsConvert; +import net.maku.system.entity.SysParamsEntity; +import net.maku.system.query.SysParamsQuery; +import net.maku.system.service.SysParamsService; +import net.maku.system.vo.SysParamsVO; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; +import java.util.List; + +/** + * 参数管理 + * + * @author 阿沐 babamu@126.com + */ +@RestController +@RequestMapping("sys/params") +@Tag(name = "参数管理") +@AllArgsConstructor +public class SysParamsController { + private final SysParamsService sysParamsService; + + @GetMapping("page") + @Operation(summary = "分页") + @PreAuthorize("hasAuthority('sys:params:all')") + public Result> page(@Valid SysParamsQuery query) { + PageResult page = sysParamsService.page(query); + + return Result.ok(page); + } + + @GetMapping("{id}") + @Operation(summary = "信息") + @PreAuthorize("hasAuthority('sys:params:all')") + public Result get(@PathVariable("id") Long id) { + SysParamsEntity entity = sysParamsService.getById(id); + + return Result.ok(SysParamsConvert.INSTANCE.convert(entity)); + } + + @PostMapping + @Operation(summary = "保存") + @PreAuthorize("hasAuthority('sys:params:all')") + public Result save(@RequestBody SysParamsVO vo) { + sysParamsService.save(vo); + + return Result.ok(); + } + + @PutMapping + @Operation(summary = "修改") + @PreAuthorize("hasAuthority('sys:params:all')") + public Result update(@RequestBody @Valid SysParamsVO vo) { + sysParamsService.update(vo); + + return Result.ok(); + } + + @DeleteMapping + @Operation(summary = "删除") + @PreAuthorize("hasAuthority('sys:params:all')") + public Result delete(@RequestBody List idList) { + sysParamsService.delete(idList); + + return Result.ok(); + } +} \ No newline at end of file diff --git a/maku-boot-system/src/main/java/net/maku/system/convert/SysParamsConvert.java b/maku-boot-system/src/main/java/net/maku/system/convert/SysParamsConvert.java new file mode 100644 index 0000000..9e40594 --- /dev/null +++ b/maku-boot-system/src/main/java/net/maku/system/convert/SysParamsConvert.java @@ -0,0 +1,25 @@ +package net.maku.system.convert; + +import net.maku.system.entity.SysParamsEntity; +import net.maku.system.vo.SysParamsVO; +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +import java.util.List; + +/** + * 参数管理 + * + * @author 阿沐 babamu@126.com + */ +@Mapper +public interface SysParamsConvert { + SysParamsConvert INSTANCE = Mappers.getMapper(SysParamsConvert.class); + + SysParamsEntity convert(SysParamsVO vo); + + SysParamsVO convert(SysParamsEntity entity); + + List convertList(List list); + +} \ No newline at end of file diff --git a/maku-boot-system/src/main/java/net/maku/system/dao/SysParamsDao.java b/maku-boot-system/src/main/java/net/maku/system/dao/SysParamsDao.java new file mode 100644 index 0000000..54a71de --- /dev/null +++ b/maku-boot-system/src/main/java/net/maku/system/dao/SysParamsDao.java @@ -0,0 +1,19 @@ +package net.maku.system.dao; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import net.maku.framework.common.dao.BaseDao; +import net.maku.system.entity.SysParamsEntity; +import org.apache.ibatis.annotations.Mapper; + +/** + * 参数管理 + * + * @author 阿沐 babamu@126.com + */ +@Mapper +public interface SysParamsDao extends BaseDao { + + default boolean isExist(String paramKey) { + return this.exists(new QueryWrapper().eq("param_key" , paramKey)); + } +} \ No newline at end of file diff --git a/maku-boot-system/src/main/java/net/maku/system/entity/SysParamsEntity.java b/maku-boot-system/src/main/java/net/maku/system/entity/SysParamsEntity.java new file mode 100644 index 0000000..f279b0c --- /dev/null +++ b/maku-boot-system/src/main/java/net/maku/system/entity/SysParamsEntity.java @@ -0,0 +1,44 @@ +package net.maku.system.entity; + +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import lombok.EqualsAndHashCode; +import net.maku.framework.common.entity.BaseEntity; + +/** + * 参数管理 + * + * @author 阿沐 babamu@126.com + */ +@Data +@EqualsAndHashCode(callSuper = false) +@TableName("sys_params") +public class SysParamsEntity extends BaseEntity { + + /** + * 参数名称 + */ + private String paramName; + + /** + * 系统参数 + */ + private Integer paramType; + + /** + * 参数键 + */ + private String paramKey; + + /** + * 参数值 + */ + private String paramValue; + + /** + * 备注 + */ + private String remark; + + +} \ No newline at end of file diff --git a/maku-boot-system/src/main/java/net/maku/system/query/SysParamsQuery.java b/maku-boot-system/src/main/java/net/maku/system/query/SysParamsQuery.java new file mode 100644 index 0000000..c77dd91 --- /dev/null +++ b/maku-boot-system/src/main/java/net/maku/system/query/SysParamsQuery.java @@ -0,0 +1,26 @@ +package net.maku.system.query; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.EqualsAndHashCode; +import net.maku.framework.common.query.Query; + +/** + * 参数管理查询 + * + * @author 阿沐 babamu@126.com + */ +@Data +@EqualsAndHashCode(callSuper = false) +@Schema(description = "参数管理查询") +public class SysParamsQuery extends Query { + @Schema(description = "系统参数") + private Integer paramType; + + @Schema(description = "参数键") + private String paramKey; + + @Schema(description = "参数值") + private String paramValue; + +} \ No newline at end of file diff --git a/maku-boot-system/src/main/java/net/maku/system/service/SysParamsService.java b/maku-boot-system/src/main/java/net/maku/system/service/SysParamsService.java new file mode 100644 index 0000000..958cf7c --- /dev/null +++ b/maku-boot-system/src/main/java/net/maku/system/service/SysParamsService.java @@ -0,0 +1,47 @@ +package net.maku.system.service; + +import net.maku.framework.common.page.PageResult; +import net.maku.framework.common.service.BaseService; +import net.maku.system.entity.SysParamsEntity; +import net.maku.system.query.SysParamsQuery; +import net.maku.system.vo.SysParamsVO; + +import java.util.List; + +/** + * 参数管理 + * + * @author 阿沐 babamu@126.com + */ +public interface SysParamsService extends BaseService { + + PageResult page(SysParamsQuery query); + + void save(SysParamsVO vo); + + void update(SysParamsVO vo); + + void delete(List idList); + + /** + * 根据paramKey,获取字符串值 + * + * @param paramKey 参数Key + */ + String getString(String paramKey); + + /** + * 根据paramKey,获取整型值 + * + * @param paramKey 参数Key + */ + int getInt(String paramKey); + + /** + * 根据paramKey,获取对象值 + * + * @param paramKey 参数Key + * @param valueType 类型 + */ + T getJSONObject(String paramKey, Class valueType); +} \ No newline at end of file diff --git a/maku-boot-system/src/main/java/net/maku/system/service/impl/SysParamsServiceImpl.java b/maku-boot-system/src/main/java/net/maku/system/service/impl/SysParamsServiceImpl.java new file mode 100644 index 0000000..e2011a6 --- /dev/null +++ b/maku-boot-system/src/main/java/net/maku/system/service/impl/SysParamsServiceImpl.java @@ -0,0 +1,134 @@ +package net.maku.system.service.impl; + +import cn.hutool.core.util.StrUtil; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import lombok.AllArgsConstructor; +import net.maku.framework.common.exception.ServerException; +import net.maku.framework.common.page.PageResult; +import net.maku.framework.common.service.impl.BaseServiceImpl; +import net.maku.framework.common.utils.JsonUtils; +import net.maku.system.cache.SysParamsCache; +import net.maku.system.convert.SysParamsConvert; +import net.maku.system.dao.SysParamsDao; +import net.maku.system.entity.SysParamsEntity; +import net.maku.system.query.SysParamsQuery; +import net.maku.system.service.SysParamsService; +import net.maku.system.vo.SysParamsVO; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.PostConstruct; +import java.util.List; + +/** + * 参数管理 + * + * @author 阿沐 babamu@126.com + */ +@Service +@AllArgsConstructor +public class SysParamsServiceImpl extends BaseServiceImpl implements SysParamsService { + private final SysParamsCache sysParamsCache; + + @PostConstruct + public void init() { + // 查询列表 + List list = baseMapper.selectList(null); + + // 保存到缓存 + sysParamsCache.saveList(list); + } + + @Override + public PageResult page(SysParamsQuery query) { + IPage page = baseMapper.selectPage(getPage(query), getWrapper(query)); + + return new PageResult<>(SysParamsConvert.INSTANCE.convertList(page.getRecords()), page.getTotal()); + } + + private LambdaQueryWrapper getWrapper(SysParamsQuery query) { + LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); + + wrapper.like(StrUtil.isNotBlank(query.getParamKey()), SysParamsEntity::getParamKey, query.getParamKey()); + wrapper.eq(StrUtil.isNotBlank(query.getParamValue()), SysParamsEntity::getParamValue, query.getParamValue()); + wrapper.eq(query.getParamType() != null, SysParamsEntity::getParamType, query.getParamType()); + wrapper.orderByDesc(SysParamsEntity::getId); + + return wrapper; + } + + @Override + public void save(SysParamsVO vo) { + // 判断 参数键 是否存在 + boolean exist = baseMapper.isExist(vo.getParamKey()); + if (exist) { + throw new ServerException("参数键已存在"); + } + + SysParamsEntity entity = SysParamsConvert.INSTANCE.convert(vo); + + baseMapper.insert(entity); + + // 保存到缓存 + sysParamsCache.save(entity.getParamKey(), entity.getParamValue()); + } + + @Override + public void update(SysParamsVO vo) { + SysParamsEntity entity = baseMapper.selectById(vo.getId()); + + // 如果 参数键 修改过 + if (!StrUtil.equalsIgnoreCase(entity.getParamKey(), vo.getParamKey())) { + // 判断 新参数键 是否存在 + boolean exist = baseMapper.isExist(vo.getParamKey()); + if (exist) { + throw new ServerException("参数键已存在"); + } + + // 删除修改前的缓存 + sysParamsCache.del(entity.getParamKey()); + } + + // 修改数据 + updateById(SysParamsConvert.INSTANCE.convert(vo)); + + // 保存到缓存 + sysParamsCache.save(vo.getParamKey(), vo.getParamValue()); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void delete(List idList) { + // 查询列表 + List list = baseMapper.selectBatchIds(idList); + + // 删除数据 + removeByIds(idList); + + // 删除缓存 + String[] keys = list.stream().map(SysParamsEntity::getParamKey).toArray(String[]::new); + sysParamsCache.del(keys); + } + + @Override + public String getString(String paramKey) { + return sysParamsCache.get(paramKey); + } + + @Override + public int getInt(String paramKey) { + String value = getString(paramKey); + + return Integer.parseInt(value); + } + + @Override + public T getJSONObject(String paramKey, Class valueType) { + String value = getString(paramKey); + + return JsonUtils.parseObject(value, valueType); + } + +} \ No newline at end of file diff --git a/maku-boot-system/src/main/java/net/maku/system/vo/SysParamsVO.java b/maku-boot-system/src/main/java/net/maku/system/vo/SysParamsVO.java new file mode 100644 index 0000000..2a20196 --- /dev/null +++ b/maku-boot-system/src/main/java/net/maku/system/vo/SysParamsVO.java @@ -0,0 +1,60 @@ +package net.maku.system.vo; + +import com.fasterxml.jackson.annotation.JsonFormat; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import net.maku.framework.common.utils.DateUtils; + +import java.io.Serializable; +import java.util.Date; + +/** + * 参数管理 + * + * @author 阿沐 babamu@126.com + */ +@Data +@Schema(description = "参数管理") +public class SysParamsVO implements Serializable { + private static final long serialVersionUID = 1L; + + @Schema(description = "id") + private Long id; + + @Schema(description = "参数名称") + private String paramName; + + @Schema(description = "系统参数") + private Integer paramType; + + @Schema(description = "参数键") + private String paramKey; + + @Schema(description = "参数值") + private String paramValue; + + @Schema(description = "备注") + private String remark; + + @Schema(description = "版本号") + private Integer version; + + @Schema(description = "删除标识") + private Integer deleted; + + @Schema(description = "创建者") + private Long creator; + + @Schema(description = "创建时间") + @JsonFormat(pattern = DateUtils.DATE_TIME_PATTERN) + private Date createTime; + + @Schema(description = "更新者") + private Long updater; + + @Schema(description = "更新时间") + @JsonFormat(pattern = DateUtils.DATE_TIME_PATTERN) + private Date updateTime; + + +} \ No newline at end of file diff --git a/maku-boot-system/src/main/resources/mapper/SysParamsDao.xml b/maku-boot-system/src/main/resources/mapper/SysParamsDao.xml new file mode 100644 index 0000000..47129f8 --- /dev/null +++ b/maku-boot-system/src/main/resources/mapper/SysParamsDao.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file