package com.chinaztt.mes.quality.service.impl; import cn.hutool.core.collection.CollectionUtil; import com.alibaba.druid.sql.visitor.functions.Trim; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; 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.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.toolkit.SqlHelper; import com.chinaztt.mes.basic.entity.Param; import com.chinaztt.mes.basic.service.ParamService; import com.chinaztt.mes.basic.util.DictUtils; import com.chinaztt.mes.common.numgen.NumberGenerator; import com.chinaztt.mes.quality.dto.TestStandardDTO; import com.chinaztt.mes.quality.entity.ReportSample; import com.chinaztt.mes.quality.entity.TestStandard; import com.chinaztt.mes.quality.entity.TestStandardParam; import com.chinaztt.mes.quality.excel.TestStandardData; import com.chinaztt.mes.quality.mapper.TestStandardMapper; import com.chinaztt.mes.quality.mapper.TestStandardParamMapper; import com.chinaztt.mes.quality.service.ReportSampleService; import com.chinaztt.mes.quality.service.TestStandardParamService; import com.chinaztt.mes.quality.service.TestStandardService; import com.chinaztt.ztt.admin.api.entity.SysDictItem; import com.chinaztt.ztt.common.core.util.R; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestBody; import java.util.*; import java.util.stream.Collectors; /** * 检测标准 * * @author cxf * @date 2021-04-22 09:03:06 */ @Service @AllArgsConstructor @Transactional(rollbackFor = Exception.class) public class TestStandardServiceImpl extends ServiceImpl implements TestStandardService { private NumberGenerator testStandardNumberGenerator; private TestStandardParamMapper testStandardParamMapper; private TestStandardParamService testStandardParamService; private ParamService paramService; private ReportSampleService reportSampleService; private TestStandardMapper testStandardMapper; private DictUtils dictUtils; @Override public boolean save(TestStandard testStandard) { checkStandardNo(testStandard); return SqlHelper.retBool(baseMapper.insert(testStandard)); } @Override public boolean updateById(TestStandard testStandard) { checkStandardNo(testStandard); return SqlHelper.retBool(baseMapper.updateById(testStandard)); } private void checkStandardNo(TestStandard testStandard) { if (StringUtils.isBlank(testStandard.getStandardNo())) { // 1. 自动生成编号 testStandard.setStandardNo(testStandardNumberGenerator.generateNumberWithPrefix(TestStandard.DIGIT, TestStandard.PREFIX, TestStandard::getStandardNo)); } else { // 2.判断是否重复 List repeatNos = baseMapper.selectList(Wrappers.query().lambda().eq(TestStandard::getStandardNo, testStandard.getStandardNo()).ne(TestStandard::getId, testStandard.getId())); if (CollectionUtil.isNotEmpty(repeatNos)) { throw new RuntimeException("编号重复"); } } } public TestStandard copyTestStandard(TestStandard testStandard){ TestStandard newTestStandard = new TestStandard(); if(testStandard!=null){ newTestStandard.setStandardNo(testStandardNumberGenerator.generateNumberWithPrefix(TestStandard.DIGIT, TestStandard.PREFIX, TestStandard::getStandardNo)); newTestStandard.setStandardName(testStandard.getStandardName()); newTestStandard.setActive(true); newTestStandard.setRemark(testStandard.getRemark()); newTestStandard.setState(testStandard.getState()); newTestStandard.setVersion(testStandard.getVersion()); baseMapper.insert(newTestStandard); //查询检测的参数 List testStandardParamList = testStandardParamMapper.selectList(Wrappers.lambdaQuery().eq(TestStandardParam::getTestStandardId,testStandard.getId())); for (TestStandardParam testStandardParam:testStandardParamList){ testStandardParam.setTestStandardId(newTestStandard.getId()); testStandardParamMapper.insert(testStandardParam); } } return newTestStandard; } @Override public R copyTestById(Long id) { TestStandard testStandard = baseMapper.selectById(id); if (testStandard!=null){ TestStandard newTestStandard = new TestStandard(); checkStandardNo(newTestStandard); newTestStandard.setStandardName(testStandard.getStandardName()); newTestStandard.setVersion(testStandard.getVersion()); newTestStandard.setState(testStandard.getState()); newTestStandard.setRemark(testStandard.getRemark()); newTestStandard.setActive(testStandard.getActive()); newTestStandard.setJudgeFormula(testStandard.getJudgeFormula()); newTestStandard.setInspectionType(testStandard.getInspectionType()); newTestStandard.setOperationId(testStandard.getOperationId()); baseMapper.insert(newTestStandard); List testStandardParamList = testStandardParamMapper.selectList(Wrappers.lambdaQuery().eq(TestStandardParam::getTestStandardId,testStandard.getId())); if (CollectionUtil.isNotEmpty(testStandardParamList)){ for (TestStandardParam testStandardParam:testStandardParamList){ TestStandardParam newTestStandardParam = new TestStandardParam(); newTestStandardParam.setTestStandardId(newTestStandard.getId()); newTestStandardParam.setActive(testStandardParam.getActive()); newTestStandardParam.setCode(testStandardParam.getCode()); newTestStandardParam.setIndex(testStandardParam.getIndex()); newTestStandardParam.setParameterItem(testStandardParam.getParameterItem()); newTestStandardParam.setReferenceValue(testStandardParam.getReferenceValue()); newTestStandardParam.setType(testStandardParam.getType()); newTestStandardParam.setUnit(testStandardParam.getUnit()); newTestStandardParam.setParamType(testStandardParam.getParamType()); newTestStandardParam.setJudgeFormula(testStandardParam.getJudgeFormula()); newTestStandardParam.setValueFormula(testStandardParam.getValueFormula()); newTestStandardParam.setIsReference(testStandardParam.getIsReference()); newTestStandardParam.setWireCore(testStandardParam.getWireCore()); testStandardParamMapper.insert(newTestStandardParam); } } } return R.ok(); } @Override public void importExcel(List cachedDataList) { // 根据标准编号分组 LinkedHashMap> groupMap = cachedDataList.stream().collect(Collectors.groupingBy(TestStandardData::getStandardNo, LinkedHashMap::new, Collectors.toList())); List paramList = new ArrayList<>(); // 循环新增 for (Map.Entry> dataList : groupMap.entrySet()) { TestStandard testStandard = baseMapper.selectOne(Wrappers.query().lambda().eq(TestStandard::getStandardNo, dataList.getKey())); // 如果检测标准不存在则为新增操作 if (testStandard == null) { testStandard = new TestStandard(); // 找出存在标准名称的主数据 Optional optional = dataList.getValue().stream().filter(e -> e.getStandardName() != null).findFirst(); if (!optional.isPresent()) { throw new RuntimeException("标准编号为" + dataList.getValue().get(0).getStandardNo() + "不能没有标准名称"); } //根据工序名称获取工序id Long operationId = baseMapper.getOperationIdByOperationName(optional.get().getOperationName()); if(operationId != null){ testStandard.setOperationId(operationId); } testStandard.setStandardNo(optional.get().getStandardNo()); testStandard.setStandardName(optional.get().getStandardName()); testStandard.setJudgeFormula(org.springframework.util.StringUtils.trimAllWhitespace(optional.get().getJudgeFormula())); if(false){ //用户在Excel中维护好直接导入,重复的原始code,系统无法替换 if(StringUtils.isNotBlank(testStandard.getJudgeFormula())){ if(testStandard.getJudgeFormula().contains("_")){ throw new RuntimeException("检测标准主表判断公式 = " + testStandard.getJudgeFormula() + " -> 包含字符 < _ > -> 被系统规则征用"); } } } List dict = dictUtils.getDict("apply_report_type"); Boolean door = false; if(CollectionUtils.isNotEmpty(dict)){ for(SysDictItem sysDictItem : dict){ if(sysDictItem.getLabel().equals(optional.get().getInspectionType())){ testStandard.setInspectionType(sysDictItem.getValue()); door = true; break; } } }else{ throw new RuntimeException("数据字典为空 -> 数据字典code = " + "【apply_report_type】"); } //testStandard.setInspectionType(optional.get().getInspectionType()); if(!door){ throw new RuntimeException("检测类型 = " + "【" + optional.get().getInspectionType() + "】在数据字典中不存在"); } baseMapper.insert(testStandard); } long index = 1; for (TestStandardData testStandardData : dataList.getValue()) { Param param = paramService.getOne(Wrappers.lambdaQuery().eq(Param::getCode, testStandardData.getCode())); if (param == null) { throw new RuntimeException("参数编号为"+ testStandardData.getCode() +"在参数列表中未找到"); } if(true){ if(param.getCode().contains("_")){ throw new RuntimeException("参数编号 = " + param.getCode() + " -> 包含字符 < _ > -> 被系统规则征用"); } if(testStandardData.getParameterItem().contains("_")){ throw new RuntimeException("检测项名称 = " + testStandardData.getParameterItem() + " -> 包含字符 < _ > -> 被系统规则征用"); } } TestStandardParam standardParam = new TestStandardParam(); //统一在检测标准批准时校正,保证导入时的效率 if(StringUtils.isNotBlank(testStandardData.getWireCore()) && testStandardData.getWireCore().trim().length() > 0){ if(testStandardData.getWireCore().contains("_")){ throw new RuntimeException("线芯 = " + testStandardData.getWireCore() + " -> 包含被系统规则征用的字符 = < _ >"); }else{ standardParam.setWireCore(testStandardData.getWireCore());//线芯 } } standardParam.setCode(param.getCode()); standardParam.setParameterItem(testStandardData.getParameterItem()); standardParam.setParameterFormat(param.getParameterFormat()); standardParam.setType(param.getType()); standardParam.setUnit(param.getUnit()); String referenceValue = StringUtils.isNotBlank(testStandardData.getReferenceValue1()) ? testStandardData.getReferenceValue1():""; if (StringUtils.isNotBlank(testStandardData.getReferenceValue2())) { referenceValue = referenceValue + "-" + testStandardData.getReferenceValue2(); } standardParam.setReferenceValue(referenceValue);//参考值 standardParam.setTestStandardId(testStandard.getId()); standardParam.setIndex(index); standardParam.setJudgeFormula(org.springframework.util.StringUtils.trimAllWhitespace(testStandardData.getParamJudgeFormula())); standardParam.setValueFormula(org.springframework.util.StringUtils.trimAllWhitespace(testStandardData.getValueFormula())); if(StringUtils.isNotBlank(testStandardData.getIsReference()) && testStandardData.getIsReference().equals("是")){ standardParam.setIsReference(true); }else{ standardParam.setIsReference(false); } if(StringUtils.isNotBlank(testStandardData.getIsCheck()) && testStandardData.getIsCheck().equals("是")){ standardParam.setIsCheck(true); }else{ standardParam.setIsCheck(false); } standardParam.setParamType(testStandardData.getParamType()); standardParam.setPosition(testStandardData.getPosition()); standardParam.setAisle(testStandardData.getAisle()); paramList.add(standardParam); index++; } } testStandardParamService.saveBatch(paramList, paramList.size()); } @Override public R removeByIds(List ids) { List deleteIds = new ArrayList<>(); for (Long id : ids) { int count = reportSampleService.count(Wrappers.lambdaQuery().eq(ReportSample::getIsMoTestStandard, false).eq(ReportSample::getTestStandardId, id)); if (count == 0) { deleteIds.add(id); } } if (deleteIds.size() == 0) { return R.failed("所选检测标准已被引用,无法删除"); } this.baseMapper.deleteBatchIds(deleteIds); testStandardParamMapper.deleteByStandardIds(deleteIds); return R.ok(); } @Override public TestStandard getByNo(String no) { TestStandard testStandard = testStandardMapper.selectOne(Wrappers.lambdaQuery().eq(TestStandard::getActive, true) .eq(TestStandard::getStandardNo, no)); if (testStandard == null) { throw new RuntimeException("根据检测标准编号,未查到基础检测标准,请确认"); } return testStandard; } @Override public IPage> pageInfo(Page page, QueryWrapper gen) { return baseMapper.pageInfo(page,gen); } @Override public TestStandardDTO getInfoById(Long id) { return baseMapper.getInfoById(id); } @Override public void approveTestStandard(TestStandard testStandard){ baseMapper.updateById(testStandard); //保存明细(校正导入的检测标准参数) List testStandardParamList = testStandardParamMapper.selectList(Wrappers.lambdaQuery().eq(TestStandardParam::getTestStandardId,testStandard.getId())); testStandardParamService.saveList(testStandardParamList); } }