李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 *    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);
            }
        }
    }
}