| | |
| | | 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.dto.PageTestObjectDto; |
| | | import com.ruoyi.basic.dto.TestItemDto; |
| | | import com.ruoyi.basic.mapper.*; |
| | | import com.ruoyi.basic.pojo.*; |
| | | import com.ruoyi.basic.service.CapacityScopeService; |
| | | import com.ruoyi.basic.service.StandardProductListService; |
| | | import com.ruoyi.basic.service.StructureItemParameterService; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import lombok.AllArgsConstructor; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.Stream; |
| | | |
| | | /** |
| | | * 检验项目参数(StructureItemParameter)表服务实现类 |
| | |
| | | @AllArgsConstructor |
| | | public class CapacityScopeServiceImpl extends ServiceImpl<StructureItemParameterMapper, StructureItemParameter> implements CapacityScopeService { |
| | | |
| | | private LaboratoryMapper laboratoryMapper; |
| | | |
| | | private StructureItemParameterMapper structureItemParameterMapper; |
| | | |
| | | private StructureTestObjectMapper structureTestObjectMapper; |
| | | |
| | | private ProductMapper productMapper; |
| | | |
| | | private ProductService productService; |
| | | |
| | | private StructureTestObjectService structureTestObjectService; |
| | | |
| | | private StructureTestObjectPartMapper structureTestObjectPartMapper; |
| | | |
| | |
| | | |
| | | private StructureItemParameterService structureItemParameterService; |
| | | |
| | | private WorkShopMapper workShopMapper; |
| | | |
| | | private ModelMapper modelMapper; |
| | | |
| | | @Override |
| | | public IPage<StructureItemParameter> selectItemParameterList(Page page, StructureItemParameter itemParameter) { |
| | | return structureItemParameterMapper.selectItemParameterList(page, QueryWrappers.queryWrappers(itemParameter)); |
| | | return structureItemParameterMapper.selectItemParameterList(page, QueryWrappers.queryWrappers(itemParameter)); |
| | | } |
| | | |
| | | @Override |
| | | public int addItemParameter(StructureItemParameter itemParameter) { |
| | | if (itemParameter.getBsm().equals("")||itemParameter.getBsm()==null){ |
| | | if (itemParameter.getBsm().equals("") || itemParameter.getBsm() == null) { |
| | | itemParameter.setBsm("0"); |
| | | } |
| | | int insert = structureItemParameterMapper.insert(itemParameter); |
| | | return insert; |
| | | return structureItemParameterMapper.insert(itemParameter); |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | @Override |
| | | public IPage<PageTestObjectDto> selectTestObjectList(Page page, PageTestObjectDto pageTestObjectDto) { |
| | | String partNo = pageTestObjectDto.getPartNo(); |
| | | pageTestObjectDto.setPartNo(null); |
| | | return structureTestObjectMapper.selectTestObjectList(page, QueryWrappers.queryWrappers(pageTestObjectDto),partNo); |
| | | 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.<StructureTestObject>lambdaQuery().eq(StructureTestObject::getSpecimenName, testObject.getSpecimenName())); |
| | | if(count.compareTo(0L) > 0){ |
| | | if (count.compareTo(0L) > 0) { |
| | | throw new BaseException("检验对象不能重复"); |
| | | } |
| | | |
| | |
| | | List<Product> products = productMapper.selectList(Wrappers.<Product>lambdaQuery() |
| | | .eq(Product::getObjectId, id)); |
| | | List<Integer> productIds = products.stream().map(Product::getId).collect(Collectors.toList()); |
| | | productPartMapper.delete(Wrappers.<ProductPart>lambdaQuery() |
| | | .in(ProductPart::getProductId, productIds)); |
| | | if (CollectionUtils.isNotEmpty(productIds)) { |
| | | productPartMapper.delete(Wrappers.<ProductPart>lambdaQuery() |
| | | .in(ProductPart::getProductId, productIds)); |
| | | |
| | | // 删除产品维护 |
| | | productMapper.delete(Wrappers.<Product>lambdaQuery() |
| | | .in(Product::getId, productIds)); |
| | | // 删除产品维护 |
| | | productMapper.delete(Wrappers.<Product>lambdaQuery() |
| | | .in(Product::getId, productIds)); |
| | | } |
| | | |
| | | return structureTestObjectMapper.deleteById(id); |
| | | } |
| | |
| | | // 查询所有对象一样的检验项目 |
| | | List<StandardProductList> standardProductLists = standardProductListService.list(Wrappers.<StandardProductList>lambdaUpdate() |
| | | .eq(StandardProductList::getSampleType, oldTestObject.getSpecimenName())); |
| | | if (CollectionUtils.isNotEmpty(standardProductLists)){ |
| | | if (CollectionUtils.isNotEmpty(standardProductLists)) { |
| | | for (StandardProductList standardProductList : standardProductLists) { |
| | | // 修改所有的对象名称和数型结构 |
| | | standardProductList.setSampleType(testObject.getSpecimenName()); |
| | |
| | | Long count = structureTestObjectMapper.selectCount(Wrappers.<StructureTestObject>lambdaQuery() |
| | | .eq(StructureTestObject::getSpecimenName, testObject.getSpecimenName()) |
| | | .ne(StructureTestObject::getId, testObject.getId())); |
| | | if(count.compareTo(0L) > 0){ |
| | | if (count.compareTo(0L) > 0) { |
| | | throw new BaseException("检验对象不能重复"); |
| | | } |
| | | |
| | |
| | | |
| | | @Override |
| | | public List<StructureTestObject> selectTestObjectByName() { |
| | | return structureTestObjectMapper.selectList(Wrappers.<StructureTestObject>lambdaQuery().select(StructureTestObject::getSpecimenName,StructureTestObject::getId)); |
| | | return structureTestObjectMapper.selectList(Wrappers.<StructureTestObject>lambdaQuery().select(StructureTestObject::getSpecimenName, StructureTestObject::getId)); |
| | | } |
| | | |
| | | //设备里面选择检验项目(树形结构) |
| | |
| | | public List<TestItemDto> getItemTree() { |
| | | return structureItemParameterMapper.getItemTree(); |
| | | } |
| | | |
| | | @Override |
| | | public List<TestItemDto> getItemTreeProduct(StructureTestObjectDto structureTestObjectDto) { |
| | | return structureItemParameterMapper.getItemTreeProduct(structureTestObjectDto.getObjectType()); |
| | | } |
| | | |
| | | @Override |
| | | public List<Model> getProductTypes(ModelDto modelDto) { |
| | | return modelMapper.selectList(Wrappers.<Model>lambdaQuery().eq(Model::getProductId, modelDto.getProductId())); |
| | | } |
| | | |
| | | @Override |
| | | public List<ProductPart> getPartNoList(ProductPartDto productPartDto) { |
| | | return productPartMapper.selectList(Wrappers.<ProductPart>lambdaQuery().eq(ProductPart::getProductId, productPartDto.getProductId())); |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public void importPartExcel(List<StructureTestObjectData> list) { |
| | | // 阶段2: 批量查询现有结构测试对象 |
| | | Map<String, StructureTestObject> existStructureTestObjects = structureTestObjectMapper.selectList( |
| | | Wrappers.<StructureTestObject>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<String> workShopNames = list.stream() |
| | | .map(StructureTestObjectData::getWorkShopName) |
| | | .filter(StringUtils::isNotEmpty) |
| | | .collect(Collectors.toSet()); |
| | | |
| | | Map<String, WorkShop> workShopCache = workShopNames.isEmpty() ? |
| | | Collections.emptyMap() : |
| | | workShopMapper.selectList(Wrappers.<WorkShop>lambdaQuery() |
| | | .in(WorkShop::getName, workShopNames)) |
| | | .stream() |
| | | .collect(Collectors.toMap(WorkShop::getName, Function.identity())); |
| | | |
| | | // 阶段4: 准备批量操作数据 |
| | | List<StructureTestObject> structureTestObjectsToInsert = new ArrayList<>(); |
| | | List<StructureTestObject> structureTestObjectsToUpdate = new ArrayList<>(); |
| | | List<Product> productsToInsert = new ArrayList<>(); |
| | | List<Product> productsToUpdate = new ArrayList<>(); |
| | | |
| | | // 阶段5: 主处理逻辑(仅处理结构测试对象) |
| | | List<StructureTestObjectData> 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<String, WorkShop> workShopCache, |
| | | List<Product> insertList, |
| | | List<Product> updateList) { |
| | | // 构建产品唯一标识 |
| | | String productKey = data.getName() + "|" + data.getNameEn() + "|" + data.getObjectType(); |
| | | |
| | | // 检查内存中是否已处理过该产品 |
| | | Optional<Product> existingProduct = findProductInBatchLists(productKey, insertList, updateList); |
| | | |
| | | // 如果内存中不存在,查询数据库 |
| | | if (!existingProduct.isPresent()) { |
| | | Product dbProduct = productMapper.selectOne(Wrappers.<Product>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<Product> findProductInBatchLists(String productKey, |
| | | List<Product> insertList, |
| | | List<Product> 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<String, WorkShop> 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<String, WorkShop> 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<String, StructureTestObject> existStructureTestObjects, |
| | | List<StructureTestObject> insertedObjects) { |
| | | |
| | | insertedObjects.forEach(obj -> { |
| | | String key = obj.getSpecimenName() + "|" + obj.getSpecimenNameEn() + "|" + obj.getObjectType(); |
| | | existStructureTestObjects.get(key).setId(obj.getId()); // 更新缓存中的ID |
| | | }); |
| | | } |
| | | |
| | | // 处理产品数据(独立方法) |
| | | private void processProducts(List<StructureTestObjectData> pendingProducts, |
| | | Map<String, StructureTestObject> structureTestObjectCache, |
| | | Map<String, WorkShop> workShopCache, |
| | | List<Product> insertList, |
| | | List<Product> 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<StructureTestObject> toInsert, |
| | | List<StructureTestObject> toUpdate) { |
| | | if (!toInsert.isEmpty()) { |
| | | structureTestObjectMapper.insertBatchSomeColumn(toInsert); |
| | | } |
| | | if (!toUpdate.isEmpty()) { |
| | | structureTestObjectService.updateBatchById(toUpdate); |
| | | } |
| | | } |
| | | |
| | | private void executeProductBatch(List<Product> toInsert, |
| | | List<Product> toUpdate) { |
| | | if (!toInsert.isEmpty()) { |
| | | productMapper.insertBatchSomeColumn(toInsert); |
| | | } |
| | | if (!toUpdate.isEmpty()) { |
| | | productService.updateBatchById(toUpdate); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |