修复收藏等问题
This commit is contained in:
@@ -88,6 +88,20 @@ public interface IMusicInfoService
|
||||
|
||||
List<MusicInfo> selectMusicInfoByReId(String reId);
|
||||
|
||||
/**
|
||||
* 获取上一首播放的音乐ID
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param playMode 播放模式:1-顺序播放,2-随机播放
|
||||
* @param playlistType 播放列表类型:1-历史播放,2-分类播放,3-推荐播放,4-收藏播放,5-喜欢播放
|
||||
* @param categoryId 分类ID,当playlistType=2时必填
|
||||
* @param collectId 收藏ID,当playlistType=4时必填
|
||||
* @param reId 推荐ID,当playlistType=3时必填
|
||||
* @param currentMusicId 当前播放的音乐ID,用于顺序播放时获取上一首
|
||||
* @return 上一首音乐ID
|
||||
*/
|
||||
String getPrevMusicId(Long userId, Integer playMode, Integer playlistType, String categoryId, String collectId, String reId, String currentMusicId);
|
||||
|
||||
/**
|
||||
* 获取下一首播放的音乐ID
|
||||
*
|
||||
|
||||
@@ -2,20 +2,15 @@ package com.ruoyi.system.service.impl;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.ruoyi.common.core.domain.entity.*;
|
||||
import com.ruoyi.common.enums.Audio;
|
||||
import com.ruoyi.common.enums.MusicType;
|
||||
import com.ruoyi.common.utils.DateUtils;
|
||||
import com.ruoyi.common.utils.MusicUtil;
|
||||
import com.ruoyi.common.utils.SecurityUtils;
|
||||
import com.ruoyi.system.config.AliConfig;
|
||||
import com.ruoyi.system.mapper.*;
|
||||
import com.ruoyi.system.service.IMusicInfoService;
|
||||
import com.ruoyi.system.service.IUserHistoryService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -261,6 +256,102 @@ public class MusicInfoServiceImpl implements IMusicInfoService
|
||||
return musicInfoMapper.selectMusicInfoByReId(reId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上一首播放的音乐ID
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param playMode 播放模式:1-顺序播放,2-随机播放
|
||||
* @param playlistType 播放列表类型:1-历史播放,2-分类播放,3-推荐播放,4-收藏播放,5-喜欢播放
|
||||
* @param categoryId 分类ID,当playlistType=2时必填
|
||||
* @param collectId 收藏ID,当playlistType=4时必填
|
||||
* @param reId 推荐ID,当playlistType=3时必填
|
||||
* @param currentMusicId 当前播放的音乐ID,用于顺序播放时获取上一首
|
||||
* @return 上一首音乐ID
|
||||
*/
|
||||
@Override
|
||||
public String getPrevMusicId(Long userId, Integer playMode, Integer playlistType, String categoryId, String collectId, String reId, String currentMusicId) {
|
||||
List<MusicInfo> musicList = null;
|
||||
|
||||
// 根据播放列表类型获取对应的音乐列表
|
||||
switch (playlistType) {
|
||||
case 1: // 历史播放
|
||||
musicList = musicInfoMapper.selectHistoryMusicByUser(userId);
|
||||
break;
|
||||
case 2: // 分类播放
|
||||
if (categoryId == null || categoryId.isEmpty()) {
|
||||
return null; // 分类播放必须提供分类ID
|
||||
}
|
||||
musicList = musicInfoMapper.selectMusicInfoByCid(categoryId,new MusicInfo());
|
||||
break;
|
||||
case 3: // 推荐播放
|
||||
if (reId == null || reId.isEmpty()) {
|
||||
return null; // 推荐播放必须提供推荐ID
|
||||
}
|
||||
musicList = musicInfoMapper.selectRecommendMusicByReId(reId);
|
||||
break;
|
||||
case 4: // 收藏播放
|
||||
if (collectId == null || collectId.isEmpty()) {
|
||||
return null; // 收藏播放必须提供收藏ID
|
||||
}
|
||||
musicList = musicInfoMapper.selectMusicByCollectId(collectId);
|
||||
break;
|
||||
case 5: // 喜欢播放
|
||||
musicList = musicInfoMapper.selectLikeMusicByUser(userId);
|
||||
break;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
||||
// 如果列表为空,返回null
|
||||
if (musicList == null || musicList.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
// 根据播放模式获取上一首音乐ID
|
||||
if (playMode == 1) { // 顺序播放
|
||||
// 按照ID排序列表
|
||||
musicList.sort((a, b) -> {
|
||||
Long idA = a.getMusicId();
|
||||
Long idB = b.getMusicId();
|
||||
return idA.compareTo(idB);
|
||||
});
|
||||
|
||||
|
||||
|
||||
// 如果没有当前音乐ID,返回列表中的最后一首
|
||||
if (currentMusicId == null || currentMusicId.isEmpty()) {
|
||||
return musicList.get(musicList.size() - 1).getMusicId().toString();
|
||||
}
|
||||
|
||||
// 查找当前音乐在列表中的位置
|
||||
int currentIndex = -1;
|
||||
for (int i = 0; i < musicList.size(); i++) {
|
||||
if (musicList.get(i).getMusicId().toString().equals(currentMusicId)) {
|
||||
currentIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// 如果找到当前音乐,返回上一首;否则返回最后一首
|
||||
if (currentIndex != -1) {
|
||||
int prevIndex = (currentIndex - 1 + musicList.size()) % musicList.size(); // 循环播放
|
||||
return musicList.get(prevIndex).getMusicId().toString();
|
||||
} else {
|
||||
String lastMusicId = musicList.get(musicList.size() - 1).getMusicId().toString();
|
||||
//System.out.println("未找到当前音乐,返回最后一首: " + lastMusicId);
|
||||
return lastMusicId;
|
||||
}
|
||||
} else if (playMode == 2) { // 随机播放
|
||||
// 生成随机索引
|
||||
int randomIndex = (int) (Math.random() * musicList.size());
|
||||
String randomMusicId = musicList.get(randomIndex).getMusicId().toString();
|
||||
//System.out.println("随机播放,索引: " + randomIndex + ", ID: " + randomMusicId);
|
||||
return randomMusicId;
|
||||
} else {
|
||||
return null; // 不支持的播放模式
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下一首播放的音乐ID
|
||||
*
|
||||
|
||||
@@ -104,6 +104,6 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
</select>
|
||||
|
||||
<update id="updateImgByCollectId">
|
||||
update user_collect set img=#{img} where collectId = #{collectId}
|
||||
update user_collect set img=#{img} where collect_id = #{collectId}
|
||||
</update>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user