/*
|
* 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.quality.service.impl;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.chinaztt.mes.common.numgen.NumberGenerator;
|
import com.chinaztt.mes.quality.entity.SamplingRule;
|
import com.chinaztt.mes.quality.mapper.SamplingRuleMapper;
|
import com.chinaztt.mes.quality.service.SamplingRuleService;
|
import lombok.AllArgsConstructor;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
/**
|
* 抽检规则
|
*
|
* @author shz
|
* @date 2023-04-13 09:48:37
|
*/
|
@Service
|
@AllArgsConstructor
|
@Transactional(rollbackFor = Exception.class)
|
public class SamplingRuleServiceImpl extends ServiceImpl<SamplingRuleMapper, SamplingRule> implements SamplingRuleService {
|
|
private NumberGenerator<SamplingRule> samplingRuleNumberGenerator;
|
|
@Override
|
public Boolean fullSave(SamplingRule samplingRule) {
|
// 唯一性校验
|
uniquenessCheck(samplingRule);
|
|
// 生成编码
|
samplingRule.setRuleNo(samplingRuleNumberGenerator.generateNumberWithPrefix(SamplingRule.DIGIT, SamplingRule.PREFIX, SamplingRule::getRuleNo));
|
|
checkSamplingPosition(samplingRule);
|
|
return this.save(samplingRule);
|
}
|
|
@Override
|
public Boolean fullUpdate(SamplingRule samplingRule) {
|
// 唯一性校验
|
uniquenessCheck(samplingRule);
|
|
checkSamplingPosition(samplingRule);
|
|
return this.updateById(samplingRule);
|
}
|
|
// 唯一性校验
|
private void uniquenessCheck(SamplingRule samplingRule) {
|
// 1.校验名称唯一性
|
int num;
|
if (samplingRule.getId() != null) {
|
num = this.count(Wrappers.<SamplingRule>lambdaQuery()
|
.eq(SamplingRule::getSamplingOperationId, samplingRule.getSamplingOperationId())
|
.eq(SamplingRule::getApplyType, samplingRule.getApplyType())
|
.ne(SamplingRule::getId, samplingRule.getId()));
|
} else {
|
num = this.count(Wrappers.<SamplingRule>lambdaQuery()
|
.eq(SamplingRule::getSamplingOperationId, samplingRule.getSamplingOperationId())
|
.eq(SamplingRule::getApplyType, samplingRule.getApplyType()));
|
}
|
|
if (num > 0) {
|
throw new RuntimeException("该规则已存在");
|
}
|
|
if (samplingRule.getSamplingNumerator() > samplingRule.getSamplingDenominator()) {
|
throw new RuntimeException("抽检数量不能大于抽检样品总数");
|
}
|
}
|
|
private void checkSamplingPosition(SamplingRule samplingRule) {
|
if (samplingRule.getSamplingPosition() == null) {
|
return;
|
}
|
if (samplingRule.getSamplingNumerator() == 1) {
|
samplingRule.setSamplingPosition("1");
|
} else {
|
String[] split = samplingRule.getSamplingPosition().split(",");
|
if (split.length != samplingRule.getSamplingNumerator()) {
|
throw new RuntimeException("抽检位置个数与抽检分子不一致");
|
}
|
|
int previousNum = 0;
|
for (String samplingPosition : split) {
|
if (!samplingPosition.matches("^[1-9]\\d*$") || Integer.parseInt(samplingPosition) > samplingRule.getSamplingDenominator() || Integer.parseInt(samplingPosition) <= previousNum) {
|
throw new RuntimeException("抽检位置不合法");
|
}
|
previousNum = Integer.parseInt(samplingPosition);
|
}
|
}
|
}
|
}
|