新增参数管理

This commit is contained in:
阿沐 2022-11-16 23:50:12 +08:00
parent 7b0d497748
commit edfac98e94
10 changed files with 515 additions and 0 deletions

View File

@ -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<SysParamsEntity> list) {
if (CollUtil.isEmpty(list)) {
return;
}
// list 转成 map
Map<String, Object> 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);
}
}

View File

@ -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<PageResult<SysParamsVO>> page(@Valid SysParamsQuery query) {
PageResult<SysParamsVO> page = sysParamsService.page(query);
return Result.ok(page);
}
@GetMapping("{id}")
@Operation(summary = "信息")
@PreAuthorize("hasAuthority('sys:params:all')")
public Result<SysParamsVO> 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<String> save(@RequestBody SysParamsVO vo) {
sysParamsService.save(vo);
return Result.ok();
}
@PutMapping
@Operation(summary = "修改")
@PreAuthorize("hasAuthority('sys:params:all')")
public Result<String> update(@RequestBody @Valid SysParamsVO vo) {
sysParamsService.update(vo);
return Result.ok();
}
@DeleteMapping
@Operation(summary = "删除")
@PreAuthorize("hasAuthority('sys:params:all')")
public Result<String> delete(@RequestBody List<Long> idList) {
sysParamsService.delete(idList);
return Result.ok();
}
}

View File

@ -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<SysParamsVO> convertList(List<SysParamsEntity> list);
}

View File

@ -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<SysParamsEntity> {
default boolean isExist(String paramKey) {
return this.exists(new QueryWrapper<SysParamsEntity>().eq("param_key" , paramKey));
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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<SysParamsEntity> {
PageResult<SysParamsVO> page(SysParamsQuery query);
void save(SysParamsVO vo);
void update(SysParamsVO vo);
void delete(List<Long> idList);
/**
* 根据paramKey获取字符串值
*
* @param paramKey 参数Key
*/
String getString(String paramKey);
/**
* 根据paramKey获取整型值
*
* @param paramKey 参数Key
*/
int getInt(String paramKey);
/**
* 根据paramKey获取对象值
*
* @param paramKey 参数Key
* @param valueType 类型
*/
<T> T getJSONObject(String paramKey, Class<T> valueType);
}

View File

@ -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<SysParamsDao, SysParamsEntity> implements SysParamsService {
private final SysParamsCache sysParamsCache;
@PostConstruct
public void init() {
// 查询列表
List<SysParamsEntity> list = baseMapper.selectList(null);
// 保存到缓存
sysParamsCache.saveList(list);
}
@Override
public PageResult<SysParamsVO> page(SysParamsQuery query) {
IPage<SysParamsEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));
return new PageResult<>(SysParamsConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
}
private LambdaQueryWrapper<SysParamsEntity> getWrapper(SysParamsQuery query) {
LambdaQueryWrapper<SysParamsEntity> 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<Long> idList) {
// 查询列表
List<SysParamsEntity> 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> T getJSONObject(String paramKey, Class<T> valueType) {
String value = getString(paramKey);
return JsonUtils.parseObject(value, valueType);
}
}

View File

@ -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;
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="net.maku.system.dao.SysParamsDao">
</mapper>