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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.cultivate.event.base.examidentify.CoursePositionTaskTriggerAbstract;
import jnpf.cultivate.mapper.FtbCultivatePositionCourseExamMapper;
import jnpf.cultivate.mapper.FtbCultivatePositionCourseIdentityMapper;
import jnpf.model.cultivate.bo.TriggerEventBO;
import jnpf.model.cultivate.dto.identify.IdentifyApplyDataPushDto;
import jnpf.model.cultivate.event.JnpfApplicationEvent;
import jnpf.model.cultivate.event.dto.certificate.CertificateEventDTO;
import jnpf.model.cultivate.event.dto.examidentify.ExamIdentifyTriggerEventDTO;
import jnpf.model.cultivate.po.position.FtbCultivatePositionCourseExam;
import jnpf.model.cultivate.po.position.FtbCultivatePositionCourseIdentity;
import jnpf.model.cultivate.po.position.FtbCultivatePositionIdentifyResult;
import jnpf.model.cultivate.resp.TriggerExamDto;
import jnpf.util.context.SpringContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 岗位学习课程触发器处理器
 *
 * @author wangchunxiang
 * @date 2024/09/14
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class CourseTriggerProcessor extends CoursePositionTaskTriggerAbstract {

    private final FtbCultivatePositionCourseExamMapper ftbCultivatePositionCourseExamMapper;
    private final FtbCultivatePositionCourseIdentityMapper ftbCultivatePositionCourseIdentityMapper;

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


    @Override
    public void triggerExamHandle(TriggerExamDto triggerExamDto) {
        triggerExamDto.setExamSource(1);
    }

    @Override
    public void identifyApplyDataHandle(IdentifyApplyDataPushDto identifyApplyDataPushDto, CoursePositionTaskTriggerAbstract.PracticalIdentificationTrigger practicalIdentificationTrigger) {
        identifyApplyDataPushDto.setSource(1);
    }

    @Override
    public void positionIdentifyDataHandle(FtbCultivatePositionIdentifyResult ftbCultivatePositionIdentifyResult) {
        ftbCultivatePositionIdentifyResult.setType(2);
    }

    @Override
    public TriggerExamDto buildTriggerExamDto(ExamIdentifyTriggerEventDTO triggerEvent) {
        // 发起课程考试
        LambdaQueryWrapper<FtbCultivatePositionCourseExam> courseExamLambdaQueryWrapper = Wrappers.lambdaQuery();
        courseExamLambdaQueryWrapper.eq(FtbCultivatePositionCourseExam::getCourseId, triggerEvent.getCourseId());
        courseExamLambdaQueryWrapper.eq(FtbCultivatePositionCourseExam::getPostRankId, triggerEvent.getPostId());
        courseExamLambdaQueryWrapper.eq(FtbCultivatePositionCourseExam::getEnabledMark, 0);
        courseExamLambdaQueryWrapper.last("limit 1");
        FtbCultivatePositionCourseExam ftbCultivatePositionCourseExam = ftbCultivatePositionCourseExamMapper.selectOne(courseExamLambdaQueryWrapper);
        log.info("忽略此日志，发起课程学习考试参数:{}", ftbCultivatePositionCourseExam);
        if (Objects.nonNull(ftbCultivatePositionCourseExam)) {
            return super.handleExamTrigger(triggerEvent.getUserId(), ftbCultivatePositionCourseExam.getExamId(),
                    ftbCultivatePositionCourseExam.getPostRankId(), ftbCultivatePositionCourseExam.getId());
        }
        return null;
    }

    @Override
    public IdentifyApplyDataPushDto buildIdentifyApplyDataPushDto(ExamIdentifyTriggerEventDTO triggerEvent) {
        // 发起课程实操鉴定
        LambdaQueryWrapper<FtbCultivatePositionCourseIdentity> courseIdentityLambdaQueryWrapper = Wrappers.lambdaQuery();
        courseIdentityLambdaQueryWrapper.eq(FtbCultivatePositionCourseIdentity::getCourseId, triggerEvent.getCourseId());
        courseIdentityLambdaQueryWrapper.eq(FtbCultivatePositionCourseIdentity::getPostRankId, triggerEvent.getPostId());
        courseIdentityLambdaQueryWrapper.eq(FtbCultivatePositionCourseIdentity::getEnabledMark, 0);
        courseIdentityLambdaQueryWrapper.last("limit 1");
        FtbCultivatePositionCourseIdentity ftbCultivatePositionCourseIdentity = ftbCultivatePositionCourseIdentityMapper.selectOne(courseIdentityLambdaQueryWrapper);
        log.info("忽略此日志，发起课程学习实操鉴定参数:{}", ftbCultivatePositionCourseIdentity);
        if (Objects.nonNull(ftbCultivatePositionCourseIdentity)) {
            CoursePositionTaskTriggerAbstract.PracticalIdentificationTrigger practicalIdentificationTrigger = CoursePositionTaskTriggerAbstract.PracticalIdentificationTrigger.builder()
                    .userId(triggerEvent.getUserId())
                    .postId(triggerEvent.getPostId())
                    .identityId(ftbCultivatePositionCourseIdentity.getIdentityId())
                    .postLearnId(ftbCultivatePositionCourseIdentity.getPostLearnId())
                    .courseId(triggerEvent.getCourseId())
                    .type(2)
                    .build();
            return super.handlePracticalIdentification(practicalIdentificationTrigger);
        }
        return null;
    }

    @Override
    public boolean isHasIndetify(ExamIdentifyTriggerEventDTO triggerEvent) {
        // 发起课程实操鉴定
        LambdaQueryWrapper<FtbCultivatePositionCourseIdentity> courseIdentityLambdaQueryWrapper = Wrappers.lambdaQuery();
        courseIdentityLambdaQueryWrapper.eq(FtbCultivatePositionCourseIdentity::getCourseId, triggerEvent.getCourseId());
        courseIdentityLambdaQueryWrapper.eq(FtbCultivatePositionCourseIdentity::getPostRankId, triggerEvent.getPostId());
        courseIdentityLambdaQueryWrapper.eq(FtbCultivatePositionCourseIdentity::getEnabledMark, 0);
        courseIdentityLambdaQueryWrapper.last("limit 1");
        FtbCultivatePositionCourseIdentity ftbCultivatePositionCourseIdentity = ftbCultivatePositionCourseIdentityMapper.selectOne(courseIdentityLambdaQueryWrapper);
        log.info("忽略此日志，发起课程学习实操鉴定参数:{}", ftbCultivatePositionCourseIdentity);
        if (Objects.nonNull(ftbCultivatePositionCourseIdentity)) {
           return true;
        }
        return false;
    }

    @Override
    public void postProcessing(int i, ExamIdentifyTriggerEventDTO triggerEvent) {
        // 无实操鉴定和考试，直接颁发课程证书
        if (((i & EXAM_NOT_TRIGGER) == EXAM_NOT_TRIGGER) && ((i & IDENTIFY_NOT_TRIGGER) == IDENTIFY_NOT_TRIGGER)) {
            // 颁发证书   0 课程证书 1 岗位学习证书, 2 任务证书
            Integer issuanceType=0;
//            if (StringUtils.isNotEmpty(triggerEvent.getPostId())) {
//                issuanceType=1;
//            }
            if (StringUtils.isNotEmpty(triggerEvent.getTaskId())) {
                issuanceType=2;
            }
            SpringContext.getApplicationContext()
                    .publishEvent(new JnpfApplicationEvent<>(
                            CertificateEventDTO.builder()
                                    .source(0)
                                    .courseId(triggerEvent.getCourseId())
                                    .issuanceType(issuanceType)
                                    .postId(triggerEvent.getPostId())
                                    .userId(triggerEvent.getUserId())
                                    .taskId(triggerEvent.getTaskId())
                                    .build()));
        }
    }
}
