liding
8 小时以前 94509204d25f7c0ad213ae2322be2bd5bfd17424
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
package com.ruoyi.basic.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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.basic.dto.CoalFieldDto;
import com.ruoyi.basic.entity.CoalField;
import com.ruoyi.basic.mapper.CoalFieldMapper;
import com.ruoyi.basic.service.CoalFieldService;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.bean.BeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 煤质信息表,记录煤炭质量检测相关数据 服务实现类
 * </p>
 *
 * @author ruoyi
 * @since 2025-06-09
 */
@Service
@RequiredArgsConstructor
public class CoalFieldServiceImpl extends ServiceImpl<CoalFieldMapper, CoalField> implements CoalFieldService {
 
    private final CoalFieldMapper coalFieldMapper;
 
    // 正则表达式:匹配 "fieldXX" 格式,XX 为两位数字
    private static final Pattern FIELD_PATTERN = Pattern.compile("^field(\\d{2})$");
 
    @Override
    public IPage<CoalField> selectCoalFieldList(Page page, CoalFieldDto coalFieldDto) {
        LambdaQueryWrapper<CoalField> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(coalFieldDto.getSearchAll())) {
            queryWrapper.like(CoalField::getFieldName, coalFieldDto.getSearchAll());
        }
        queryWrapper.orderByDesc(CoalField::getCreateTime);
        return coalFieldMapper.selectPage(page, queryWrapper);
    }
 
    @Override
    public int addOrEditCoalField(CoalFieldDto coalFieldDto) {
        CoalField coalField = new CoalField();
        BeanUtils.copyProperties(coalFieldDto, coalField);
        if (Objects.isNull(coalFieldDto.getId())) {
            // 新增操作:生成唯一的 fieldXX 编号
            String nextFieldNumber = generateNextFieldNumber();
            coalField.setFields(nextFieldNumber);
            return coalFieldMapper.insert(coalField);
        } else {
            // 更新操作:不修改 fieldNumber
            return coalFieldMapper.updateById(coalField);
        }
    }
 
    @Override
    public int delCoalFieldByIds(Long[] ids) {
        // 检查参数
        if (ids == null || ids.length == 0) {
            return 0;
        }
        // 构造更新条件
        UpdateWrapper<CoalField> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids)
                .set("deleted", 1);  // 设置 deleted 为 1 表示已删除
        // 执行批量逻辑删除
        return coalFieldMapper.update(null, updateWrapper);
    }
 
    @Override
    public List<CoalField> selectAllList(CoalFieldDto coalFieldDto) {
        return coalFieldMapper.selectList(null);
    }
 
    @Override
    public List<CoalFieldDto> getFieldsByNames(Set<String> fieldNames) {
        // 1. 参数校验
        if (fieldNames == null || fieldNames.isEmpty()) {
            throw new IllegalArgumentException("字段名集合不能为空");
        }
 
        // 2. 过滤空值
        Set<String> filteredNames = fieldNames.stream()
                .filter(name -> name != null && !name.trim().isEmpty())
                .collect(Collectors.toSet());
 
        if (filteredNames.isEmpty()) {
            return Collections.emptyList();
        }
 
        // 3. 查询数据库
        try {
            List<CoalField> entities = coalFieldMapper.getFieldsByNames(filteredNames);
 
            // 4. 实体转DTO
            return entities.stream()
                    .map(this::convertToDto)
                    .collect(Collectors.toList());
 
        } catch (Exception e) {
            throw new BaseException("查询煤质字段信息失败,请稍后重试");
        }
    }
 
    private CoalFieldDto convertToDto(CoalField entity) {
        CoalFieldDto dto = new CoalFieldDto();
        dto.setId(entity.getId());
        dto.setFields(entity.getFields());
        dto.setFieldName(entity.getFieldName());
        return dto;
    }
 
    @Override
    public Set<String> getFieldNamesByNames(Set<String> fieldNames) {
        return coalFieldMapper.getFieldNamesByNames(fieldNames);
    }
 
    private String generateNextFieldNumber() {
        // 获取所有已存在的 CoalField 记录,包括已删除的
        LambdaQueryWrapper<CoalField> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(CoalField::getDeleted, 0, 1);
        List<CoalField> existingFields = coalFieldMapper.selectList(queryWrapper);
 
        // 提取并解析所有已存在的 fieldXX 编号
        int maxNumber = 0;
        for (CoalField field : existingFields) {
            String fields = field.getFields();
            if (fields != null) {
                Matcher matcher = FIELD_PATTERN.matcher(fields);
                if (matcher.matches()) {
                    String numberPart = matcher.group(1);
                    int currentNumber = Integer.parseInt(numberPart);
                    maxNumber = Math.max(maxNumber, currentNumber);
                }
            }
        }
 
        // 生成下一个编号(格式为两位数字,不足补零)
        int nextNumber = maxNumber + 1;
        return String.format("field%02d", nextNumber);
    }
}