package jnpf.cultivate.event.base.course;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.cultivate.mapper.FtbCultivatePositionCourceChapterLearningMapper;
import jnpf.cultivate.mapper.FtbCultivatePositionCourceLearningMapper;
import jnpf.cultivate.service.FtbCultivateCourseSettingService;
import jnpf.cultivate.service.FtbCultivateCourseTriggerLogService;
import jnpf.model.cultivate.event.dto.course.CourseProcessLearn;
import jnpf.model.cultivate.po.course.FtbCultivateCourseTriggerLog;
import jnpf.model.cultivate.po.position.FtbCultivatePositionCourceChapterLearning;
import jnpf.model.cultivate.po.position.FtbCultivatePositionCourceLearning;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

public abstract class CourseProcessLearnAbstract<T extends CourseProcessLearn> {

    @Resource
    protected FtbCultivatePositionCourceLearningMapper ftbCultivatePositionCourceLearningMapper;

    @Resource
    protected FtbCultivatePositionCourceChapterLearningMapper ftbCultivatePositionCourceChapterLearningMapper;

    @Resource
    protected FtbCultivateCourseSettingService ftbCultivateCourseSettingService;

    @Resource
    private FtbCultivateCourseTriggerLogService ftbCultivateCourseTriggerLogService;

    protected void courseProcessLearn(T baseData) {
        // 学习课程章节进度
        LambdaUpdateWrapper<FtbCultivatePositionCourceChapterLearning> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(FtbCultivatePositionCourceChapterLearning::getChapterId, baseData.getChapterId());
        updateWrapper.eq(FtbCultivatePositionCourceChapterLearning::getUserId, baseData.getUserId());
        updateWrapper.eq(FtbCultivatePositionCourceChapterLearning::getEnabledMark, 0);
        updateWrapper.set(FtbCultivatePositionCourceChapterLearning::getState, 1);
        updateWrapper.set(FtbCultivatePositionCourceChapterLearning::getChapterTime, baseData.getDuration());
        ftbCultivatePositionCourceChapterLearningMapper.update(new FtbCultivatePositionCourceChapterLearning(), updateWrapper);
        // 判断章节是否全部学习完成，学习完成，变更课程为全部学习完成状态
        LambdaQueryWrapper<FtbCultivatePositionCourceChapterLearning> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbCultivatePositionCourceChapterLearning::getCourceId, baseData.getCourseId());
        queryWrapper.eq(FtbCultivatePositionCourceChapterLearning::getUserId, baseData.getUserId());
        // 是否存在章节状态未学习
        queryWrapper.eq(FtbCultivatePositionCourceChapterLearning::getState, 0);
        queryWrapper.eq(FtbCultivatePositionCourceChapterLearning::getEnabledMark, 0);
        // 学习课程状态变更
        LambdaQueryWrapper<FtbCultivatePositionCourceLearning> courceLearningLambdaQueryWrapper = Wrappers.lambdaQuery();
        courceLearningLambdaQueryWrapper.eq(FtbCultivatePositionCourceLearning::getCourceId, baseData.getCourseId());
        courceLearningLambdaQueryWrapper.eq(FtbCultivatePositionCourceLearning::getUserId, baseData.getUserId());
        courceLearningLambdaQueryWrapper.eq(FtbCultivatePositionCourceLearning::getEnabledMark, 0);
        FtbCultivatePositionCourceLearning ftbCultivatePositionCourceLearning = ftbCultivatePositionCourceLearningMapper.selectOne(courceLearningLambdaQueryWrapper);
        if (ftbCultivatePositionCourceLearning.getState() == 0) {
            // 课程学习中
            ftbCultivatePositionCourceLearning.setState(2);
        }
        // 课程学习完后实操鉴定和考试触发
        if (ftbCultivatePositionCourceChapterLearningMapper.selectCount(queryWrapper) == 0) {
            ftbCultivatePositionCourceLearning.setState(1);
            courseLearnCompleteHandle(baseData);
            saveTriggerLog(baseData);
        }
        ftbCultivatePositionCourceLearningMapper.updateById(ftbCultivatePositionCourceLearning);
        // 随堂测试结果存储
        ftbCultivateCourseSettingService.onSiteTestResultStorage(baseData.getFtbChapterTestDTOs(),
                baseData.getCourseId(), baseData.getChapterId());
    }


    private void saveTriggerLog(T baseData) {
        if(baseData.getStudyType()==null || StringUtils.isEmpty(baseData.getRelationBusinessId())){
            return;
        }
        FtbCultivateCourseTriggerLog log = new FtbCultivateCourseTriggerLog();
        log.setBusinessId(baseData.getCourseId());
        log.setType(baseData.getStudyType());
        log.setUserId(baseData.getUserId());
        log.setCourseId(baseData.getCourseId());
        log.setBusinessId(baseData.getRelationBusinessId());
        log.setCreatorUserId(baseData.getUserId());
        log.setCreatorTime(new Date());
        ftbCultivateCourseTriggerLogService.save(log);
    }


    /**
     * 查询课是否开始学习
     *
     * @param userId    用户ID
     * @param courseIds 课程ID集合
     * @return boolean  false-未开始学习  true-开始学习
     */
    protected boolean checkCourseStudyStatus(String userId, List<String> courseIds) {
        LambdaQueryWrapper<FtbCultivatePositionCourceLearning> query = Wrappers.lambdaQuery();
        query.eq(FtbCultivatePositionCourceLearning::getUserId, userId);
        query.in(FtbCultivatePositionCourceLearning::getCourceId, courseIds);
        query.in(FtbCultivatePositionCourceLearning::getState, List.of("1"));//学习状态 1已学习，0未学习,2学习中
        query.eq(FtbCultivatePositionCourceLearning::getEnabledMark, 0);
        return ftbCultivatePositionCourceLearningMapper.selectCount(query) >0;
    }

    /**
     * 该用户是否完成课程
     *
     * @param userId    用户ID
     * @param courseIds 课程ID集合
     * @return boolean
     */
    protected boolean hasTheCourseBeenCompleted(String userId, List<String> courseIds) {
        LambdaQueryWrapper<FtbCultivatePositionCourceLearning> query = Wrappers.lambdaQuery();
        query.eq(FtbCultivatePositionCourceLearning::getUserId, userId);
        query.in(FtbCultivatePositionCourceLearning::getCourceId, courseIds);
        query.in(FtbCultivatePositionCourceLearning::getState, List.of("0", "2"));
        query.eq(FtbCultivatePositionCourceLearning::getEnabledMark, 0);
        return ftbCultivatePositionCourceLearningMapper.selectCount(query) == 0;
    }

    /**
     * 课程学习完成后置处理，用于触发课程相关的逻辑
     *
     * @param baseData
     */
    public abstract void courseLearnCompleteHandle(T baseData);


}
