liyong
4 小时以前 1ca5584d7e3200a9af65a099bd26d3593e2ba702
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
167
168
169
170
package com.ruoyi.technology.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.technology.bean.dto.TechnologyParamDto;
import com.ruoyi.technology.bean.vo.TechnologyParamVo;
import com.ruoyi.technology.mapper.TechnologyParamMapper;
import com.ruoyi.technology.pojo.TechnologyParam;
import com.ruoyi.technology.service.TechnologyParamService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
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;
 
@Service
@RequiredArgsConstructor
public class TechnologyParamServiceImpl extends ServiceImpl<TechnologyParamMapper, TechnologyParam> implements TechnologyParamService {
 
    private static final List<Integer> VALID_PARAM_TYPES = Arrays.asList(1, 2, 3, 4);
    private static final String PARAM_CODE_PREFIX = "PARAM_";
    private static final Byte DATE_PARAM_TYPE = (byte) 4;
 
    /**
     * 分页查询基础参数并格式化日期类型展示。
     */
    @Override
    public IPage<TechnologyParamVo> baseParamList(Page<TechnologyParamDto> page, TechnologyParamDto baseParam) {
        LambdaQueryWrapper<TechnologyParam> queryWrapper = new LambdaQueryWrapper<>();
        if (baseParam != null && StringUtils.isNotEmpty(baseParam.getParamName())) {
            queryWrapper.like(TechnologyParam::getParamName, baseParam.getParamName());
        }
        queryWrapper.orderByDesc(TechnologyParam::getId);
 
        Page<TechnologyParam> entityPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        Page<TechnologyParam> paramPage = page(entityPage, queryWrapper);
 
        Page<TechnologyParamVo> resultPage = new Page<>(paramPage.getCurrent(), paramPage.getSize(), paramPage.getTotal());
        List<TechnologyParamVo> records = new ArrayList<>(paramPage.getRecords().size());
        for (TechnologyParam item : paramPage.getRecords()) {
            TechnologyParamVo vo = new TechnologyParamVo();
            BeanUtils.copyProperties(item, vo);
            if (DATE_PARAM_TYPE.equals(item.getParamType()) && StringUtils.isNotEmpty(item.getParamFormat())) {
                vo.setParamFormat(toDisplayDatePattern(item.getParamFormat()));
            }
            records.add(vo);
        }
        resultPage.setRecords(records);
        return resultPage;
    }
 
    /**
     * 新增基础参数并生成唯一参数编码。
     */
    @Override
    public int addBaseParam(TechnologyParamDto baseParam) {
        if (baseParam == null) {
            throw new RuntimeException("新增参数不能为空");
        }
        checkBaseParam(baseParam);
        baseParam.setParamCode(generateParamCode());
        baseParam.setCreateUser(SecurityUtils.getUsername());
        baseParam.setCreateTime(LocalDateTime.now());
        if (baseParam.getIsRequired() == null) {
            baseParam.setIsRequired((byte) 0);
        }
        return baseMapper.insert(baseParam);
    }
 
    /**
     * 修改基础参数并保留原有参数编码。
     */
    @Override
    public int updateBaseParam(TechnologyParamDto baseParam) {
        if (baseParam == null || baseParam.getId() == null) {
            throw new RuntimeException("修改参数ID不能为空");
        }
        checkBaseParam(baseParam);
        TechnologyParam current = baseMapper.selectById(baseParam.getId());
        if (current == null) {
            throw new RuntimeException("参数不存在");
        }
        if (StringUtils.isEmpty(baseParam.getParamCode())) {
            baseParam.setParamCode(current.getParamCode());
        }
        baseParam.setUpdateUser(SecurityUtils.getUsername());
        baseParam.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(baseParam);
    }
 
    /**
     * 参数定义合法校验。
     */
    private void checkBaseParam(TechnologyParamDto baseParam) {
        if (StringUtils.isEmpty(baseParam.getParamName())) {
            throw new RuntimeException("参数名称不能为空");
        }
        if (baseParam.getParamType() == null || !VALID_PARAM_TYPES.contains(Integer.valueOf(baseParam.getParamType()))) {
            throw new RuntimeException("非法参数类型");
        }
 
        if (DATE_PARAM_TYPE.equals(baseParam.getParamType())) {
            if (StringUtils.isEmpty(baseParam.getParamFormat())) {
                throw new RuntimeException("日期类型必须配置参数格式(如: yyyy-MM-dd)");
            }
            try {
                String standardPattern = normalizeDatePattern(baseParam.getParamFormat());
                DateTimeFormatter.ofPattern(standardPattern);
                baseParam.setParamFormat(standardPattern);
            } catch (Exception e) {
                throw new RuntimeException("日期格式非法: " + baseParam.getParamFormat());
            }
        } else {
            baseParam.setParamFormat(baseParam.getParamFormat());
        }
    }
 
    /**
     * 生成参数唯一编码,供工艺参数和生产订单快照引用。
     */
    private String generateParamCode() {
        LambdaQueryWrapper<TechnologyParam> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(TechnologyParam::getParamCode)
                .likeRight(TechnologyParam::getParamCode, PARAM_CODE_PREFIX)
                .orderByDesc(TechnologyParam::getParamCode)
                .last("limit 1");
 
        TechnologyParam last = baseMapper.selectOne(wrapper);
        int nextNum = 1;
        if (last != null && StringUtils.isNotEmpty(last.getParamCode())) {
            try {
                String numStr = last.getParamCode().replace(PARAM_CODE_PREFIX, "");
                nextNum = Integer.parseInt(numStr) + 1;
            } catch (Exception e) {
                log.error("解析 paramCode 异常", e);
            }
        }
        return PARAM_CODE_PREFIX + String.format("%04d", nextNum);
    }
 
    private String normalizeDatePattern(String pattern) {
        return pattern.trim()
                .replace('Y', 'y')
                .replace('D', 'd')
                .replace('S', 's');
    }
 
    private String toDisplayDatePattern(String pattern) {
        return normalizeDatePattern(pattern);
    }
 
    /**
     * 批量删除基础参数。
     */
    @Override
    public int deleteBaseParamByIds(Long[] ids) {
        if (ids == null || ids.length == 0) {
            throw new RuntimeException("删除ID不能为空");
        }
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }
}