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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.cultivate.event.base.examidentify.PositionTaskTriggerAbstract;
import jnpf.cultivate.mapper.FtbCultivateLearnTaskExamMapper;
import jnpf.cultivate.mapper.FtbCultivateLearnTaskIdentificationMapper;
import jnpf.cultivate.service.FtbCultivateMyLearnTaskInfoService;
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.learn.FtbCultivateLearnTaskExam;
import jnpf.model.cultivate.po.learn.FtbCultivateLearnTaskIdentification;
import jnpf.model.cultivate.resp.TriggerExamDto;
import jnpf.util.context.SpringContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 任务学习触发器处理器
 *
 * @author wangchunxiang
 * @date 2024/09/18
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class TaskTriggerProcessor extends PositionTaskTriggerAbstract {

    private final FtbCultivateLearnTaskExamMapper ftbCultivateLearnTaskExamMapper;
    private final FtbCultivateLearnTaskIdentificationMapper ftbCultivateLearnTaskIdentificationMapper;
    private final FtbCultivateMyLearnTaskInfoService ftbCultivateMyLearnTaskInfoService;

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

    @Override
    public void afterTriggerIdentifyApply(IdentifyApplyDataPushDto identifyApplyDataPushDto, ExamIdentifyTriggerEventDTO triggerEvent, String identificationRecordId) {
        // ignore 鉴定触发后不做任何处理
    }


    @Override
    public TriggerExamDto buildTriggerExamDto(ExamIdentifyTriggerEventDTO triggerEvent) {
        // 发起任务考试
        LambdaQueryWrapper<FtbCultivateLearnTaskExam> taskExamQueryWrapper = Wrappers.lambdaQuery();
        taskExamQueryWrapper.eq(FtbCultivateLearnTaskExam::getEnableMark, 0);
        taskExamQueryWrapper.eq(FtbCultivateLearnTaskExam::getTaskId, triggerEvent.getTaskId());
        taskExamQueryWrapper.last("limit 1");
        FtbCultivateLearnTaskExam ftbCultivateLearnTaskExam = ftbCultivateLearnTaskExamMapper.selectOne(taskExamQueryWrapper);
        log.info("任务学习触发器发起考试参数:{}", ftbCultivateLearnTaskExam);
        if (Objects.nonNull(ftbCultivateLearnTaskExam)) {
            TriggerExamDto triggerExamDto = new TriggerExamDto();
            triggerExamDto.setExamSource(4);
            triggerExamDto.setRelationTaskId(triggerEvent.getTaskId());
            triggerExamDto.setExamId(ftbCultivateLearnTaskExam.getExamId());
            triggerExamDto.setUserId(triggerEvent.getUserId());
            return triggerExamDto;
        }
        return null;
    }

    @Override
    public IdentifyApplyDataPushDto buildIdentifyApplyDataPushDto(ExamIdentifyTriggerEventDTO triggerEvent) {
        // 发起任务鉴定
        LambdaQueryWrapper<FtbCultivateLearnTaskIdentification> identityQuery = Wrappers.lambdaQuery();
        identityQuery.eq(FtbCultivateLearnTaskIdentification::getEnableMark, 0);
        identityQuery.eq(FtbCultivateLearnTaskIdentification::getTaskId, triggerEvent.getTaskId());
        identityQuery.last("limit 1");
        FtbCultivateLearnTaskIdentification ftbCultivateLearnTaskIdentification = ftbCultivateLearnTaskIdentificationMapper.selectOne(identityQuery);
        log.info("任务学习触发器发起鉴定参数:{}", ftbCultivateLearnTaskIdentification);
        if (Objects.nonNull(ftbCultivateLearnTaskIdentification)) {
            IdentifyApplyDataPushDto identifyApplyDataPushDto = new IdentifyApplyDataPushDto();
            identifyApplyDataPushDto.setTableId(ftbCultivateLearnTaskIdentification.getIdentificationId());
            identifyApplyDataPushDto.setIdentifyUserId(triggerEvent.getUserId());
            identifyApplyDataPushDto.setSource(4);//鉴定来源(0手动发起，1课程学习鉴定，2岗位学习鉴定，3本人申请4,任务鉴定)'
            identifyApplyDataPushDto.setDataId(triggerEvent.getTaskId());
            identifyApplyDataPushDto.setPostId(triggerEvent.getPostId());
            // 鉴定规则(0否,1,是 考试合格才能鉴定)    是否打开可见性, 0 打开, 1 关闭
            identifyApplyDataPushDto.setIsTurnOnVisibility(0);
            if (ftbCultivateLearnTaskIdentification.getIdentificationRule() == 1) {
                identifyApplyDataPushDto.setIsTurnOnVisibility(1);
            }
            return identifyApplyDataPushDto;
        }
        return null;
    }

    @Override
    public boolean isHasIndetify(ExamIdentifyTriggerEventDTO triggerEvent) {
        // 发起任务鉴定
        LambdaQueryWrapper<FtbCultivateLearnTaskIdentification> identityQuery = Wrappers.lambdaQuery();
        identityQuery.eq(FtbCultivateLearnTaskIdentification::getEnableMark, 0);
        identityQuery.eq(FtbCultivateLearnTaskIdentification::getTaskId, triggerEvent.getTaskId());
        identityQuery.last("limit 1");
        FtbCultivateLearnTaskIdentification ftbCultivateLearnTaskIdentification = ftbCultivateLearnTaskIdentificationMapper.selectOne(identityQuery);
        log.info("任务学习触发器发起鉴定参数:{}", ftbCultivateLearnTaskIdentification);
        if (Objects.nonNull(ftbCultivateLearnTaskIdentification)) {
           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)) {
            // 更新用户完成时间
            ftbCultivateMyLearnTaskInfoService.updateUserFinishTime(triggerEvent.getTaskId(), triggerEvent.getUserId(), 0);
            // 颁发证书
            SpringContext.getApplicationContext()
                    .publishEvent(new JnpfApplicationEvent<>(
                            CertificateEventDTO.builder()
                                    .source(0)
                                    .taskId(triggerEvent.getTaskId())
                                    .issuanceType(2)
                                    .userId(triggerEvent.getUserId())
                                    .build()));
        }
    }


}
