/* * 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.plan.service.impl; import cn.hutool.core.bean.BeanUtil; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.chinaztt.mes.plan.dto.MoSamplingRuleDTO; import com.chinaztt.mes.plan.entity.MoSamplingRule; import com.chinaztt.mes.plan.mapper.MoSamplingRuleMapper; import com.chinaztt.mes.plan.service.MoSamplingRuleService; import com.chinaztt.mes.quality.entity.ReportSamplingRecord; import com.chinaztt.mes.quality.entity.SamplingRule; import com.chinaztt.mes.quality.mapper.ReportSamplingRecordMapper; import com.chinaztt.mes.quality.mapper.SamplingRuleMapper; import com.chinaztt.mes.technology.entity.DocumentSamplingRule; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; /** * 制造订单抽检规则 * * @author shz * @date 2023-04-13 13:48:42 */ @Service @AllArgsConstructor @Transactional(rollbackFor = Exception.class) public class MoSamplingRuleServiceImpl extends ServiceImpl implements MoSamplingRuleService { private ReportSamplingRecordMapper reportSamplingRecordMapper; private SamplingRuleMapper samplingRuleMapper; @Override public Boolean batchSave(List moSamplingRuleList) { List moSamplingRules = new ArrayList<>(); for (MoSamplingRuleDTO moSamplingRule : moSamplingRuleList) { SamplingRule samplingRule = samplingRuleMapper.selectById(moSamplingRule.getSamplingRuleId()); int num = this.count(Wrappers.query().lambda() .eq(MoSamplingRule::getMoId, moSamplingRule.getMoId()) .eq(MoSamplingRule::getMoRoutingOperationId, moSamplingRule.getMoRoutingOperationId()) .eq(MoSamplingRule::getApplyType, samplingRule.getApplyType())); if (num == 0) { MoSamplingRule newRule = BeanUtil.copyProperties(samplingRule, MoSamplingRule.class); newRule.setMoId(moSamplingRule.getMoId()); newRule.setMoRoutingOperationId(moSamplingRule.getMoRoutingOperationId()); moSamplingRules.add(newRule); } checkSamplingPosition(moSamplingRule); } if (CollectionUtils.isNotEmpty(moSamplingRules)) { return this.saveBatch(moSamplingRules); } return false; } @Override public Boolean delById(Long id) { MoSamplingRule moSamplingRule = this.getById(id); Integer num = reportSamplingRecordMapper.selectCount(Wrappers.query().lambda() .eq(ReportSamplingRecord::getMoId, moSamplingRule.getMoId()) .eq(ReportSamplingRecord::getMoSamplingRuleId, id)); if (num > 0) { throw new RuntimeException("该制造订单已存在抽检记录,不能删除"); } return this.removeById(id); } @Override public Boolean fullUpdate(MoSamplingRule moSamplingRule) { if (moSamplingRule.getSamplingNumerator() > moSamplingRule.getSamplingDenominator()) { throw new RuntimeException("抽检分子不能大于分母"); } MoSamplingRule old = this.getById(moSamplingRule.getId()); Integer num = reportSamplingRecordMapper.selectCount(Wrappers.query().lambda() .eq(ReportSamplingRecord::getMoId, old.getMoId()) .eq(ReportSamplingRecord::getMoSamplingRuleId, moSamplingRule.getId())); if (num > 0) { throw new RuntimeException("该制造订单已存在抽检记录,不能修改"); } int count = this.count(Wrappers.lambdaQuery() .eq(MoSamplingRule::getMoId, moSamplingRule.getMoId()) .eq(MoSamplingRule::getMoRoutingOperationId, moSamplingRule.getMoRoutingOperationId()) .eq(MoSamplingRule::getApplyType, moSamplingRule.getApplyType()) .ne(MoSamplingRule::getId, moSamplingRule.getId())); if (count > 0) { throw new RuntimeException("已存在相同类型的抽检规则"); } checkSamplingPosition(moSamplingRule); return this.updateById(moSamplingRule); } private void checkSamplingPosition(MoSamplingRule moSamplingRule) { if (moSamplingRule.getSamplingPosition() == null) { return; } if (moSamplingRule.getSamplingNumerator() == 1) { moSamplingRule.setSamplingPosition("1"); } else { String[] split = moSamplingRule.getSamplingPosition().split(","); if (split.length != moSamplingRule.getSamplingNumerator()) { throw new RuntimeException("抽检位置个数与抽检分子不一致"); } int previousNum = 0; for (String samplingPosition : split) { if (!samplingPosition.matches("^[1-9]\\d*$") || Integer.parseInt(samplingPosition) > moSamplingRule.getSamplingDenominator() || Integer.parseInt(samplingPosition) <= previousNum) { throw new RuntimeException("抽检位置不合法"); } previousNum = Integer.parseInt(samplingPosition); } } } }