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

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.byzs.module.bjdx.controller.admin.course.vo.CoursePageReqVO;
import cn.iocoder.byzs.module.bjdx.controller.admin.course.vo.CourseSaveReqVO;
import cn.iocoder.byzs.module.bjdx.controller.admin.courseconfig.vo.CourseConfigPageReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.byzs.module.bjdx.controller.admin.course.vo.*;
import cn.iocoder.byzs.module.bjdx.dal.dataobject.course.CourseDO;
import cn.iocoder.byzs.framework.common.pojo.PageResult;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;

import cn.iocoder.byzs.module.bjdx.dal.mysql.course.CourseMapper;

import static cn.iocoder.byzs.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.byzs.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.byzs.module.bjdx.enums.ErrorCodeConstants.*;

/**
 * 课程 Service 实现类
 *
 * @author lyb
 */
@Service
@Validated
public class CourseServiceImpl implements CourseService {

    @Resource
    private CourseMapper courseMapper;

    @Override
    public Long createCourse(CourseSaveReqVO createReqVO) {
        // 插入
        CourseDO course = BeanUtils.toBean(createReqVO, CourseDO.class);
        courseMapper.insert(course);
        // 返回
        return course.getId();
    }

    @Override
    public void updateCourse(CourseSaveReqVO updateReqVO) {
        // 校验存在
        validateCourseExists(updateReqVO.getId());
        // 更新
        CourseDO updateObj = BeanUtils.toBean(updateReqVO, CourseDO.class);
        courseMapper.updateById(updateObj);
    }

    @Override
    public void deleteCourse(Long id) {
        // 校验存在
        validateCourseExists(id);
        // 删除
        courseMapper.deleteById(id);
    }

    @Override
        public void deleteCourseListByIds(List<Long> ids) {
        // 校验存在
        validateCourseExists(ids);
        // 删除
        courseMapper.deleteByIds(ids);
        }

    private void validateCourseExists(List<Long> ids) {
        List<CourseDO> list = courseMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(COURSE_NOT_EXISTS);
        }
    }

    private void validateCourseExists(Long id) {
        if (courseMapper.selectById(id) == null) {
            throw exception(COURSE_NOT_EXISTS);
        }
    }

    @Override
    public CourseDO getCourse(Long id) {
        return courseMapper.selectById(id);
    }

    @Override
    public PageResult<CourseRespVO> getCoursePage(CoursePageReqVO pageReqVO) {

        List<CoursePageReqVO> coursePageReqVOS = courseMapper.selectCoursePage(pageReqVO);

        // 调用服务层方法获取分页结果
        return new PageResult<>(
                coursePageReqVOS.stream()
                        .map(doItem -> BeanUtils.toBean(doItem, CourseRespVO.class))
                        .collect(Collectors.toList()),
                (long) coursePageReqVOS.size()
        );
    }

    @Override
    public List<CoursePageReqVO> getCourseList(CoursePageReqVO pageReqVO) {
        return courseMapper.selectCoursePage(pageReqVO);
    }

}