CollectionController.java 11 KB

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