ShowroomController.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package com.izouma.nineth.web;
  2. import cn.hutool.core.collection.CollUtil;
  3. import com.izouma.nineth.domain.NewsLike;
  4. import com.izouma.nineth.domain.ShowCollection;
  5. import com.izouma.nineth.domain.Showroom;
  6. import com.izouma.nineth.domain.User;
  7. import com.izouma.nineth.dto.PageQuery;
  8. import com.izouma.nineth.enums.AuthStatus;
  9. import com.izouma.nineth.exception.BusinessException;
  10. import com.izouma.nineth.repo.NewsLikeRepo;
  11. import com.izouma.nineth.repo.ShowCollectionRepo;
  12. import com.izouma.nineth.repo.ShowroomRepo;
  13. import com.izouma.nineth.service.ShowroomService;
  14. import com.izouma.nineth.utils.SecurityUtils;
  15. import com.izouma.nineth.utils.excel.ExcelUtils;
  16. import lombok.AllArgsConstructor;
  17. import org.apache.commons.lang3.ObjectUtils;
  18. import org.springframework.data.domain.Page;
  19. import org.springframework.security.access.prepost.PreAuthorize;
  20. import org.springframework.web.bind.annotation.*;
  21. import javax.servlet.http.HttpServletResponse;
  22. import java.io.IOException;
  23. import java.util.Comparator;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.stream.Collectors;
  28. @RestController
  29. @RequestMapping("/showroom")
  30. @AllArgsConstructor
  31. public class ShowroomController extends BaseController {
  32. private ShowroomService showroomService;
  33. private ShowroomRepo showroomRepo;
  34. private ShowCollectionRepo showCollectionRepo;
  35. private NewsLikeRepo newsLikeRepo;
  36. @PostMapping("/save")
  37. public Showroom save(@RequestBody Showroom record) {
  38. return showroomService.save(SecurityUtils.getAuthenticatedUser().getId(), record);
  39. }
  40. @PostMapping("/update")
  41. public Showroom update(@RequestBody Showroom record) {
  42. return showroomService.update(SecurityUtils.getAuthenticatedUser().getId(), record);
  43. }
  44. @PostMapping("/create")
  45. public Showroom create(@RequestParam Long userId) {
  46. return showroomService.save(userId);
  47. }
  48. @PostMapping("/all")
  49. public Page<Showroom> all(@RequestBody PageQuery pageQuery) {
  50. Page<Showroom> all = showroomService.all(pageQuery);
  51. List<Long> ids = all.map(Showroom::getId).getContent();
  52. Map<Long, List<ShowCollection>> collect = showCollectionRepo.findAllByShowroomIdIn(ids)
  53. .stream()
  54. .collect(Collectors.groupingBy(ShowCollection::getShowroomId));
  55. showroomService.all(pageQuery);
  56. User user = SecurityUtils.getAuthenticatedUser();
  57. Map<Long, Long> likesMap = new HashMap<>();
  58. if (user != null && !user.isAdmin() && CollUtil.isNotEmpty(ids)) {
  59. List<NewsLike> likes = newsLikeRepo.findByUserIdAndShowroomIdIn(user
  60. .getId(), ids);
  61. likesMap = likes.stream()
  62. .collect(Collectors.groupingBy(NewsLike::getShowroomId, Collectors.counting()));
  63. }
  64. Map<Long, Long> finalLikesMap = likesMap;
  65. return all.map(showroom -> {
  66. List<ShowCollection> collections = collect.get(showroom.getId());
  67. if (CollUtil.isNotEmpty(collections)) {
  68. collections.sort(Comparator.comparingInt(ShowCollection::getSort));
  69. showroom.setCollections(collections);
  70. }
  71. showroom.setLiked(ObjectUtils.isNotEmpty(finalLikesMap.get(showroom.getId())));
  72. return showroom;
  73. });
  74. }
  75. @GetMapping("/get/{id}")
  76. public Showroom get(@PathVariable Long id) {
  77. Showroom showroom = showroomRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  78. showroom.setCollections(showCollectionRepo.findAllByShowroomIdOrderBySort(id));
  79. User user = SecurityUtils.getAuthenticatedUser();
  80. if (user != null && !user.isAdmin()) {
  81. List<NewsLike> likes = newsLikeRepo.findByUserIdAndShowroomId(user
  82. .getId(), id);
  83. showroom.setLiked(CollUtil.isNotEmpty(likes));
  84. }
  85. return showroom;
  86. }
  87. @PreAuthorize("hasRole('ADMIN')")
  88. @PostMapping("/del/{id}")
  89. public void del(@PathVariable Long id) {
  90. showCollectionRepo.deleteAllByShowroomId(id);
  91. showroomRepo.softDelete(id);
  92. }
  93. @GetMapping("/excel")
  94. @ResponseBody
  95. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  96. List<Showroom> data = all(pageQuery).getContent();
  97. ExcelUtils.export(response, data);
  98. }
  99. @GetMapping("/{id}/share")
  100. public void addShare(@PathVariable Long id) {
  101. showroomRepo.addShare(id, 1);
  102. }
  103. @PostMapping("/pass")
  104. public void pass(@RequestParam Long id) {
  105. showroomService.audit(id, AuthStatus.SUCCESS, null);
  106. }
  107. @PostMapping("/deny")
  108. public void deny(@RequestParam Long id, String reason) {
  109. showroomService.audit(id, AuthStatus.FAIL, reason);
  110. }
  111. }