ShowroomController.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. return showroomService.show(pageQuery);
  49. }
  50. @GetMapping("/get/{id}")
  51. public Showroom get(@PathVariable Long id) {
  52. Showroom showroom = showroomRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  53. List<ShowCollection> origin = showCollectionRepo.findAllByShowroomIdOrderBySort(id);
  54. List<ShowCollection> neo = new ArrayList<>();
  55. if (origin != null) {
  56. origin.forEach(orig -> collectionRepo.findById(orig.getCollectionId())
  57. .ifPresent(collection -> {
  58. orig.setStatus(showroomService.getStatus(collection));
  59. orig.setPrice(collection.getPrice());
  60. neo.add(orig);
  61. }));
  62. }
  63. showroom.setCollections(neo);
  64. User user = SecurityUtils.getAuthenticatedUser();
  65. if (user != null && !user.isAdmin()) {
  66. List<NewsLike> likes = newsLikeRepo.findByUserIdAndShowroomId(user
  67. .getId(), id);
  68. showroom.setLiked(CollUtil.isNotEmpty(likes));
  69. }
  70. return showroom;
  71. }
  72. @PreAuthorize("hasRole('ADMIN')")
  73. @PostMapping("/del/{id}")
  74. public void del(@PathVariable Long id) {
  75. showCollectionRepo.deleteAllByShowroomId(id);
  76. showroomRepo.softDelete(id);
  77. }
  78. @GetMapping("/excel")
  79. @ResponseBody
  80. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  81. List<Showroom> data = all(pageQuery).getContent();
  82. ExcelUtils.export(response, data);
  83. }
  84. @GetMapping("/{id}/share")
  85. public void addShare(@PathVariable Long id) {
  86. showroomRepo.addShare(id, 1);
  87. }
  88. //加浏览
  89. @GetMapping("/{id}/view")
  90. public void addView(@PathVariable Long id) {
  91. //增加浏览量
  92. Long userId = SecurityUtils.getAuthenticatedUser().getId();
  93. if (ObjectUtils.isEmpty(userId)) {
  94. return;
  95. }
  96. List<HeatInfo> list = heatInfoRepo.findByUserIdAndShowroomIdAndType(userId, id, HeatType.VIEW);
  97. if (!list.isEmpty()) return;
  98. int weight = sysConfigService.getInt("heat_view_weight");
  99. heatInfoRepo.save(HeatInfo.builder()
  100. .userId(userId)
  101. .showroomId(id)
  102. .type(HeatType.VIEW)
  103. .value(weight)
  104. .build());
  105. showroomRepo.addHeat(id, weight);
  106. }
  107. @PostMapping("/pass")
  108. public void pass(@RequestParam Long id) {
  109. showroomService.audit(id, AuthStatus.SUCCESS, null);
  110. }
  111. @PostMapping("/deny")
  112. public void deny(@RequestParam Long id, String reason) {
  113. showroomService.audit(id, AuthStatus.FAIL, reason);
  114. }
  115. }