CollectionController.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.domain.Collection;
  3. import com.izouma.nineth.domain.CollectionPrivilege;
  4. import com.izouma.nineth.domain.FileObject;
  5. import com.izouma.nineth.dto.*;
  6. import com.izouma.nineth.exception.BusinessException;
  7. import com.izouma.nineth.repo.CollectionPrivilegeRepo;
  8. import com.izouma.nineth.repo.CollectionRepo;
  9. import com.izouma.nineth.repo.NewsRepo;
  10. import com.izouma.nineth.service.CacheService;
  11. import com.izouma.nineth.service.CollectionService;
  12. import com.izouma.nineth.service.LikeService;
  13. import com.izouma.nineth.utils.SecurityUtils;
  14. import com.izouma.nineth.utils.excel.ExcelUtils;
  15. import io.swagger.annotations.ApiOperation;
  16. import lombok.AllArgsConstructor;
  17. import org.apache.commons.lang3.ObjectUtils;
  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. private CollectionPrivilegeRepo collectionPrivilegeRepo;
  41. @PreAuthorize("hasRole('ADMIN')")
  42. @PostMapping("/save")
  43. public Collection save(@RequestBody CollectionInfoDTO record) {
  44. Collection collection = collectionService.update(record);
  45. CollectionPrivilege collectionPrivilege = collectionPrivilegeRepo.findByCollectionId(record.getId());
  46. if (ObjectUtils.isEmpty(collectionPrivilege)) {
  47. collectionPrivilege = new CollectionPrivilege(record, record.getId());
  48. collectionPrivilege.setId(null);
  49. collectionPrivilegeRepo.save(collectionPrivilege);
  50. return collection;
  51. }
  52. collectionPrivilegeRepo.update(collectionPrivilege.getId(), record.getId(), record.getHeadBg(),
  53. record.getMaxCollection(), record.getShowroomBg(), record.isVip());
  54. return collection;
  55. }
  56. @PreAuthorize("hasRole('ADMIN')")
  57. @PostMapping("/create")
  58. public Collection create(@RequestBody CollectionInfoDTO record) {
  59. Collection collection = new Collection();
  60. BeanUtils.copyProperties(record, collection);
  61. collection = collectionService.create(collection);
  62. CollectionPrivilege collectionPrivilege = new CollectionPrivilege();
  63. BeanUtils.copyProperties(record, collectionPrivilege);
  64. collectionPrivilege.setCollectionId(collection.getId());
  65. collectionPrivilegeRepo.save(collectionPrivilege);
  66. return collection;
  67. }
  68. //@PreAuthorize("hasRole('ADMIN')")
  69. @PostMapping("/all")
  70. public Page<CollectionDTO> all(@RequestBody PageQuery pageQuery) {
  71. return collectionService.toDTO(collectionService.all(pageQuery).toPage());
  72. }
  73. @GetMapping("/get/{id}")
  74. public CollectionDTO get(@PathVariable Long id) {
  75. return collectionService.toDTO(collectionRepo.findById(id)
  76. .orElseThrow(new BusinessException("无记录")), true, true);
  77. }
  78. @GetMapping("/getInfo/{id}")
  79. public CollectionInfoDTO getInfo(@PathVariable Long id) {
  80. return collectionRepo.findInfoById(id).orElseThrow(new BusinessException("无记录"));
  81. }
  82. // @PreAuthorize("hasRole('ADMIN')")
  83. // @PostMapping("/del/{id}")
  84. // public void del(@PathVariable Long id) {
  85. // collectionRepo.softDelete(id);
  86. // }
  87. @GetMapping("/excel")
  88. @ResponseBody
  89. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  90. List<Collection> data = collectionService.all(pageQuery).getContent();
  91. ExcelUtils.export(response, data);
  92. }
  93. @GetMapping("/{id}/like")
  94. @ApiOperation("点赞")
  95. @CacheEvict("collection")
  96. public void like(@PathVariable Long id) {
  97. likeService.like(SecurityUtils.getAuthenticatedUser().getId(), id);
  98. }
  99. @GetMapping("/{id}/unlike")
  100. @ApiOperation("取消点赞")
  101. @CacheEvict("collection")
  102. public void unlike(@PathVariable Long id) {
  103. likeService.unlike(SecurityUtils.getAuthenticatedUser().getId(), id);
  104. }
  105. @GetMapping("/myLikes")
  106. @ApiOperation("我收藏的")
  107. public List<CollectionDTO> myLikes() {
  108. return collectionService.toDTO(collectionRepo.userLikes(SecurityUtils.getAuthenticatedUser().getId()));
  109. }
  110. @PreAuthorize("hasRole('ADMIN')")
  111. @PostMapping("/createBlindBox")
  112. public Collection createBlindBox(@RequestBody CreateBlindBox createBlindBox) {
  113. return collectionService.createBlindBox(createBlindBox);
  114. }
  115. @PostMapping("/appointment")
  116. public void appointment(@RequestParam Long id) {
  117. collectionService.appointment(id, SecurityUtils.getAuthenticatedUser().getId());
  118. }
  119. // @GetMapping("/recommend")
  120. // @Cacheable("recommend")
  121. public List<CollectionDTO> recommend(@RequestParam String type) {
  122. return collectionRepo.recommend(type).stream().map(rc -> {
  123. if (StringUtils.isNotBlank(rc.getRecommend().getPic())) {
  124. rc.getCollection().setPic(Collections.singletonList(new FileObject(null, rc.getRecommend()
  125. .getPic(), null, null)));
  126. }
  127. CollectionDTO collectionDTO = new CollectionDTO();
  128. BeanUtils.copyProperties(rc.getCollection(), collectionDTO);
  129. // 减少数据量
  130. collectionDTO.setDetail(null);
  131. collectionDTO.setPrivileges(null);
  132. collectionDTO.setProperties(null);
  133. return collectionDTO;
  134. }).collect(Collectors.toList());
  135. }
  136. @GetMapping("/recommend")
  137. @Cacheable("recommend")
  138. public Object recommendAll(@RequestParam(required = false) String type) {
  139. if (StringUtils.isNotEmpty(type)) {
  140. return collectionService.recommendLegacy(type);
  141. }
  142. List<RecommendDTO> recommedDTOS = new ArrayList<>();
  143. List<RecommendDTO> collectionDTOS = collectionRepo.recommend("LIST").stream().map(rc -> {
  144. if (StringUtils.isNotBlank(rc.getRecommend().getPic())) {
  145. rc.getCollection().setPic(Collections.singletonList(new FileObject(null, rc.getRecommend()
  146. .getPic(), null, null)));
  147. }
  148. CollectionDTO collectionDTO = new CollectionDTO();
  149. BeanUtils.copyProperties(rc.getCollection(), collectionDTO);
  150. // 减少数据量
  151. collectionDTO.setDetail(null);
  152. collectionDTO.setPrivileges(null);
  153. collectionDTO.setProperties(null);
  154. return new RecommendDTO(rc.getRecommend().getSort(), collectionDTO, "collection", rc.getRecommend()
  155. .getCreatedAt());
  156. }).collect(Collectors.toList());
  157. List<RecommendDTO> news = newsRepo.recommend("LIST").stream().map(rn -> {
  158. if (StringUtils.isNotBlank(rn.getRecommend().getPic())) {
  159. rn.getNews().setPic(rn.getRecommend().getPic());
  160. }
  161. return new RecommendDTO(rn.getRecommend().getSort(), rn.getNews(), "news", rn.getRecommend()
  162. .getCreatedAt());
  163. }).collect(Collectors.toList());
  164. recommedDTOS.addAll(collectionDTOS);
  165. recommedDTOS.addAll(news);
  166. recommedDTOS.sort((a, b) -> b.getSort().compareTo(a.getSort()));
  167. return recommedDTOS;
  168. }
  169. @PreAuthorize("hasRole('ADMIN')")
  170. @GetMapping("/clearRecommend")
  171. public String clearRecommend() {
  172. cacheService.clearRecommend();
  173. return "ok";
  174. }
  175. @PreAuthorize("hasAnyRole('ADMIN','COMPANY')")
  176. @PostMapping("/onShelf")
  177. public void onShelf(Long id, boolean onShelf, boolean salable) {
  178. Collection collection = collectionRepo.findById(id).orElseThrow(new BusinessException("藏品不存在"));
  179. collection.setOnShelf(onShelf);
  180. collection.setSalable(salable);
  181. collectionRepo.save(collection);
  182. }
  183. }