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.FtbCultivatePositionExamIdentifyMapper;
import jnpf.cultivate.mapper.FtbCultivatePositionExamMapper;
import jnpf.cultivate.mapper.FtbCultivatePositionIdentifyResultMapper;
import jnpf.cultivate.service.FtbCultivatePromotionNewService;
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.FtbCultivatePositionExam;
import jnpf.model.cultivate.po.position.FtbCultivatePositionExamIdentify;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 岗位学习考试鉴定触发处理器
 *
 * @author wangchunxiang
 * @date 2024/09/14
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class PositionTriggerProcessor extends CoursePositionTaskTriggerAbstract {

    private final FtbCultivatePositionExamIdentifyMapper ftbCultivatePositionExamIdentifyMapper;
    private final FtbCultivatePositionExamMapper ftbCultivatePositionExamMapper;
    private final FtbCultivatePromotionNewService ftbCultivatePromotionNewService;

    private final FtbCultivatePositionIdentifyResultMapper ftbCultivatePositionIdentifyResultMapper;

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

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

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

    @Override
    public void postProcessing(int i, ExamIdentifyTriggerEventDTO triggerEvent) {
        // 学习地图标识改岗位学习完成计算学习阶段
        ftbCultivatePromotionNewService.changeTheCurrentPersonSLearningStage(triggerEvent.getUserId(), triggerEvent.getPostId());

        // 无实操鉴定和考试，直接颁发岗位学习证书
        if (((i & EXAM_NOT_TRIGGER) == EXAM_NOT_TRIGGER) && ((i & IDENTIFY_NOT_TRIGGER) == IDENTIFY_NOT_TRIGGER)) {
            // 颁发证书
            SpringContext.getApplicationContext()
                    .publishEvent(new JnpfApplicationEvent<>(
                            CertificateEventDTO.builder()
                                    .source(0)
                                    .postId(triggerEvent.getPostId())
                                    .issuanceType(1)
                                    .userId(triggerEvent.getUserId())
                                    .build()));
        }

    }

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

    @Override
    public TriggerExamDto buildTriggerExamDto(ExamIdentifyTriggerEventDTO triggerEvent) {
        String postId = triggerEvent.getPostId();
        String userId = triggerEvent.getUserId();
        // 发起岗位考试
        LambdaQueryWrapper<FtbCultivatePositionExam> ftbCultivatePositionExamLambdaQueryWrapper = Wrappers.lambdaQuery();
        ftbCultivatePositionExamLambdaQueryWrapper.eq(FtbCultivatePositionExam::getPostRankId, postId);
        ftbCultivatePositionExamLambdaQueryWrapper.eq(FtbCultivatePositionExam::getEnabledMark, 0);
        ftbCultivatePositionExamLambdaQueryWrapper.last("limit 1");
        FtbCultivatePositionExam ftbCultivatePositionExam = ftbCultivatePositionExamMapper.selectOne(ftbCultivatePositionExamLambdaQueryWrapper);
        log.info("课程列表页调用触发忽略此日志，发起岗位学习考试参数:{}", ftbCultivatePositionExam);
        TriggerExamDto triggerExamDto = null;
        if (Objects.nonNull(ftbCultivatePositionExam)) {
            triggerExamDto = super.handleExamTrigger(userId, ftbCultivatePositionExam.getExamId()
                    , ftbCultivatePositionExam.getPostRankId(), ftbCultivatePositionExam.getId());
        }
        return triggerExamDto;
    }

    @Override
    public IdentifyApplyDataPushDto buildIdentifyApplyDataPushDto(ExamIdentifyTriggerEventDTO triggerEvent) {
        String postId = triggerEvent.getPostId();
        String userId = triggerEvent.getUserId();
        // 发起岗位实操鉴定
        LambdaQueryWrapper<FtbCultivatePositionExamIdentify> examIdentifyLambdaQueryWrapper = Wrappers.lambdaQuery();
        examIdentifyLambdaQueryWrapper.eq(FtbCultivatePositionExamIdentify::getPostRankId, postId);
        examIdentifyLambdaQueryWrapper.eq(FtbCultivatePositionExamIdentify::getEnabledMark, 0);
        examIdentifyLambdaQueryWrapper.last("limit 1");
        FtbCultivatePositionExamIdentify ftbCultivatePositionExamIdentify = ftbCultivatePositionExamIdentifyMapper.selectOne(examIdentifyLambdaQueryWrapper);
        log.info("课程列表页调用触发忽略此日志，发起岗位学习实操鉴定参数:{}", ftbCultivatePositionExamIdentify);
        if (Objects.nonNull(ftbCultivatePositionExamIdentify)) {
            // 岗位考试合格后才能进行鉴定
            Integer qualified = super.doJobQualificationTrigger(postId);
            CoursePositionTaskTriggerAbstract.PracticalIdentificationTrigger practicalIdentificationTrigger = CoursePositionTaskTriggerAbstract.PracticalIdentificationTrigger.builder()
                    .userId(userId)
                    .postId(postId)
                    .identityId(ftbCultivatePositionExamIdentify.getIdentifyId())
                    .postLearnId(ftbCultivatePositionExamIdentify.getPostLearnId())
                    .isTurnOnVisibility(qualified == 0 ? 0 : 1)
                    .type(0)
                    .build();
            return super.handlePracticalIdentification(practicalIdentificationTrigger);
        }
        return null;
    }

    /**
     * 是否有鉴定
     * @param triggerEvent
     * @return
     */
    @Override
    public boolean isHasIndetify(ExamIdentifyTriggerEventDTO triggerEvent) {
        String postId = triggerEvent.getPostId();
        String userId = triggerEvent.getUserId();
        // 发起岗位实操鉴定
        LambdaQueryWrapper<FtbCultivatePositionExamIdentify> examIdentifyLambdaQueryWrapper = Wrappers.lambdaQuery();
        examIdentifyLambdaQueryWrapper.eq(FtbCultivatePositionExamIdentify::getPostRankId, postId);
        examIdentifyLambdaQueryWrapper.eq(FtbCultivatePositionExamIdentify::getEnabledMark, 0);
        examIdentifyLambdaQueryWrapper.last("limit 1");
        FtbCultivatePositionExamIdentify ftbCultivatePositionExamIdentify = ftbCultivatePositionExamIdentifyMapper.selectOne(examIdentifyLambdaQueryWrapper);
        log.info("课程列表页调用触发忽略此日志，发起岗位学习实操鉴定参数:{}", ftbCultivatePositionExamIdentify);
        if (Objects.nonNull(ftbCultivatePositionExamIdentify)) {
            return true;
        }
        return false;
    }


}
