package com.ruoyi.basic.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.basic.mapper.BaseParamMapper;
|
import com.ruoyi.basic.pojo.BaseParam;
|
import com.ruoyi.basic.service.BaseParamService;
|
import com.ruoyi.common.utils.SecurityUtils;
|
import com.ruoyi.common.utils.StringUtils;
|
import io.swagger.annotations.Api;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Service;
|
|
import java.time.LocalDateTime;
|
import java.time.format.DateTimeFormatter;
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
import java.util.List;
|
|
/**
|
* <br>
|
* 基础参数定义接口实现类
|
* </br>
|
*
|
* @author deslrey
|
* @version 1.0
|
* @since 2026/03/13 16:41
|
*/
|
@Slf4j
|
@Service
|
@Api("基础参数定义接口实现类")
|
public class BaseParamServiceImpl extends ServiceImpl<BaseParamMapper, BaseParam> implements BaseParamService {
|
|
@Override
|
public List<BaseParam> baseParamList(BaseParam baseParam) {
|
LambdaQueryWrapper<BaseParam> queryWrapper = new LambdaQueryWrapper<>();
|
if (StringUtils.isNotEmpty(baseParam.getParamName())) {
|
queryWrapper.like(BaseParam::getParamName, baseParam.getParamName());
|
}
|
List<BaseParam> list = list(queryWrapper);
|
if (list == null || list.isEmpty()) {
|
return new ArrayList<>(0);
|
}
|
|
// 处理日期格式展示
|
list.forEach(item -> {
|
if (Integer.valueOf(4).equals(item.getParamType()) && StringUtils.isNotEmpty(item.getParamFormat())) {
|
item.setParamFormat(toUpperCasePattern(item.getParamFormat()));
|
}
|
});
|
|
return list;
|
}
|
|
@Override
|
public int addBaseParam(BaseParam baseParam) {
|
if (baseParam == null) {
|
throw new RuntimeException("新增参数不能为空");
|
}
|
// 参数校验
|
checkBaseParam(baseParam);
|
// 自动生成paramKey
|
baseParam.setParamKey(generateParamKey());
|
baseParam.setCreateUser(SecurityUtils.getUsername());
|
baseParam.setCreateTime(LocalDateTime.now());
|
// 设置默认值
|
if (baseParam.getIsRequired() == null) baseParam.setIsRequired(0);
|
if (baseParam.getValueMode() == null) baseParam.setValueMode(1);
|
|
return baseMapper.insert(baseParam);
|
}
|
|
@Override
|
public int updateBaseParam(BaseParam baseParam) {
|
if (baseParam == null || baseParam.getId() == null) {
|
throw new RuntimeException("修改参数ID不能为空");
|
}
|
// 参数校验
|
checkBaseParam(baseParam);
|
baseParam.setUpdateUser(SecurityUtils.getUsername());
|
baseParam.setUpdateTime(LocalDateTime.now());
|
|
return baseMapper.updateById(baseParam);
|
}
|
|
/**
|
* 参数定义合法校验
|
*/
|
private void checkBaseParam(BaseParam baseParam) {
|
if (StringUtils.isEmpty(baseParam.getParamName())) {
|
throw new RuntimeException("参数名称不能为空");
|
}
|
|
// 类型校验 (1:数字, 2:文本, 3:下拉选择, 4:日期时间)
|
List<Integer> validTypes = Arrays.asList(1, 2, 3, 4);
|
if (baseParam.getParamType() == null || !validTypes.contains(baseParam.getParamType())) {
|
throw new RuntimeException("非法参数类型");
|
}
|
|
// 如果是日期类型,校验日期格式配置
|
if (Integer.valueOf(4).equals(baseParam.getParamType())) {
|
if (StringUtils.isEmpty(baseParam.getParamFormat())) {
|
throw new RuntimeException("日期类型必须配置参数格式(如: yyyy-MM-dd)");
|
}
|
try {
|
String standardPattern = normalizePattern(baseParam.getParamFormat());
|
DateTimeFormatter.ofPattern(standardPattern);
|
baseParam.setParamFormat(standardPattern);
|
} catch (Exception e) {
|
throw new RuntimeException("日期格式非法: " + baseParam.getParamFormat());
|
}
|
}
|
}
|
|
/**
|
* 生成参数唯一key (PARAM_XXX)
|
*/
|
private String generateParamKey() {
|
String prefix = "PARAM_";
|
LambdaQueryWrapper<BaseParam> wrapper = new LambdaQueryWrapper<>();
|
wrapper.select(BaseParam::getParamKey)
|
.likeRight(BaseParam::getParamKey, prefix)
|
.orderByDesc(BaseParam::getParamKey)
|
.last("limit 1");
|
|
BaseParam last = baseMapper.selectOne(wrapper);
|
int nextNum = 1;
|
if (last != null && StringUtils.isNotEmpty(last.getParamKey())) {
|
try {
|
String numStr = last.getParamKey().replace(prefix, "");
|
nextNum = Integer.parseInt(numStr) + 1;
|
} catch (Exception e) {
|
log.error("解析ParamKey异常", e);
|
}
|
}
|
return prefix + String.format("%04d", nextNum);
|
}
|
|
/**
|
* 日期格式化
|
*/
|
private String normalizePattern(String pattern) {
|
if (StringUtils.isEmpty(pattern)) return "yyyy-MM-dd";
|
return pattern.replace("YYYY", "yyyy")
|
.replace("DD", "dd")
|
.replace("SS", "ss");
|
}
|
|
/**
|
* 转换为全大写显示
|
*/
|
private String toUpperCasePattern(String pattern) {
|
if (StringUtils.isEmpty(pattern)) return "";
|
return pattern.replace("yyyy", "YYYY")
|
.replace("dd", "DD")
|
.replace("ss", "SS");
|
}
|
|
@Override
|
public int deleteBaseParamByIds(Long[] ids) {
|
if (ids == null || ids.length == 0) {
|
throw new RuntimeException("删除ID不能为空");
|
}
|
return baseMapper.deleteBatchIds(Arrays.asList(ids));
|
}
|
}
|