CollectionController.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package com.izouma.nineth.web;
  2. import com.fasterxml.jackson.annotation.JsonView;
  3. import com.izouma.nineth.domain.Collection;
  4. import com.izouma.nineth.domain.CollectionPrivilege;
  5. import com.izouma.nineth.domain.FileObject;
  6. import com.izouma.nineth.dto.*;
  7. import com.izouma.nineth.exception.BusinessException;
  8. import com.izouma.nineth.repo.CollectionPrivilegeRepo;
  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.ObjectUtils;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.springframework.beans.BeanUtils;
  21. import org.springframework.cache.annotation.CacheEvict;
  22. import org.springframework.cache.annotation.Cacheable;
  23. import org.springframework.data.domain.Page;
  24. import org.springframework.data.domain.Pageable;
  25. import org.springframework.security.access.prepost.PreAuthorize;
  26. import org.springframework.web.bind.annotation.*;
  27. import javax.servlet.http.HttpServletResponse;
  28. import java.io.IOException;
  29. import java.util.*;
  30. import java.util.concurrent.ExecutionException;
  31. import java.util.stream.Collectors;
  32. @RestController
  33. @RequestMapping("/collection")
  34. @AllArgsConstructor
  35. public class CollectionController extends BaseController {
  36. private CollectionService collectionService;
  37. private CollectionRepo collectionRepo;
  38. private LikeService likeService;
  39. private NewsRepo newsRepo;
  40. private CacheService cacheService;
  41. private CollectionPrivilegeRepo collectionPrivilegeRepo;
  42. @PreAuthorize("hasAnyRole('ADMIN','SAAS')")
  43. @PostMapping("/save")
  44. public Collection save(@RequestBody CollectionInfoDTO record) {
  45. Collection collection = collectionService.update(record);
  46. CollectionPrivilege collectionPrivilege = collectionPrivilegeRepo.findByCollectionId(record.getId());
  47. if (ObjectUtils.isEmpty(collectionPrivilege)) {
  48. collectionPrivilege = new CollectionPrivilege(record, record.getId());
  49. collectionPrivilege.setId(null);
  50. collectionPrivilegeRepo.save(collectionPrivilege);
  51. return collection;
  52. }
  53. collectionPrivilegeRepo.update(collectionPrivilege.getId(), record.getId(), record.getHeadBg(),
  54. record.getMaxCollection(), record.getShowroomBg(), record.isVip());
  55. return collection;
  56. }
  57. @PreAuthorize("hasAnyRole('ADMIN','SAAS')")
  58. @PostMapping("/create")
  59. public Collection create(@RequestBody CollectionInfoDTO record) {
  60. Collection collection = new Collection();
  61. BeanUtils.copyProperties(record, collection);
  62. collection = collectionService.create(collection);
  63. CollectionPrivilege collectionPrivilege = new CollectionPrivilege();
  64. BeanUtils.copyProperties(record, collectionPrivilege);
  65. collectionPrivilege.setCollectionId(collection.getId());
  66. collectionPrivilegeRepo.save(collectionPrivilege);
  67. return collection;
  68. }
  69. //@PreAuthorize("hasRole('ADMIN')")
  70. @PostMapping("/all")
  71. @JsonView(Collection.View.Basic.class)
  72. public Page<Collection> all(@RequestBody PageQuery pageQuery) {
  73. pageQuery.getQuery().putIfAbsent("companyId", 1);
  74. Page<Collection> page = collectionService.all(pageQuery).toPage();
  75. collectionService.queryUserDetail(page.getContent());
  76. return page;
  77. }
  78. @GetMapping("/get/{id}")
  79. public Collection get(@PathVariable Long id) {
  80. return collectionService.queryUserDetail(collectionRepo.findById(id)
  81. .orElseThrow(new BusinessException("无记录")), true, true);
  82. }
  83. @GetMapping("/getInfo/{id}")
  84. public CollectionInfoDTO getInfo(@PathVariable Long id) {
  85. return collectionRepo.findInfoById(id).orElseThrow(new BusinessException("无记录"));
  86. }
  87. // @PreAuthorize("hasRole('ADMIN')")
  88. // @PostMapping("/del/{id}")
  89. // public void del(@PathVariable Long id) {
  90. // collectionRepo.softDelete(id);
  91. // }
  92. @GetMapping("/excel")
  93. @ResponseBody
  94. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  95. List<Collection> data = collectionService.all(pageQuery).getContent();
  96. ExcelUtils.export(response, data);
  97. }
  98. @GetMapping("/{id}/like")
  99. @ApiOperation("点赞")
  100. @CacheEvict("collection")
  101. public void like(@PathVariable Long id) {
  102. likeService.like(SecurityUtils.getAuthenticatedUser().getId(), id);
  103. }
  104. @GetMapping("/{id}/unlike")
  105. @ApiOperation("取消点赞")
  106. @CacheEvict("collection")
  107. public void unlike(@PathVariable Long id) {
  108. likeService.unlike(SecurityUtils.getAuthenticatedUser().getId(), id);
  109. }
  110. @GetMapping("/myLikes")
  111. @ApiOperation("我收藏的")
  112. public List<CollectionDTO> myLikes(@RequestParam(defaultValue = "1") Long companyId) {
  113. return collectionService.toDTO(collectionRepo.userLikes(SecurityUtils.getAuthenticatedUser().getId(), companyId));
  114. }
  115. @PreAuthorize("hasAnyRole('ADMIN','SAAS')")
  116. @PostMapping("/createBlindBox")
  117. public Collection createBlindBox(@RequestBody CreateBlindBox createBlindBox) throws Exception {
  118. return collectionService.createBlindBox(createBlindBox);
  119. }
  120. @PostMapping("/appointment")
  121. public void appointment(@RequestParam Long id) {
  122. collectionService.appointment(id, SecurityUtils.getAuthenticatedUser().getId());
  123. }
  124. @GetMapping("/recommend")
  125. @Cacheable("recommend")
  126. public Object recommendAll(@RequestParam(required = false) String type, @RequestParam(defaultValue = "1") Long companyId) {
  127. if (StringUtils.isNotEmpty(type)) {
  128. return collectionService.recommendLegacy(type, companyId);
  129. }
  130. List<RecommendDTO> recommedDTOS = new ArrayList<>();
  131. List<RecommendDTO> collectionDTOS = collectionRepo.recommend("LIST", companyId).stream().map(rc -> {
  132. if (StringUtils.isNotBlank(rc.getRecommend().getPic())) {
  133. rc.getCollection().setPic(Collections.singletonList(new FileObject(null, rc.getRecommend()
  134. .getPic(), null, null)));
  135. }
  136. CollectionDTO collectionDTO = new CollectionDTO();
  137. BeanUtils.copyProperties(rc.getCollection(), collectionDTO);
  138. // 减少数据量
  139. collectionDTO.setDetail(null);
  140. collectionDTO.setPrivileges(null);
  141. collectionDTO.setProperties(null);
  142. return new RecommendDTO(rc.getRecommend().getSort(), collectionDTO, "collection", rc.getRecommend()
  143. .getCreatedAt());
  144. }).collect(Collectors.toList());
  145. List<RecommendDTO> news = newsRepo.recommend("LIST", companyId).stream().map(rn -> {
  146. if (StringUtils.isNotBlank(rn.getRecommend().getPic())) {
  147. rn.getNews().setPic(rn.getRecommend().getPic());
  148. }
  149. return new RecommendDTO(rn.getRecommend().getSort(), rn.getNews(), "news", rn.getRecommend()
  150. .getCreatedAt());
  151. }).collect(Collectors.toList());
  152. recommedDTOS.addAll(collectionDTOS);
  153. recommedDTOS.addAll(news);
  154. recommedDTOS.sort((a, b) -> b.getSort().compareTo(a.getSort()));
  155. return recommedDTOS;
  156. }
  157. @PreAuthorize("hasRole('ADMIN')")
  158. @GetMapping("/clearRecommend")
  159. public String clearRecommend() {
  160. cacheService.clearRecommend();
  161. return "ok";
  162. }
  163. @PreAuthorize("hasAnyRole('ADMIN','COMPANY')")
  164. @PostMapping("/onShelf")
  165. public void onShelf(Long id, boolean onShelf, boolean salable) {
  166. Collection collection = collectionRepo.findById(id).orElseThrow(new BusinessException("藏品不存在"));
  167. collection.setOnShelf(onShelf);
  168. collection.setSalable(salable);
  169. collectionRepo.save(collection);
  170. }
  171. @GetMapping("/byTag")
  172. @JsonView(Collection.View.Basic.class)
  173. public Page<Collection> byTag(@RequestParam Long tagId,
  174. @RequestParam(required = false, defaultValue = "0") List<Long> excludeUserId,
  175. Pageable pageable) {
  176. return collectionService.byTag(tagId, excludeUserId, pageable);
  177. }
  178. @PreAuthorize("hasAnyRole('ADMIN','COMPANY')")
  179. @PostMapping("/onShelfOasis")
  180. public List<Collection> onShelfOasis(@RequestBody List<Long> oasisIds) {
  181. return collectionService.setOasisOnShelf(oasisIds);
  182. }
  183. @PreAuthorize("hasAnyRole('ADMIN','COMPANY')")
  184. @PostMapping("/setScancodeOasis")
  185. public List<Collection> setScancodeOasis(@RequestBody List<Long> oasisIds) {
  186. return collectionService.setOasisScancode(oasisIds);
  187. }
  188. @GetMapping("/count/{search}")
  189. public Map<String, String> countNum(@PathVariable String search) {
  190. PageQuery pageQuery = new PageQuery();
  191. pageQuery.setPage(0);
  192. pageQuery.setSize(1);
  193. pageQuery.getQuery().put("onShelf", true);
  194. pageQuery.getQuery().put("source", "TRANSFER");
  195. pageQuery.getQuery().put("del", false);
  196. pageQuery.setSearch(search);
  197. pageQuery.getQuery().put("salable", false);
  198. long onlyShowNum = collectionService.all(pageQuery).getTotal();
  199. pageQuery.getQuery().put("salable", true);
  200. long consignmentNum = collectionService.all(pageQuery).getTotal();
  201. pageQuery.getQuery().remove("salable");
  202. pageQuery.getQuery().put("inPaying", true);
  203. long transactingNum = collectionService.all(pageQuery).getTotal();
  204. Map<String, String> map = new HashMap<>();
  205. map.put("onlyShowNum", String.valueOf(onlyShowNum));
  206. map.put("consignmentNum", String.valueOf(consignmentNum));
  207. map.put("transactingNum", String.valueOf(transactingNum));
  208. return map;
  209. }
  210. }