From ed43cc3bb7f8332ad3853f64a8de9cc645cd7d22 Mon Sep 17 00:00:00 2001 From: pikaqiudeshujia Date: Wed, 3 Jul 2024 13:24:34 +0800 Subject: [PATCH] =?UTF-8?q?release-=20=E5=90=AC=E6=AD=8C=E5=9F=8B=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../luoo/comment/config/RabbitMQConfig.java | 27 ++++ .../com/luoo/comment/dao/MusicPointDao.java | 15 ++ .../comment/listener/MusicPointListener.java | 38 +++++ .../com/luoo/comment/pojo/MusicPoint.java | 70 ++++++++++ .../comment/service/MusicPointService.java | 28 ++++ .../src/main/java/dto/MusicPointDto.java | 72 ++++++++++ .../com/luoo/music/client/UserClient.java | 2 +- .../luoo/music/controller/SongController.java | 34 +++-- .../luoo/music/dao/ArtistAlbumSongDao.java | 4 + .../com/luoo/music/service/SongService.java | 131 +++++++++++++----- 10 files changed, 375 insertions(+), 46 deletions(-) create mode 100644 luoo_comment/src/main/java/com/luoo/comment/config/RabbitMQConfig.java create mode 100644 luoo_comment/src/main/java/com/luoo/comment/dao/MusicPointDao.java create mode 100644 luoo_comment/src/main/java/com/luoo/comment/listener/MusicPointListener.java create mode 100644 luoo_comment/src/main/java/com/luoo/comment/pojo/MusicPoint.java create mode 100644 luoo_comment/src/main/java/com/luoo/comment/service/MusicPointService.java create mode 100644 luoo_common/src/main/java/dto/MusicPointDto.java diff --git a/luoo_comment/src/main/java/com/luoo/comment/config/RabbitMQConfig.java b/luoo_comment/src/main/java/com/luoo/comment/config/RabbitMQConfig.java new file mode 100644 index 0000000..dc92223 --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/config/RabbitMQConfig.java @@ -0,0 +1,27 @@ +package com.luoo.comment.config; + +import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory; +import org.springframework.amqp.rabbit.connection.ConnectionFactory; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class RabbitMQConfig { + + @Bean + public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { + RabbitTemplate template = new RabbitTemplate(connectionFactory); + template.setMessageConverter(new Jackson2JsonMessageConverter()); + return template; + } + + @Bean + public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) { + SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); + factory.setConnectionFactory(connectionFactory); + factory.setMessageConverter(new Jackson2JsonMessageConverter()); + return factory; + } +} diff --git a/luoo_comment/src/main/java/com/luoo/comment/dao/MusicPointDao.java b/luoo_comment/src/main/java/com/luoo/comment/dao/MusicPointDao.java new file mode 100644 index 0000000..84598b8 --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/dao/MusicPointDao.java @@ -0,0 +1,15 @@ +package com.luoo.comment.dao; + +import com.luoo.comment.pojo.MusicPoint; +import org.springframework.data.mongodb.repository.MongoRepository; + +/** + * @Author: yawei.huang + * @Package: com.luoo.comment.dao + * @Project: luoo_parent + * @Date: 2024/7/3 9:26 + * @Filename: MusicPointDao + * @Describe: + */ +public interface MusicPointDao extends MongoRepository { +} diff --git a/luoo_comment/src/main/java/com/luoo/comment/listener/MusicPointListener.java b/luoo_comment/src/main/java/com/luoo/comment/listener/MusicPointListener.java new file mode 100644 index 0000000..f944bfc --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/listener/MusicPointListener.java @@ -0,0 +1,38 @@ +package com.luoo.comment.listener; + +import com.luoo.comment.pojo.MusicPoint; +import com.luoo.comment.service.MusicPointService; +import dto.MusicPointDto; +import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.rabbit.annotation.RabbitHandler; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + + +/** + * @Author: yawei.huang + * @Package: com.luoo.comment.listener + * @Project: luoo_parent + * @Date: 2024/7/3 9:32 + * @Filename: MusicPointListener + * @Describe: + */ +@Component +@Slf4j +@RabbitListener(queues = "musicPoint") +public class MusicPointListener { + + @Autowired + private MusicPointService musicPointService; + + @RabbitHandler + public void receive(MusicPointDto message) { + log.info("message:{}", message); + MusicPoint musicPoint = new MusicPoint(); + BeanUtils.copyProperties(message, musicPoint); + musicPointService.save(musicPoint); + } + +} diff --git a/luoo_comment/src/main/java/com/luoo/comment/pojo/MusicPoint.java b/luoo_comment/src/main/java/com/luoo/comment/pojo/MusicPoint.java new file mode 100644 index 0000000..d37bf37 --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/pojo/MusicPoint.java @@ -0,0 +1,70 @@ +package com.luoo.comment.pojo; + +import com.fasterxml.jackson.annotation.JsonFormat; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; + +import java.io.Serializable; +import java.util.Date; + +/** + * @Author: yawei.huang + * @Package: com.luoo.comment.pojo + * @Project: luoo_parent + * @Date: 2024/7/3 9:14 + * @Filename: musicPoint + * @Describe: 音乐埋点 谁-在什么时间-听了什么歌-属于哪个专辑/期刊-属于哪个音乐人-属于哪个厂牌 + */ +@Data +public class MusicPoint implements Serializable { + + @ApiModelProperty(value = "id") + private String _id; + + @ApiModelProperty(value = "用户id") + private String userId; + + @ApiModelProperty(value = "用户昵称") + private String userName; + + @ApiModelProperty(value = "歌曲id") + private String songId; + + @ApiModelProperty(value = "歌曲名称") + private String songName; + + @ApiModelProperty(value = "期刊id") + private String journalId; + + @ApiModelProperty(value = "期刊封面") + private String journalImage; + + @ApiModelProperty(value = "专辑id") + private String albumId; + + @ApiModelProperty(value = "专辑名称") + private String albumName; + + @ApiModelProperty(value = "音乐人id") + private String artistId; + + @ApiModelProperty(value = "音乐人名称") + private String artistName; + + @ApiModelProperty(value = "厂牌id") + private String bandId; + + @ApiModelProperty(value = "厂牌名称") + private String bandName; + + @ApiModelProperty(value = "发生时间") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date eventTime; + + @ApiModelProperty(value = "节点") + private String node; + + +} diff --git a/luoo_comment/src/main/java/com/luoo/comment/service/MusicPointService.java b/luoo_comment/src/main/java/com/luoo/comment/service/MusicPointService.java new file mode 100644 index 0000000..10d760b --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/service/MusicPointService.java @@ -0,0 +1,28 @@ +package com.luoo.comment.service; + +import com.luoo.comment.dao.MusicPointDao; +import com.luoo.comment.pojo.MusicPoint; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.Resource; + +/** + * @Author: yawei.huang + * @Package: com.luoo.comment.service + * @Project: luoo_parent + * @Date: 2024/7/3 9:30 + * @Filename: MusicPointService + * @Describe: + */ +@Service +public class MusicPointService { + + @Resource + private MusicPointDao musicPointDao; + + @Transactional(rollbackFor = Exception.class) + public void save(MusicPoint musicPoint) { + musicPointDao.save(musicPoint); + } +} diff --git a/luoo_common/src/main/java/dto/MusicPointDto.java b/luoo_common/src/main/java/dto/MusicPointDto.java new file mode 100644 index 0000000..5794b49 --- /dev/null +++ b/luoo_common/src/main/java/dto/MusicPointDto.java @@ -0,0 +1,72 @@ +package dto; + +import com.fasterxml.jackson.annotation.JsonFormat; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; + +import javax.validation.constraints.NotBlank; +import java.io.Serializable; +import java.util.Date; + +/** + * @Author: yawei.huang + * @Package: com.luoo.comment.pojo + * @Project: luoo_parent + * @Date: 2024/7/3 9:14 + * @Filename: musicPoint + * @Describe: 音乐埋点 谁-在什么时间-听了什么歌-属于哪个专辑/期刊-属于哪个音乐人-属于哪个厂牌 + */ +@Data +public class MusicPointDto implements Serializable { + + @ApiModelProperty(value = "id") + private String _id; + + @ApiModelProperty(value = "用户id") + private String userId; + + @ApiModelProperty(value = "用户昵称") + private String userName; + + @ApiModelProperty(value = "歌曲id") + @NotBlank + private String songId; + + @ApiModelProperty(value = "歌曲名称") + private String songName; + + @ApiModelProperty(value = "期刊id") + private String journalId; + + @ApiModelProperty(value = "期刊封面") + private String journalImage; + + @ApiModelProperty(value = "专辑id") + private String albumId; + + @ApiModelProperty(value = "专辑名称") + private String albumName; + + @ApiModelProperty(value = "音乐人id") + private String artistId; + + @ApiModelProperty(value = "音乐人名称") + private String artistName; + + @ApiModelProperty(value = "厂牌id") + private String bandId; + + @ApiModelProperty(value = "厂牌名称") + private String bandName; + + @ApiModelProperty(value = "发生时间") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date eventTime; + + @ApiModelProperty(value = "节点") + private String node; + + +} diff --git a/luoo_music/src/main/java/com/luoo/music/client/UserClient.java b/luoo_music/src/main/java/com/luoo/music/client/UserClient.java index 0b9ce0a..16eb780 100644 --- a/luoo_music/src/main/java/com/luoo/music/client/UserClient.java +++ b/luoo_music/src/main/java/com/luoo/music/client/UserClient.java @@ -53,6 +53,6 @@ public interface UserClient { * @param userId 用户id * @return 是否通过 */ - @GetMapping("/my/info/real/{userId}") + @GetMapping("/my/info/real/name/{userId}") Boolean getRealNameInfo(@PathVariable String userId); } diff --git a/luoo_music/src/main/java/com/luoo/music/controller/SongController.java b/luoo_music/src/main/java/com/luoo/music/controller/SongController.java index 443bc9f..d771682 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/SongController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/SongController.java @@ -1,37 +1,34 @@ package com.luoo.music.controller; +import annotation.GlobalInterceptor; +import annotation.VerifyParam; +import api.PageResult; +import api.Result; +import api.StatusCode; import com.luoo.music.dto.mapper.SongMapper; import com.luoo.music.dto.request.CollectQueryReq; -import com.luoo.music.dto.request.FuzzySearchReq; import com.luoo.music.dto.request.RandomTopicReq; import com.luoo.music.dto.response.RandomTopicDTO; import com.luoo.music.dto.response.SongRespDTO; import com.luoo.music.pojo.JournalSong; import com.luoo.music.service.JournalService; import com.luoo.music.service.JournalSongService; +import com.luoo.music.service.SongService; import com.luoo.music.service.UserCollectInfoService; import com.luoo.music.util.DeviceTypeUtil; - -import annotation.GlobalInterceptor; -import annotation.VerifyParam; -import api.PageResult; -import api.Result; -import api.StatusCode; import constants.Constants; +import dto.MusicPointDto; import dto.UserLoginDto; import enums.CollectTypeEnum; import enums.VerifyRegexEnum; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiImplicitParam; -import io.swagger.annotations.ApiImplicitParams; -import io.swagger.annotations.ApiOperation; -import util.JwtUtil; - +import io.swagger.annotations.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.util.CollectionUtils; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import util.JwtUtil; import java.util.*; import java.util.concurrent.TimeUnit; @@ -52,6 +49,8 @@ public class SongController { private RedisTemplate redisTemplate; @Autowired private JwtUtil jwtUtil; + @Autowired + private SongService songService; @ApiOperation(value = "1.根据期刊号查询歌曲信息", notes = "若为游客,期刊号须在最新10期内") @GetMapping("/getByJournalNo/{journalNo}") @@ -211,4 +210,13 @@ public class SongController { songRespDTO.setHaveCollect(isCollect); return Result.success(songRespDTO); } + + @ApiOperation(value = "5.用户听歌埋点处理", notes = "用户听歌埋点处理") + @PostMapping("/musicPoint") + + public Result musicPoint(@ApiParam(value = "Header中的token信息", required = true) @RequestHeader("Authorization") String token, + @RequestBody @Validated MusicPointDto musicPoint) { + songService.musicPoint(token, musicPoint); + return Result.success(); + } } diff --git a/luoo_music/src/main/java/com/luoo/music/dao/ArtistAlbumSongDao.java b/luoo_music/src/main/java/com/luoo/music/dao/ArtistAlbumSongDao.java index 5fb4e52..877afa6 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/ArtistAlbumSongDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/ArtistAlbumSongDao.java @@ -6,6 +6,8 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; +import java.util.List; + /** * @Author: yawei.huang * @Package: com.luoo.music.dao @@ -19,4 +21,6 @@ public interface ArtistAlbumSongDao extends JpaRepository findAllBySongId(String songId); } diff --git a/luoo_music/src/main/java/com/luoo/music/service/SongService.java b/luoo_music/src/main/java/com/luoo/music/service/SongService.java index 0e6a27e..b4f73e1 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/SongService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/SongService.java @@ -1,9 +1,12 @@ package com.luoo.music.service; -import com.luoo.music.dao.SongDao; -import com.luoo.music.dto.response.SongRespDTO; -import com.luoo.music.pojo.Song; - +import com.luoo.music.dao.*; +import com.luoo.music.pojo.*; +import constants.ErrorConstants; +import dto.MusicPointDto; +import dto.UserLoginDto; +import exception.BizException; +import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; @@ -12,12 +15,14 @@ import org.springframework.data.domain.PageRequest; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; 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.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Map; @@ -30,11 +35,28 @@ public class SongService { @Autowired private IdWorker idWorker; + @Autowired + private JwtUtil jwtUtil; + + @Autowired + private JournalSongDao journalSongDao; + + @Autowired + private SongInfoDao songInfoDao; + + @Autowired + private ArtistAlbumSongDao artistAlbumSongDao; + + @Autowired + private ArtistAlbumDao artistAlbumDao; + + @Autowired + private RabbitTemplate rabbitTemplate; /** * 查询全部列表 */ - public List findAll(){ + public List findAll() { return songDao.findAll(); } @@ -44,13 +66,14 @@ public class SongService { public Page findSearch(Map whereMap, int page, int size) { Specification specification = createSpecification(whereMap); - PageRequest pageRequest = PageRequest.of(page-1, size); + PageRequest pageRequest = PageRequest.of(page - 1, size); return songDao.findAll(specification, pageRequest); } /** * 条件查询 + * * @param whereMap * @return */ @@ -62,44 +85,49 @@ public class SongService { /** * 根据ID查询实体 + * * @param id * @return */ - @Cacheable(value = "song",key = "#id") + @Cacheable(value = "song", key = "#id") public Song findById(String id) { return songDao.findById(id).get(); } - /** * 增加 + * * @param song */ public void add(Song song) { - song.setId( idWorker.nextId()+"" ); + song.setId(idWorker.nextId() + ""); songDao.save(song); } /** * 修改 + * * @param song */ - @CacheEvict(value = "song",key = "#song.id") + @CacheEvict(value = "song", key = "#song.id") public void update(Song song) { songDao.save(song); } /** * 删除 + * * @param id */ - @CacheEvict(value = "song",key = "#id") + @CacheEvict(value = "song", key = "#id") public void deleteById(String id) { songDao.deleteById(id); } + /** * 动态条件构建 + * * @param searchMap * @return */ @@ -111,46 +139,85 @@ public class SongService { public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { List predicateList = new ArrayList(); // ID - if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) { - predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%")); + if (searchMap.get("id") != null && !"".equals(searchMap.get("id"))) { + predicateList.add(cb.like(root.get("id").as(String.class), "%" + (String) searchMap.get("id") + "%")); } // 专栏ID - if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) { - predicateList.add(cb.like(root.get("name").as(String.class), "%"+(String)searchMap.get("name")+"%")); + if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) { + predicateList.add(cb.like(root.get("name").as(String.class), "%" + (String) searchMap.get("name") + "%")); } // 用户ID - if (searchMap.get("artist")!=null && !"".equals(searchMap.get("artist"))) { - predicateList.add(cb.like(root.get("artist").as(String.class), "%"+(String)searchMap.get("artist")+"%")); + if (searchMap.get("artist") != null && !"".equals(searchMap.get("artist"))) { + predicateList.add(cb.like(root.get("artist").as(String.class), "%" + (String) searchMap.get("artist") + "%")); } // 剘刊号 - if (searchMap.get("volid")!=null && !"".equals(searchMap.get("volid"))) { - predicateList.add(cb.like(root.get("volid").as(String.class), "%"+(String)searchMap.get("volid")+"%")); + if (searchMap.get("volid") != null && !"".equals(searchMap.get("volid"))) { + predicateList.add(cb.like(root.get("volid").as(String.class), "%" + (String) searchMap.get("volid") + "%")); } // 标题 - if (searchMap.get("album")!=null && !"".equals(searchMap.get("album"))) { - predicateList.add(cb.like(root.get("album").as(String.class), "%"+(String)searchMap.get("album")+"%")); + if (searchMap.get("album") != null && !"".equals(searchMap.get("album"))) { + predicateList.add(cb.like(root.get("album").as(String.class), "%" + (String) searchMap.get("album") + "%")); } // 文章正文 - if (searchMap.get("url")!=null && !"".equals(searchMap.get("url"))) { - predicateList.add(cb.like(root.get("url").as(String.class), "%"+(String)searchMap.get("url")+"%")); + if (searchMap.get("url") != null && !"".equals(searchMap.get("url"))) { + predicateList.add(cb.like(root.get("url").as(String.class), "%" + (String) searchMap.get("url") + "%")); } - return cb.and( predicateList.toArray(new Predicate[predicateList.size()])); + return cb.and(predicateList.toArray(new Predicate[predicateList.size()])); } }; } - public List findByVolid(String volid){ - return songDao.findSongsByVolid(volid); + + public List findByVolid(String volid) { + return songDao.findSongsByVolid(volid); } - public List random(Integer limit) { - return songDao.random(limit); - } + public List random(Integer limit) { + return songDao.random(limit); + } - public List orderByField(List objectIds) { - return songDao.orderByField(objectIds); - } + public List orderByField(List objectIds) { + return songDao.orderByField(objectIds); + } + + /** + * 听歌埋点 + * + * @param token 登录token + * @param musicPoint 音乐埋点对象 + */ + public void musicPoint(String token, MusicPointDto musicPoint) { + UserLoginDto user = jwtUtil.getUserLoginDto(token); + if (user != null) { + musicPoint.setUserId(user.getUserId()); + } else { + // 用户校验失败,请重新登录 + throw new BizException(ErrorConstants.USER_VERIFICATION_FAILURE); + } + musicPoint.set_id(idWorker.nextId() + ""); + SongInfo songById = songInfoDao.getSongById(musicPoint.getSongId()); + musicPoint.setSongName(songById.getName()); + + List bySongId = journalSongDao.findBySongId(musicPoint.getSongId()); + if (!bySongId.isEmpty()) { + musicPoint.setJournalId(bySongId.get(0).getJournalNoSongId()); + musicPoint.setJournalImage(bySongId.get(0).getImage()); + } + + List artistAlbumSongList = artistAlbumSongDao.findAllBySongId(musicPoint.getSongId()); + if (!artistAlbumSongList.isEmpty()) { + musicPoint.setAlbumId(artistAlbumSongList.get(0).getAlbumId()); + ArtistAlbum artistAlbum = artistAlbumDao.findById(artistAlbumSongList.get(0).getAlbumId()).get(); + musicPoint.setAlbumName(artistAlbum.getName()); + musicPoint.setArtistId(artistAlbum.getArtistId()); + musicPoint.setArtistName(artistAlbum.getArtistName()); + } + + musicPoint.setEventTime(new Date()); + rabbitTemplate.convertAndSend("musicPoint",musicPoint); + + } }