From 575d1492ad7cef8ae51c2f4f2eca68a5d48e3c53 Mon Sep 17 00:00:00 2001 From: shahaibo <1023316923@qq.com> Date: Wed, 12 Mar 2025 23:22:41 +0800 Subject: [PATCH] =?UTF-8?q?BUGFIX:=20=E9=9A=90=E8=97=8F=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E4=B8=8D=E5=87=BAminio=E7=9A=84=E7=9B=B8=E5=85=B3=E6=95=B0?= =?UTF-8?q?=E6=8D=AE;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ai/da/common/utils/MinioUtil.java | 10 ++ .../service/impl/CollectionServiceImpl.java | 80 +++++++++---- .../ai/da/service/impl/DesignServiceImpl.java | 3 + .../da/service/impl/LibraryServiceImpl.java | 108 ++++++++++++------ 4 files changed, 144 insertions(+), 57 deletions(-) diff --git a/src/main/java/com/ai/da/common/utils/MinioUtil.java b/src/main/java/com/ai/da/common/utils/MinioUtil.java index d3cdc76f..f622b66f 100644 --- a/src/main/java/com/ai/da/common/utils/MinioUtil.java +++ b/src/main/java/com/ai/da/common/utils/MinioUtil.java @@ -440,6 +440,16 @@ public class MinioUtil { return getPreSignedUrl(bucketName, String.valueOf(fileName), expiry); } + public boolean doesObjectExist(String path) { + if (!path.contains("/")) { + throw new BusinessException("the.path.is.error"); + } + int index = path.indexOf("/"); + String bucketName = path.substring(0, index); + String objectName = path.substring(index + 1); + return doesObjectExist(bucketName, objectName); + } + public boolean doesObjectExist(String bucketName, String objectName) { try { minioClient.statObject( diff --git a/src/main/java/com/ai/da/service/impl/CollectionServiceImpl.java b/src/main/java/com/ai/da/service/impl/CollectionServiceImpl.java index 5f29b7c9..bf598dcd 100644 --- a/src/main/java/com/ai/da/service/impl/CollectionServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/CollectionServiceImpl.java @@ -101,13 +101,21 @@ public class CollectionServiceImpl extends ServiceImpl> maps = collectionElements .stream() .collect(Collectors.groupingBy(CollectionElement::getLevel1Type)); + maps.forEach((k, v) -> { CollectionLevel1TypeEnum level1TypeEnum = CollectionLevel1TypeEnum.uploadOf(k); if (Objects.isNull(level1TypeEnum)) { @@ -115,43 +123,67 @@ public class CollectionServiceImpl extends ServiceImpl { + List moodBoards = CopyUtil.copyList(v, CollectionElementVO.class, (o, d) -> { d.setDesignType(DesignTypeEnum.COLLECTION.getRealName()); - d.setUrl(minioUtil.getPreSignedUrl(o.getUrl(), 24 * 60)); - })); - break; - case PRINT_BOARD: - response.setPrintBoards(CopyUtil.copyList(v, CollectionElementVO.class, (o, d) -> { - d.setIsPin(o.getHasPin()); - d.setDesignType(DesignTypeEnum.COLLECTION.getRealName()); - d.setUrl(minioUtil.getPreSignedUrl(o.getUrl(), 24 * 60)); - })); - break; - case SKETCH_BOARD: - response.setSketchBoards(CopyUtil.copyList(v, CollectionElementVO.class, (o, d) -> { - d.setIsPin(o.getHasPin()); - d.setDesignType(DesignTypeEnum.COLLECTION.getRealName()); - d.setUrl(minioUtil.getPreSignedUrl(o.getUrl(), 24 * 60)); String url = o.getUrl(); - if (url.contains(".")) { - String[] split = url.split("\\."); - d.setUrlWithWhiteSide(minioUtil.getPreSignedUrl(split[0] + "-show." + split[1], 24 * 60)); - }else { - d.setUrlWithWhiteSide(minioUtil.getPreSignedUrl(url + "-show", 24 * 60)); + if (minioUtil.doesObjectExist(url)) { + d.setUrl(minioUtil.getPreSignedUrl(url, 24 * 60)); } - })); + }); + response.setMoodBoards(moodBoards.stream() + .filter(d -> minioUtil.doesObjectExist(d.getUrl())) + .collect(Collectors.toList())); break; + + case PRINT_BOARD: + List printBoards = CopyUtil.copyList(v, CollectionElementVO.class, (o, d) -> { + d.setIsPin(o.getHasPin()); + d.setDesignType(DesignTypeEnum.COLLECTION.getRealName()); + String url = o.getUrl(); + if (minioUtil.doesObjectExist(url)) { + d.setUrl(minioUtil.getPreSignedUrl(url, 24 * 60)); + } + }); + response.setPrintBoards(printBoards.stream() + .filter(d -> minioUtil.doesObjectExist(d.getUrl())) + .collect(Collectors.toList())); + break; + + case SKETCH_BOARD: + List sketchBoards = CopyUtil.copyList(v, CollectionElementVO.class, (o, d) -> { + d.setIsPin(o.getHasPin()); + d.setDesignType(DesignTypeEnum.COLLECTION.getRealName()); + String url = o.getUrl(); + if (minioUtil.doesObjectExist(url)) { + d.setUrl(minioUtil.getPreSignedUrl(url, 24 * 60)); + if (url.contains(".")) { + String[] split = url.split("\\."); + d.setUrlWithWhiteSide(minioUtil.getPreSignedUrl(split[0] + "-show." + split[1], 24 * 60)); + } else { + d.setUrlWithWhiteSide(minioUtil.getPreSignedUrl(url + "-show", 24 * 60)); + } + } + }); + response.setSketchBoards(sketchBoards.stream() + .filter(d -> minioUtil.doesObjectExist(d.getUrl())) + .collect(Collectors.toList())); + break; + case COLOR_BOARD: response.setColorBoards(resolveColorBoard(v)); break; + case MARKETING_SKETCH: response.setMarketingSketchs(CopyUtil.copyList(v, CollectionElementVO.class, (o, d) -> { d.setDesignType(DesignTypeEnum.COLLECTION.getRealName()); })); break; + default: + break; } }); + if (CollectionUtil.isNotEmpty(response.getColorBoards())) { List collect = response.getColorBoards().stream() .distinct() diff --git a/src/main/java/com/ai/da/service/impl/DesignServiceImpl.java b/src/main/java/com/ai/da/service/impl/DesignServiceImpl.java index 07441e6c..eb616a64 100644 --- a/src/main/java/com/ai/da/service/impl/DesignServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/DesignServiceImpl.java @@ -1347,6 +1347,7 @@ public class DesignServiceImpl extends ServiceImpl impleme throw new BusinessException("design.not.found"); } List designItemDetails = designItemDetailService.selectByDesignItemId(designItemId); + designItemDetails.removeIf(designItemDetail -> !minioUtil.doesObjectExist(designItemDetail.getPath())); if (CollectionUtil.isEmpty(designItemDetails)) { throw new BusinessException("designItemDetails.not.found"); } @@ -1383,6 +1384,7 @@ public class DesignServiceImpl extends ServiceImpl impleme if (!StringUtil.isNullOrEmpty(o.getUndividedLayer())) d.setUndividedLayer(minioUtil.getPreSignedUrl(o.getUndividedLayer(), CommonConstant.MINIO_IMAGE_EXPIRE_TIME)); // 根据designItemDetailId获取印花 List prints = designItemDetailPrintService.getByDesignItemDetailId(o.getId(), "print"); + prints.removeIf(print -> !minioUtil.doesObjectExist(print.getPath())); // 判断有无印花 if (CollectionUtil.isNotEmpty(prints)) { // 有印花 @@ -1409,6 +1411,7 @@ public class DesignServiceImpl extends ServiceImpl impleme d.setMinIOPath(o.getPath()); d.setPrintObject(new DesignPythonItemPrint()); })); + response.getOthers().removeIf(o -> !minioUtil.doesObjectExist(o.getMinIOPath())); return editDesignItemLayer(flag, designPythonOutfit, minioUtil.getPreSignedUrl(designPythonOutfit.getDesignUrl(), 24 * 60), editResponseColor(designItemDetails, response)); diff --git a/src/main/java/com/ai/da/service/impl/LibraryServiceImpl.java b/src/main/java/com/ai/da/service/impl/LibraryServiceImpl.java index 2c237cf9..623b4643 100644 --- a/src/main/java/com/ai/da/service/impl/LibraryServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/LibraryServiceImpl.java @@ -182,51 +182,93 @@ public class LibraryServiceImpl extends ServiceImpl impl } } queryWrapper.orderByDesc("id"); - IPage page = getBaseMapper().selectPage( - new Page<>(query.getPage(), query.getSize()), queryWrapper); - if (CollectionUtils.isEmpty(page.getRecords())) { + // 1. 先查询所有符合条件的 Library 数据 + List libraryList = getBaseMapper().selectList(queryWrapper); + if (CollectionUtils.isEmpty(libraryList)) { return PageBaseResponse.success(new Page<>()); } + + // 2. 封装打点内容 Map map = null; - //封装打点内容 - Boolean isExist = page.getRecords().stream() + boolean isExist = libraryList.stream() .anyMatch(library -> library.getLevel1Type().equals(LibraryLevel1TypeEnum.MODELS.getRealName())); if (isExist) { - List libarayIds = page.getRecords().stream() + List libraryIds = libraryList.stream() .filter(library -> library.getLevel1Type().equals(LibraryLevel1TypeEnum.MODELS.getRealName())) .map(Library::getId) .collect(Collectors.toList()); - List libraryModelPointVOS = libraryModelPointService.selectByLibraryIds(libarayIds); + List libraryModelPointVOS = libraryModelPointService.selectByLibraryIds(libraryIds); if (!CollectionUtils.isEmpty(libraryModelPointVOS)) { - map = libraryModelPointVOS.stream().collect(Collectors.toMap(LibraryModelPointVO::getRelationId, v -> v)); + map = libraryModelPointVOS.stream() + .collect(Collectors.toMap(LibraryModelPointVO::getRelationId, v -> v)); } } Map finalMap = map; - IPage convert = page.convert((Function) library -> { - QueryLibraryPageVO libraryPageVO = CopyUtil.copyObject(library, QueryLibraryPageVO.class); - libraryPageVO.setDesignType(DesignTypeEnum.LIBRARY.getRealName()); - libraryPageVO.setMinIOPath(library.getUrl()); - libraryPageVO.setUrl(minioUtil.getPreSignedUrl(library.getUrl(), 24 * 60)); - if (finalMap != null && finalMap.containsKey(library.getId())) { - libraryPageVO.setLibraryModelPoint(finalMap.get(library.getId())); - } - if (libraryPageVO.getLevel1Type().equals(LibraryLevel1TypeEnum.SKETCH_BOARD.getRealName())) { - if (!StringUtils.isEmpty(libraryPageVO.getLevel2Type())) { - Position position = Position.getPosition(libraryPageVO.getLevel2Type()); - libraryPageVO.setLevel2TypeEnum(new BizJson(position.getValue(), position.name(), BusinessException.getMessageFromResource(position.name()))); - } - } - if (libraryPageVO.getLevel1Type().equals(LibraryLevel1TypeEnum.PRINT_BOARD.getRealName())) { - PrintboardLevel2TypeEnum printboardLevel2TypeEnum = PrintboardLevel2TypeEnum.fromName(libraryPageVO.getLevel2Type()); - libraryPageVO.setLevel2TypeEnum(new BizJson(printboardLevel2TypeEnum.getValue(), printboardLevel2TypeEnum.name(), BusinessException.getMessageFromResource(printboardLevel2TypeEnum.name()))); - } - if (libraryPageVO.getLevel1Type().equals(LibraryLevel1TypeEnum.DESIGN_ELEMENTS.getRealName())) { - DesignElementsEnum designElementsEnum = DesignElementsEnum.fromName(libraryPageVO.getLevel2Type()); - libraryPageVO.setLevel2TypeEnum(new BizJson(designElementsEnum.getValue(), designElementsEnum.name(), BusinessException.getMessageFromResource(designElementsEnum.name()))); - } - return libraryPageVO; - }); - return PageBaseResponse.success(convert); + + // 3. 转换 Library 为 QueryLibraryPageVO,并通过 minioUtil 过滤掉不存在的 URL + List convertedList = libraryList.stream() + .map(library -> { + QueryLibraryPageVO libraryPageVO = CopyUtil.copyObject(library, QueryLibraryPageVO.class); + libraryPageVO.setDesignType(DesignTypeEnum.LIBRARY.getRealName()); + libraryPageVO.setMinIOPath(library.getUrl()); + String url = library.getUrl(); + if (minioUtil.doesObjectExist(url)) { + libraryPageVO.setUrl(minioUtil.getPreSignedUrl(url, 24 * 60)); + if (finalMap != null && finalMap.containsKey(library.getId())) { + libraryPageVO.setLibraryModelPoint(finalMap.get(library.getId())); + } + if (libraryPageVO.getLevel1Type().equals(LibraryLevel1TypeEnum.SKETCH_BOARD.getRealName())) { + if (!StringUtils.isEmpty(libraryPageVO.getLevel2Type())) { + Position position = Position.getPosition(libraryPageVO.getLevel2Type()); + libraryPageVO.setLevel2TypeEnum(new BizJson( + position.getValue(), + position.name(), + BusinessException.getMessageFromResource(position.name())) + ); + } + } + if (libraryPageVO.getLevel1Type().equals(LibraryLevel1TypeEnum.PRINT_BOARD.getRealName())) { + PrintboardLevel2TypeEnum printboardLevel2TypeEnum = PrintboardLevel2TypeEnum.fromName(libraryPageVO.getLevel2Type()); + libraryPageVO.setLevel2TypeEnum(new BizJson( + printboardLevel2TypeEnum.getValue(), + printboardLevel2TypeEnum.name(), + BusinessException.getMessageFromResource(printboardLevel2TypeEnum.name())) + ); + } + if (libraryPageVO.getLevel1Type().equals(LibraryLevel1TypeEnum.DESIGN_ELEMENTS.getRealName())) { + DesignElementsEnum designElementsEnum = DesignElementsEnum.fromName(libraryPageVO.getLevel2Type()); + libraryPageVO.setLevel2TypeEnum(new BizJson( + designElementsEnum.getValue(), + designElementsEnum.name(), + BusinessException.getMessageFromResource(designElementsEnum.name())) + ); + } + return libraryPageVO; + } else { + // 如果对应的 MinIO 文件不存在,则返回 null,后续过滤掉 + return null; + } + }) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + + // 4. 手动分页 + int total = convertedList.size(); + int currentPage = query.getPage(); // 当前页码(从1开始) + int pageSize = query.getSize(); // 每页大小 + int fromIndex = (currentPage - 1) * pageSize; + int toIndex = Math.min(total, currentPage * pageSize); + List pageList = new ArrayList<>(); + if (fromIndex < total) { + pageList = convertedList.subList(fromIndex, toIndex); + } + + // 5. 构造 IPage 对象(使用 MyBatis-Plus 的 Page 实现) + IPage pageResult = new Page<>(currentPage, pageSize, total); + pageResult.setRecords(pageList); + + // 6. 返回结果 + return PageBaseResponse.success(pageResult); } @Override