package com.izouma.nineth.web; import cn.hutool.core.collection.CollUtil; import com.izouma.nineth.domain.NewsLike; import com.izouma.nineth.domain.ShowCollection; import com.izouma.nineth.domain.Showroom; import com.izouma.nineth.domain.User; import com.izouma.nineth.dto.PageQuery; import com.izouma.nineth.enums.AuthStatus; import com.izouma.nineth.exception.BusinessException; import com.izouma.nineth.repo.NewsLikeRepo; import com.izouma.nineth.repo.ShowCollectionRepo; import com.izouma.nineth.repo.ShowroomRepo; import com.izouma.nineth.service.ShowroomService; import com.izouma.nineth.utils.SecurityUtils; import com.izouma.nineth.utils.excel.ExcelUtils; import lombok.AllArgsConstructor; import org.apache.commons.lang3.ObjectUtils; import org.springframework.data.domain.Page; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @RestController @RequestMapping("/showroom") @AllArgsConstructor public class ShowroomController extends BaseController { private ShowroomService showroomService; private ShowroomRepo showroomRepo; private ShowCollectionRepo showCollectionRepo; private NewsLikeRepo newsLikeRepo; @PostMapping("/save") public Showroom save(@RequestBody Showroom record) { return showroomService.save(SecurityUtils.getAuthenticatedUser().getId(), record); } @PostMapping("/update") public Showroom update(@RequestBody Showroom record) { return showroomService.update(SecurityUtils.getAuthenticatedUser().getId(), record); } @PostMapping("/create") public Showroom create(@RequestParam Long userId) { return showroomService.save(userId); } @PostMapping("/all") public Page all(@RequestBody PageQuery pageQuery) { Page all = showroomService.all(pageQuery); List ids = all.map(Showroom::getId).getContent(); Map> collect = showCollectionRepo.findAllByShowroomIdIn(ids) .stream() .collect(Collectors.groupingBy(ShowCollection::getShowroomId)); showroomService.all(pageQuery); User user = SecurityUtils.getAuthenticatedUser(); Map likesMap = new HashMap<>(); if (user != null && !user.isAdmin() && CollUtil.isNotEmpty(ids)) { List likes = newsLikeRepo.findByUserIdAndShowroomIdIn(user .getId(), ids); likesMap = likes.stream() .collect(Collectors.groupingBy(NewsLike::getShowroomId, Collectors.counting())); } Map finalLikesMap = likesMap; return all.map(showroom -> { List collections = collect.get(showroom.getId()); if (CollUtil.isNotEmpty(collections)) { collections.sort(Comparator.comparingInt(ShowCollection::getSort)); showroom.setCollections(collections); } showroom.setLiked(ObjectUtils.isNotEmpty(finalLikesMap.get(showroom.getId()))); return showroom; }); } @GetMapping("/get/{id}") public Showroom get(@PathVariable Long id) { Showroom showroom = showroomRepo.findById(id).orElseThrow(new BusinessException("无记录")); showroom.setCollections(showCollectionRepo.findAllByShowroomIdOrderBySort(id)); User user = SecurityUtils.getAuthenticatedUser(); if (user != null && !user.isAdmin()) { List likes = newsLikeRepo.findByUserIdAndShowroomId(user .getId(), id); showroom.setLiked(CollUtil.isNotEmpty(likes)); } return showroom; } @PreAuthorize("hasRole('ADMIN')") @PostMapping("/del/{id}") public void del(@PathVariable Long id) { showCollectionRepo.deleteAllByShowroomId(id); showroomRepo.softDelete(id); } @GetMapping("/excel") @ResponseBody public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException { List data = all(pageQuery).getContent(); ExcelUtils.export(response, data); } @GetMapping("/{id}/share") public void addShare(@PathVariable Long id) { showroomRepo.addShare(id, 1); } @PostMapping("/pass") public void pass(@RequestParam Long id) { showroomService.audit(id, AuthStatus.SUCCESS, null); } @PostMapping("/deny") public void deny(@RequestParam Long id, String reason) { showroomService.audit(id, AuthStatus.FAIL, reason); } }