package jnpf.cultivate.event.base.examidentify;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.cultivate.event.base.TriggerProcessor;
import jnpf.model.cultivate.dto.identify.IdentifyApplyDataPushDto;
import jnpf.model.cultivate.event.dto.examidentify.ExamIdentifyTriggerEventDTO;
import jnpf.model.cultivate.po.position.FtbCultivatePositionIdentifyResult;
import jnpf.model.cultivate.resp.TriggerExamDto;

import java.util.Date;
import java.util.Objects;

/**
 * 考试鉴定触发器处理器
 *
 * @author wangchunxiang
 * @date 2024/09/14
 */
public interface ExamIdentifyTriggerProcessor extends TriggerProcessor<ExamIdentifyTriggerEventDTO> {
    int original = 0;
    /**
     * 未触发考试标识
     */
    int EXAM_NOT_TRIGGER = 2;
    /**
     * 未触发鉴定标识
     */
    int IDENTIFY_NOT_TRIGGER = 4;

    @Override
    default void triggerHandle(ExamIdentifyTriggerEventDTO triggerEvent) {
        int i = original;
        // 考试参数构建
        TriggerExamDto triggerExamDto = buildTriggerExamDto(triggerEvent);
        // 考试触发
        if (Objects.nonNull(triggerExamDto)) {
            handleTriggerExam(triggerExamDto);
        } else {
            i = i | EXAM_NOT_TRIGGER;
        }
        // 鉴定参数构建
        IdentifyApplyDataPushDto identifyApplyDataPushDto = buildIdentifyApplyDataPushDto(triggerEvent);
        // 鉴定触发
        if (Objects.nonNull(identifyApplyDataPushDto)) {
            String identificationRecordId = handleTriggerIdentifyApply(identifyApplyDataPushDto);
            // 鉴定触发后置处理
            if (StrUtil.isNotBlank(identificationRecordId)) {
                afterTriggerIdentifyApply(identifyApplyDataPushDto, triggerEvent, identificationRecordId);
            }
        } else {
            boolean hasIndetify = isHasIndetify(triggerEvent);
            if (hasIndetify) {
                return;
            }
            i = i | IDENTIFY_NOT_TRIGGER;

        }
        // 实操鉴定和考试触发后置处理
        postProcessing(i, triggerEvent);
    }

    /**
     * 触发考试
     */
    void handleTriggerExam(TriggerExamDto triggerExam);

    /**
     * 鉴定触发
     *
     * @param identifyApplyDataPushDto 触发鉴定参数
     * @return {@link String } 鉴定记录id
     */
    String handleTriggerIdentifyApply(IdentifyApplyDataPushDto identifyApplyDataPushDto);

    /**
     * 鉴定触发后置处理器
     *
     * @param identifyApplyDataPushDto 触发鉴定参数
     * @param triggerEvent             trigger事件
     * @param identificationRecordId   鉴定记录id
     */
    void afterTriggerIdentifyApply(IdentifyApplyDataPushDto identifyApplyDataPushDto, ExamIdentifyTriggerEventDTO triggerEvent, String identificationRecordId);

    /**
     * 考试参数构建
     */
    TriggerExamDto buildTriggerExamDto(ExamIdentifyTriggerEventDTO triggerEvent);

    /**
     * 鉴定参数构建
     */
    IdentifyApplyDataPushDto buildIdentifyApplyDataPushDto(ExamIdentifyTriggerEventDTO triggerEvent);

    /**
     * 是否有鉴定 主要处理幂等性问题
     *
     * @param triggerEvent
     * @return boolean false-没有  true-有
     */
    boolean isHasIndetify(ExamIdentifyTriggerEventDTO triggerEvent);

    /**
     * 后置处理(主要是证书颁发)
     *
     * @param i            根据或与运算判断那个执行成功
     * @param triggerEvent trigger事件
     */
    default void postProcessing(int i, ExamIdentifyTriggerEventDTO triggerEvent) {
    }

}
