lrj
2 天以前 9f8395fab13ca4b230a0f7d62636e209745c91d4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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;
    }
}