diff --git a/src/main/java/com/ai/da/mapper/primary/UserLikeGroupMapper.java b/src/main/java/com/ai/da/mapper/primary/UserLikeGroupMapper.java index bcf63b13..702e6e4d 100644 --- a/src/main/java/com/ai/da/mapper/primary/UserLikeGroupMapper.java +++ b/src/main/java/com/ai/da/mapper/primary/UserLikeGroupMapper.java @@ -2,8 +2,11 @@ package com.ai.da.mapper.primary; import com.ai.da.common.config.mybatis.plus.CommonMapper; import com.ai.da.mapper.primary.entity.UserLikeGroup; +import com.ai.da.mapper.primary.entity.CollectionElementRelModel; +import org.apache.ibatis.annotations.Param; import java.util.List; +import java.util.Map; /** * Mapper 接口 @@ -16,4 +19,57 @@ public interface UserLikeGroupMapper extends CommonMapper { Long insertUserLikeGroup(UserLikeGroup userLikeGroup); List getMoreThan50UserLikeAccount(); + + /** + * 批量查询模块内容,使用JOIN优化避免N+1查询 + * @param userLikeGroupId 用户喜欢组ID + * @param projectId 项目ID + * @param relationTypes 关联类型列表 + * @return 模块内容映射列表 + */ + List> getModuleContentBatch(@Param("userLikeGroupId") Long userLikeGroupId, + @Param("projectId") Long projectId, + @Param("relationTypes") List relationTypes); + + /** + * 批量查询ToProductImageResult及相关数据 + * @param projectId 项目ID + * @param resultType 结果类型 + * @return ToProductImageResult及相关数据列表 + */ + List> getToProductImageResultBatch(@Param("projectId") Long projectId, + @Param("resultType") String resultType); + + /** + * 批量查询UserLike及相关设计数据 + * @param relationIds 关联ID列表 + * @return UserLike及相关数据列表 + */ + List> getUserLikeBatch(@Param("relationIds") List relationIds); + + /** + * 批量查询ToProductElement数据 + * @param elementIds 元素ID列表 + * @return ToProductElement数据列表 + */ + List> getToProductElementBatch(@Param("elementIds") List elementIds); + + /** + * 批量查询TDesignPythonOutfit数据 + * @param outfitIds 服装ID列表 + * @return TDesignPythonOutfit数据列表 + */ + List> getDesignPythonOutfitBatch(@Param("outfitIds") List outfitIds); + + /** + * 批量查询ToProductImageRecord数据 + * @param recordIds ToProductImageRecord ID列表 + * @return 查询结果 + */ + List> getToProductImageRecordBatch(@Param("recordIds") List recordIds); + + /** + * 批量查询CollectionElementRelModel + */ + List getCollectionElementRelModelBatch(@Param("collectionElementIds") List collectionElementIds); } diff --git a/src/main/java/com/ai/da/service/impl/UserLikeGroupServiceImpl.java b/src/main/java/com/ai/da/service/impl/UserLikeGroupServiceImpl.java index 45946802..6e88c6c2 100644 --- a/src/main/java/com/ai/da/service/impl/UserLikeGroupServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/UserLikeGroupServiceImpl.java @@ -1950,137 +1950,133 @@ public class UserLikeGroupServiceImpl extends ServiceImpl userLikeSortQueryWrapper = new QueryWrapper<>(); - userLikeSortQueryWrapper.lambda().eq(CollectionSort::getProjectId, projectDTO.getId()); - userLikeSortQueryWrapper.lambda().isNull(CollectionSort::getParentId); - userLikeSortQueryWrapper.lambda().eq(CollectionSort::getRelationType, CollectionType.DESIGN.getValue()); - userLikeSortQueryWrapper.lambda().orderByAsc(CollectionSort::getSort); - List userLikeSortList = collectionSortMapper.selectList(userLikeSortQueryWrapper); - if (CollectionUtils.isEmpty(userLikeSortList)) { + // 使用批量查询获取design模块的所有数据 + List relationTypes = Arrays.asList( + CollectionType.DESIGN.getValue(), + CollectionType.TO_PRODUCT_IMAGE.getValue(), + CollectionType.RELIGHT.getValue(), + CollectionType.POSE_TRANSFORM.getValue() + ); + + List> moduleContentList = userLikeGroupMapper.getModuleContentBatch( + null, projectDTO.getId(), relationTypes); + + if (CollectionUtils.isEmpty(moduleContentList)) { moduleChooseVO.setDesign(vo); - }else { + } else { + // 按parent_id分组处理数据 + Map>> groupedByParent = moduleContentList.stream() + .collect(Collectors.groupingBy(item -> { + Object parentId = item.get("parent_id"); + return parentId == null ? 0L : ((Number) parentId).longValue(); + })); + List list = new ArrayList<>(); - for (CollectionSort collectionSort : userLikeSortList) { - UserLike userLike = userLikeMapper.selectById(collectionSort.getRelationId()); - UserLikeVO o = CopyUtil.copyObject(userLike, UserLikeVO.class); - TDesignPythonOutfit tDesignPythonOutfit1 = designPythonOutfitMapper.selectById(o.getDesignOutfitId()); - o.setUrl(tDesignPythonOutfit1.getDesignUrl()); - if (o.getUrl().contains("/")) { - int index = o.getUrl().lastIndexOf("/"); - o.setPictureName(o.getUrl().substring(index + 1)); - } - o.setDesignOutfitUrl(getMinioUrl(o.getUrl())); - QueryWrapper qw = new QueryWrapper<>(); - qw.lambda().eq(TDesignPythonOutfit::getDesignItemId, o.getDesignItemId()); - List tDesignPythonOutfits = designPythonOutfitMapper.selectList(qw); - if (CollectionUtil.isNotEmpty(tDesignPythonOutfits)) { - TDesignPythonOutfit tDesignPythonOutfit = tDesignPythonOutfits.get(0); - o.setDesignOutfitId(tDesignPythonOutfit.getId()); - } - - o.setSort(collectionSort.getSort()); - o.setUserLikeSortId(collectionSort.getId()); - o.setRelationType(collectionSort.getRelationType()); - o.setCollectionType(CollectionType.DESIGN.getValue()); - o.setResultType(CollectionType.DESIGN.getValue()); - QueryWrapper childCollectionQw = new QueryWrapper<>(); - childCollectionQw.lambda().eq(CollectionSort::getParentId, collectionSort.getId()); - childCollectionQw.lambda().orderByAsc(CollectionSort::getSort); - List childSortList = collectionSortMapper.selectList(childCollectionQw); - List childList = new ArrayList<>(); - for (CollectionSort userLikeSort : childSortList) { - if (userLikeSort.getRelationType().equals(CollectionType.TO_PRODUCT_IMAGE.getValue())) { - ToProductImageResult toProductImageResult = toProductImageResultMapper.selectById(userLikeSort.getRelationId()); - if (isGenerateTaskFailed(toProductImageResult.getStatus(), toProductImageResult.getCreateTime())){ - continue; - } - toProductImageResult.setUrl(getMinioUrl(toProductImageResult.getUrl())); - ToProductImageResultVO toProductImageResultVO = CopyUtil.copyObject(toProductImageResult, ToProductImageResultVO.class); - - ToProductImageRecord toProductImageRecord = toProductImageRecordMapper.selectById(toProductImageResult.getToProductImageRecordId()); - if (Objects.isNull(toProductImageRecord)) { - log.warn("toProductRecord不存在,projectId:{}, toProductImageResultId:{}", projectDTO.getId(), toProductImageResult.getId()); - continue; - } - toProductImageResultVO.setPrompt(toProductImageRecord.getPrompt()); - - if (toProductImageResultVO.getElementType().equals("ProductElement")) { - ToProductElement toProductElement = toProductElementMapper.selectById(toProductImageResultVO.getElementId()); - toProductImageResultVO.setSourceUrl(getMinioUrl(toProductElement.getUrl())); - }else if ((toProductImageResultVO.getElementType().equals("DesignOutfit"))) { - TDesignPythonOutfit tDesignPythonOutfit = designPythonOutfitMapper.selectById(toProductImageResultVO.getElementId()); - toProductImageResultVO.setSourceUrl(getMinioUrl(tDesignPythonOutfit.getDesignUrl())); - }else { - ToProductImageResult toProductImageResult1 = toProductImageResultMapper.selectById(toProductImageResultVO.getElementId()); - toProductImageResultVO.setSourceUrl(getMinioUrl(toProductImageResult1.getUrl())); - } - toProductImageResultVO.setCollectionType(CollectionType.TO_PRODUCT_IMAGE.getValue()); - toProductImageResultVO.setSort(userLikeSort.getSort()); - toProductImageResultVO.setUserLikeSortId(userLikeSort.getId()); - toProductImageResultVO.setRelationType(userLikeSort.getRelationType()); - toProductImageResultVO.setParentId(userLikeSort.getParentId()); - childList.add(toProductImageResultVO); - } else if (userLikeSort.getRelationType().equals(CollectionType.RELIGHT.getValue())) { - ToProductImageResult toProductImageResult = toProductImageResultMapper.selectById(userLikeSort.getRelationId()); - if (isGenerateTaskFailed(toProductImageResult.getStatus(), toProductImageResult.getCreateTime())){ - continue; - } - toProductImageResult.setUrl(getMinioUrl(toProductImageResult.getUrl())); - ToProductImageResultVO toProductImageResultVO = CopyUtil.copyObject(toProductImageResult, ToProductImageResultVO.class); - - ToProductImageRecord toProductImageRecord = toProductImageRecordMapper.selectById(toProductImageResult.getToProductImageRecordId()); - if (Objects.isNull(toProductImageRecord)) { - log.warn("toProductRecord不存在,projectId:{}, toProductImageResultId:{}", projectDTO.getId(), toProductImageResult.getId()); - continue; - } - toProductImageResultVO.setPrompt(toProductImageRecord.getPrompt()); - - if (toProductImageResultVO.getElementType().equals("ProductElement")) { - ToProductElement toProductElement = toProductElementMapper.selectById(toProductImageResultVO.getElementId()); - toProductImageResultVO.setSourceUrl(getMinioUrl(toProductElement.getUrl())); - }else if ((toProductImageResultVO.getElementType().equals("DesignOutfit"))) { - TDesignPythonOutfit tDesignPythonOutfit = designPythonOutfitMapper.selectById(toProductImageResultVO.getElementId()); - toProductImageResultVO.setSourceUrl(getMinioUrl(tDesignPythonOutfit.getDesignUrl())); - }else { - ToProductImageResult toProductImageResult1 = toProductImageResultMapper.selectById(toProductImageResultVO.getElementId()); - toProductImageResultVO.setSourceUrl(getMinioUrl(toProductImageResult1.getUrl())); - } - toProductImageResultVO.setCollectionType(CollectionType.RELIGHT.getValue()); - toProductImageResultVO.setSort(userLikeSort.getSort()); - toProductImageResultVO.setUserLikeSortId(userLikeSort.getId()); - toProductImageResultVO.setRelationType(userLikeSort.getRelationType()); - toProductImageResultVO.setParentId(userLikeSort.getParentId()); - childList.add(toProductImageResultVO); - } else if (userLikeSort.getRelationType().equals(CollectionType.POSE_TRANSFORM.getValue())) { - PoseTransformation item = poseTransformationMapper.selectById(userLikeSort.getRelationId()); - if (isGenerateTaskFailed(item.getTaskStatus(), item.getCreateTime())){ - continue; - } - PoseTransformationVO poseTransformationVO = new PoseTransformationVO(); - poseTransformationVO.setId(item.getId()); - poseTransformationVO.setTaskId(item.getUniqueId()); - poseTransformationVO.setProductImage(getMinioUrl(item.getProductImage())); - poseTransformationVO.setGifUrl(getMinioUrl(item.getGifUrl())); - poseTransformationVO.setVideoUrl(getMinioUrl(item.getVideoUrl())); - poseTransformationVO.setFirstFrameUrl(getMinioUrl(item.getFirstFrameUrl())); - poseTransformationVO.setIsLiked(item.getIsLiked()); - poseTransformationVO.setCollectionType(CollectionType.POSE_TRANSFORM.getValue()); - poseTransformationVO.setSort(userLikeSort.getSort()); - poseTransformationVO.setUserLikeSortId(userLikeSort.getId()); - poseTransformationVO.setRelationType(userLikeSort.getRelationType()); - poseTransformationVO.setResultType(CollectionType.POSE_TRANSFORM.getValue()); - poseTransformationVO.setParentId(userLikeSort.getParentId()); - poseTransformationVO.setModelName(item.getModelName()); - poseTransformationVO.setPoseId(item.getPoseId()); - poseTransformationVO.setStatus(item.getTaskStatus()); - childList.add(poseTransformationVO); + // 处理顶级design项目(parent_id为null的) + List> topLevelDesigns = groupedByParent.get(0L); + if (topLevelDesigns != null) { + for (Map designData : topLevelDesigns) { + if (!CollectionType.DESIGN.getValue().equals(designData.get("relation_type"))) { + continue; } - } - o.setChildList(childList); - list.add(o); + UserLikeVO o = new UserLikeVO(); + o.setId(((Number) designData.get("user_like_id")).longValue()); + o.setDesignItemId(((Number) designData.get("design_item_id")).longValue()); + o.setDesignOutfitId(((Number) designData.get("design_outfit_id")).longValue()); + + String designUrl = (String) designData.get("design_url"); + o.setUrl(designUrl); + if (designUrl != null && designUrl.contains("/")) { + int index = designUrl.lastIndexOf("/"); + o.setPictureName(designUrl.substring(index + 1)); + } + o.setDesignOutfitUrl(getMinioUrl(designUrl)); + + o.setSort(((Number) designData.get("sort")).intValue()); + o.setUserLikeSortId(((Number) designData.get("sort_id")).longValue()); + o.setRelationType((String) designData.get("relation_type")); + o.setCollectionType(CollectionType.DESIGN.getValue()); + o.setResultType(CollectionType.DESIGN.getValue()); + + // 处理子项目 + List childList = new ArrayList<>(); + Long parentSortId = ((Number) designData.get("sort_id")).longValue(); + List> childItems = groupedByParent.get(parentSortId); + + if (childItems != null) { + for (Map childData : childItems) { + String relationType = (String) childData.get("relation_type"); + + if (CollectionType.TO_PRODUCT_IMAGE.getValue().equals(relationType) || + CollectionType.RELIGHT.getValue().equals(relationType)) { + + // 检查任务状态 + String status = (String) childData.get("result_status"); + Object createTimeObj = childData.get("result_create_time"); + LocalDateTime createTime = createTimeObj != null ? + ((java.sql.Timestamp) createTimeObj).toLocalDateTime() : null; + if (isGenerateTaskFailed(status, createTime)) { + continue; + } + + ToProductImageResultVO toProductImageResultVO = new ToProductImageResultVO(); + toProductImageResultVO.setId(((Number) childData.get("to_product_result_id")).longValue()); + toProductImageResultVO.setUrl(getMinioUrl((String) childData.get("result_url"))); + toProductImageResultVO.setElementType((String) childData.get("element_type")); + toProductImageResultVO.setElementId(((Number) childData.get("element_id")).longValue()); + toProductImageResultVO.setPrompt((String) childData.get("prompt")); + + // 设置sourceUrl + String elementType = (String) childData.get("element_type"); + if ("ProductElement".equals(elementType)) { + toProductImageResultVO.setSourceUrl(getMinioUrl((String) childData.get("element_url"))); + } else if ("DesignOutfit".equals(elementType)) { + toProductImageResultVO.setSourceUrl(getMinioUrl((String) childData.get("design_url"))); + } + + toProductImageResultVO.setSort(((Number) childData.get("sort")).intValue()); + toProductImageResultVO.setUserLikeSortId(((Number) childData.get("sort_id")).longValue()); + toProductImageResultVO.setRelationType(relationType); + toProductImageResultVO.setParentId(((Number) childData.get("parent_id")).longValue()); + childList.add(toProductImageResultVO); + + } else if (CollectionType.POSE_TRANSFORM.getValue().equals(relationType)) { + // 检查任务状态 + String status = (String) childData.get("pose_status"); + Object createTimeObj = childData.get("pose_create_time"); + LocalDateTime createTime = createTimeObj != null ? + ((java.sql.Timestamp) createTimeObj).toLocalDateTime() : null; + if (isGenerateTaskFailed(status, createTime)) { + continue; + } + + PoseTransformationVO poseTransformationVO = new PoseTransformationVO(); + poseTransformationVO.setId(((Number) childData.get("pose_id")).longValue()); + poseTransformationVO.setTaskId((String) childData.get("pose_unique_id")); + poseTransformationVO.setProductImage(getMinioUrl((String) childData.get("pose_product_image"))); + poseTransformationVO.setGifUrl(getMinioUrl((String) childData.get("pose_gif_url"))); + poseTransformationVO.setVideoUrl(getMinioUrl((String) childData.get("pose_video_url"))); + poseTransformationVO.setFirstFrameUrl(getMinioUrl((String) childData.get("pose_first_frame_url"))); + poseTransformationVO.setIsLiked(((Number) childData.get("pose_is_liked")).byteValue()); + poseTransformationVO.setCollectionType(CollectionType.POSE_TRANSFORM.getValue()); + poseTransformationVO.setSort(((Number) childData.get("sort")).intValue()); + poseTransformationVO.setUserLikeSortId(((Number) childData.get("sort_id")).longValue()); + poseTransformationVO.setRelationType(relationType); + poseTransformationVO.setResultType(CollectionType.POSE_TRANSFORM.getValue()); + poseTransformationVO.setParentId(((Number) childData.get("parent_id")).longValue()); + poseTransformationVO.setModelName((String) childData.get("pose_model_name")); + poseTransformationVO.setPoseId(((Number) childData.get("pose_pose_id")).intValue()); + poseTransformationVO.setStatus(status); + childList.add(poseTransformationVO); + } + } + } + o.setChildList(childList); + list.add(o); + } } vo.setUserLikeDetails(list); } @@ -2126,6 +2122,28 @@ public class UserLikeGroupServiceImpl extends ServiceImpl recordIds = allResults.stream() + .map(ToProductImageResult::getToProductImageRecordId) + .filter(Objects::nonNull) + .distinct() + .collect(Collectors.toList()); + + Map recordPromptMap = new HashMap<>(); + if (!recordIds.isEmpty()) { + List> recordData = userLikeGroupMapper.getToProductImageRecordBatch(recordIds); + recordPromptMap = recordData.stream() + .collect(Collectors.toMap( + data -> ((Number) data.get("record_id")).longValue(), + data -> { + String prompt = (String) data.get("prompt"); + return prompt != null ? prompt : ""; // 提供默认值 + }, + (existing, replacement) -> existing + )); + } + + // 处理URL转换和分类 List likedList = new ArrayList<>(); List normalList = new ArrayList<>(); @@ -2137,10 +2155,10 @@ public class UserLikeGroupServiceImpl extends ServiceImpl list = new ArrayList<>(); if (CollectionUtil.isNotEmpty(collectionElements)) { + // 批量查询CollectionElementRelModel数据 + List collectionElementIds = collectionElements.stream() + .map(CollectionElement::getId) + .collect(Collectors.toList()); + List relModels = userLikeGroupMapper.getCollectionElementRelModelBatch(collectionElementIds); + Map relModelMap = relModels.stream() + .collect(Collectors.toMap(CollectionElementRelModel::getCollectionElementId, rel -> rel)); + for (CollectionElement collectionElement : collectionElements) { CollectionElementVO collectionElementVO = CopyUtil.copyObject(collectionElement, CollectionElementVO.class); - CollectionElementRelModel relModel = getCollectionElementRelModel(collectionElementVO.getId()); + CollectionElementRelModel relModel = relModelMap.get(collectionElementVO.getId()); collectionElementVO.setUrl(getMinioUrl(collectionElementVO.getUrl())); - collectionElementVO.setMannequinRelationId(relModel.getRelationId()); - collectionElementVO.setMannequinRelationType(relModel.getRelationType()); + if (relModel != null) { + collectionElementVO.setMannequinRelationId(relModel.getRelationId()); + collectionElementVO.setMannequinRelationType(relModel.getRelationType()); + } collectionElementVO.setCollectionId(collectionElement.getId()); list.add(collectionElementVO); } diff --git a/src/main/resources/mapper/primary/UserLikeGroupMapper.xml b/src/main/resources/mapper/primary/UserLikeGroupMapper.xml index ed91e883..00d7cb95 100644 --- a/src/main/resources/mapper/primary/UserLikeGroupMapper.xml +++ b/src/main/resources/mapper/primary/UserLikeGroupMapper.xml @@ -35,4 +35,160 @@ ORDER BY COUNT(*) DESC ; + + + + + + + + + + + + + + + + + + + + +