package com.ruoyi.basic.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; 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.ruoyi.basic.dto.*; import com.ruoyi.basic.excel.StructureTestObjectData; import com.ruoyi.basic.service.*; import com.ruoyi.common.exception.base.BaseException; import com.ruoyi.common.utils.QueryWrappers; import com.ruoyi.basic.mapper.*; import com.ruoyi.basic.pojo.*; import com.ruoyi.common.utils.StringUtils; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; /** * 检验项目参数(StructureItemParameter)表服务实现类 * * @author makejava * @since 2024-02-26 16:21:17 */ @Service @AllArgsConstructor public class CapacityScopeServiceImpl extends ServiceImpl implements CapacityScopeService { private StructureItemParameterMapper structureItemParameterMapper; private StructureTestObjectMapper structureTestObjectMapper; private ProductMapper productMapper; private ProductService productService; private StructureTestObjectService structureTestObjectService; private StructureTestObjectPartMapper structureTestObjectPartMapper; private ProductPartMapper productPartMapper; private StandardProductListService standardProductListService; private StandardTreeMapper standardTreeMapper; private StructureItemParameterService structureItemParameterService; private WorkShopMapper workShopMapper; private ModelMapper modelMapper; @Override public IPage selectItemParameterList(Page page, StructureItemParameter itemParameter) { return structureItemParameterMapper.selectItemParameterList(page, QueryWrappers.queryWrappers(itemParameter)); } @Override public int addItemParameter(StructureItemParameter itemParameter) { if (itemParameter.getBsm().equals("") || itemParameter.getBsm() == null) { itemParameter.setBsm("0"); } return structureItemParameterMapper.insert(itemParameter); } @Override public int delItemParameter(Integer id) { return structureItemParameterMapper.deleteById(id); } @Override public int upItemParameter(StructureItemParameter itemParameter) { return structureItemParameterMapper.updateById(itemParameter); } @Override public IPage selectTestObjectList(Page page, PageTestObjectDto pageTestObjectDto) { String specimenName = pageTestObjectDto.getSample(); pageTestObjectDto.setSample(null); return structureTestObjectMapper.selectTestObjectList(page, QueryWrappers.queryWrappers(pageTestObjectDto), specimenName); } @Override public int addTestObject(StructureTestObject testObject) { Long count = structureTestObjectMapper.selectCount(Wrappers.lambdaQuery().eq(StructureTestObject::getSpecimenName, testObject.getSpecimenName())); if (count.compareTo(0L) > 0) { throw new BaseException("检验对象不能重复"); } return structureTestObjectMapper.insert(testObject); } @Override public int delTestObject(Integer id) { // 产出检验对象产品维护 structureTestObjectPartMapper.delete(Wrappers.lambdaQuery() .eq(StructureTestObjectPart::getTestObjectId, id)); // 删除产品维护的零件绑定 List products = productMapper.selectList(Wrappers.lambdaQuery() .eq(Product::getObjectId, id)); List productIds = products.stream().map(Product::getId).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(productIds)) { productPartMapper.delete(Wrappers.lambdaQuery() .in(ProductPart::getProductId, productIds)); // 删除产品维护 productMapper.delete(Wrappers.lambdaQuery() .in(Product::getId, productIds)); } return structureTestObjectMapper.deleteById(id); } @Override public int upTestObject(StructureTestObject testObject) { // 查询旧的检验对象 StructureTestObject oldTestObject = structureTestObjectMapper.selectById(testObject.getId()); if (!oldTestObject.getSpecimenName().equals(testObject.getSpecimenName())) { // 查询所有对象一样的检验项目 List standardProductLists = standardProductListService.list(Wrappers.lambdaUpdate() .eq(StandardProductList::getSampleType, oldTestObject.getSpecimenName())); if (CollectionUtils.isNotEmpty(standardProductLists)) { for (StandardProductList standardProductList : standardProductLists) { // 修改所有的对象名称和数型结构 standardProductList.setSampleType(testObject.getSpecimenName()); // 需要截取第三级, 避免三四级名称一样修改错误 String[] trees = standardProductList.getTree().split(" - "); trees[2] = testObject.getSpecimenName(); List list = CollUtil.newArrayList(trees); String newName = CollUtil.join(list, " - "); standardProductList.setTree(newName); } standardProductListService.updateBatchById(standardProductLists); } // 修改检验项目参数的检验对象 // 拼接 ["object", 查询检验项目参数修改绑定的检验对象 String format = "[\"{}\","; String sampleOld = StrUtil.format(format, oldTestObject.getSpecimenName()); List itemParameterList = structureItemParameterService.list(Wrappers.lambdaQuery() .like(StructureItemParameter::getSample, sampleOld)); if (CollectionUtils.isNotEmpty(itemParameterList)) { for (StructureItemParameter structureItemParameter : itemParameterList) { // 修改绑定的样品名称 String sampleNew = StrUtil.format(format, testObject.getSpecimenName()); String sampleUp = structureItemParameter.getSample().replace(sampleOld, sampleNew); structureItemParameter.setSample(sampleUp); } structureItemParameterService.updateBatchById(itemParameterList); } // 修改树的型号 standardTreeMapper.update(null, Wrappers.lambdaUpdate() .eq(StandardTree::getSampleType, oldTestObject.getSpecimenName()) .set(StandardTree::getSampleType, testObject.getSpecimenName())); } Long count = structureTestObjectMapper.selectCount(Wrappers.lambdaQuery() .eq(StructureTestObject::getSpecimenName, testObject.getSpecimenName()) .ne(StructureTestObject::getId, testObject.getId())); if (count.compareTo(0L) > 0) { throw new BaseException("检验对象不能重复"); } return structureTestObjectMapper.updateById(testObject); } @Override public List selectTestObjectByName() { return structureTestObjectMapper.selectList(Wrappers.lambdaQuery().select(StructureTestObject::getSpecimenName, StructureTestObject::getId)); } //设备里面选择检验项目(树形结构) @Override public List> getInsProduction() { return structureItemParameterMapper.getInsProduction(); } @Override public List getItemTree() { return structureItemParameterMapper.getItemTree(); } @Override public List getItemTreeProduct(StructureTestObjectDto structureTestObjectDto) { return structureItemParameterMapper.getItemTreeProduct(structureTestObjectDto.getObjectType()); } @Override public List getProductTypes(ModelDto modelDto) { return modelMapper.selectList(Wrappers.lambdaQuery().eq(Model::getProductId, modelDto.getProductId())); } @Override public List getPartNoList(ProductPartDto productPartDto) { return productPartMapper.selectList(Wrappers.lambdaQuery().eq(ProductPart::getProductId, productPartDto.getProductId())); } @Transactional(rollbackFor = Exception.class) @Override public void importPartExcel(List list) { // 阶段2: 批量查询现有结构测试对象 Map existStructureTestObjects = structureTestObjectMapper.selectList( Wrappers.lambdaQuery() .in(StructureTestObject::getSpecimenName, list.stream().map(StructureTestObjectData::getSpecimenName).collect(Collectors.toSet())) .in(StructureTestObject::getSpecimenNameEn, list.stream().map(StructureTestObjectData::getSpecimenNameEn).collect(Collectors.toSet())) .in(StructureTestObject::getObjectType, list.stream().map(StructureTestObjectData::getObjectType).collect(Collectors.toSet())) ).stream().collect(Collectors.toMap( obj -> generateStructureKey(obj.getSpecimenName(), obj.getSpecimenNameEn(), obj.getObjectType()), // 统一键生成 Function.identity(), (existing, replacement) -> existing )); // 阶段3: 预处理车间信息 Set workShopNames = list.stream() .map(StructureTestObjectData::getWorkShopName) .filter(StringUtils::isNotEmpty) .collect(Collectors.toSet()); Map workShopCache = workShopNames.isEmpty() ? Collections.emptyMap() : workShopMapper.selectList(Wrappers.lambdaQuery() .in(WorkShop::getName, workShopNames)) .stream() .collect(Collectors.toMap(WorkShop::getName, Function.identity())); // 阶段4: 准备批量操作数据 List structureTestObjectsToInsert = new ArrayList<>(); List structureTestObjectsToUpdate = new ArrayList<>(); List productsToInsert = new ArrayList<>(); List productsToUpdate = new ArrayList<>(); // 阶段5: 主处理逻辑(仅处理结构测试对象) List pendingProducts = new ArrayList<>(); // 新增:暂存待处理的产品数据 for (StructureTestObjectData i : list) { // 处理结构测试对象(逻辑不变) String specimenKey = i.getSpecimenName() + "|" + i.getSpecimenNameEn() + "|" + i.getObjectType(); StructureTestObject structureTestObject = existStructureTestObjects.get(specimenKey); if (structureTestObject == null) { StructureTestObject newObj = createNewStructureTestObject(i, workShopCache); // 修改:传入车间缓存 structureTestObjectsToInsert.add(newObj); existStructureTestObjects.put(specimenKey, newObj); } else { updateExistingStructureTestObject(structureTestObject, i); structureTestObjectsToUpdate.add(structureTestObject); } // 暂存产品处理数据(新增) pendingProducts.add(i); } // 阶段6: 先处理结构测试对象批量操作 executeStructureTestObjectBatch(structureTestObjectsToInsert, structureTestObjectsToUpdate); // 阶段7: 更新缓存中的ID(关键新增步骤) refreshStructureTestObjectIds(existStructureTestObjects, structureTestObjectsToInsert); // 阶段8: 处理产品数据 processProducts(pendingProducts, existStructureTestObjects, workShopCache, productsToInsert, productsToUpdate); // 阶段9: 执行产品批量操作 executeProductBatch(productsToInsert, productsToUpdate); } // 更新现有结构测试对象 private void updateExistingStructureTestObject(StructureTestObject obj, StructureTestObjectData data) { obj.setCode(data.getCode()); obj.setLaboratoryId(9); } // 统一键生成方法 private String generateStructureKey(String name, String nameEn, String objectType) { return name + "|" + nameEn + "|" + objectType; } // 处理产品数据 private void processProductData(StructureTestObjectData data, Integer objectId, Map workShopCache, List insertList, List updateList) { // 构建产品唯一标识 String productKey = data.getName() + "|" + data.getNameEn() + "|" + data.getObjectType(); // 检查内存中是否已处理过该产品 Optional existingProduct = findProductInBatchLists(productKey, insertList, updateList); // 如果内存中不存在,查询数据库 if (!existingProduct.isPresent()) { Product dbProduct = productMapper.selectOne(Wrappers.lambdaQuery() .eq(Product::getName, data.getName()) .eq(Product::getNameEn, data.getNameEn()) .last("limit 1")); existingProduct = Optional.ofNullable(dbProduct); } // 创建或更新产品 Product product = existingProduct.orElseGet(Product::new); boolean isNew = product.getId() == null; // 设置产品属性 setupProduct(product, data, objectId, workShopCache); // 加入对应列表 if (isNew) { insertList.add(product); } else { updateList.add(product); } } // 在产品批量列表中查找 private Optional findProductInBatchLists(String productKey, List insertList, List updateList) { return Stream.concat(insertList.stream(), updateList.stream()) .filter(p -> (p.getName() + "|" + p.getNameEn()).equals(productKey)) .findFirst(); } // 设置产品信息 private void setupProduct(Product product, StructureTestObjectData data, Integer objectId, Map workShopCache) { product.setName(data.getName()); product.setNameEn(data.getNameEn()); product.setObjectId(objectId); // 处理车间信息 if (StringUtils.isNotEmpty(data.getWorkShopName())) { WorkShop workShop = workShopCache.get(data.getWorkShopName()); if (workShop == null) { throw new BaseException("车间信息未维护: " + data.getWorkShopName()); } product.setWorkShopId(workShop.getId()); product.setWorkShopName(data.getWorkShopName()); } } // 创建结构测试对象(使用缓存车间信息) private StructureTestObject createNewStructureTestObject(StructureTestObjectData data, Map workShopCache) { StructureTestObject obj = new StructureTestObject(); obj.setLaboratoryId(9); obj.setSpecimenName(data.getSpecimenName()); obj.setSpecimenNameEn(data.getSpecimenNameEn()); obj.setCode(data.getCode()); obj.setObjectType(data.getObjectType()); // 使用缓存获取车间信息 if (StringUtils.isNotEmpty(data.getWorkShopName())) { WorkShop workShop = workShopCache.get(data.getWorkShopName()); if (workShop == null) { throw new BaseException("车间信息未维护: " + data.getWorkShopName()); } obj.setWorkShopId(workShop.getId()); obj.setWorkShopName(data.getWorkShopName()); } else { obj.setWorkShopName(""); } return obj; } // 刷新结构测试对象ID(关键方法) private void refreshStructureTestObjectIds( Map existStructureTestObjects, List insertedObjects) { insertedObjects.forEach(obj -> { String key = obj.getSpecimenName() + "|" + obj.getSpecimenNameEn() + "|" + obj.getObjectType(); existStructureTestObjects.get(key).setId(obj.getId()); // 更新缓存中的ID }); } // 处理产品数据(独立方法) private void processProducts(List pendingProducts, Map structureTestObjectCache, Map workShopCache, List insertList, List updateList) { // 阶段5原有产品处理逻辑迁移至此 pendingProducts.forEach(i -> { String specimenKey = i.getSpecimenName() + "|" + i.getSpecimenNameEn() + "|" + i.getObjectType(); StructureTestObject sto = structureTestObjectCache.get(specimenKey); processProductData( i, sto.getId(), // 此时ID已正确设置 workShopCache, insertList, updateList ); }); } // 分拆批量操作方法 private void executeStructureTestObjectBatch(List toInsert, List toUpdate) { if (!toInsert.isEmpty()) { structureTestObjectMapper.insertBatchSomeColumn(toInsert); } if (!toUpdate.isEmpty()) { structureTestObjectService.updateBatchById(toUpdate); } } private void executeProductBatch(List toInsert, List toUpdate) { if (!toInsert.isEmpty()) { productMapper.insertBatchSomeColumn(toInsert); } if (!toUpdate.isEmpty()) { productService.updateBatchById(toUpdate); } } }