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

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.byzs.module.bjdx.controller.admin.coursequestion.vo.CourseQuestionPageReqVO;
import cn.iocoder.byzs.module.bjdx.controller.admin.coursequestion.vo.CourseQuestionSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.byzs.module.bjdx.controller.admin.coursequestion.vo.*;
import cn.iocoder.byzs.module.bjdx.dal.dataobject.coursequestion.CourseQuestionDO;
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.coursequestion.CourseQuestionMapper;

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 CourseQuestionServiceImpl implements CourseQuestionService {

    @Resource
    private CourseQuestionMapper courseQuestionMapper;

    @Override
    public Long createCourseQuestion(CourseQuestionSaveReqVO createReqVO) {
        // 插入
        CourseQuestionDO courseQuestion = BeanUtils.toBean(createReqVO, CourseQuestionDO.class);
        courseQuestionMapper.insert(courseQuestion);
        // 返回
        return courseQuestion.getId();
    }

    @Override
    public void updateCourseQuestion(CourseQuestionSaveReqVO updateReqVO) {
        // 校验存在
        validateCourseQuestionExists(updateReqVO.getId());
        // 更新
        CourseQuestionDO updateObj = BeanUtils.toBean(updateReqVO, CourseQuestionDO.class);
        courseQuestionMapper.updateById(updateObj);
    }

    @Override
    public void deleteCourseQuestion(Long id) {
        // 校验存在
        validateCourseQuestionExists(id);
        // 删除
        courseQuestionMapper.deleteById(id);
    }

    @Override
        public void deleteCourseQuestionListByIds(List<Long> ids) {
        // 校验存在
        validateCourseQuestionExists(ids);
        // 删除
        courseQuestionMapper.deleteByIds(ids);
        }

    private void validateCourseQuestionExists(List<Long> ids) {
        List<CourseQuestionDO> list = courseQuestionMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(COURSE_QUESTION_NOT_EXISTS);
        }
    }

    private void validateCourseQuestionExists(Long id) {
        if (courseQuestionMapper.selectById(id) == null) {
            throw exception(COURSE_QUESTION_NOT_EXISTS);
        }
    }

    @Override
    public CourseQuestionDO getCourseQuestion(Long id) {
        return courseQuestionMapper.selectById(id);
    }

    @Override
    public PageResult<CourseQuestionDO> getCourseQuestionPage(CourseQuestionPageReqVO pageReqVO) {
        return courseQuestionMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<CourseQuestionDO> getCourseQuestionNoPage(CourseQuestionPageReqVO pageReqVO) {
        return courseQuestionMapper.selectPage(pageReqVO);
    }

}