zouyu
2023-11-14 03ca4f4f48b3bd60cd07832dfe748f9f69167ac6
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 *    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<MoSamplingRuleMapper, MoSamplingRule> implements MoSamplingRuleService {
 
    private ReportSamplingRecordMapper reportSamplingRecordMapper;
    private SamplingRuleMapper samplingRuleMapper;
 
    @Override
    public Boolean batchSave(List<MoSamplingRuleDTO> moSamplingRuleList) {
        List<MoSamplingRule> moSamplingRules = new ArrayList<>();
        for (MoSamplingRuleDTO moSamplingRule : moSamplingRuleList) {
            SamplingRule samplingRule = samplingRuleMapper.selectById(moSamplingRule.getSamplingRuleId());
            int num = this.count(Wrappers.<MoSamplingRule>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.<ReportSamplingRecord>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.<ReportSamplingRecord>query().lambda()
                .eq(ReportSamplingRecord::getMoId, old.getMoId())
                .eq(ReportSamplingRecord::getMoSamplingRuleId, moSamplingRule.getId()));
        if (num > 0) {
            throw new RuntimeException("该制造订单已存在抽检记录,不能修改");
        }
 
        int count = this.count(Wrappers.<MoSamplingRule>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);
            }
        }
    }
}