李林
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
package com.chinaztt.mes.quality.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.chinaztt.mes.common.numgen.NumberGenerator;
import com.chinaztt.mes.quality.entity.TestRule;
import com.chinaztt.mes.quality.mapper.TestRuleMapper;
import com.chinaztt.mes.quality.service.TestRuleService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
/**
 * 检测规则
 *
 * @author cxf
 * @date 2021-04-21 09:09:39
 */
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class TestRuleServiceImpl extends ServiceImpl<TestRuleMapper, TestRule> implements TestRuleService {
    private NumberGenerator<TestRule> testRuleNumberGenerator;
 
    @Override
    public boolean save(TestRule testRule) {
        checkRuleNo(testRule);
        return SqlHelper.retBool(baseMapper.insert(testRule));
    }
 
    @Override
    public boolean updateById(TestRule testRule) {
        checkRuleNo(testRule);
        return SqlHelper.retBool(baseMapper.updateById(testRule));
    }
 
    private void checkRuleNo(TestRule testRule) {
        if (StringUtils.isBlank(testRule.getRuleNo())) {
            // 1. 自动生成编号
            testRule.setRuleNo(testRuleNumberGenerator.generateNumberWithPrefix(TestRule.DIGIT, TestRule.PREFIX, TestRule::getRuleNo));
        } else {
            // 2.判断是否重复
            List<TestRule> repeatNos = baseMapper.selectList(Wrappers.<TestRule>query().lambda().eq(TestRule::getRuleNo, testRule.getRuleNo()).ne(TestRule::getId, testRule.getId()));
            if (CollectionUtil.isNotEmpty(repeatNos)) {
                throw new RuntimeException("编号重复");
            }
        }
    }
}