FavoritesController.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package com.YuyeTech.TPlat.controller;
  2. import com.YuyeTech.TPlat.dataobject.Favorites;
  3. import com.YuyeTech.TPlat.service.FavoritesService;
  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.transaction.annotation.Transactional;
  13. import org.springframework.web.bind.annotation.GetMapping;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RequestParam;
  16. import org.springframework.web.bind.annotation.RestController;
  17. import java.util.Date;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. /**
  21. * @author:slambb
  22. * @date:2020/1/14
  23. */
  24. @RestController
  25. @RequestMapping("/favorites")
  26. @Slf4j
  27. public class FavoritesController {
  28. @Autowired
  29. private FavoritesService favoritesService;
  30. @GetMapping("/get")
  31. public ResultVO getFavorites(@RequestParam(value = "userId", required = false) String userId,
  32. @RequestParam(value = "page", defaultValue = "1") Integer page,
  33. @RequestParam(value = "size", defaultValue = "10") Integer size) {
  34. //请求userId为空时候
  35. if (userId == null) {
  36. throw new UserException(UserEnum.USER_NOT_EXIST);
  37. }
  38. //查询对应数据
  39. PageRequest request = PageRequest.of(page - 1, size, Sort.by(Sort.Direction.DESC, "createTime"));
  40. Map map = favoritesService.findByUserId(userId, 0, request);
  41. if (map == null) {
  42. return ResultVOUtil.error(UserEnum.USER_NOT_FAVORITES.getCode(), UserEnum.USER_NOT_FAVORITES.getMessage());
  43. }
  44. return ResultVOUtil.success(map);
  45. }
  46. @GetMapping("/get_by_platform")
  47. public ResultVO getFavoritesByPlatform(@RequestParam(value = "userId", required = false) String userId,
  48. @RequestParam(value = "platform", required = false) Integer platform,
  49. @RequestParam(value = "page", defaultValue = "1") Integer page,
  50. @RequestParam(value = "size", defaultValue = "10") Integer size) {
  51. //请求userId为空时候
  52. if (userId == null) {
  53. throw new UserException(UserEnum.USER_NOT_EXIST);
  54. }
  55. //查询对应数据
  56. PageRequest request = PageRequest.of(page - 1, size, Sort.by(Sort.Direction.DESC, "createTime"));
  57. Map map = favoritesService.findByUserIdAndPlatform(userId, 0,platform, request);
  58. if (map == null) {
  59. return ResultVOUtil.error(UserEnum.USER_NOT_FAVORITES.getCode(), UserEnum.USER_NOT_FAVORITES.getMessage());
  60. }
  61. return ResultVOUtil.success(map);
  62. }
  63. /**
  64. * 收藏对象
  65. *
  66. * @param userId
  67. * @param fObjectId 收藏对象id
  68. * @param favoritesType 收藏类型(0 : 是游戏,)
  69. * @return
  70. */
  71. @GetMapping("/add")
  72. public ResultVO addFavorites(@RequestParam(value = "userId", required = false) String userId,
  73. @RequestParam(value = "fObjectId") String fObjectId,
  74. @RequestParam(value = "favoritesType") Integer favoritesType) {
  75. //请求userId为空时候
  76. if (userId == null) {
  77. throw new UserException(UserEnum.USER_NOT_EXIST);
  78. }
  79. try {
  80. boolean bHasFavoriteItem = favoritesService.findByUserIdAndFObjectId(userId, fObjectId);
  81. if (bHasFavoriteItem) {
  82. Map tempMap = new HashMap();
  83. tempMap.put("bHas", true);
  84. tempMap.put("msg", "已收藏过");
  85. return ResultVOUtil.success(tempMap);
  86. }
  87. //查询对应数据
  88. Favorites favorites = new Favorites();
  89. favorites.setUserId(userId);
  90. favorites.setFObjectId(fObjectId);
  91. favorites.setFavoritesType(favoritesType);
  92. favorites.setCreateTime(new Date());
  93. //添加后返回值 Favorites tempFavorite =
  94. favoritesService.addFavorite(favorites);
  95. Map tempMap = new HashMap();
  96. tempMap.put("bHas", false);
  97. tempMap.put("msg", "新收藏");
  98. // tempMap.put("favorite",tempFavorite);
  99. return ResultVOUtil.success(tempMap);
  100. } catch (UserException e) {
  101. return ResultVOUtil.error(e.getCode(), e.getMessage());
  102. }
  103. }
  104. @GetMapping("/modify")
  105. @Transactional
  106. public ResultVO modifyFavorites(@RequestParam(value = "userId", required = false) String userId,
  107. @RequestParam(value = "fObjectId") String fObjectId,
  108. @RequestParam(value = "favoritesType") Integer favoritesType) {
  109. try {
  110. boolean bHasFavoriteItem = favoritesService.findByUserIdAndFObjectId(userId, fObjectId);
  111. //如果有数据
  112. if (bHasFavoriteItem) {
  113. String temp = favoritesService.delectFavorite(userId, fObjectId);
  114. Map tempMap = new HashMap();
  115. tempMap.put("bHas", false);
  116. tempMap.put("msg", "已删除");
  117. return ResultVOUtil.success(tempMap);
  118. }
  119. //没有数据,创建新的对象,写入数据库
  120. //查询对应数据
  121. Favorites favorites = new Favorites();
  122. favorites.setUserId(userId);
  123. favorites.setFObjectId(fObjectId);
  124. favorites.setFavoritesType(favoritesType);
  125. favorites.setCreateTime(new Date());
  126. //添加后返回值 Favorites tempFavorite =
  127. favoritesService.addFavorite(favorites);
  128. Map tempMap = new HashMap();
  129. tempMap.put("bHas", true);
  130. tempMap.put("msg", "新收藏");
  131. // tempMap.put("favorite",tempFavorite);
  132. return ResultVOUtil.success(tempMap);
  133. } catch (UserException e) {
  134. return ResultVOUtil.error(e.getCode(), e.getMessage());
  135. }
  136. }
  137. }