| | |
| | | import com.rongyichuang.carousel.dto.response.CarouselResponse; |
| | | import com.rongyichuang.carousel.service.CarouselService; |
| | | import com.rongyichuang.common.dto.PageResponse; |
| | | import com.rongyichuang.common.dto.response.MediaResponse; |
| | | import com.rongyichuang.common.enums.MediaTargetType; |
| | | import com.rongyichuang.common.repository.MediaRepository; |
| | | import com.rongyichuang.common.entity.Media; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.graphql.data.method.annotation.Argument; |
| | | import org.springframework.graphql.data.method.annotation.MutationMapping; |
| | | import org.springframework.graphql.data.method.annotation.QueryMapping; |
| | | import org.springframework.graphql.data.method.annotation.SchemaMapping; |
| | | import org.springframework.stereotype.Controller; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 轮播图GraphQL解析器 |
| | |
| | | |
| | | @Autowired |
| | | private CarouselService carouselService; |
| | | |
| | | @Autowired |
| | | private MediaRepository mediaRepository; |
| | | |
| | | @Value("${app.media-url}") |
| | | private String mediaBaseUrl; |
| | | |
| | | /** |
| | | * 分页查询轮播图 |
| | |
| | | System.out.println("更新结果: " + result); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 解析Carousel的coverImage字段 |
| | | */ |
| | | @SchemaMapping(typeName = "CarouselResponse", field = "coverImage") |
| | | public MediaResponse coverImage(CarouselResponse carousel) { |
| | | List<Media> mediaList = mediaRepository.findByTargetTypeAndTargetIdAndState( |
| | | MediaTargetType.CAROUSEL.getValue(), carousel.getId(), 1); |
| | | |
| | | // 查找第一个图片类型的媒体作为封面图 |
| | | return mediaList.stream() |
| | | .filter(media -> media.getMediaType() != null && media.getMediaType() == 1) // 1表示图片 |
| | | .findFirst() |
| | | .map(this::convertToMediaResponse) |
| | | .orElse(null); |
| | | } |
| | | |
| | | /** |
| | | * 解析Carousel的images字段 |
| | | */ |
| | | @SchemaMapping(typeName = "CarouselResponse", field = "images") |
| | | public List<MediaResponse> images(CarouselResponse carousel) { |
| | | List<Media> mediaList = mediaRepository.findByTargetTypeAndTargetIdAndState( |
| | | MediaTargetType.CAROUSEL.getValue(), carousel.getId(), 1); |
| | | |
| | | // 返回所有图片类型的媒体 |
| | | return mediaList.stream() |
| | | .filter(media -> media.getMediaType() != null && media.getMediaType() == 1) // 1表示图片 |
| | | .map(this::convertToMediaResponse) |
| | | .collect(Collectors.toList()); |
| | | } |
| | | |
| | | /** |
| | | * 解析Carousel的videos字段 |
| | | */ |
| | | @SchemaMapping(typeName = "CarouselResponse", field = "videos") |
| | | public List<MediaResponse> videos(CarouselResponse carousel) { |
| | | List<Media> mediaList = mediaRepository.findByTargetTypeAndTargetIdAndState( |
| | | MediaTargetType.CAROUSEL.getValue(), carousel.getId(), 1); |
| | | |
| | | // 返回所有视频类型的媒体 |
| | | return mediaList.stream() |
| | | .filter(media -> media.getMediaType() != null && media.getMediaType() == 2) // 2表示视频 |
| | | .map(this::convertToMediaResponse) |
| | | .collect(Collectors.toList()); |
| | | } |
| | | |
| | | /** |
| | | * 将Media实体转换为MediaResponse |
| | | */ |
| | | private MediaResponse convertToMediaResponse(Media media) { |
| | | MediaResponse response = new MediaResponse(); |
| | | response.setId(media.getId()); |
| | | response.setName(media.getName()); |
| | | response.setPath(media.getPath()); |
| | | response.setFileSize(media.getFileSize()); |
| | | response.setFileExt(media.getFileExt()); |
| | | response.setMediaType(media.getMediaType()); |
| | | response.setTargetType(media.getTargetType()); |
| | | response.setTargetId(media.getTargetId()); |
| | | response.setThumbPath(media.getThumbPath()); |
| | | response.setDuration(media.getDuration()); |
| | | response.setDescription(media.getDescription()); |
| | | |
| | | // 设置完整URL |
| | | if (mediaBaseUrl != null && !mediaBaseUrl.isEmpty() && media.getPath() != null) { |
| | | String base = mediaBaseUrl.endsWith("/") ? mediaBaseUrl.substring(0, mediaBaseUrl.length() - 1) : mediaBaseUrl; |
| | | String path = media.getPath().startsWith("/") ? media.getPath() : ("/" + media.getPath()); |
| | | response.setFullUrl(base + path); |
| | | } |
| | | |
| | | // 设置缩略图完整URL |
| | | if (mediaBaseUrl != null && !mediaBaseUrl.isEmpty() && media.getThumbPath() != null) { |
| | | String base = mediaBaseUrl.endsWith("/") ? mediaBaseUrl.substring(0, mediaBaseUrl.length() - 1) : mediaBaseUrl; |
| | | String thumbPath = media.getThumbPath().startsWith("/") ? media.getThumbPath() : ("/" + media.getThumbPath()); |
| | | response.setFullThumbUrl(base + thumbPath); |
| | | } |
| | | |
| | | return response; |
| | | } |
| | | } |