李林
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
/*
 *    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.technology.service.impl;
 
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaztt.mes.technology.entity.DocumentSamplingRule;
import com.chinaztt.mes.technology.mapper.DocumentSamplingRuleMapper;
import com.chinaztt.mes.technology.service.DocumentSamplingRuleService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
/**
 * 工艺文件抽检规则
 *
 * @author shz
 * @date 2023-04-13 13:49:26
 */
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class DocumentSamplingRuleServiceImpl extends ServiceImpl<DocumentSamplingRuleMapper, DocumentSamplingRule> implements DocumentSamplingRuleService {
 
    @Override
    public Boolean fullUpdate(DocumentSamplingRule documentSamplingRule) {
        if (documentSamplingRule.getSamplingNumerator() > documentSamplingRule.getSamplingDenominator()) {
            throw new RuntimeException("抽检分子不能大于分母");
        }
 
        checkSamplingPosition(documentSamplingRule);
 
        int count = this.count(Wrappers.<DocumentSamplingRule>lambdaQuery()
                .eq(DocumentSamplingRule::getDocBomId, documentSamplingRule.getDocBomId())
                .eq(DocumentSamplingRule::getRoutingOperationId, documentSamplingRule.getRoutingOperationId())
                .eq(DocumentSamplingRule::getSamplingOperationId, documentSamplingRule.getSamplingOperationId())
                .eq(DocumentSamplingRule::getApplyType, documentSamplingRule.getApplyType())
                .ne(DocumentSamplingRule::getId, documentSamplingRule.getId()));
        if (count > 0) {
            throw new RuntimeException("已存在相同类型抽检规则");
        }
        return this.updateById(documentSamplingRule);
    }
 
    private void checkSamplingPosition(DocumentSamplingRule documentSamplingRule) {
        if (documentSamplingRule.getSamplingPosition() == null) {
            return;
        }
        if (documentSamplingRule.getSamplingNumerator() == 1) {
            documentSamplingRule.setSamplingPosition("1");
        } else {
            String[] split = documentSamplingRule.getSamplingPosition().split(",");
            if (split.length != documentSamplingRule.getSamplingNumerator()) {
                throw new RuntimeException("抽检位置个数与抽检分子不一致");
            }
            int previousNum = 0;
            for (String samplingPosition : split) {
                if (!samplingPosition.matches("^[1-9]\\d*$") || Integer.parseInt(samplingPosition) > documentSamplingRule.getSamplingDenominator() || Integer.parseInt(samplingPosition) <= previousNum) {
                    throw new RuntimeException("抽检位置不合法");
                }
                previousNum = Integer.parseInt(samplingPosition);
            }
        }
    }
}