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 cn.iocoder.byzs.module.bjdx.dal.dataobject.coursequestion.CourseQuestionDO;
import cn.iocoder.byzs.module.bjdx.dal.mysql.coursequestion.CourseQuestionMapper;
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.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 CourseQuestionMapper courseQuestionMapper;
    @Resource
    private CourseQuestOptionMapper courseQuestOptionMapper;

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

        boolean isAnswer = createReqVO.getCqoIsAnswer().equals("true");
        if (isAnswer){
            List<CourseQuestOptionDO> list = courseQuestOptionMapper.selectListByQuestId(Collections.singleton(createReqVO.getCqoQuestId()));
            list.forEach(item -> {
                if (!item.getId().equals(courseQuestOption.getId())){
                    item.setCqoIsAnswer("false");
                    courseQuestOptionMapper.updateById(item);
                }
            });

            courseQuestionMapper.updateById(new CourseQuestionDO().setId(courseQuestOption.getCqoQuestId()).setCqQuestAnswerId(courseQuestOption.getId()));
        }
        // 返回
        return courseQuestOption.getId();
    }

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

        boolean isAnswer = updateReqVO.getCqoIsAnswer().equals("true");
        if (isAnswer){
            List<CourseQuestOptionDO> list = courseQuestOptionMapper.selectListByQuestId(Collections.singleton(updateReqVO.getCqoQuestId()));
            list.forEach(item -> {
                if (!item.getId().equals(updateReqVO.getId())){
                    item.setCqoIsAnswer("false");
                    courseQuestOptionMapper.updateById(item);
                }
            });

            courseQuestionMapper.updateById(new CourseQuestionDO().setId(updateReqVO.getCqoQuestId()).setCqQuestAnswerId(updateReqVO.getId()));
        }
        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);
    }

}