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));
|
}
|
}
|