| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- package com.izouma.nineth.web;
- import com.fasterxml.jackson.annotation.JsonView;
- import com.izouma.nineth.domain.Banner;
- import com.izouma.nineth.domain.Collection;
- import com.izouma.nineth.domain.CollectionPrivilege;
- import com.izouma.nineth.domain.FileObject;
- import com.izouma.nineth.dto.*;
- import com.izouma.nineth.enums.BannerType;
- import com.izouma.nineth.exception.BusinessException;
- import com.izouma.nineth.repo.*;
- import com.izouma.nineth.service.CacheService;
- import com.izouma.nineth.service.CollectionService;
- import com.izouma.nineth.service.LikeService;
- import com.izouma.nineth.utils.SecurityUtils;
- import com.izouma.nineth.utils.excel.ExcelUtils;
- import io.swagger.annotations.ApiOperation;
- import lombok.AllArgsConstructor;
- import org.apache.commons.lang3.ObjectUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.BeanUtils;
- import org.springframework.cache.annotation.CacheEvict;
- import org.springframework.cache.annotation.Cacheable;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.PageImpl;
- import org.springframework.data.domain.Pageable;
- import org.springframework.security.access.prepost.PreAuthorize;
- import org.springframework.util.CollectionUtils;
- import org.springframework.web.bind.annotation.*;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.util.*;
- import java.util.stream.Collectors;
- @RestController
- @RequestMapping("/collection")
- @AllArgsConstructor
- public class CollectionController extends BaseController {
- private CollectionService collectionService;
- private CollectionRepo collectionRepo;
- private LikeService likeService;
- private NewsRepo newsRepo;
- private CacheService cacheService;
- private CollectionPrivilegeRepo collectionPrivilegeRepo;
- private AssetRepo assetRepo;
- private BannerRepo bannerRepo;
- @PreAuthorize("hasAnyRole('ADMIN','SAAS')")
- @PostMapping("/save")
- public Collection save(@RequestBody CollectionInfoDTO record) {
- Collection collection = collectionService.update(record);
- CollectionPrivilege collectionPrivilege = collectionPrivilegeRepo.findByCollectionId(record.getId());
- if (ObjectUtils.isEmpty(collectionPrivilege)) {
- collectionPrivilege = new CollectionPrivilege(record, record.getId());
- collectionPrivilege.setId(null);
- collectionPrivilegeRepo.save(collectionPrivilege);
- return collection;
- }
- collectionPrivilegeRepo.update(collectionPrivilege.getId(), record.getId(), record.getHeadBg(),
- record.getMaxCollection(), record.getShowroomBg(), record.isVip());
- return collection;
- }
- @PreAuthorize("hasAnyRole('ADMIN','SAAS')")
- @PostMapping("/create")
- public Collection create(@RequestBody CollectionInfoDTO record) {
- Collection collection = new Collection();
- BeanUtils.copyProperties(record, collection);
- collection = collectionService.create(collection);
- CollectionPrivilege collectionPrivilege = new CollectionPrivilege();
- BeanUtils.copyProperties(record, collectionPrivilege);
- collectionPrivilege.setCollectionId(collection.getId());
- collectionPrivilegeRepo.save(collectionPrivilege);
- return collection;
- }
- //@PreAuthorize("hasRole('ADMIN')")
- @PostMapping("/all")
- @JsonView(Collection.View.Basic.class)
- public Page<Collection> all(@RequestBody PageQuery pageQuery) {
- pageQuery.getQuery().putIfAbsent("companyId", 1);
- Page<Collection> page = collectionService.all(pageQuery).toPage();
- if (pageQuery.getQuery().get("distinctPrefix") != null) {
- List<Collection> newContent = new ArrayList<>();
- page.getContent().forEach(collection -> {
- String count = collectionService.countNum(collection.getPrefixName());
- collection.setTransferringCount(Long.valueOf(count));
- newContent.add(collection);
- });
- collectionService.queryUserDetail(page.getContent());
- return new PageImpl<>(newContent, page.getPageable(), page.getTotalElements());
- }
- collectionService.queryUserDetail(page.getContent());
- return page;
- }
- @GetMapping("/get/{id}")
- public Collection get(@PathVariable Long id) {
- return collectionService.queryUserDetail(collectionRepo.findById(id)
- .orElseThrow(new BusinessException("无记录")), true, true);
- }
- @GetMapping("/getInfo/{id}")
- public CollectionInfoDTO getInfo(@PathVariable Long id) {
- return collectionRepo.findInfoById(id).orElseThrow(new BusinessException("无记录"));
- }
- // @PreAuthorize("hasRole('ADMIN')")
- // @PostMapping("/del/{id}")
- // public void del(@PathVariable Long id) {
- // collectionRepo.softDelete(id);
- // }
- @GetMapping("/excel")
- @ResponseBody
- public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
- List<Collection> data = collectionService.all(pageQuery).getContent();
- ExcelUtils.export(response, data);
- }
- @GetMapping("/{id}/like")
- @ApiOperation("点赞")
- @CacheEvict("collection")
- public void like(@PathVariable Long id) {
- likeService.like(SecurityUtils.getAuthenticatedUser().getId(), id);
- }
- @GetMapping("/{id}/unlike")
- @ApiOperation("取消点赞")
- @CacheEvict("collection")
- public void unlike(@PathVariable Long id) {
- likeService.unlike(SecurityUtils.getAuthenticatedUser().getId(), id);
- }
- @GetMapping("/myLikes")
- @ApiOperation("我收藏的")
- public List<CollectionDTO> myLikes(@RequestParam(defaultValue = "1") Long companyId) {
- return collectionService
- .toDTO(collectionRepo.userLikes(SecurityUtils.getAuthenticatedUser().getId(), companyId));
- }
- @PreAuthorize("hasAnyRole('ADMIN','SAAS')")
- @PostMapping("/createBlindBox")
- public Collection createBlindBox(@RequestBody CreateBlindBox createBlindBox) throws Exception {
- return collectionService.createBlindBox(createBlindBox);
- }
- @PostMapping("/appointment")
- public void appointment(@RequestParam Long id) {
- collectionService.appointment(id, SecurityUtils.getAuthenticatedUser().getId());
- }
- @GetMapping("/recommend")
- @Cacheable("recommend")
- public Object recommendAll(@RequestParam(required = false) String type, @RequestParam(defaultValue = "1") Long companyId) {
- if (StringUtils.isNotEmpty(type)) {
- return collectionService.recommendLegacy(type, companyId);
- }
- List<RecommendDTO> recommedDTOS = new ArrayList<>();
- List<RecommendDTO> collectionDTOS = collectionRepo.recommend("LIST", companyId).stream().map(rc -> {
- if (StringUtils.isNotBlank(rc.getRecommend().getPic())) {
- rc.getCollection().setPic(Collections.singletonList(new FileObject(null, rc.getRecommend()
- .getPic(), null, null)));
- }
- CollectionDTO collectionDTO = new CollectionDTO();
- BeanUtils.copyProperties(rc.getCollection(), collectionDTO);
- // 减少数据量
- collectionDTO.setDetail(null);
- collectionDTO.setPrivileges(null);
- collectionDTO.setProperties(null);
- return new RecommendDTO(rc.getRecommend().getSort(), collectionDTO, "collection", rc.getRecommend()
- .getCreatedAt());
- }).collect(Collectors.toList());
- List<RecommendDTO> news = newsRepo.recommend("LIST", companyId).stream().map(rn -> {
- if (StringUtils.isNotBlank(rn.getRecommend().getPic())) {
- rn.getNews().setPic(rn.getRecommend().getPic());
- }
- return new RecommendDTO(rn.getRecommend().getSort(), rn.getNews(), "news", rn.getRecommend()
- .getCreatedAt());
- }).collect(Collectors.toList());
- recommedDTOS.addAll(collectionDTOS);
- recommedDTOS.addAll(news);
- recommedDTOS.sort((a, b) -> b.getSort().compareTo(a.getSort()));
- return recommedDTOS;
- }
- @PreAuthorize("hasRole('ADMIN')")
- @GetMapping("/clearRecommend")
- public String clearRecommend() {
- cacheService.clearRecommend();
- return "ok";
- }
- @PreAuthorize("hasAnyRole('ADMIN','COMPANY')")
- @PostMapping("/onShelf")
- public void onShelf(Long id, boolean onShelf, boolean salable) {
- Collection collection = collectionRepo.findById(id).orElseThrow(new BusinessException("藏品不存在"));
- collection.setOnShelf(onShelf);
- collection.setSalable(salable);
- collectionRepo.save(collection);
- }
- @GetMapping("/byTag")
- @JsonView(Collection.View.Basic.class)
- public Page<Collection> byTag(@RequestParam Long tagId,
- @RequestParam(required = false, defaultValue = "0") List<Long> excludeUserId,
- Pageable pageable) {
- return collectionService.byTag(tagId, excludeUserId, pageable);
- }
- @PreAuthorize("hasAnyRole('ADMIN','COMPANY')")
- @PostMapping("/onShelfOasis")
- public List<Collection> onShelfOasis(@RequestBody List<Long> oasisIds) {
- return collectionService.setOasisOnShelf(oasisIds);
- }
- @PreAuthorize("hasAnyRole('ADMIN','COMPANY')")
- @PostMapping("/setScancodeOasis")
- public List<Collection> setScancodeOasis(@RequestBody List<Long> oasisIds) {
- return collectionService.setOasisScancode(oasisIds);
- }
- @GetMapping("/count")
- public Map<String, String> countNum(@RequestParam String search) {
- List<Banner> list = bannerRepo.findByLinkContentAndTypeAndDel(search, BannerType.MARKET, false);
- if (CollectionUtils.isEmpty(list)) {
- return null;
- }
- PageQuery pageQuery = new PageQuery();
- pageQuery.setPage(0);
- pageQuery.setSize(1);
- pageQuery.getQuery().put("onShelf", true);
- pageQuery.getQuery().put("source", "TRANSFER");
- pageQuery.getQuery().put("type", "DEFAULT,BLIND_BOX");
- pageQuery.getQuery().put("del", false);
- pageQuery.setSearch(search);
- pageQuery.getQuery().put("salable", false);
- long onlyShowNum = collectionService.all(pageQuery).getTotal();
- pageQuery.getQuery().put("salable", true);
- long consignmentNum = collectionService.all(pageQuery).getTotal();
- pageQuery.getQuery().remove("salable");
- // pageQuery.getQuery().put("inPaying", true);
- long transactingNum = collectionService.all(pageQuery).getTotal();
- Long tranferCount = assetRepo.countNameLikeNotDestroyed("%" + search + "%");
- Map<String, String> map = new HashMap<>();
- map.put("onlyShowNum", String.valueOf(onlyShowNum));
- map.put("consignmentNum", String.valueOf(consignmentNum));
- map.put("transactingNum", String.valueOf(transactingNum));
- map.put("tranferingNum", String.valueOf(tranferCount - 1));
- return map;
- }
- }
|