ShowroomController.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package com.izouma.nineth.web;
  2. import cn.hutool.core.collection.CollUtil;
  3. import com.izouma.nineth.domain.Collection;
  4. import com.izouma.nineth.domain.*;
  5. import com.izouma.nineth.dto.PageQuery;
  6. import com.izouma.nineth.enums.AuthStatus;
  7. import com.izouma.nineth.enums.HeatType;
  8. import com.izouma.nineth.exception.BusinessException;
  9. import com.izouma.nineth.repo.*;
  10. import com.izouma.nineth.service.ShowroomService;
  11. import com.izouma.nineth.service.SysConfigService;
  12. import com.izouma.nineth.utils.SecurityUtils;
  13. import com.izouma.nineth.utils.excel.ExcelUtils;
  14. import lombok.AllArgsConstructor;
  15. import org.apache.commons.lang3.ObjectUtils;
  16. import org.springframework.data.domain.Page;
  17. import org.springframework.security.access.prepost.PreAuthorize;
  18. import org.springframework.web.bind.annotation.*;
  19. import javax.servlet.http.HttpServletResponse;
  20. import java.io.IOException;
  21. import java.util.*;
  22. import java.util.stream.Collectors;
  23. @RestController
  24. @RequestMapping("/showroom")
  25. @AllArgsConstructor
  26. public class ShowroomController extends BaseController {
  27. private ShowroomService showroomService;
  28. private ShowroomRepo showroomRepo;
  29. private ShowCollectionRepo showCollectionRepo;
  30. private NewsLikeRepo newsLikeRepo;
  31. private CollectionRepo collectionRepo;
  32. private HeatInfoRepo heatInfoRepo;
  33. private SysConfigService sysConfigService;
  34. @PostMapping("/save")
  35. public Showroom save(@RequestBody Showroom record) {
  36. return showroomService.save(SecurityUtils.getAuthenticatedUser().getId(), record);
  37. }
  38. @PostMapping("/update")
  39. public Showroom update(@RequestBody Showroom record) {
  40. return showroomService.update(SecurityUtils.getAuthenticatedUser().getId(), record);
  41. }
  42. @PostMapping("/create")
  43. public Showroom create(@RequestParam Long userId, @RequestParam String type) {
  44. return showroomService.save(userId, type);
  45. }
  46. @PostMapping("/all")
  47. public Page<Showroom> all(@RequestBody PageQuery pageQuery) {
  48. Page<Showroom> all = showroomService.all(pageQuery);
  49. List<Long> ids = all.map(Showroom::getId).getContent();
  50. Map<Long, List<ShowCollection>> collect = showCollectionRepo.findAllByShowroomIdIn(ids)
  51. .stream()
  52. .collect(Collectors.groupingBy(ShowCollection::getShowroomId));
  53. showroomService.all(pageQuery);
  54. User user = SecurityUtils.getAuthenticatedUser();
  55. Map<Long, Long> likesMap = new HashMap<>();
  56. if (user != null && !user.isAdmin() && CollUtil.isNotEmpty(ids)) {
  57. List<NewsLike> likes = newsLikeRepo.findByUserIdAndShowroomIdIn(user
  58. .getId(), ids);
  59. likesMap = likes.stream()
  60. .collect(Collectors.groupingBy(NewsLike::getShowroomId, Collectors.counting()));
  61. }
  62. Map<Long, Long> finalLikesMap = likesMap;
  63. return all.map(showroom -> {
  64. List<ShowCollection> collections = collect.get(showroom.getId());
  65. List<ShowCollection> neo = new ArrayList<>();
  66. if (collections != null) {
  67. collections.forEach(orig -> {
  68. Collection collection = collectionRepo.findById(orig.getCollectionId())
  69. .orElseThrow(new BusinessException("暂无"));
  70. orig.setStatus(showroomService.getStatus(collection));
  71. orig.setPrice(collection.getPrice());
  72. neo.add(orig);
  73. });
  74. }
  75. if (CollUtil.isNotEmpty(neo)) {
  76. neo.sort(Comparator.comparingInt(ShowCollection::getSort));
  77. showroom.setCollections(neo);
  78. }
  79. showroom.setLiked(ObjectUtils.isNotEmpty(finalLikesMap.get(showroom.getId())));
  80. return showroom;
  81. });
  82. }
  83. @GetMapping("/get/{id}")
  84. public Showroom get(@PathVariable Long id) {
  85. Showroom showroom = showroomRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  86. List<ShowCollection> origin = showCollectionRepo.findAllByShowroomIdOrderBySort(id);
  87. List<ShowCollection> neo = new ArrayList<>();
  88. if (origin != null) {
  89. origin.forEach(orig -> collectionRepo.findById(orig.getCollectionId())
  90. .ifPresent(collection -> {
  91. orig.setStatus(showroomService.getStatus(collection));
  92. orig.setPrice(collection.getPrice());
  93. neo.add(orig);
  94. }));
  95. }
  96. showroom.setCollections(neo);
  97. User user = SecurityUtils.getAuthenticatedUser();
  98. if (user != null && !user.isAdmin()) {
  99. List<NewsLike> likes = newsLikeRepo.findByUserIdAndShowroomId(user
  100. .getId(), id);
  101. showroom.setLiked(CollUtil.isNotEmpty(likes));
  102. }
  103. return showroom;
  104. }
  105. @PreAuthorize("hasRole('ADMIN')")
  106. @PostMapping("/del/{id}")
  107. public void del(@PathVariable Long id) {
  108. showCollectionRepo.deleteAllByShowroomId(id);
  109. showroomRepo.softDelete(id);
  110. }
  111. @GetMapping("/excel")
  112. @ResponseBody
  113. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  114. List<Showroom> data = all(pageQuery).getContent();
  115. ExcelUtils.export(response, data);
  116. }
  117. @GetMapping("/{id}/share")
  118. public void addShare(@PathVariable Long id) {
  119. showroomRepo.addShare(id, 1);
  120. }
  121. //加浏览
  122. @GetMapping("/{id}/view")
  123. public void addView(@PathVariable Long id) {
  124. //增加浏览量
  125. Long userId = SecurityUtils.getAuthenticatedUser().getId();
  126. if (ObjectUtils.isEmpty(userId)) {
  127. return;
  128. }
  129. List<HeatInfo> list = heatInfoRepo.findByUserIdAndShowroomIdAndType(userId, id, HeatType.VIEW);
  130. if (!list.isEmpty()) return;
  131. int weight = sysConfigService.getInt("heat_view_weight");
  132. heatInfoRepo.save(HeatInfo.builder()
  133. .userId(userId)
  134. .showroomId(id)
  135. .type(HeatType.VIEW)
  136. .value(weight)
  137. .build());
  138. showroomRepo.addHeat(id, weight);
  139. }
  140. @PostMapping("/pass")
  141. public void pass(@RequestParam Long id) {
  142. showroomService.audit(id, AuthStatus.SUCCESS, null);
  143. }
  144. @PostMapping("/deny")
  145. public void deny(@RequestParam Long id, String reason) {
  146. showroomService.audit(id, AuthStatus.FAIL, reason);
  147. }
  148. }