CollectionController.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.domain.Collection;
  3. import com.izouma.nineth.domain.FileObject;
  4. import com.izouma.nineth.dto.CollectionDTO;
  5. import com.izouma.nineth.dto.CreateBlindBox;
  6. import com.izouma.nineth.dto.PageQuery;
  7. import com.izouma.nineth.dto.RecommendDTO;
  8. import com.izouma.nineth.exception.BusinessException;
  9. import com.izouma.nineth.repo.CollectionRepo;
  10. import com.izouma.nineth.repo.NewsRepo;
  11. import com.izouma.nineth.service.CacheService;
  12. import com.izouma.nineth.service.CollectionService;
  13. import com.izouma.nineth.service.LikeService;
  14. import com.izouma.nineth.utils.SecurityUtils;
  15. import com.izouma.nineth.utils.excel.ExcelUtils;
  16. import io.swagger.annotations.ApiOperation;
  17. import lombok.AllArgsConstructor;
  18. import org.apache.commons.lang3.StringUtils;
  19. import org.springframework.beans.BeanUtils;
  20. import org.springframework.cache.annotation.CacheEvict;
  21. import org.springframework.cache.annotation.Cacheable;
  22. import org.springframework.data.domain.Page;
  23. import org.springframework.security.access.prepost.PreAuthorize;
  24. import org.springframework.web.bind.annotation.*;
  25. import javax.servlet.http.HttpServletResponse;
  26. import java.io.IOException;
  27. import java.util.ArrayList;
  28. import java.util.Collections;
  29. import java.util.List;
  30. import java.util.stream.Collectors;
  31. @RestController
  32. @RequestMapping("/collection")
  33. @AllArgsConstructor
  34. public class CollectionController extends BaseController {
  35. private CollectionService collectionService;
  36. private CollectionRepo collectionRepo;
  37. private LikeService likeService;
  38. private NewsRepo newsRepo;
  39. private CacheService cacheService;
  40. @PreAuthorize("hasRole('ADMIN')")
  41. @PostMapping("/save")
  42. public Collection save(@RequestBody Collection record) {
  43. return collectionService.update(record);
  44. }
  45. @PreAuthorize("hasRole('ADMIN')")
  46. @PostMapping("/create")
  47. public Collection create(@RequestBody Collection record) {
  48. return collectionService.cre ate(record);
  49. }
  50. //@PreAuthorize("hasRole('ADMIN')")
  51. @PostMapping("/all")
  52. public Page<CollectionDTO> all(@RequestBody PageQuery pageQuery) {
  53. return collectionService.toDTO(collectionService.all(pageQuery).toPage());
  54. }
  55. @GetMapping("/get/{id}")
  56. public CollectionDTO get(@PathVariable Long id) {
  57. return collectionService.toDTO(collectionRepo.findById(id)
  58. .orElseThrow(new BusinessException("无记录")), true, true);
  59. }
  60. // @PreAuthorize("hasRole('ADMIN')")
  61. // @PostMapping("/del/{id}")
  62. // public void del(@PathVariable Long id) {
  63. // collectionRepo.softDelete(id);
  64. // }
  65. @GetMapping("/excel")
  66. @ResponseBody
  67. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  68. List<Collection> data = collectionService.all(pageQuery).getContent();
  69. ExcelUtils.export(response, data);
  70. }
  71. @GetMapping("/{id}/like")
  72. @ApiOperation("点赞")
  73. @CacheEvict("collection")
  74. public void like(@PathVariable Long id) {
  75. likeService.like(SecurityUtils.getAuthenticatedUser().getId(), id);
  76. }
  77. @GetMapping("/{id}/unlike")
  78. @ApiOperation("取消点赞")
  79. @CacheEvict("collection")
  80. public void unlike(@PathVariable Long id) {
  81. likeService.unlike(SecurityUtils.getAuthenticatedUser().getId(), id);
  82. }
  83. @GetMapping("/myLikes")
  84. @ApiOperation("我收藏的")
  85. public List<CollectionDTO> myLikes() {
  86. return collectionService.toDTO(collectionRepo.userLikes(SecurityUtils.getAuthenticatedUser().getId()));
  87. }
  88. @PreAuthorize("hasRole('ADMIN')")
  89. @PostMapping("/createBlindBox")
  90. public Collection createBlindBox(@RequestBody CreateBlindBox createBlindBox) {
  91. return collectionService.createBlindBox(createBlindBox);
  92. }
  93. @PostMapping("/appointment")
  94. public void appointment(@RequestParam Long id) {
  95. collectionService.appointment(id, SecurityUtils.getAuthenticatedUser().getId());
  96. }
  97. // @GetMapping("/recommend")
  98. // @Cacheable("recommend")
  99. public List<CollectionDTO> recommend(@RequestParam String type) {
  100. return collectionRepo.recommend(type).stream().map(rc -> {
  101. if (StringUtils.isNotBlank(rc.getRecommend().getPic())) {
  102. rc.getCollection().setPic(Collections.singletonList(new FileObject(null, rc.getRecommend()
  103. .getPic(), null, null)));
  104. }
  105. CollectionDTO collectionDTO = new CollectionDTO();
  106. BeanUtils.copyProperties(rc.getCollection(), collectionDTO);
  107. // 减少数据量
  108. collectionDTO.setDetail(null);
  109. collectionDTO.setPrivileges(null);
  110. collectionDTO.setProperties(null);
  111. return collectionDTO;
  112. }).collect(Collectors.toList());
  113. }
  114. @GetMapping("/recommend")
  115. @Cacheable("recommend")
  116. public Object recommendAll(@RequestParam(required = false) String type) {
  117. if (StringUtils.isNotEmpty(type)) {
  118. return collectionService.recommendLegacy(type);
  119. }
  120. List<RecommendDTO> recommedDTOS = new ArrayList<>();
  121. List<RecommendDTO> collectionDTOS = collectionRepo.recommend("LIST").stream().map(rc -> {
  122. if (StringUtils.isNotBlank(rc.getRecommend().getPic())) {
  123. rc.getCollection().setPic(Collections.singletonList(new FileObject(null, rc.getRecommend()
  124. .getPic(), null, null)));
  125. }
  126. CollectionDTO collectionDTO = new CollectionDTO();
  127. BeanUtils.copyProperties(rc.getCollection(), collectionDTO);
  128. // 减少数据量
  129. collectionDTO.setDetail(null);
  130. collectionDTO.setPrivileges(null);
  131. collectionDTO.setProperties(null);
  132. return new RecommendDTO(rc.getRecommend().getSort(), collectionDTO, "collection");
  133. }).collect(Collectors.toList());
  134. List<RecommendDTO> news = newsRepo.recommend("LIST").stream().map(rn -> {
  135. if (StringUtils.isNotBlank(rn.getRecommend().getPic())) {
  136. rn.getNews().setPic(rn.getRecommend().getPic());
  137. }
  138. return new RecommendDTO(rn.getRecommend().getSort(), rn.getNews(), "news");
  139. }).collect(Collectors.toList());
  140. recommedDTOS.addAll(collectionDTOS);
  141. recommedDTOS.addAll(news);
  142. recommedDTOS.sort((a, b) -> b.getSort().compareTo(a.getSort()));
  143. return recommedDTOS;
  144. }
  145. @PreAuthorize("hasRole('ADMIN')")
  146. @GetMapping("/clearRecommend")
  147. public String clearRecommend() {
  148. cacheService.clearRecommend();
  149. return "ok";
  150. }
  151. }