86 lines
3.0 KiB
Java
86 lines
3.0 KiB
Java
package com.guwan.backend.service.impl;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.guwan.backend.pojo.dto.ReadingStatistics;
|
|
import com.guwan.backend.pojo.entity.ReadingProgress;
|
|
import com.guwan.backend.mapper.ReadingProgressMapper;
|
|
import com.guwan.backend.service.ReadingProgressService;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
@Slf4j
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class ReadingProgressServiceImpl implements ReadingProgressService {
|
|
|
|
private final ReadingProgressMapper progressMapper;
|
|
|
|
@Override
|
|
@Transactional
|
|
public ReadingProgress updateProgress(ReadingProgress progress) {
|
|
progress.setLastReadTime(LocalDateTime.now());
|
|
|
|
// 检查是否存在记录
|
|
ReadingProgress existing = getProgress(progress.getUserId(), progress.getBookId());
|
|
if (existing != null) {
|
|
progress.setId(existing.getId());
|
|
progressMapper.updateById(progress);
|
|
} else {
|
|
progressMapper.insert(progress);
|
|
}
|
|
return progress;
|
|
}
|
|
|
|
@Override
|
|
public ReadingProgress getProgress(Long userId, Long bookId) {
|
|
return progressMapper.selectOne(
|
|
new LambdaQueryWrapper<ReadingProgress>()
|
|
.eq(ReadingProgress::getUserId, userId)
|
|
.eq(ReadingProgress::getBookId, bookId)
|
|
);
|
|
}
|
|
|
|
@Override
|
|
public IPage<ReadingProgress> getUserProgress(Long userId, Integer pageNum, Integer pageSize) {
|
|
return progressMapper.selectPage(
|
|
new Page<>(pageNum, pageSize),
|
|
new LambdaQueryWrapper<ReadingProgress>()
|
|
.eq(ReadingProgress::getUserId, userId)
|
|
.orderByDesc(ReadingProgress::getLastReadTime)
|
|
);
|
|
}
|
|
|
|
@Override
|
|
public IPage<ReadingProgress> getProgressByStatus(Long userId, String status, Integer pageNum, Integer pageSize) {
|
|
return progressMapper.selectPage(
|
|
new Page<>(pageNum, pageSize),
|
|
new LambdaQueryWrapper<ReadingProgress>()
|
|
.eq(ReadingProgress::getUserId, userId)
|
|
.eq(ReadingProgress::getStatus, status)
|
|
.orderByDesc(ReadingProgress::getLastReadTime)
|
|
);
|
|
}
|
|
|
|
@Override
|
|
@Transactional
|
|
public void updateReadingTime(Long userId, Long bookId, Integer minutes) {
|
|
ReadingProgress progress = getProgress(userId, bookId);
|
|
if (progress != null) {
|
|
progress.setReadingTime(progress.getReadingTime() + minutes);
|
|
progress.setLastReadTime(LocalDateTime.now());
|
|
progressMapper.updateById(progress);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public ReadingStatistics getReadingStatistics(Long userId) {
|
|
// TODO: 实现阅读统计逻辑
|
|
return new ReadingStatistics();
|
|
}
|
|
} |