gongchunyi
7 小时以前 14999b3d001b6eff1220f7a6701c0796eee1089c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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));
    }
}