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 implements TestRuleService { private NumberGenerator 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 repeatNos = baseMapper.selectList(Wrappers.query().lambda().eq(TestRule::getRuleNo, testRule.getRuleNo()).ne(TestRule::getId, testRule.getId())); if (CollectionUtil.isNotEmpty(repeatNos)) { throw new RuntimeException("编号重复"); } } } }