| | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | |
| | | 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, false); |
| | | 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); |
| | | } |
| | |
| | | throw new RuntimeException("修改参数ID不能为空"); |
| | | } |
| | | // 参数校验 |
| | | checkBaseParam(baseParam, true); |
| | | checkBaseParam(baseParam); |
| | | baseParam.setUpdateUser(SecurityUtils.getUsername()); |
| | | baseParam.setUpdateTime(LocalDateTime.now()); |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 生成参数唯一key |
| | | * 参数定义合法校验 |
| | | */ |
| | | 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_"; |
| | | // 查询当前最大key |
| | | 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) { |
| | | String lastKey = last.getParamKey(); |
| | | String numStr = lastKey.replace(prefix, ""); |
| | | 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 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(区间)"); |
| | | private String normalizePattern(String pattern) { |
| | | if (StringUtils.isEmpty(pattern)) return "yyyy-MM-dd"; |
| | | return pattern.replace("YYYY", "yyyy") |
| | | .replace("DD", "dd") |
| | | .replace("SS", "ss"); |
| | | } |
| | | |
| | | // 单值模式 |
| | | 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); |
| | | } |
| | | /** |
| | | * 转换为全大写显示 |
| | | */ |
| | | private String toUpperCasePattern(String pattern) { |
| | | if (StringUtils.isEmpty(pattern)) return ""; |
| | | return pattern.replace("yyyy", "YYYY") |
| | | .replace("dd", "DD") |
| | | .replace("ss", "SS"); |
| | | } |
| | | |
| | | @Override |
| | |
| | | if (ids == null || ids.length == 0) { |
| | | throw new RuntimeException("删除ID不能为空"); |
| | | } |
| | | |
| | | return baseMapper.deleteBatchIds(Arrays.asList(ids)); |
| | | } |
| | | } |