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.math.BigDecimal; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** *
* 基础参数定义接口实现类 *
* * @author deslrey * @version 1.0 * @since 2026/03/13 16:41 */ @Slf4j @Service @Api("基础参数定义接口实现类") public class BaseParamServiceImpl extends ServiceImpl implements BaseParamService { @Override public List baseParamList(BaseParam baseParam) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); if (StringUtils.isNotEmpty(baseParam.getParamName())) { queryWrapper.like(BaseParam::getParamName, baseParam.getParamName()); } List list = list(queryWrapper); if (list == null || list.isEmpty()) { return new ArrayList<>(0); } return list; } @Override public int addBaseParam(BaseParam baseParam) { if (baseParam == null) { throw new RuntimeException("新增参数不能为空"); } // 参数校验 checkBaseParam(baseParam, false); // 自动生成paramKey baseParam.setParamKey(generateParamKey()); baseParam.setCreateUser(SecurityUtils.getUsername()); baseParam.setCreateTime(LocalDateTime.now()); return baseMapper.insert(baseParam); } @Override public int updateBaseParam(BaseParam baseParam) { if (baseParam == null || baseParam.getId() == null) { throw new RuntimeException("修改参数ID不能为空"); } // 参数校验 checkBaseParam(baseParam, true); baseParam.setUpdateUser(SecurityUtils.getUsername()); baseParam.setUpdateTime(LocalDateTime.now()); return baseMapper.updateById(baseParam); } /** * 生成参数唯一key */ private String generateParamKey() { String prefix = "PARAM_"; // 查询当前最大key LambdaQueryWrapper 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) { String lastKey = last.getParamKey(); String numStr = lastKey.replace(prefix, ""); nextNum = Integer.parseInt(numStr) + 1; } return prefix + String.format("%04d", nextNum); } /** * 参数合法性校验 */ private void checkBaseParam(BaseParam baseParam, boolean isUpdate) { if (baseParam == null) { throw new RuntimeException("参数对象不能为空"); } if (StringUtils.isEmpty(baseParam.getParamName())) { throw new RuntimeException("参数名称不能为空"); } if (baseParam.getParamType() == null || !(baseParam.getParamType() == 1 || baseParam.getParamType() == 2)) { throw new RuntimeException("参数类型必须为1(数字)或2(文本)"); } if (baseParam.getValueMode() == null || !(baseParam.getValueMode() == 1 || baseParam.getValueMode() == 2)) { throw new RuntimeException("值模式必须为1(单值)或2(区间)"); } // 单值模式 if (baseParam.getValueMode() == 1) { if (StringUtils.isEmpty(baseParam.getDefaultValue())) { throw new RuntimeException("单值参数默认值不能为空"); } if (baseParam.getParamType() == 1) { try { new BigDecimal(baseParam.getDefaultValue()); } catch (Exception e) { throw new RuntimeException("默认值必须为数字"); } } baseParam.setDefaultMin(null); baseParam.setDefaultMax(null); } // 区间模式 if (baseParam.getValueMode() == 2) { if (baseParam.getParamType() != 1) { throw new RuntimeException("只有数字类型才能使用区间模式"); } if (baseParam.getDefaultMin() == null || baseParam.getDefaultMax() == null) { throw new RuntimeException("区间参数最小值和最大值不能为空"); } if (baseParam.getDefaultMin().compareTo(baseParam.getDefaultMax()) > 0) { throw new RuntimeException("最小值不能大于最大值"); } baseParam.setDefaultValue(null); } } @Override public int deleteBaseParamByIds(Long[] ids) { if (ids == null || ids.length == 0) { throw new RuntimeException("删除ID不能为空"); } return baseMapper.deleteBatchIds(Arrays.asList(ids)); } }