| | |
| | | import com.rongyichuang.activity.service.ActivityService; |
| | | import com.rongyichuang.common.dto.PageRequest; |
| | | 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 org.springframework.util.StringUtils; |
| | | |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Controller |
| | | public class ActivityResolver { |
| | |
| | | @Autowired |
| | | private ActivityService activityService; |
| | | |
| | | @Autowired |
| | | private MediaRepository mediaRepository; |
| | | |
| | | @Value("${app.media-url}") |
| | | private String mediaBaseUrl; |
| | | |
| | | /** |
| | | * 分页查询比赛列表 |
| | | */ |
| | | @QueryMapping |
| | | public PageResponse<ActivityResponse> activities(@Argument int page, @Argument int size, @Argument String name) { |
| | | PageRequest pageRequest = new PageRequest(page, size); |
| | | return activityService.findCompetitions(pageRequest, name); |
| | | return activityService.findActivities(pageRequest, name); |
| | | } |
| | | |
| | | /** |
| | |
| | | * 获取比赛的所有阶段 |
| | | */ |
| | | @QueryMapping |
| | | public List<ActivityResponse> activityStages(@Argument Long competitionId) { |
| | | return activityService.findStagesByCompetitionId(competitionId); |
| | | public List<ActivityResponse> activityStages(@Argument Long activityId) { |
| | | return activityService.findStagesByActivityId(activityId); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @QueryMapping |
| | | public List<ActivityResponse> ongoingActivities() { |
| | | return activityService.findOngoingCompetitions(); |
| | | return activityService.findOngoingActivities(); |
| | | } |
| | | |
| | | /** |
| | |
| | | public Boolean deleteActivity(@Argument Long id) { |
| | | return activityService.deleteActivity(id); |
| | | } |
| | | |
| | | /** |
| | | * 解析Activity的coverImage字段 |
| | | */ |
| | | @SchemaMapping(typeName = "Activity", field = "coverImage") |
| | | public MediaResponse coverImage(ActivityResponse activity) { |
| | | List<Media> mediaList = mediaRepository.findByTargetTypeAndTargetIdAndState( |
| | | MediaTargetType.ACTIVITY.getValue(), activity.getId(), 1); |
| | | |
| | | // 查找第一个图片类型的媒体作为封面图 |
| | | return mediaList.stream() |
| | | .filter(media -> media.getMediaType() != null && media.getMediaType() == 1) // 1表示图片 |
| | | .findFirst() |
| | | .map(this::convertToMediaResponse) |
| | | .orElse(null); |
| | | } |
| | | |
| | | /** |
| | | * 解析Activity的images字段 |
| | | */ |
| | | @SchemaMapping(typeName = "Activity", field = "images") |
| | | public List<MediaResponse> images(ActivityResponse activity) { |
| | | List<Media> mediaList = mediaRepository.findByTargetTypeAndTargetIdAndState( |
| | | MediaTargetType.ACTIVITY.getValue(), activity.getId(), 1); |
| | | |
| | | // 返回所有图片类型的媒体 |
| | | return mediaList.stream() |
| | | .filter(media -> media.getMediaType() != null && media.getMediaType() == 1) // 1表示图片 |
| | | .map(this::convertToMediaResponse) |
| | | .collect(Collectors.toList()); |
| | | } |
| | | |
| | | /** |
| | | * 解析Activity的videos字段 |
| | | */ |
| | | @SchemaMapping(typeName = "Activity", field = "videos") |
| | | public List<MediaResponse> videos(ActivityResponse activity) { |
| | | List<Media> mediaList = mediaRepository.findByTargetTypeAndTargetIdAndState( |
| | | MediaTargetType.ACTIVITY.getValue(), activity.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; |
| | | } |
| | | } |