zss
16 小时以前 b4b1e13d6cb732c32df34e6ef978c77a4f3623b9
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
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;
 
/**
 * <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);
        }
        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<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, "");
            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));
    }
}