/* * Copyright (c) 2018-2025, ztt All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the pig4cloud.com developer nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * Author: ztt */ package com.chinaztt.mes.basic.service.impl; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.chinaztt.mes.basic.entity.DiscTool; import com.chinaztt.mes.basic.mapper.DiscToolMapper; import com.chinaztt.mes.basic.service.DiscToolService; import com.chinaztt.mes.common.numgen.NumberGenerator; import groovy.transform.Trait; import lombok.AllArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import org.springframework.dao.DuplicateKeyException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * 盘具基础信息 * * @author shz * @date 2023-03-02 16:16:05 */ @Transactional(rollbackFor = Exception.class) @AllArgsConstructor @Service public class DiscToolServiceImpl extends ServiceImpl implements DiscToolService { private NumberGenerator discToolNumberGenerator; @Override public boolean fullSave(DiscTool discTool) { if (discTool.getPartId() == null || StrUtil.isBlank(discTool.getDiscLength())) { throw new RuntimeException("盘具零件或长度不能为空"); } if (StrUtil.isNotBlank(discTool.getMeasurement())) { this.checkMeasurementFormat(discTool.getMeasurement()); } // 校验盘具长度格式 this.checkLengthFormat(discTool); List discTools = this.baseMapper.selectListByPartIdAndLengths(discTool.getPartId(), discTool.getMinLength(), discTool.getMaxLength(), null, null); if (CollectionUtil.isNotEmpty(discTools)) { throw new RuntimeException("该零件已存在相同尺寸的盘具"); } discTool.setDiscNumber(discToolNumberGenerator.generateNumberWithPrefix(DiscTool.DIGIT, DiscTool.PREFIX, DiscTool::getDiscNumber)); try { return this.save(discTool); } catch (DuplicateKeyException e) { throw new RuntimeException("编号已存在,请重新保存"); } } @Override public boolean fullUpdate(DiscTool discTool) { if (discTool.getPartId() == null || StrUtil.isBlank(discTool.getDiscLength())) { throw new RuntimeException("盘具零件或长度不能为空"); } // 校验盘具长度格式 this.checkLengthFormat(discTool); List discTools = this.baseMapper.selectListByPartIdAndLengths(discTool.getPartId(), discTool.getMinLength(), discTool.getMaxLength(), discTool.getId(), null); if (CollectionUtil.isNotEmpty(discTools)) { throw new RuntimeException("该零件已存在相同尺寸的盘具"); } return this.updateById(discTool); } @Override public List getListByPartAndLength(List discToolList) { List result = new ArrayList<>(); for (DiscTool discTool : discToolList) { List discTools = this.baseMapper.selectListByPartIdAndLengths(discTool.getPartId(), null, null, null, new BigDecimal(discTool.getDiscLength())); if (CollectionUtil.isNotEmpty(discTools)) { discTool.setMeasurement(discTools.get(0).getMeasurement()); result.add(discTool); } } return result; } @Override public void checkMeasurementFormat(String measurement) { String[] split = measurement.split("\\*"); if (split.length != 3 || !NumberUtils.isCreatable(split[0]) || !NumberUtils.isCreatable(split[1]) || !NumberUtils.isCreatable(split[2])) { throw new RuntimeException("盘具尺寸格式不正确"); } } private void checkLengthFormat(DiscTool discTool) { if (!discTool.getDiscLength().contains("-")) { throw new RuntimeException("盘具长度格式不正确"); } else { String[] split = discTool.getDiscLength().split("-"); if (split.length != 2) { throw new RuntimeException("盘具长度格式不正确"); } if (!NumberUtils.isCreatable(split[0]) || !NumberUtils.isCreatable(split[1]) || new BigDecimal(split[0]).compareTo(new BigDecimal(split[1])) >= 0) { throw new RuntimeException("盘具长度格式不正确"); } discTool.setMinLength(new BigDecimal(split[0])); discTool.setMaxLength(new BigDecimal(split[1])); } } }