package cn.iocoder.byzs.module.web.service.course;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;
import cn.iocoder.byzs.module.bjdx.controller.admin.course.vo.CoursePageReqVO;
import cn.iocoder.byzs.module.bjdx.controller.admin.courseconfig.vo.CourseConfigRespVO;
import cn.iocoder.byzs.module.bjdx.service.course.CourseService;
import cn.iocoder.byzs.module.bjdx.service.courseconfig.CourseConfigService;
import cn.iocoder.byzs.module.web.controller.admin.course.vo.WebCourseVO;
import cn.iocoder.byzs.module.web.dal.mysql.course.WebCourseMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * web课程 Service 实现类
 *
 * @author lyb
 */
@Service
@Validated
public class WebCourseServiceImpl{

    @Resource
    private CourseService courseService;
    @Resource
    private CourseConfigService courseConfigService;
    @Resource
    private WebCourseMapper webCourseMapper;


    public List<WebCourseVO> getCourseVoByTypeId(Long typeId) {
        List<CoursePageReqVO> courseList = courseService.getCourseList(new CoursePageReqVO().setCourseType(typeId));

        // 目前数据少先循环查询即可
//        List<Long> idList = courseList.stream()
//                .map(CoursePageReqVO::getId)
//                .collect(Collectors.toList());

        //优化需要走web端类
//        webCourseMapper

        List<WebCourseVO> courseVoList = new ArrayList<>();

        for (CoursePageReqVO course : courseList) {
            List<CourseConfigRespVO> courseConfigQuestion = courseConfigService.getCourseConfigQuestion(course.getId());
            List<CourseConfigRespVO> list = new ArrayList<>();
            if (CollUtil.isNotEmpty(courseConfigQuestion)) {
                // 根据 ccTime 升序排序（已满足需求）
                list = courseConfigQuestion.stream().sorted(Comparator.comparing(CourseConfigRespVO::getCcTime)).toList();
            }
            courseVoList.add(BeanUtils.toBean(course, WebCourseVO.class).setCourseConfigList(list));
        }

        return courseVoList;
    }

}