| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- package com.izouma.nineth.service;
- import cn.hutool.core.collection.CollUtil;
- import com.izouma.nineth.domain.Collection;
- import com.izouma.nineth.domain.*;
- import com.izouma.nineth.dto.PageQuery;
- import com.izouma.nineth.enums.AssetStatus;
- import com.izouma.nineth.enums.AuthStatus;
- import com.izouma.nineth.enums.CollectionType;
- import com.izouma.nineth.enums.ShowroomType;
- import com.izouma.nineth.exception.BusinessException;
- import com.izouma.nineth.repo.*;
- import com.izouma.nineth.utils.JpaUtils;
- import com.izouma.nineth.utils.ObjUtils;
- import com.izouma.nineth.utils.SecurityUtils;
- import lombok.AllArgsConstructor;
- import org.apache.commons.collections.CollectionUtils;
- import org.apache.commons.lang3.ObjectUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.data.domain.Page;
- import org.springframework.stereotype.Service;
- import java.time.LocalDateTime;
- import java.util.*;
- import java.util.stream.Collectors;
- @Service
- @AllArgsConstructor
- public class ShowroomService {
- private ShowroomRepo showroomRepo;
- private AssetRepo assetRepo;
- private CollectionRepo collectionRepo;
- private ShowCollectionRepo showCollectionRepo;
- private CollectionPrivilegeRepo collectionPrivilegeRepo;
- private CacheService cacheService;
- private UserRepo userRepo;
- private SysConfigService sysConfigService;
- private NewsLikeRepo newsLikeRepo;
- public Page<Showroom> all(PageQuery pageQuery) {
- return showroomRepo
- .findAll(JpaUtils.toSpecification(pageQuery, Showroom.class), JpaUtils.toPageRequest(pageQuery));
- }
- /*
- 不做盲盒
- */
- public Showroom save(Long userId, Showroom showroom) {
- Asset asset = assetRepo.findById(showroom.getAssetId()).orElseThrow(new BusinessException("资产不存在"));
- if (!userId.equals(asset.getUserId())) {
- throw new BusinessException("该资产不属于你");
- }
- if (!CollectionType.SHOWROOM.equals(asset.getType())) {
- throw new BusinessException("不是展厅藏品");
- }
- if (!AssetStatus.NORMAL.equals(asset.getStatus())) {
- throw new BusinessException("该状态不可创建展厅");
- }
- if (asset.isConsignment()) {
- throw new BusinessException("寄售中不可以创建");
- }
- if (showroomRepo.findByAssetId(showroom.getAssetId()).isPresent()) {
- throw new BusinessException("已创建过展厅");
- }
- List<ShowCollection> showCollections = showroom.getCollections();
- CollectionPrivilege cp = collectionPrivilegeRepo.findByCollectionId(asset.getCollectionId());
- if (cp.getMaxCollection() < showCollections.size()) {
- throw new BusinessException("选择的藏品数量大于最多可放置数量");
- }
- List<Long> collectionIds = showCollections.stream()
- .map(ShowCollection::getCollectionId)
- .distinct()
- .collect(Collectors.toList());
- List<Collection> collections = collectionRepo.findAllByIdIn(collectionIds);
- Map<Long, Collection> collectionMap = new HashMap<>();
- collections.forEach(collection -> {
- if (!userId.equals(collection.getOwnerId())) {
- throw new BusinessException("该藏品不属于你");
- }
- collectionMap.put(collection.getId(), collection);
- });
- showroom.setUserId(userId);
- showroom.setHeadBg(cp.getHeadBg());
- showroom.setShowroomBg(cp.getShowroomBg());
- showroom.setMaxCollection(cp.getMaxCollection());
- showroom.setNickname(asset.getOwner());
- showroom.setAvatar(asset.getOwnerAvatar());
- Showroom show = showroomRepo.save(showroom);
- showCollections.forEach(showCollection -> {
- Collection collection = collectionMap.get(showCollection.getCollectionId());
- if (ObjectUtils.isNotEmpty(collection)) {
- FileObject pic = collection.getPic().get(0);
- showCollection.setPic(pic.getUrl());
- if ("video/mp4".equals(pic.getType())) {
- showCollection.setPic(pic.getThumb());
- }
- showCollection.setShowroomId(show.getId());
- showCollection.setAssetId(collection.getAssetId());
- showCollection.setStatus(getStatus(collection));
- showCollection.setPrice(collection.getPrice());
- showCollectionRepo.save(showCollection);
- }
- });
- return show;
- }
- public Showroom save(Asset asset) {
- if (!AssetStatus.NORMAL.equals(asset.getStatus())) {
- throw new BusinessException("该状态不可创建展厅");
- }
- if (asset.isConsignment()) {
- throw new BusinessException("寄售中不可以创建");
- }
- if (showroomRepo.findByAssetId(asset.getId()).isPresent()) {
- throw new BusinessException("已创建过展厅");
- }
- CollectionPrivilege cp = collectionPrivilegeRepo.findByCollectionId(asset.getCollectionId());
- Showroom showroom = Showroom.builder()
- .headBg(cp.getHeadBg())
- .showroomBg(cp.getShowroomBg())
- .maxCollection(cp.getMaxCollection())
- .publish(false)
- .userId(asset.getUserId())
- .avatar(asset.getOwnerAvatar())
- .assetId(asset.getId())
- .nickname(asset.getOwner())
- .type(ShowroomType.USER)
- .status(AuthStatus.SUCCESS)
- .build();
- showroom = showroomRepo.save(showroom);
- return showroom;
- }
- public Showroom save(Long userId, String type) {
- User user = userRepo.findByIdAndDelFalse(userId).orElseThrow(new BusinessException("无用户"));
- if (!user.isCompany()) {
- throw new BusinessException("无用户权限");
- }
- int maxCollection = 1;
- if ("COMPANY".equals(type)) {
- // if (showroomRepo.findByUserIdAndType(user.getId(), "COMPANY").isPresent()) {
- // throw new BusinessException("已创建过展厅");
- // }
- maxCollection = sysConfigService.getInt("max_collection");
- }
- Showroom showroom = Showroom.builder()
- .headBg("")
- .showroomBg("")
- .maxCollection(maxCollection)
- .publish(false)
- .userId(user.getId())
- .nickname(user.getNickname())
- .avatar(user.getAvatar())
- .type(ShowroomType.valueOf(type))
- .status(AuthStatus.NOT_AUTH)
- .build();
- showroom = showroomRepo.save(showroom);
- return showroom;
- }
- public Showroom update(Long userId, Showroom showroom) {
- Showroom recordRoom = showroomRepo.findById(showroom.getId()).orElseThrow(new BusinessException("无展厅"));
- showroom.setMaxCollection(recordRoom.getMaxCollection());
- // 上传的藏品
- List<ShowCollection> showCollections = showroom.getCollections();
- List<ShowCollection> recordShowColls = showCollectionRepo.findAllByShowroomId(showroom.getId());
- Map<Long, ShowCollection> showCollectionMap = recordShowColls.stream()
- .collect(Collectors.toMap(ShowCollection::getId, coll -> coll));
- Map<Long, ShowCollection> showCollectionMap1 = recordShowColls.stream()
- .collect(Collectors.toMap(ShowCollection::getCollectionId, coll -> coll));
- Set<Long> removeRecord = new HashSet<>(showCollectionMap.keySet());
- if (CollUtil.isNotEmpty(showCollections)) {
- showCollections = showCollections.stream().distinct().collect(Collectors.toList());
- if (recordRoom.getMaxCollection() < showCollections.size()) {
- throw new BusinessException("选择的藏品数量大于最多可放置数量");
- }
- List<Long> collectionIds = showCollections
- .stream()
- .filter(show -> ObjectUtils.isEmpty(show.getId()))
- .map(ShowCollection::getCollectionId)
- .collect(Collectors.toList());
- List<Collection> collections = collectionRepo.findAllByIdIn(collectionIds);
- Map<Long, Collection> collectionMap = new HashMap<>();
- collections.forEach(collection -> {
- // if (!userId.equals(collection.getOwnerId())) {
- // throw new BusinessException("该藏品不属于你");
- // }
- collectionMap.put(collection.getId(), collection);
- });
- showCollections.forEach(coll -> {
- if (coll.getId() != null) {
- ShowCollection showCollection = showCollectionMap.get(coll.getId());
- if (ObjectUtils.isNotEmpty(showCollection)) {
- ObjUtils.merge(showCollection, coll);
- showCollectionRepo.save(showCollection);
- // 移除掉
- removeRecord.remove(coll.getId());
- }
- } else {
- ShowCollection showCollection = showCollectionMap1.get(coll.getCollectionId());
- if (ObjectUtils.isNotEmpty(showCollection)) {
- ObjUtils.merge(showCollection, coll);
- showCollectionRepo.save(showCollection);
- removeRecord.remove(showCollection.getId());
- } else {
- Collection collection = collectionMap.get(coll.getCollectionId());
- if (ObjectUtils.isNotEmpty(collection)) {
- if (ShowroomType.COMPANY_BOX.equals(recordRoom.getType())) {
- if (!CollectionType.BLIND_BOX.equals(collection.getType())) {
- throw new BusinessException("盲盒展厅,只能添加盲盒");
- }
- }
- FileObject pic = collection.getPic().get(0);
- coll.setPic(pic.getUrl());
- if ("video/mp4".equals(pic.getType())) {
- coll.setPic(pic.getThumb());
- }
- coll.setShowroomId(recordRoom.getId());
- // 可能没有
- coll.setAssetId(collection.getAssetId());
- coll.setStatus(getStatus(collection));
- coll.setPrice(collection.getPrice());
- showCollectionRepo.save(coll);
- }
- }
- }
- });
- } else {
- // 删掉本次没有的
- showCollectionRepo.deleteAllByShowroomId(recordRoom.getId());
- }
- if (CollUtil.isNotEmpty(removeRecord)) {
- showCollectionRepo.deleteAllByIdIn(removeRecord);
- }
- ObjUtils.merge(recordRoom, showroom);
- recordRoom.setPublish(true);
- showroom = showroomRepo.save(recordRoom);
- cacheService.clearShowroom(showroom.getId());
- return showroom;
- }
- public void audit(Long id, AuthStatus status, String reason) {
- Showroom showroom = showroomRepo.findById(id).orElseThrow(new BusinessException("无展厅"));
- if (ShowroomType.USER.equals(showroom.getType())) {
- // 非企业类型不需要审核
- showroom.setStatus(null);
- showroomRepo.save(showroom);
- return;
- }
- showroom.setStatus(status);
- showroom.setReason(reason);
- showroomRepo.save(showroom);
- cacheService.clearShowroom(id);
- }
- public String getStatus(Collection collection) {
- boolean salable = collection.isSalable();
- if (collection.getSaleTime() != null) {
- if (collection.getSaleTime().isAfter(LocalDateTime.now())) {
- return "仅展示";
- }
- }
- if (!salable) {
- return "仅展示";
- }
- if (collection.getSoldOut() > 1) {
- return "寄售中";
- }
- if (collection.isNoSoldOut()) {
- return "仅展示";
- }
- if (collection.getSoldOut() != 1 & collection.getStock() == 0) {
- return "仅展示";
- }
- return "寄售中";
- }
- public String getStatusById(Long collectionId) {
- Collection collection = collectionRepo.findById(collectionId).orElseThrow(new BusinessException("暂无"));
- return getStatus(collection);
- }
- public Page<Showroom> show(PageQuery pageQuery) {
- if (Objects.equals(pageQuery.getQuery().get("type"), "COMPANY")) {
- pageQuery.getQuery().put("type", "COMPANY,COMPANY_BOX");
- }
- Page<Showroom> all = this.all(pageQuery);
- List<Long> ids = all.map(Showroom::getId).getContent();
- Map<Long, List<ShowCollection>> collect = showCollectionRepo.findAllByShowroomIdIn(ids)
- .stream()
- .collect(Collectors.groupingBy(ShowCollection::getShowroomId));
- User user = SecurityUtils.getAuthenticatedUser();
- Map<Long, Long> likesMap = new HashMap<>();
- if (user != null && !user.isAdmin() && CollUtil.isNotEmpty(ids)) {
- List<NewsLike> likes = newsLikeRepo.findByUserIdAndShowroomIdIn(user
- .getId(), ids);
- likesMap = likes.stream()
- .collect(Collectors.groupingBy(NewsLike::getShowroomId, Collectors.counting()));
- }
- Map<Long, Long> finalLikesMap = likesMap;
- return all.map(showroom -> {
- List<ShowCollection> collections = collect.get(showroom.getId());
- if (ShowroomType.COMPANY_BOX.equals(showroom.getType()) && CollectionUtils.isNotEmpty(collections)) {
- collections.forEach(orig -> collectionRepo.findById(orig.getCollectionId())
- .ifPresent(collection -> {
- //展示数量
- if (collection.isNoSoldOut()) {
- showroom.setNum(collection.getStock());
- } else {
- String name = StringUtils.isEmpty(collection.getPrefixName()) ?
- collection.getName() : collection.getPrefixName();
- int num = collectionRepo.countAllByNameLike("%" + name + "%");
- showroom.setNum(num);
- }
- }));
- }
- if (CollUtil.isNotEmpty(collections)) {
- collections.sort(Comparator.comparingInt(ShowCollection::getSort));
- showroom.setCollections(collections);
- }
- showroom.setLiked(ObjectUtils.isNotEmpty(finalLikesMap.get(showroom.getId())));
- return showroom;
- });
- }
- }
|