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;
/**
*
* 煤质信息表,记录煤炭质量检测相关数据 服务实现类
*
*
* @author ruoyi
* @since 2025-06-09
*/
@Service
@RequiredArgsConstructor
public class CoalFieldServiceImpl extends ServiceImpl implements CoalFieldService {
private final CoalFieldMapper coalFieldMapper;
// 正则表达式:匹配 "fieldXX" 格式,XX 为两位数字
private static final Pattern FIELD_PATTERN = Pattern.compile("^field(\\d{2})$");
@Override
public IPage selectCoalFieldList(Page page, CoalFieldDto coalFieldDto) {
LambdaQueryWrapper 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 updateWrapper = new UpdateWrapper<>();
updateWrapper.in("id", ids)
.set("deleted", 1); // 设置 deleted 为 1 表示已删除
// 执行批量逻辑删除
return coalFieldMapper.update(null, updateWrapper);
}
@Override
public List selectAllList(CoalFieldDto coalFieldDto) {
return coalFieldMapper.selectList(null);
}
@Override
public List getFieldsByNames(Set fieldNames) {
// 1. 参数校验
if (fieldNames == null || fieldNames.isEmpty()) {
throw new IllegalArgumentException("字段名集合不能为空");
}
// 2. 过滤空值
Set filteredNames = fieldNames.stream()
.filter(name -> name != null && !name.trim().isEmpty())
.collect(Collectors.toSet());
if (filteredNames.isEmpty()) {
return Collections.emptyList();
}
// 3. 查询数据库
try {
List 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 getFieldNamesByNames(Set fieldNames) {
return coalFieldMapper.getFieldNamesByNames(fieldNames);
}
private String generateNextFieldNumber() {
// 获取所有已存在的 CoalField 记录,包括已删除的
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(CoalField::getDeleted, 0, 1);
List 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);
}
}