lrj
昨天 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
package com.rongyichuang.common.api;
 
import com.rongyichuang.common.dto.request.MediaInput;
import com.rongyichuang.common.dto.response.MediaResponse;
import com.rongyichuang.common.entity.Media;
import com.rongyichuang.common.repository.MediaRepository;
import com.rongyichuang.common.service.MediaService;
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.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@Controller
public class MediaGraphqlApi {
 
    private final MediaRepository mediaRepository;
    private final MediaService mediaService;
    
    @Autowired
    private JdbcTemplate jdbcTemplate;
 
    @Value("${app.media-url}")
    private String mediaBaseUrl;
 
    public MediaGraphqlApi(MediaRepository mediaRepository, MediaService mediaService) {
        this.mediaRepository = mediaRepository;
        this.mediaService = mediaService;
    }
 
    @QueryMapping
    public List<MediaResponse> mediasByTarget(@Argument Integer targetType, @Argument Long targetId) {
        try {
            // 使用 JDBC 直接查询,避免 Hibernate 类型转换问题
            String sql = "SELECT id, name, path, file_size, file_ext, media_type, target_type, target_id, thumb_path, duration, description " +
                        "FROM t_media WHERE target_type = ? AND target_id = ?";
            
            List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql, targetType, targetId);
            
            List<MediaResponse> responses = new ArrayList<>();
            for (Map<String, Object> row : rows) {
                MediaResponse r = new MediaResponse();
                r.setId(((Number) row.get("id")).longValue());
                r.setName((String) row.get("name"));
                r.setPath((String) row.get("path"));
                r.setFileSize(row.get("file_size") != null ? ((Number) row.get("file_size")).intValue() : null);
                r.setFileExt((String) row.get("file_ext"));
                r.setMediaType(row.get("media_type") != null ? ((Number) row.get("media_type")).intValue() : null);
                r.setTargetType(row.get("target_type") != null ? ((Number) row.get("target_type")).intValue() : null);
                r.setTargetId(row.get("target_id") != null ? ((Number) row.get("target_id")).longValue() : null);
                
                // 设置完整URL
                if (mediaBaseUrl != null && !mediaBaseUrl.isEmpty() && r.getPath() != null) {
                    String base = mediaBaseUrl.endsWith("/") ? mediaBaseUrl.substring(0, mediaBaseUrl.length() - 1) : mediaBaseUrl;
                    String p = r.getPath().startsWith("/") ? r.getPath() : ("/" + r.getPath());
                    r.setFullUrl(base + p);
                }
                
                responses.add(r);
            }
            
            return responses;
        } catch (Exception e) {
            // 如果查询失败,返回空列表
            System.err.println("查询媒体失败: " + e.getMessage());
            e.printStackTrace();
            return List.of();
        }
    }
 
    private MediaResponse toResponse(Media m) {
        MediaResponse r = new MediaResponse();
        r.setId(m.getId());
        r.setName(m.getName());
        r.setPath(m.getPath());
        r.setFileSize(m.getFileSize());
        r.setFileExt(m.getFileExt());
        r.setMediaType(m.getMediaType());
        r.setTargetType(m.getTargetType());
        r.setTargetId(m.getTargetId());
        r.setThumbPath(m.getThumbPath());
        r.setDuration(m.getDuration());
        r.setDescription(m.getDescription());
        
        // 追加 fullUrl(前端也可自行拼接)
        if (mediaBaseUrl != null && !mediaBaseUrl.isEmpty() && m.getPath() != null) {
            String base = mediaBaseUrl.endsWith("/") ? mediaBaseUrl.substring(0, mediaBaseUrl.length() - 1) : mediaBaseUrl;
            String p = m.getPath().startsWith("/") ? m.getPath() : ("/" + m.getPath());
            r.setFullUrl(base + p);
        }
        
        // 追加 fullThumbUrl
        if (mediaBaseUrl != null && !mediaBaseUrl.isEmpty() && m.getThumbPath() != null) {
            String base = mediaBaseUrl.endsWith("/") ? mediaBaseUrl.substring(0, mediaBaseUrl.length() - 1) : mediaBaseUrl;
            String p = m.getThumbPath().startsWith("/") ? m.getThumbPath() : ("/" + m.getThumbPath());
            r.setFullThumbUrl(base + p);
        }
        
        return r;
    }
 
    @MutationMapping
    public Media saveMedia(@Argument MediaInput input) {
        System.out.println("=== saveMedia GraphQL调用 ===");
        System.out.println("输入参数: " + input);
        System.out.println("targetType: " + input.getTargetType());
        System.out.println("targetId: " + input.getTargetId());
        System.out.println("thumbPath: " + input.getThumbPath());
        
        try {
            Media result;
            
            // 如果有缩略图路径,使用支持缩略图的方法
            if (input.getThumbPath() != null && !input.getThumbPath().trim().isEmpty()) {
                result = mediaService.saveMedia(
                    input.getName(),
                    input.getPath(),
                    input.getFileSize(),
                    input.getFileExt(),
                    input.getMediaType(),
                    input.getTargetType(),
                    input.getTargetId(),
                    input.getThumbPath()
                );
                System.out.println("保存成功(含缩略图),媒体ID: " + result.getId());
            } else {
                result = mediaService.saveMedia(
                    input.getName(),
                    input.getPath(),
                    input.getFileSize(),
                    input.getFileExt(),
                    input.getMediaType(),
                    input.getTargetType(),
                    input.getTargetId()
                );
                System.out.println("保存成功,媒体ID: " + result.getId());
            }
            
            return result;
        } catch (Exception e) {
            System.err.println("保存媒体失败: " + e.getMessage());
            e.printStackTrace();
            throw e;
        }
    }
 
    @MutationMapping
    public Boolean deleteMedia(@Argument Long id) {
        System.out.println("=== deleteMedia GraphQL调用 ===");
        System.out.println("要删除的媒体ID: " + id);
        
        try {
            Boolean result = mediaService.deleteMedia(id);
            System.out.println("删除结果: " + result);
            return result;
        } catch (Exception e) {
            System.err.println("删除媒体失败: " + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }
}