RecentlyPlayingController.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package com.YuyeTech.TPlat.controller;
  2. import com.YuyeTech.TPlat.dataobject.RecentlyPlaying;
  3. import com.YuyeTech.TPlat.service.RecentlyPlayingService;
  4. import com.YuyeTech.TPlat.VO.ResultVO;
  5. import com.YuyeTech.TPlat.enums.UserEnum;
  6. import com.YuyeTech.TPlat.exception.UserException;
  7. import com.YuyeTech.TPlat.utils.ResultVOUtil;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.data.domain.PageRequest;
  11. import org.springframework.data.domain.Sort;
  12. import org.springframework.web.bind.annotation.GetMapping;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RequestParam;
  15. import org.springframework.web.bind.annotation.RestController;
  16. import java.util.*;
  17. /**
  18. * 最近在在返回接口
  19. *
  20. * @author:slambb
  21. * @date:2020/1/15
  22. */
  23. @RestController
  24. @RequestMapping("/recently_playing")
  25. @Slf4j
  26. public class RecentlyPlayingController {
  27. @Autowired
  28. private RecentlyPlayingService recentlyPlayingService;
  29. /**
  30. * 添加用户在玩列表
  31. *
  32. * @param userId
  33. * @param fObjectId 对象id,如游戏id
  34. * @param recentlyType 对象类型(0:游戏 TODO 1:视频)
  35. * @return
  36. */
  37. @GetMapping("/add")
  38. public ResultVO addRecentlyPlaying(@RequestParam(value = "userId", required = false) String userId,
  39. @RequestParam(value = "fObjectId") String fObjectId,
  40. @RequestParam(value = "recentlyType") Integer recentlyType) {
  41. //请求userId为空时候
  42. if (userId == null) {
  43. throw new UserException(UserEnum.USER_NOT_EXIST);
  44. }
  45. try {
  46. RecentlyPlaying getRecently = recentlyPlayingService.findByUserIdAndFObjectId(userId, fObjectId);
  47. // DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义格式
  48. // Date date = new Date(System.currentTimeMillis());//获取时间
  49. //
  50. // String dateString = df.format(date);//进行转换
  51. if (getRecently != null) {
  52. // tempMap.put("msg", "已收藏过");
  53. getRecently.setCreateTime(new Date());
  54. RecentlyPlaying temp = recentlyPlayingService.addRecentlyItem(getRecently);
  55. if(temp!=null){
  56. Map tempMap = new HashMap();
  57. tempMap.put("createTime", temp.getCreateTime());
  58. return ResultVOUtil.success(tempMap);
  59. }else{
  60. throw new UserException(UserEnum.RECENTLY_ADD_ERROR);
  61. }
  62. }
  63. //查询对应数据
  64. RecentlyPlaying recentlyPlayingItem = new RecentlyPlaying();
  65. recentlyPlayingItem.setUserId(userId);
  66. recentlyPlayingItem.setFObjectId(fObjectId);
  67. recentlyPlayingItem.setRecentlyType(recentlyType);
  68. recentlyPlayingItem.setCreateTime(new Date());
  69. RecentlyPlaying callbackItem = recentlyPlayingService.addRecentlyItem(recentlyPlayingItem);
  70. if(callbackItem!=null){
  71. Map tempMap = new HashMap();
  72. tempMap.put("createTime", callbackItem.getCreateTime());
  73. return ResultVOUtil.success(tempMap);
  74. }else{
  75. throw new UserException(UserEnum.RECENTLY_ADD_ERROR);
  76. }
  77. } catch (UserException e) {
  78. return ResultVOUtil.error(e.getCode(), e.getMessage());
  79. }
  80. }
  81. @GetMapping("/get")
  82. public ResultVO getRecentlyPlaying(@RequestParam(value = "userId", required = false) String userId,
  83. @RequestParam(value = "page", defaultValue = "1") Integer page,
  84. @RequestParam(value = "size", defaultValue = "10") Integer size) {
  85. //请求userId为空时候
  86. if (userId == null) {
  87. throw new UserException(UserEnum.USER_NOT_EXIST);
  88. }
  89. //查询对应数据
  90. // Map map = recentlyPlayingService.findByUserId(userId, 0 );
  91. PageRequest request = PageRequest.of(page - 1, size,Sort.by(Sort.Direction.DESC,"createTime"));
  92. Map map = recentlyPlayingService.findByUserId(userId,0,request);
  93. if(map == null){
  94. return ResultVOUtil.error(UserEnum.USER_NOT_RECENTLY.getCode(),UserEnum.USER_NOT_RECENTLY.getMessage());
  95. }
  96. return ResultVOUtil.success(map);
  97. }
  98. @GetMapping("/get_by_platform")
  99. public ResultVO getRecentlyPlayingByPlatform(@RequestParam(value = "userId", required = false) String userId,
  100. @RequestParam(value = "platform", required = false) Integer platform,
  101. @RequestParam(value = "page", defaultValue = "1") Integer page,
  102. @RequestParam(value = "size", defaultValue = "10") Integer size) {
  103. //请求userId为空时候
  104. if (userId == null) {
  105. throw new UserException(UserEnum.USER_NOT_EXIST);
  106. }
  107. //查询对应数据
  108. // Map map = recentlyPlayingService.findByUserId(userId, 0 );
  109. PageRequest request = PageRequest.of(page - 1, size,Sort.by(Sort.Direction.DESC,"createTime"));
  110. Map map = recentlyPlayingService.findByUserIdAndPlatform(userId,0,platform,request);
  111. if(map == null){
  112. return ResultVOUtil.error(UserEnum.USER_NOT_RECENTLY.getCode(),UserEnum.USER_NOT_RECENTLY.getMessage());
  113. }
  114. return ResultVOUtil.success(map);
  115. }
  116. }