package com.rongyichuang.carousel.resolver;
|
|
import com.rongyichuang.carousel.dto.request.CarouselInput;
|
import com.rongyichuang.carousel.dto.request.CarouselSortOrderInput;
|
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解析器
|
*/
|
@Controller
|
public class CarouselResolver {
|
|
@Autowired
|
private CarouselService carouselService;
|
|
@Autowired
|
private MediaRepository mediaRepository;
|
|
@Value("${app.media-url}")
|
private String mediaBaseUrl;
|
|
/**
|
* 分页查询轮播图
|
*/
|
@QueryMapping
|
public PageResponse<CarouselResponse> carousels(@Argument int page, @Argument int size, @Argument String title) {
|
return carouselService.getCarousels(page, size, title);
|
}
|
|
/**
|
* 根据ID查询轮播图
|
*/
|
@QueryMapping
|
public CarouselResponse carousel(@Argument Long id) {
|
return carouselService.getCarousel(id);
|
}
|
|
/**
|
* 获取播放列表
|
*/
|
@QueryMapping
|
public List<CarouselResponse> carouselPlayList() {
|
return carouselService.getPlayList();
|
}
|
|
/**
|
* 保存轮播图
|
*/
|
@MutationMapping
|
public CarouselResponse saveCarousel(@Argument CarouselInput carousel) {
|
return carouselService.saveCarousel(carousel);
|
}
|
|
/**
|
* 删除轮播图
|
*/
|
@MutationMapping
|
public Boolean deleteCarousel(@Argument Long id) {
|
return carouselService.deleteCarousel(id);
|
}
|
|
/**
|
* 批量更新播放顺序
|
*/
|
@MutationMapping
|
public Boolean updateCarouselSortOrders(@Argument List<CarouselSortOrderInput> sortOrders) {
|
System.out.println("=== 收到排序更新请求 ===");
|
System.out.println("排序数据: " + sortOrders);
|
for (CarouselSortOrderInput input : sortOrders) {
|
System.out.println("ID: " + input.getId() + ", 排序值: " + input.getSortOrder());
|
}
|
Boolean result = carouselService.updateSortOrders(sortOrders);
|
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;
|
}
|
}
|