package jnpf.cultivate.event.impl.trigger.position;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.cultivate.event.base.TriggerProcessor;
import jnpf.cultivate.event.base.course.CourseProcessLearnAbstract;
import jnpf.cultivate.mapper.FtbCultivateCourseMapper;
import jnpf.cultivate.mapper.FtbCultivatePositionCourseMapper;
import jnpf.model.cultivate.bo.TriggerEventBO;
import jnpf.model.cultivate.event.JnpfApplicationEvent;
import jnpf.model.cultivate.event.dto.examidentify.ExamIdentifyTriggerEventDTO;
import jnpf.model.cultivate.event.dto.position.PositionCourseProcessDTO;
import jnpf.model.cultivate.po.course.FtbCultivateCourse;
import jnpf.model.cultivate.po.position.FtbCultivatePositionCourse;
import jnpf.util.context.SpringContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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

@Component
@RequiredArgsConstructor
@Slf4j
public class PositionProcessLearnProcessor extends CourseProcessLearnAbstract<PositionCourseProcessDTO> implements TriggerProcessor<PositionCourseProcessDTO> {

    private final FtbCultivatePositionCourseMapper ftbCultivatePositionCourseMapper;
    private final FtbCultivateCourseMapper ftbCultivateCourseMapper;

    @Override
    public boolean triggerTypeConfirmation(TriggerEventBO.TriggerEventType triggerEventType) {
        return triggerEventType == TriggerEventBO.TriggerEventType.POSITION_COURSE_EVENT;
    }

    @Override
    public void triggerHandle(PositionCourseProcessDTO triggerEvent) {
        super.courseProcessLearn(triggerEvent);
        // 本岗位必修课程学习是否完成
        LambdaQueryWrapper<FtbCultivatePositionCourse> ftbCultivatePositionCourseLambdaQueryWrapper = Wrappers.lambdaQuery();
        ftbCultivatePositionCourseLambdaQueryWrapper.select(FtbCultivatePositionCourse::getCourseId);
        ftbCultivatePositionCourseLambdaQueryWrapper.eq(FtbCultivatePositionCourse::getPostRankId, triggerEvent.getPostId());
        ftbCultivatePositionCourseLambdaQueryWrapper.eq(FtbCultivatePositionCourse::getEnabledMark, 0);
        ftbCultivatePositionCourseLambdaQueryWrapper.eq(FtbCultivatePositionCourse::getCompulsory, 0);
        List<FtbCultivatePositionCourse> ftbCultivatePositionCourses = ftbCultivatePositionCourseMapper.selectList(ftbCultivatePositionCourseLambdaQueryWrapper);
        List<String> courseIds = ftbCultivatePositionCourses.stream().map(FtbCultivatePositionCourse::getCourseId).collect(Collectors.toList());
        //岗位学习的课程必须有一个必学
        if (CollUtil.isEmpty(courseIds)) {
            log.info("本岗位课程被删除，不触发岗位考试和岗位实操鉴定，参数为:{}", triggerEvent);
            return;
        }

        //找到上架的岗位学习课程
        QueryWrapper<FtbCultivateCourse> courseQuery = new QueryWrapper<>();
        courseQuery.lambda()
                .in(FtbCultivateCourse::getId, courseIds)
                .eq(FtbCultivateCourse::getEnableMark, 0)
                .eq(FtbCultivateCourse::getIsGroundIng, 1);
        List<FtbCultivateCourse> courseList = ftbCultivateCourseMapper.selectList(courseQuery);
        if (CollectionUtil.isEmpty(courseList)) {
            return;
        }
        courseIds = courseList.stream().map(FtbCultivateCourse::getId).collect(Collectors.toList());


        boolean b = super.hasTheCourseBeenCompleted(triggerEvent.getUserId(), courseIds);
        log.error("courseIds:{},userId={},b={}", courseIds, triggerEvent.getUserId(), b);
        if (b) {
            SpringContext.getApplicationContext()
                    .publishEvent(new JnpfApplicationEvent<>(
                            ExamIdentifyTriggerEventDTO.builder()
                                    .postId(triggerEvent.getPostId())
                                    .userId(triggerEvent.getUserId())
                                    .triggerEventType(TriggerEventBO.TriggerEventType.POST)
                                    .build()));
        }
    }

    @Override
    public void courseLearnCompleteHandle(PositionCourseProcessDTO baseData) {
        SpringContext.getApplicationContext()
                .publishEvent(new JnpfApplicationEvent<>(
                        ExamIdentifyTriggerEventDTO.builder()
                                .courseId(baseData.getCourseId())
                                .postId(baseData.getPostId())
                                .userId(baseData.getUserId())
                                .triggerEventType(TriggerEventBO.TriggerEventType.POSITION_COURSE)
                                .build()));
    }

}
