李林
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 *    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.basic.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaztt.mes.basic.entity.DiscTool;
import com.chinaztt.mes.basic.mapper.DiscToolMapper;
import com.chinaztt.mes.basic.service.DiscToolService;
import com.chinaztt.mes.common.numgen.NumberGenerator;
import groovy.transform.Trait;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
/**
 * 盘具基础信息
 *
 * @author shz
 * @date 2023-03-02 16:16:05
 */
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
@Service
public class DiscToolServiceImpl extends ServiceImpl<DiscToolMapper, DiscTool> implements DiscToolService {
 
    private NumberGenerator<DiscTool> discToolNumberGenerator;
 
    @Override
    public boolean fullSave(DiscTool discTool) {
        if (discTool.getPartId() == null || StrUtil.isBlank(discTool.getDiscLength())) {
            throw new RuntimeException("盘具零件或长度不能为空");
        }
 
        if (StrUtil.isNotBlank(discTool.getMeasurement())) {
            this.checkMeasurementFormat(discTool.getMeasurement());
        }
 
        // 校验盘具长度格式
        this.checkLengthFormat(discTool);
 
        List<DiscTool> discTools = this.baseMapper.selectListByPartIdAndLengths(discTool.getPartId(), discTool.getMinLength(), discTool.getMaxLength(), null, null);
        if (CollectionUtil.isNotEmpty(discTools)) {
            throw new RuntimeException("该零件已存在相同尺寸的盘具");
        }
        discTool.setDiscNumber(discToolNumberGenerator.generateNumberWithPrefix(DiscTool.DIGIT, DiscTool.PREFIX, DiscTool::getDiscNumber));
        try {
            return this.save(discTool);
        } catch (DuplicateKeyException e) {
            throw new RuntimeException("编号已存在,请重新保存");
        }
    }
 
    @Override
    public boolean fullUpdate(DiscTool discTool) {
        if (discTool.getPartId() == null || StrUtil.isBlank(discTool.getDiscLength())) {
            throw new RuntimeException("盘具零件或长度不能为空");
        }
 
        // 校验盘具长度格式
        this.checkLengthFormat(discTool);
 
        List<DiscTool> discTools = this.baseMapper.selectListByPartIdAndLengths(discTool.getPartId(), discTool.getMinLength(), discTool.getMaxLength(), discTool.getId(), null);
        if (CollectionUtil.isNotEmpty(discTools)) {
            throw new RuntimeException("该零件已存在相同尺寸的盘具");
        }
 
        return this.updateById(discTool);
    }
 
    @Override
    public List<DiscTool> getListByPartAndLength(List<DiscTool> discToolList) {
        List<DiscTool> result = new ArrayList<>();
        for (DiscTool discTool : discToolList) {
            List<DiscTool> discTools = this.baseMapper.selectListByPartIdAndLengths(discTool.getPartId(), null, null, null, new BigDecimal(discTool.getDiscLength()));
            if (CollectionUtil.isNotEmpty(discTools)) {
                discTool.setMeasurement(discTools.get(0).getMeasurement());
                result.add(discTool);
            }
        }
        return result;
    }
 
    @Override
    public void checkMeasurementFormat(String measurement) {
        String[] split = measurement.split("\\*");
        if (split.length != 3 || !NumberUtils.isCreatable(split[0]) || !NumberUtils.isCreatable(split[1]) || !NumberUtils.isCreatable(split[2])) {
            throw new RuntimeException("盘具尺寸格式不正确");
        }
    }
 
    private void checkLengthFormat(DiscTool discTool) {
        if (!discTool.getDiscLength().contains("-")) {
            throw new RuntimeException("盘具长度格式不正确");
        } else {
            String[] split = discTool.getDiscLength().split("-");
            if (split.length != 2) {
                throw new RuntimeException("盘具长度格式不正确");
            }
            if (!NumberUtils.isCreatable(split[0]) || !NumberUtils.isCreatable(split[1]) || new BigDecimal(split[0]).compareTo(new BigDecimal(split[1])) >= 0) {
                throw new RuntimeException("盘具长度格式不正确");
            }
            discTool.setMinLength(new BigDecimal(split[0]));
            discTool.setMaxLength(new BigDecimal(split[1]));
        }
    }
}