李林
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
package com.chinaztt.mes.plan.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaztt.mes.plan.entity.MoTestStandardParam;
import com.chinaztt.mes.plan.mapper.MoTestStandardParamMapper;
import com.chinaztt.mes.plan.service.MoTestStandardParamService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
import java.util.regex.Pattern;
 
/**
 * 制造订单-检测标准参数
 *
 * @author cxf
 * @date 2021-04-27 13:27:40
 */
@Service
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
public class MoTestStandardParamServiceImpl extends ServiceImpl<MoTestStandardParamMapper, MoTestStandardParam> implements MoTestStandardParamService {
 
    /**
     * 保留小数(匹配#.00)
     */
    private static final String MATH_FORMAT_PATTEN_STR = "^#.\\d0{0,9}";
    private static final Pattern PATTEN_MIN_MATH_FORMAT = Pattern.compile(MATH_FORMAT_PATTEN_STR);
 
    @Override
    public boolean saveList(List<MoTestStandardParam> moTestStandardParamList) {
        if (CollectionUtil.isNotEmpty(moTestStandardParamList)) {
            // 先删除
            baseMapper.delete(Wrappers.<MoTestStandardParam>lambdaQuery().eq(MoTestStandardParam::getMoTestStandardId, moTestStandardParamList.get(0).getMoTestStandardId()));
            // 后更新/新增
            moTestStandardParamList.stream().forEach(moTestStandardParam -> {
                if (moTestStandardParam.getId() != null && moTestStandardParam.getId() != 0) {
                    if (StringUtils.isNotBlank(moTestStandardParam.getParameterFormat())) {
                        boolean matches = moTestStandardParam.getParameterFormat().matches(PATTEN_MIN_MATH_FORMAT.pattern());
                        if (!matches) {
                            throw new RuntimeException("参数格式错误");
                        }
                    }
                    baseMapper.activeById(moTestStandardParam.getId());
                    baseMapper.updateById(moTestStandardParam);
                } else {
                    if (StringUtils.isNotBlank(moTestStandardParam.getParameterFormat())) {
                        boolean matches = moTestStandardParam.getParameterFormat().matches(PATTEN_MIN_MATH_FORMAT.pattern());
                        if (!matches) {
                            throw new RuntimeException("参数格式错误");
                        }
                    }
                    baseMapper.insert(moTestStandardParam);
                }
            });
        }
        return true;
    }
}