package jnpf.cultivate.event.base.examidentify;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.cultivate.mapper.FtbCultivateCourseMapper;
import jnpf.cultivate.mapper.FtbCultivatePositionIdentifyResultMapper;
import jnpf.cultivate.service.CultivateIdentifyApplyService;
import jnpf.cultivate.service.FtbCultivateExamUserService;
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 lombok.Builder;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * 岗位课程、鉴定和考试触发抽象类
 *
 * @author wangchunxiang
 * @date 2024/09/14
 */
public abstract class CoursePositionTaskTriggerAbstract extends PositionTaskTriggerAbstract {

    @Autowired
    protected FtbCultivatePositionIdentifyResultMapper ftbCultivatePositionIdentifyResultMapper;

    @Autowired
    protected FtbCultivateCourseMapper cultivateCourseMapper;

    @Autowired
    protected CultivateIdentifyApplyService cultivateIdentifyApplyService;

    @Autowired
    protected FtbCultivateExamUserService ftbCultivateExamUserService;

    /**
     * 通用岗位和课程考试触发
     */
    protected TriggerExamDto handleExamTrigger(String userId, String examId, String postRankId, String postLearnId) {
        TriggerExamDto triggerExamDto = new TriggerExamDto();
        triggerExamDto.setUserId(userId);
        triggerExamDto.setExamId(examId);
        triggerExamDto.setRelationPositionId(postRankId);
        triggerExamDto.setRelationId(postLearnId);
        triggerExamHandle(triggerExamDto);
        return triggerExamDto;
    }

    /**
     * 岗位和课程实操鉴定触发
     */
    protected IdentifyApplyDataPushDto handlePracticalIdentification(CoursePositionTaskTriggerAbstract.PracticalIdentificationTrigger practicalIdentificationTrigger) {
        // 鉴定触发一次判断，岗位id+用户id
        LambdaQueryWrapper<FtbCultivatePositionIdentifyResult> ftbCultivatePositionIdentifyResultLambdaQueryWrapper = Wrappers.lambdaQuery();
        ftbCultivatePositionIdentifyResultLambdaQueryWrapper.eq(FtbCultivatePositionIdentifyResult::getUserId, practicalIdentificationTrigger.getUserId());
        if(practicalIdentificationTrigger.type==2) {
            //岗位只能一个鉴定 而且触发了就不在触发    课程可以有多个鉴定
            ftbCultivatePositionIdentifyResultLambdaQueryWrapper.eq(FtbCultivatePositionIdentifyResult::getIdentifyId, practicalIdentificationTrigger.getIdentityId());
        }
        ftbCultivatePositionIdentifyResultLambdaQueryWrapper.eq(FtbCultivatePositionIdentifyResult::getEnabledMark, 0);
        ftbCultivatePositionIdentifyResultLambdaQueryWrapper.eq(FtbCultivatePositionIdentifyResult::getPostRankId, practicalIdentificationTrigger.getPostId());
        ftbCultivatePositionIdentifyResultLambdaQueryWrapper.eq(FtbCultivatePositionIdentifyResult::getType, practicalIdentificationTrigger.type);
        ftbCultivatePositionIdentifyResultLambdaQueryWrapper.last("limit 1");
        if (ftbCultivatePositionIdentifyResultMapper.selectCount(ftbCultivatePositionIdentifyResultLambdaQueryWrapper) == 0) {
            IdentifyApplyDataPushDto identifyApplyDataPushDto = new IdentifyApplyDataPushDto();
            identifyApplyDataPushDto.setDataId(practicalIdentificationTrigger.getPostLearnId());
            identifyApplyDataPushDto.setTableId(practicalIdentificationTrigger.getIdentityId());
            identifyApplyDataPushDto.setIdentifyUserId(practicalIdentificationTrigger.getUserId());
            identifyApplyDataPushDto.setStudyFinishTime(new Date());
            identifyApplyDataPushDto.setIsTurnOnVisibility(practicalIdentificationTrigger.getIsTurnOnVisibility());
            identifyApplyDataPushDto.setPostId(practicalIdentificationTrigger.postId);
            // 鉴定特殊处理
            identifyApplyDataHandle(identifyApplyDataPushDto, practicalIdentificationTrigger);
            return identifyApplyDataPushDto;
        }
        return null;
    }

    @Override
    public void afterTriggerIdentifyApply(IdentifyApplyDataPushDto identifyApplyDataPushDto, ExamIdentifyTriggerEventDTO triggerEvent, String identificationRecordId) {

        if (StringUtils.isBlank(identificationRecordId)) {
            return;
        }
        LambdaQueryWrapper<FtbCultivatePositionIdentifyResult> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(FtbCultivatePositionIdentifyResult::getIdentifyRecordId, identificationRecordId);
        wrapper.eq(FtbCultivatePositionIdentifyResult::getEnabledMark, 0);
        Long count = ftbCultivatePositionIdentifyResultMapper.selectCount(wrapper);
        if (count > 0) {
            return;
        }

        FtbCultivatePositionIdentifyResult ftbCultivatePositionIdentifyResult = new FtbCultivatePositionIdentifyResult();
        ftbCultivatePositionIdentifyResult.setPostLearnId(identifyApplyDataPushDto.getDataId());
        ftbCultivatePositionIdentifyResult.setIdentifyRecordId(identificationRecordId);
        ftbCultivatePositionIdentifyResult.setUserId(identifyApplyDataPushDto.getIdentifyUserId());
        ftbCultivatePositionIdentifyResult.setPostRankId(triggerEvent.getPostId());
        ftbCultivatePositionIdentifyResult.setEnabledMark(0);
        ftbCultivatePositionIdentifyResult.setIdentifyId(identifyApplyDataPushDto.getTableId());
        // 鉴定结果特殊处理
        positionIdentifyDataHandle(ftbCultivatePositionIdentifyResult);
        ftbCultivatePositionIdentifyResultMapper.insert(ftbCultivatePositionIdentifyResult);
    }

    /**
     * 考试触发特殊处理
     */
    public abstract void triggerExamHandle(TriggerExamDto triggerExamDto);

    /**
     * 鉴定特殊处理
     */
    public abstract void identifyApplyDataHandle(IdentifyApplyDataPushDto identifyApplyDataPushDto, CoursePositionTaskTriggerAbstract.PracticalIdentificationTrigger practicalIdentificationTrigger);

    /**
     * 鉴定结果特殊处理
     */
    public abstract void positionIdentifyDataHandle(FtbCultivatePositionIdentifyResult ftbCultivatePositionIdentifyResult);


    @Data
    @Builder
    public static class PracticalIdentificationTrigger {
        /**
         * 用户id(必传)
         */
        String userId;
        /**
         * 岗位id（必传）
         */
        String postId;
        /**
         * 实操鉴定id（必传）
         */
        String identityId;
        /**
         * 岗位学习id
         */
        String postLearnId;
        /**
         * 课程id
         */
        String courseId;
        /**
         * 是否打开可见性, 0 打开, 1 关闭
         * 默认为0
         */
        Integer isTurnOnVisibility;

        /**
         * 来源，0岗位学习实操鉴定，2课程实操鉴定
         */
        Integer type;
    }

}
