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:${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 String 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 = ? AND target_id REGEXP '^[0-9]+$'";
|
|
List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql, targetType, Long.parseLong(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());
|
// 追加 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);
|
}
|
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());
|
|
try {
|
Media 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 String id) {
|
System.out.println("=== deleteMedia GraphQL调用 ===");
|
System.out.println("要删除的媒体ID: " + id);
|
|
try {
|
Long mediaId = Long.parseLong(id);
|
System.out.println("转换后的媒体ID: " + mediaId);
|
|
Boolean result = mediaService.deleteMedia(mediaId);
|
System.out.println("删除结果: " + result);
|
return result;
|
} catch (Exception e) {
|
System.err.println("删除媒体失败: " + e.getMessage());
|
e.printStackTrace();
|
return false;
|
}
|
}
|
}
|