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

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.byzs.framework.common.pojo.PageParam;
import cn.iocoder.byzs.framework.common.pojo.PageResult;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;
import cn.iocoder.byzs.module.bjdx.controller.admin.courseconfig.vo.CourseConfigPageReqVO;
import cn.iocoder.byzs.module.bjdx.controller.admin.courseconfig.vo.CourseConfigRespVO;
import cn.iocoder.byzs.module.bjdx.controller.admin.courseconfig.vo.CourseConfigSaveReqVO;
import cn.iocoder.byzs.module.bjdx.controller.admin.coursequestion.vo.CourseQuestionRespVO;
import cn.iocoder.byzs.module.bjdx.dal.dataobject.courseconfig.CourseConfigDO;
import cn.iocoder.byzs.module.bjdx.dal.dataobject.coursequestoption.CourseQuestOptionDO;
import cn.iocoder.byzs.module.bjdx.dal.mysql.courseconfig.CourseConfigMapper;
import cn.iocoder.byzs.module.bjdx.dal.mysql.coursequestion.CourseQuestionMapper;
import cn.iocoder.byzs.module.bjdx.dal.mysql.coursequestoption.CourseQuestOptionMapper;
import cn.iocoder.byzs.module.bjdx.service.coursequestion.CourseQuestionService;
import cn.iocoder.byzs.module.bjdx.service.coursequestion.CourseQuestionServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.byzs.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.byzs.module.bjdx.enums.ErrorCodeConstants.COURSE_CONFIG_NOT_EXISTS;

/**
 * 课程配置 Service 实现类
 *
 * @author 博雅智算源码
 */
@Service
@Validated
public class CourseConfigServiceImpl implements CourseConfigService {

    @Resource
    private CourseConfigMapper courseConfigMapper;
    @Resource
    private CourseQuestionService questionService;

    @Override
    public Long createCourseConfig(CourseConfigSaveReqVO createReqVO) {
        // 插入
        CourseConfigDO courseConfig = BeanUtils.toBean(createReqVO, CourseConfigDO.class);
        courseConfigMapper.insert(courseConfig);
        // 返回
        return courseConfig.getId();
    }

    @Override
    public void updateCourseConfig(CourseConfigSaveReqVO updateReqVO) {
        // 校验存在
        validateCourseConfigExists(updateReqVO.getId());
        // 更新
        CourseConfigDO updateObj = BeanUtils.toBean(updateReqVO, CourseConfigDO.class);
        courseConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteCourseConfig(Long id) {
        // 校验存在
        validateCourseConfigExists(id);
        // 删除
        courseConfigMapper.deleteById(id);
    }

    @Override
        public void deleteCourseConfigListByIds(List<Long> ids) {
        // 校验存在
        validateCourseConfigExists(ids);
        // 删除
        courseConfigMapper.deleteByIds(ids);
        }

    private void validateCourseConfigExists(List<Long> ids) {
        List<CourseConfigDO> list = courseConfigMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(COURSE_CONFIG_NOT_EXISTS);
        }
    }

    private void validateCourseConfigExists(Long id) {
        if (courseConfigMapper.selectById(id) == null) {
            throw exception(COURSE_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public CourseConfigDO getCourseConfig(Long id) {
        return courseConfigMapper.selectById(id);
    }

    @Override
    public List<CourseConfigRespVO> getCourseConfigQuestion(Long ccCourseId) {
        //课程配置列表
        CourseConfigPageReqVO courseConfigPageReqVO = new CourseConfigPageReqVO();
        courseConfigPageReqVO.setCcCourseId(Math.toIntExact(ccCourseId))
                .setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CourseConfigPageReqVO> courseConfigPageReqVOS = courseConfigMapper.selectCourseConfigPage(courseConfigPageReqVO);

        //课程配置列表转VO
        List<CourseConfigRespVO> courseConfigList = courseConfigPageReqVOS.stream()
                .map(doItem -> BeanUtils.toBean(doItem, CourseConfigRespVO.class))
                .toList();
        if (courseConfigList.isEmpty()) {return courseConfigList;}

        //设置课程配置-试题列表(引用)
        Map<Long, CourseQuestionRespVO> questionMap = questionService.setCourseQuestionList(courseConfigList.stream().map(CourseConfigRespVO::getCcQuestId).collect(Collectors.toList()));
        courseConfigList.forEach(item -> item.setCourseQuestion(questionMap.get(item.getCcQuestId())));

        return courseConfigList;
    }

    @Override
    public List<CourseConfigRespVO> getCourseConfigQuestionByCourseIds(List<Long> ccCourseIds) {

        //课程配置列表
        List<CourseConfigRespVO> courseConfigList = courseConfigMapper.getCourseConfigQuestionByCourseIds(ccCourseIds);
        if (courseConfigList.isEmpty()) {return courseConfigList;}

        //设置课程配置-试题列表(引用)
        Map<Long, CourseQuestionRespVO> questionMap = questionService.setCourseQuestionList(courseConfigList.stream().map(CourseConfigRespVO::getCcQuestId).collect(Collectors.toList()));
        courseConfigList.forEach(item -> item.setCourseQuestion(questionMap.get(item.getCcQuestId())));

        return courseConfigList;
    }

    @Override
    public PageResult<CourseConfigPageReqVO> getCourseConfigPage(CourseConfigPageReqVO pageReqVO) {
        pageReqVO.setPageNo((pageReqVO.getPageNo() - 1) * pageReqVO.getPageSize());
        List<CourseConfigPageReqVO> courseConfigPageReqVOS = courseConfigMapper.selectCourseConfigPage(pageReqVO);
        Integer count = courseConfigMapper.selectCourseConfigCount(pageReqVO);

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

}