hejianhao
2026-08-28 16a57a8fc6868cea9171a1119698572407a2fc03
tcm-training-admin-backend/src/main/java/com/tcm/training/admin/app/course/service/AppHealthCourseService.java
@@ -311,17 +311,25 @@
        HealthCourse course = requireEnabledCourse(courseId);
        List<HealthCourseVideo> enabledVideos = listEnabledVideos(courseId);
        Map<Long, VideoProgressSnapshot> videoProgressSnapshotMap = buildVideoProgressSnapshotMap(userId, Collections.singletonList(courseId));
        PurchaseStateSnapshot purchaseStateSnapshot = resolvePurchaseState(courseId);
        List<AppMyHealthCourseVideoItemVO> videos = new ArrayList<AppMyHealthCourseVideoItemVO>(enabledVideos.size());
        Long currentVideoId = null;
        LocalDateTime latestLearnAt = null;
        for (HealthCourseVideo video : enabledVideos) {
            VideoProgressSnapshot snapshot = videoProgressSnapshotMap.get(video.getId());
            boolean freeAccess = isFreeVideo(video.getSalePrice());
            boolean purchased = purchaseStateSnapshot.isCourseOwned()
                    || purchaseStateSnapshot.getPurchasedVideoIds().contains(video.getId());
            boolean locked = !freeAccess && !purchased;
            AppMyHealthCourseVideoItemVO itemVO = new AppMyHealthCourseVideoItemVO();
            itemVO.setVideoId(video.getId());
            itemVO.setChapterName(trimToEmpty(video.getChapterName()));
            itemVO.setDurationSeconds(defaultNumber(video.getDurationSeconds()));
            itemVO.setDurationText(formatVideoDuration(itemVO.getDurationSeconds()));
            itemVO.setFreeAccess(Boolean.valueOf(freeAccess));
            itemVO.setPurchased(Boolean.valueOf(purchased));
            itemVO.setLocked(Boolean.valueOf(locked));
            itemVO.setVideoUrl(trimToEmpty(video.getVideoUrl()));
            itemVO.setCompleted(Boolean.valueOf(snapshot != null && snapshot.isCompleted()));
            itemVO.setProgressPercent(Integer.valueOf(calculateVideoDisplayProgressPercent(
@@ -332,14 +340,20 @@
            itemVO.setLastPlayPositionSeconds(Integer.valueOf(resolveVideoResumePosition(snapshot)));
            itemVO.setDownloadable(Boolean.valueOf(hasVideoDownloadAccess(userId, courseId, video.getId())));
            videos.add(itemVO);
            if (snapshot != null && snapshot.getLatestLearnAt() != null
            // 默认播放章节只允许落在当前用户可观看的小节上,未购小节保留展示但不自动选中。
            if (!locked && snapshot != null && snapshot.getLatestLearnAt() != null
                    && (latestLearnAt == null || snapshot.getLatestLearnAt().isAfter(latestLearnAt))) {
                latestLearnAt = snapshot.getLatestLearnAt();
                currentVideoId = video.getId();
            }
        }
        if (currentVideoId == null && !enabledVideos.isEmpty()) {
            currentVideoId = enabledVideos.get(0).getId();
        if (currentVideoId == null) {
            for (AppMyHealthCourseVideoItemVO itemVO : videos) {
                if (!Boolean.TRUE.equals(itemVO.getLocked())) {
                    currentVideoId = itemVO.getVideoId();
                    break;
                }
            }
        }
        AppMyHealthCourseDetailVO detailVO = new AppMyHealthCourseDetailVO();
@@ -508,9 +522,6 @@
        AppUser user = requireEnabledUser(userId);
        HealthCourse course = requireEnabledCourse(courseId);
        String paymentMethod = normalizePaymentMethod(request == null ? null : request.getPaymentMethod());
        if (course.getSalePrice() == null || course.getSalePrice().compareTo(BigDecimal.ZERO) <= 0) {
            throw new IllegalArgumentException("当前课程无需购买");
        }
        CoursePurchaseOrder existingOrder = findLatestActivePaidCourseOrder(userId, courseId);
        if (existingOrder != null) {
            return buildPurchaseResult(existingOrder, true, resolvePurchasedVideoIds(userId, courseId, true));
@@ -994,6 +1005,8 @@
            videoVO.setLocked(Boolean.valueOf(locked));
            // 已购买章节需要直接拿到视频地址,前端才能切换为下载入口。
            videoVO.setVideoUrl(trimToEmpty(video.getVideoUrl()));
            videoVO.setCreatedAt(video.getCreatedAt());
            videoVO.setUpdatedAt(video.getUpdatedAt());
            result.add(videoVO);
        }
        return result;