This repository has been archived on 2025-05-14. You can view files and clone it, but cannot push or open issues or pull requests.
copy-kamanote/src/main/java/com/example/copykamanotes/service/impl/CollectionServiceImpl.java
LingandRX 0244267ab7 feat(question): 添加问题相关功能- 新增题单相关实体和接口
- 实现问题列表获取、问题创建、更新和删除功能
- 添加用户问题列表获取和单个问题获取功能
- 实现问题搜索功能
- 优化消息服务实现
- 重构部分现有服务的代码结构
2025-05-13 22:05:45 +08:00

157 lines
6.3 KiB
Java

package com.example.copykamanotes.service.impl;
import com.example.copykamanotes.annotation.NeedLogin;
import com.example.copykamanotes.mapper.CollectionMapper;
import com.example.copykamanotes.mapper.CollectionNoteMapper;
import com.example.copykamanotes.mapper.NoteMapper;
import com.example.copykamanotes.model.base.ApiResponse;
import com.example.copykamanotes.model.base.EmptyVO;
import com.example.copykamanotes.model.dto.collection.CollectionQueryParams;
import com.example.copykamanotes.model.dto.collection.CreateCollectionBody;
import com.example.copykamanotes.model.dto.collection.UpdateCollectionBody;
import com.example.copykamanotes.model.entity.Collection;
import com.example.copykamanotes.model.entity.CollectionNote;
import com.example.copykamanotes.model.vo.collection.CollectionVO;
import com.example.copykamanotes.model.vo.collection.CreateCollectionVO;
import com.example.copykamanotes.scope.RequestScopeData;
import com.example.copykamanotes.service.CollectionService;
import com.example.copykamanotes.utils.ApiResponseUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collections;
import java.util.List;
import java.util.Set;
@Service
@RequiredArgsConstructor
public class CollectionServiceImpl implements CollectionService {
private final RequestScopeData requestScopeData;
private final CollectionMapper collectionMapper;
private final CollectionNoteMapper collectionNoteMapper;
private final NoteMapper noteMapper;
@Override
public ApiResponse<List<CollectionVO>> getCollection(CollectionQueryParams queryParams) {
List<Collection> collections = collectionMapper.findByCreatorId(queryParams.getCreatorId());
List<Integer> collectionIds = collections.stream().map(Collection::getCollectionId).toList();
final Set<Integer> collectionNoteIdCollectionIds;
if (queryParams.getNoteId() != null) {
collectionNoteIdCollectionIds = collectionNoteMapper.filterCollectionIdsByNoteId(queryParams.getNoteId(), collectionIds);
} else {
collectionNoteIdCollectionIds = Collections.emptySet();
}
List<CollectionVO> collectionVOList = collections.stream().map(collection -> {
CollectionVO collectionVO = new CollectionVO();
BeanUtils.copyProperties(collection, collectionVO);
if (queryParams.getNoteId() == null) return collectionVO;
CollectionVO.NoteStatus noteStatus = new CollectionVO.NoteStatus();
noteStatus.setIsCollected(collectionNoteIdCollectionIds.contains(collection.getCollectionId()));
noteStatus.setNoteId(queryParams.getNoteId());
collectionVO.setNoteStatus(noteStatus);
return collectionVO;
}).toList();
return ApiResponseUtils.success("获取收藏夹列表成功", collectionVOList);
}
@Override
@NeedLogin
public ApiResponse<CreateCollectionVO> createCollection(CreateCollectionBody createCollectionBody) {
Long creatorId = requestScopeData.getUserId();
Collection collection = new Collection();
BeanUtils.copyProperties(createCollectionBody, collection);
collection.setCreatorId(creatorId);
try {
collectionMapper.insert(collection);
CreateCollectionVO createCollectionVO = new CreateCollectionVO();
createCollectionVO.setCollectionId(collection.getCollectionId());
return ApiResponseUtils.success("创建成功", createCollectionVO);
} catch (Exception e) {
return ApiResponseUtils.error("创建失败");
}
}
@Override
@NeedLogin
@Transactional
public ApiResponse<EmptyVO> deleteCollection(Integer id) {
Long creatorId = requestScopeData.getUserId();
Collection collection = collectionMapper.findByIdAndCreatorId(id, creatorId);
if (collection == null) {
return ApiResponseUtils.error("收藏夹不存在");
}
try {
collectionMapper.deleteById(id);
collectionNoteMapper.deleteByCollectionId(id);
return ApiResponseUtils.success("删除成功");
} catch (Exception e) {
return ApiResponseUtils.error("删除失败");
}
}
@Override
@NeedLogin
@Transactional
public ApiResponse<EmptyVO> batchModifyCollection(UpdateCollectionBody updateCollectionBody) {
Long userId = requestScopeData.getUserId();
Integer noteId = updateCollectionBody.getNoteId();
UpdateCollectionBody.UpdateItem[] collections = updateCollectionBody.getCollections();
for (UpdateCollectionBody.UpdateItem collection : collections) {
Integer collectionId = collection.getCollectionId();
String action = collection.getAction();
Collection collectionEntity = collectionMapper.findByIdAndCreatorId(collectionId, userId);
if (collectionEntity == null)
return ApiResponseUtils.error("收藏夹不存在");
if (action.equals("create")) {
try {
if (collectionMapper.countByCreatorIdAndNoteId(userId, noteId) == 0) {
noteMapper.collectNote(noteId);
}
CollectionNote collectionNote = new CollectionNote();
collectionNote.setCollectionId(collectionId);
collectionNote.setNoteId(noteId);
collectionNoteMapper.insert(collectionNote);
} catch (Exception e) {
return ApiResponseUtils.error("收藏失败");
}
}
if (action.equals("delete")) {
try {
collectionNoteMapper.deleteByCollectionIdAndNoteId(collectionId, noteId);
if (collectionMapper.countByCreatorIdAndNoteId(userId, noteId) == 0) {
noteMapper.unCollectNote(noteId);
}
} catch (Exception e) {
return ApiResponseUtils.error("取消收藏失败");
}
}
}
return ApiResponseUtils.success("操作成功");
}
}