|
|
|
@ -0,0 +1,311 @@
|
|
|
|
|
package com.luoo.music.service;
|
|
|
|
|
|
|
|
|
|
import api.PageResult;
|
|
|
|
|
import com.luoo.music.dao.ArtistAlbumDao;
|
|
|
|
|
import com.luoo.music.dao.ArtistAlbumSongDao;
|
|
|
|
|
import com.luoo.music.dao.SongInfoDao;
|
|
|
|
|
import com.luoo.music.dao.SongTagDao;
|
|
|
|
|
import com.luoo.music.dto.response.AlbumAddDTO;
|
|
|
|
|
import com.luoo.music.dto.response.AlbumSearchDTO;
|
|
|
|
|
import com.luoo.music.dto.response.AlbumSongAddDTO;
|
|
|
|
|
import com.luoo.music.dto.response.AlbumUpdateDTO;
|
|
|
|
|
import com.luoo.music.pojo.ArtistAlbum;
|
|
|
|
|
import com.luoo.music.pojo.ArtistAlbumSong;
|
|
|
|
|
import com.luoo.music.pojo.SongInfo;
|
|
|
|
|
import com.luoo.music.pojo.SongTag;
|
|
|
|
|
import com.luoo.music.util.Constants;
|
|
|
|
|
import dto.UserLoginDto;
|
|
|
|
|
import enums.AlbumStateEnum;
|
|
|
|
|
import enums.SongInfoStateEnum;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.data.domain.Page;
|
|
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
|
|
import org.springframework.data.domain.Sort;
|
|
|
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
import util.IdWorker;
|
|
|
|
|
import util.JwtUtil;
|
|
|
|
|
|
|
|
|
|
import javax.persistence.criteria.CriteriaBuilder;
|
|
|
|
|
import javax.persistence.criteria.CriteriaQuery;
|
|
|
|
|
import javax.persistence.criteria.Predicate;
|
|
|
|
|
import javax.persistence.criteria.Root;
|
|
|
|
|
import java.time.LocalDate;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Author: yawei.huang
|
|
|
|
|
* @Package: com.luoo.music.service
|
|
|
|
|
* @Project: luoo_parent
|
|
|
|
|
* @Date: 2024/4/28 11:27
|
|
|
|
|
* @Filename: AlbumService
|
|
|
|
|
* @Describe:
|
|
|
|
|
*/
|
|
|
|
|
@Service
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class AlbumService {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private ArtistAlbumDao artistAlbumDao;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private ArtistAlbumSongDao artistAlbumSongDao;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private SongInfoDao songInfoDao;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private SongTagDao songTagDao;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private IdWorker idWorker;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private JwtUtil jwtUtil;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private S3Service s3Service;
|
|
|
|
|
|
|
|
|
|
public PageResult<ArtistAlbum> getList(AlbumSearchDTO albumSearchDTO, Integer page, Integer size) {
|
|
|
|
|
List<ArtistAlbum> result = new ArrayList<>();
|
|
|
|
|
Sort sort = new Sort(Sort.Direction.DESC, "create_time");
|
|
|
|
|
PageRequest pageRequest = PageRequest.of(page - 1, size, sort);
|
|
|
|
|
Page<ArtistAlbum> albumPage;
|
|
|
|
|
if (!Objects.isNull(albumSearchDTO)) {
|
|
|
|
|
Specification<ArtistAlbum> artistAlbumSpecification = buildSearchSpecification(albumSearchDTO);
|
|
|
|
|
albumPage = artistAlbumDao.findAll(artistAlbumSpecification, pageRequest);
|
|
|
|
|
} else {
|
|
|
|
|
albumPage = artistAlbumDao.findAll(pageRequest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long totalElements = albumPage.getTotalElements();
|
|
|
|
|
|
|
|
|
|
return new PageResult<>(totalElements, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检索条件
|
|
|
|
|
*
|
|
|
|
|
* @param param 查询对象
|
|
|
|
|
* @return 检索条件
|
|
|
|
|
*/
|
|
|
|
|
private Specification<ArtistAlbum> buildSearchSpecification(AlbumSearchDTO param) {
|
|
|
|
|
return (Root<ArtistAlbum> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> {
|
|
|
|
|
List<Predicate> predicateList = new ArrayList<Predicate>();
|
|
|
|
|
if (StringUtils.isNotBlank(param.getArtistId())) {
|
|
|
|
|
predicateList.add(builder.equal(root.get("artistId"), param.getArtistId()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (param.getState() != null) {
|
|
|
|
|
predicateList.add(builder.equal(root.get("state"), param.getState()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (param.getTimeQuery()) {
|
|
|
|
|
case 1:
|
|
|
|
|
// 今年
|
|
|
|
|
LocalDate now = LocalDate.now();
|
|
|
|
|
int currentYear = now.getYear();
|
|
|
|
|
predicateList.add(builder.equal(builder.function("YEAR", Integer.class, root.get("createTime")), currentYear));
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
// 最近一月
|
|
|
|
|
LocalDate oneMonthAgo = LocalDate.now().minusMonths(1);
|
|
|
|
|
predicateList.add(builder.greaterThanOrEqualTo(root.get("createTime"), oneMonthAgo.atStartOfDay()));
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
// 最近一周
|
|
|
|
|
LocalDate oneWeekAgo = LocalDate.now().minusWeeks(1);
|
|
|
|
|
predicateList.add(builder.greaterThanOrEqualTo(root.get("createTime"), oneWeekAgo.atStartOfDay()));
|
|
|
|
|
break;
|
|
|
|
|
case 4:
|
|
|
|
|
// 24小时内
|
|
|
|
|
LocalDate oneDayAge = LocalDate.now().minusDays(1);
|
|
|
|
|
predicateList.add(builder.greaterThanOrEqualTo(root.get("createTime"), oneDayAge.atStartOfDay()));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return builder.and(predicateList.toArray(new Predicate[predicateList.size()]));
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 查询专辑详情
|
|
|
|
|
*
|
|
|
|
|
* @param id 专辑id
|
|
|
|
|
* @return 专辑详情,和他所包含的歌曲list
|
|
|
|
|
*/
|
|
|
|
|
public ArtistAlbum getOne(String id) {
|
|
|
|
|
ArtistAlbum artistAlbum = artistAlbumDao.findById(id).get();
|
|
|
|
|
if (artistAlbum == null) {
|
|
|
|
|
throw new RuntimeException("专辑不存在,请刷新后重试");
|
|
|
|
|
}
|
|
|
|
|
List<SongInfo> songInfoList = songInfoDao.findByAlbumId(id);
|
|
|
|
|
artistAlbum.setSongList(songInfoList);
|
|
|
|
|
return artistAlbum;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发布新专辑
|
|
|
|
|
*
|
|
|
|
|
* @param token token,用来解析当前用户对象
|
|
|
|
|
* @param albumAddDTO 新增专辑对象实体类
|
|
|
|
|
*/
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void addAlbum(String token, AlbumAddDTO albumAddDTO) {
|
|
|
|
|
|
|
|
|
|
// 处理专辑对象
|
|
|
|
|
ArtistAlbum artistAlbum = new ArtistAlbum();
|
|
|
|
|
BeanUtils.copyProperties(albumAddDTO, artistAlbum);
|
|
|
|
|
artistAlbum.setId(String.valueOf(idWorker.nextId()));
|
|
|
|
|
UserLoginDto user = jwtUtil.getUserLoginDto(token);
|
|
|
|
|
if (user != null) {
|
|
|
|
|
artistAlbum.setCreateUser(user.getUserId());
|
|
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("用户校验失败,请重新登录");
|
|
|
|
|
}
|
|
|
|
|
artistAlbum.setState(AlbumStateEnum.SAVE.getCode());
|
|
|
|
|
artistAlbumDao.save(artistAlbum);
|
|
|
|
|
|
|
|
|
|
// 处理歌曲对象
|
|
|
|
|
List<AlbumSongAddDTO> songAddList = albumAddDTO.getSongAddList();
|
|
|
|
|
if (songAddList.isEmpty()) {
|
|
|
|
|
throw new RuntimeException("专辑音乐不能为空!");
|
|
|
|
|
} else {
|
|
|
|
|
for (AlbumSongAddDTO albumSongAddDTO : songAddList) {
|
|
|
|
|
addSongForAlbum(albumSongAddDTO, user, artistAlbum);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 修改专辑信息
|
|
|
|
|
*
|
|
|
|
|
* @param token token
|
|
|
|
|
* @param albumUpdateDTO 修改专辑对象
|
|
|
|
|
*/
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void updateAlbum(String token, AlbumUpdateDTO albumUpdateDTO) {
|
|
|
|
|
ArtistAlbum artistAlbum = new ArtistAlbum();
|
|
|
|
|
BeanUtils.copyProperties(albumUpdateDTO, artistAlbum);
|
|
|
|
|
|
|
|
|
|
UserLoginDto user = jwtUtil.getUserLoginDto(token);
|
|
|
|
|
if (user != null) {
|
|
|
|
|
artistAlbum.setUpdateUser(user.getUserId());
|
|
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("用户校验失败,请重新登录");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
artistAlbumDao.save(artistAlbum);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除专辑
|
|
|
|
|
*
|
|
|
|
|
* @param id 专辑id
|
|
|
|
|
*/
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void deleteAlbum(String id) {
|
|
|
|
|
ArtistAlbum artistAlbum = artistAlbumDao.findById(id).get();
|
|
|
|
|
artistAlbumDao.delete(artistAlbum);
|
|
|
|
|
|
|
|
|
|
artistAlbumSongDao.deleteByAlbumId(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除专辑-歌曲绑定关系
|
|
|
|
|
*
|
|
|
|
|
* @param id 专辑-歌曲绑定关系的id
|
|
|
|
|
*/
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void deleteAlbumSong(String id) {
|
|
|
|
|
ArtistAlbumSong artistAlbumSong = artistAlbumSongDao.findById(id).get();
|
|
|
|
|
if (artistAlbumSong.getId() == null) {
|
|
|
|
|
throw new RuntimeException("该专辑不存在此歌曲");
|
|
|
|
|
}
|
|
|
|
|
artistAlbumSongDao.delete(artistAlbumSong);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 后期编辑时为专辑新增一首歌
|
|
|
|
|
*
|
|
|
|
|
* @param token token
|
|
|
|
|
* @param id 专辑id
|
|
|
|
|
* @param albumSongAddDTO 新增歌曲对象
|
|
|
|
|
*/
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void addNewSong(String token, String id, AlbumSongAddDTO albumSongAddDTO) {
|
|
|
|
|
UserLoginDto user = jwtUtil.getUserLoginDto(token);
|
|
|
|
|
ArtistAlbum artistAlbum = artistAlbumDao.findById(id).get();
|
|
|
|
|
addSongForAlbum(albumSongAddDTO, user, artistAlbum);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 为专辑新增歌曲
|
|
|
|
|
*
|
|
|
|
|
* @param albumSongAddDTO 新增歌曲对象
|
|
|
|
|
* @param user 当前登录用户
|
|
|
|
|
* @param artistAlbum 专辑对象
|
|
|
|
|
*/
|
|
|
|
|
private void addSongForAlbum(AlbumSongAddDTO albumSongAddDTO, UserLoginDto user, ArtistAlbum artistAlbum) {
|
|
|
|
|
|
|
|
|
|
String albumName = artistAlbum.getName();
|
|
|
|
|
String artistName = artistAlbum.getArtistName();
|
|
|
|
|
|
|
|
|
|
// 保存歌曲
|
|
|
|
|
SongInfo songInfo = new SongInfo();
|
|
|
|
|
BeanUtils.copyProperties(albumSongAddDTO, songInfo);
|
|
|
|
|
songInfo.setId(String.valueOf(idWorker.nextId()));
|
|
|
|
|
songInfo.setAlbum(albumName);
|
|
|
|
|
songInfo.setState(SongInfoStateEnum.IN_USE.getCode());
|
|
|
|
|
songInfo.setArtist(artistName);
|
|
|
|
|
if (user != null) {
|
|
|
|
|
artistAlbum.setCreateUser(user.getUserId());
|
|
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("用户校验失败,请重新登录");
|
|
|
|
|
}
|
|
|
|
|
songInfoDao.save(songInfo);
|
|
|
|
|
|
|
|
|
|
String id = songInfo.getId();
|
|
|
|
|
|
|
|
|
|
// 歌词上传,处理lyricUrl字段
|
|
|
|
|
String lyric = songInfo.getLyric();
|
|
|
|
|
if (lyric != null) {
|
|
|
|
|
String lyricUrl = String.format("%s/%d%s", id, idWorker.nextId(), ".lyric");
|
|
|
|
|
String key = Constants.SONG_KEY_PREFIX + lyricUrl;
|
|
|
|
|
int upload = s3Service.uploadText(Constants.BUCKET, key, lyric);
|
|
|
|
|
songInfoDao.updateSongLyricAndUrl(id, lyric, lyricUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 曲风绑定
|
|
|
|
|
List<String> tags = albumSongAddDTO.getTags();
|
|
|
|
|
if (!tags.isEmpty()) {
|
|
|
|
|
songTagDao.deleteBySongId(id);
|
|
|
|
|
List<SongTag> songTagList = new ArrayList<>();
|
|
|
|
|
for (String item : tags) {
|
|
|
|
|
SongTag songTag = new SongTag();
|
|
|
|
|
songTag.setId(String.valueOf(idWorker.nextId()));
|
|
|
|
|
songTag.setSongId(id);
|
|
|
|
|
songTag.setTagId(item);
|
|
|
|
|
songTagList.add(songTag);
|
|
|
|
|
}
|
|
|
|
|
songTagDao.saveAll(songTagList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 专辑-音乐绑定
|
|
|
|
|
ArtistAlbumSong artistAlbumSong = ArtistAlbumSong.builder()
|
|
|
|
|
.id(String.valueOf(idWorker.nextId()))
|
|
|
|
|
.songId(id)
|
|
|
|
|
.albumId(artistAlbum.getId())
|
|
|
|
|
.build();
|
|
|
|
|
artistAlbumSongDao.save(artistAlbumSong);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|