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

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.byzs.module.bjdx.controller.admin.coursequestoption.vo.CourseQuestOptionPageReqVO;
import cn.iocoder.byzs.module.bjdx.controller.admin.coursequestoption.vo.CourseQuestOptionSaveReqVO;
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.coursequestoption.vo.*;
import cn.iocoder.byzs.module.bjdx.dal.dataobject.coursequestoption.CourseQuestOptionDO;
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.coursequestoption.CourseQuestOptionMapper;

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 CourseQuestOptionServiceImpl implements CourseQuestOptionService {

    @Resource
    private CourseQuestOptionMapper courseQuestOptionMapper;

    @Override
    public Long createCourseQuestOption(CourseQuestOptionSaveReqVO createReqVO) {
        // 插入
        CourseQuestOptionDO courseQuestOption = BeanUtils.toBean(createReqVO, CourseQuestOptionDO.class);
        courseQuestOptionMapper.insert(courseQuestOption);
        // 返回
        return courseQuestOption.getId();
    }

    @Override
    public void updateCourseQuestOption(CourseQuestOptionSaveReqVO updateReqVO) {
        // 校验存在
        validateCourseQuestOptionExists(updateReqVO.getId());
        // 更新
        CourseQuestOptionDO updateObj = BeanUtils.toBean(updateReqVO, CourseQuestOptionDO.class);
        courseQuestOptionMapper.updateById(updateObj);
    }

    @Override
    public void deleteCourseQuestOption(Long id) {
        // 校验存在
        validateCourseQuestOptionExists(id);
        // 删除
        courseQuestOptionMapper.deleteById(id);
    }

    @Override
        public void deleteCourseQuestOptionListByIds(List<Long> ids) {
        // 校验存在
        validateCourseQuestOptionExists(ids);
        // 删除
        courseQuestOptionMapper.deleteByIds(ids);
        }

    private void validateCourseQuestOptionExists(List<Long> ids) {
        List<CourseQuestOptionDO> list = courseQuestOptionMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(COURSE_QUEST_OPTION_NOT_EXISTS);
        }
    }

    private void validateCourseQuestOptionExists(Long id) {
        if (courseQuestOptionMapper.selectById(id) == null) {
            throw exception(COURSE_QUEST_OPTION_NOT_EXISTS);
        }
    }

    @Override
    public CourseQuestOptionDO getCourseQuestOption(Long id) {
        return courseQuestOptionMapper.selectById(id);
    }

    @Override
    public PageResult<CourseQuestOptionDO> getCourseQuestOptionPage(CourseQuestOptionPageReqVO pageReqVO) {
        return courseQuestOptionMapper.selectPage(pageReqVO);
    }

}