package jnpf.cultivate.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import jnpf.base.ActionResult;
import jnpf.base.entity.SuperBaseEntity;
import jnpf.cultivate.mapper.*;
import jnpf.cultivate.service.*;
import jnpf.cultivate.utils.UserApiV2Util;
import jnpf.entity.cultivate.CultivateIdentifyTable;
import jnpf.model.cultivate.dto.position.*;
import jnpf.model.cultivate.dto.position.web.FtbCultivatePositionJobLearnCourseCertificateDTO;
import jnpf.model.cultivate.dto.position.web.FtbCultivatePositionJobLearnCourseStateSwitchDTO;
import jnpf.model.cultivate.dto.position.web.FtbJobLearnCourseRetakeCertificateDTO;
import jnpf.model.cultivate.entiy.BaseEntity;
import jnpf.model.cultivate.event.JnpfApplicationEvent;
import jnpf.model.cultivate.event.dto.course.PositionCourseEventDTO;
import jnpf.model.cultivate.po.FtbCultivatePositionCourseCertificate;
import jnpf.model.cultivate.po.certificate.FtbCertificateEntity;
import jnpf.model.cultivate.po.course.FtbCultivateCourse;
import jnpf.model.cultivate.po.exam.FtbCultivateExam;
import jnpf.model.cultivate.po.mesgg.CultivateMessageInfo;
import jnpf.model.cultivate.po.position.*;
import jnpf.model.cultivate.resp.InnerExamDto;
import jnpf.model.cultivate.resp.PaperVo;
import jnpf.model.cultivate.v2.promotion.vo.V2CultivatePositionVo;
import jnpf.model.cultivate.v2.promotion.vo.V2CultivatePostAndGradeVo;
import jnpf.model.cultivate.vo.identify.IdentifyTableInfoApiVo;
import jnpf.model.cultivate.vo.position.*;
import jnpf.model.cultivate.vo.promotion.FtbCultivatePromotionLevelMapVO;
import jnpf.permission.vo.v2.grades.GradeVO;
import jnpf.permission.vo.v2.position.PositionVO;
import jnpf.util.StringUtil;
import jnpf.util.context.SpringContext;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class FtbCultivatePositionServiceImpl extends ServiceImpl<FtbCultivatePositionMapper, FtbCultivatePosition> implements FtbCultivatePositionService {

    protected Log log = LogFactory.getLog(getClass());

    private final FtbCultivatePositionExamMapper ftbCultivatePositionExamMapper;
    private final FtbCultivatePositionExamIdentifyMapper ftbCultivatePositionExamIdentifyMapper;
    private final FtbCultivatePositionCourseExamMapper ftbCultivatePositionCourseExamMapper;
    private final FtbCultivatePositionCourseMapper ftbCultivatePositionCourseMapper;
    private final FtbCultivatePositionCourseIdentityMapper ftbCultivatePositionCourseIdentityMapper;
    private final FtbCultivateExamMapper ftbCultivateExamMapper;
    private final CultivateIdentifyTableMapper cultivateIdentifyTableMapper;
    private final FtbCultivateExamService ftbCultivateExamService;
    private final CultivateIdentifyTableService cultivateIdentifyTableService;
    private final FtbCultivateCourseMapper ftbCultivateCourseMapper;
    private final FtbCultivateMessageInfoMapper ftbCultivateMessageInfoMapper;
    private final FtbCultivateExamUserService ftbCultivateExamUserService;
    private final FtbCultivatePromotionNewService ftbCultivatePromotionNewService;
    private final FtbCultivatePositionCertificateMapper ftbCultivatePositionCertificateMapper;
    private final FtbCultivateCertificateMapper ftbCultivateCertificateMapper;
    private final FtbCultivatePositionCourseCertificateMapper ftbCultivatePositionCourseCertificateMapper;
    private final FtbCultivatePositionSettingMapper ftbCultivatePositionSettingMapper;

    @Autowired
    private UserApiV2Util userApiV2Util;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addJobLearning(FtbCultivatePositionJobLearnDTO ftbCultivatePositionJobLearnDTO) {
        LambdaQueryWrapper<FtbCultivatePosition> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbCultivatePosition::getPostId, ftbCultivatePositionJobLearnDTO.getPostId());
        queryWrapper.eq(FtbCultivatePosition::getEnabledMark, 0);
        if (baseMapper.selectCount(queryWrapper) > 0) {
            throw new RuntimeException("该岗位已存在学习记录");
        }
        FtbCultivatePosition ftbCultivatePosition = ftbCultivatePositionJobLearnDTO.convertFtbCultivatePosition(ftbCultivatePositionJobLearnDTO);
        baseMapper.insert(ftbCultivatePosition);
        return ftbCultivatePosition.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void upperLower(FtbCultivatePositionShelvesDTO ftbCultivateShelvesDTO) {
        LambdaUpdateWrapper<FtbCultivatePosition> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(SuperBaseEntity.SuperIBaseEntity::getId, ftbCultivateShelvesDTO.getId());
        updateWrapper.set(FtbCultivatePosition::getIsGrounding, ftbCultivateShelvesDTO.getIsGrounding());
        baseMapper.update(new FtbCultivatePosition(), updateWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addJobLearningExam(FtbCultivatePositionJobLearnExamDTO ftbCultivatePositionJobLearnExamDTO) {
        jobLearningExaminationJudgment(ftbCultivatePositionJobLearnExamDTO.getPostLearnId(), ftbCultivatePositionJobLearnExamDTO.getExamId());
        // 是否存在，存在则更新
        LambdaQueryWrapper<FtbCultivatePositionExam> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbCultivatePositionExam::getEnabledMark, 0);
        queryWrapper.eq(FtbCultivatePositionExam::getPostLearnId, ftbCultivatePositionJobLearnExamDTO.getPostLearnId());
        queryWrapper.eq(FtbCultivatePositionExam::getPostRankId, ftbCultivatePositionJobLearnExamDTO.getPostRankId());
        FtbCultivatePositionExam exam = ftbCultivatePositionExamMapper.selectOne(queryWrapper);
        if (Objects.nonNull(exam)) {
            exam.setExamId(ftbCultivatePositionJobLearnExamDTO.getExamId());
            ftbCultivatePositionExamMapper.updateById(exam);
            return;
        }
        FtbCultivatePositionExam ftbCultivatePositionExam = ftbCultivatePositionJobLearnExamDTO
                .convertFtbCultivatePositionExam(ftbCultivatePositionJobLearnExamDTO);
        ftbCultivatePositionExamMapper.insert(ftbCultivatePositionExam);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addPostLearningPracticalAppraisal(FtbCultivatePositionLearnPracticalDTO ftbCultivatePositionLearnPracticalDTO) {
        onTheJobLearningPracticalAppraisal(ftbCultivatePositionLearnPracticalDTO.getPostLearnId(),
                ftbCultivatePositionLearnPracticalDTO.getIdentifyId());
        // 是否存在，存在则更新
        LambdaQueryWrapper<FtbCultivatePositionExamIdentify> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbCultivatePositionExamIdentify::getPostLearnId, ftbCultivatePositionLearnPracticalDTO.getPostLearnId());
        queryWrapper.eq(FtbCultivatePositionExamIdentify::getEnabledMark, 0);
        queryWrapper.eq(FtbCultivatePositionExamIdentify::getPostRankId, ftbCultivatePositionLearnPracticalDTO.getPostRankId());
        FtbCultivatePositionExamIdentify identify = ftbCultivatePositionExamIdentifyMapper.selectOne(queryWrapper);
        if (Objects.nonNull(identify)) {
            identify.setIdentifyId(ftbCultivatePositionLearnPracticalDTO.getIdentifyId());
            ftbCultivatePositionExamIdentifyMapper.updateById(identify);
            return;
        }
        FtbCultivatePositionExamIdentify ftbCultivatePositionExamIdentify = ftbCultivatePositionLearnPracticalDTO
                .convertFtbCultivatePositionExamIdentify(ftbCultivatePositionLearnPracticalDTO);
        ftbCultivatePositionExamIdentifyMapper.insert(ftbCultivatePositionExamIdentify);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addJobLearningCourses(FtbCultivatePositionLearingCourselDTO ftbCultivatePositionLearnPracticalDTO) {
        // 校验是否重复添加
        LambdaQueryWrapper<FtbCultivatePositionCourse> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbCultivatePositionCourse::getPostRankId, ftbCultivatePositionLearnPracticalDTO.getPostRankId());
        queryWrapper.eq(FtbCultivatePositionCourse::getPostLearnId, ftbCultivatePositionLearnPracticalDTO.getPostLearnId());
        queryWrapper.in(FtbCultivatePositionCourse::getCourseId, ftbCultivatePositionLearnPracticalDTO.getCourseIds());
        queryWrapper.eq(FtbCultivatePositionCourse::getEnabledMark, 0);
        if (ftbCultivatePositionCourseMapper.selectCount(queryWrapper) > 0) {
            throw new RuntimeException("存在课程重复，请勿添加重复岗位学习课程");
        }

        List<FtbCultivatePositionCourse> ftbCultivatePositionCourses = ftbCultivatePositionLearnPracticalDTO.getCourseIds()
                .stream()
                .map(courseId -> {
                    FtbCultivatePositionCourse ftbCultivatePositionCourse = ftbCultivatePositionLearnPracticalDTO
                            .convertFtbCultivatePositionCourse(ftbCultivatePositionLearnPracticalDTO);
                    ftbCultivatePositionCourse.setCourseId(courseId);
                    return ftbCultivatePositionCourse;
                }).collect(Collectors.toList());

        String sqlStatement = SqlHelper.getSqlStatement(FtbCultivatePositionCourseMapper.class, SqlMethod.INSERT_ONE);
        SqlHelper.executeBatch(FtbCultivatePositionCourse.class,
                log,
                ftbCultivatePositionCourses,
                IService.DEFAULT_BATCH_SIZE,
                (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity));

        // 岗位课程事件
        SpringContext.getApplicationContext().publishEvent(new JnpfApplicationEvent<>(
                PositionCourseEventDTO.builder()
                        .courseIds(ftbCultivatePositionLearnPracticalDTO.getCourseIds())
                        .postIds(List.of(ftbCultivatePositionLearnPracticalDTO.getPostRankId()))
                        .eventType(PositionCourseEventDTO.EventType.POST)
                        .whetherToChange(false)
                        .build()));

    }

    @Override
    public List<FtbCultivatePositionJobLearnCourseVO> jobLearningCourseList(FtbCultivatePositionCoursePageDTO ftbCultivatePositionCoursePageDTO) {
        return baseMapper.jobLearningCourseList(ftbCultivatePositionCoursePageDTO);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void studyCourseExamAdded(FtbStudyCourseExamAddDTO ftbStudyCourseExamAddDTO) {
        jobLearningExaminationJudgment(ftbStudyCourseExamAddDTO.getPostLearnId(), ftbStudyCourseExamAddDTO.getExamId());
        FtbCultivatePositionCourseExam ftbCultivatePositionCourseExam = ftbStudyCourseExamAddDTO
                .convertFtbCultivatePositionCourseExam(ftbStudyCourseExamAddDTO);
        ftbCultivatePositionCourseExamMapper.insert(ftbCultivatePositionCourseExam);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void studyCourseIdentityAdded(FtbStudyCourseIdentityAddDTO ftbStudyCourseIdentityAddDTO) {
        onTheJobLearningPracticalAppraisal(ftbStudyCourseIdentityAddDTO.getPostLearnId(), ftbStudyCourseIdentityAddDTO.getIdentityId());
        FtbCultivatePositionCourseIdentity ftbCultivatePositionCourseIdentity = ftbStudyCourseIdentityAddDTO
                .convertFtbCultivatePositionCourseIdentity(ftbStudyCourseIdentityAddDTO);
        ftbCultivatePositionCourseIdentityMapper.insert(ftbCultivatePositionCourseIdentity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void courseExamDeletion(String courseExamId) {
        FtbCultivatePositionCourseExam ftbCultivatePositionCourseExam = ftbCultivatePositionCourseExamMapper.selectById(courseExamId);
        LambdaUpdateWrapper<FtbCultivatePositionCourseExam> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(SuperBaseEntity.SuperIBaseEntity::getId, courseExamId);
        updateWrapper.set(FtbCultivatePositionCourseExam::getEnabledMark, 1);
        ftbCultivatePositionCourseExamMapper.update(new FtbCultivatePositionCourseExam(), updateWrapper);
        // 级联删除
        InnerExamDto innerExamDto = new InnerExamDto();
        innerExamDto.setExamId(ftbCultivatePositionCourseExam.getExamId());
        innerExamDto.setRelationRankId(ftbCultivatePositionCourseExam.getPostRankId());
        innerExamDto.setExamSource(1);
        innerExamDto.setRelationId(courseExamId);
        ftbCultivateExamUserService.cancelPositionExam(innerExamDto);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void courseIdentityDeletion(String courseIdentityId) {
        LambdaUpdateWrapper<FtbCultivatePositionCourseIdentity> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(SuperBaseEntity.SuperIBaseEntity::getId, courseIdentityId);
        updateWrapper.set(FtbCultivatePositionCourseIdentity::getEnabledMark, 1);
        ftbCultivatePositionCourseIdentityMapper.update(new FtbCultivatePositionCourseIdentity(), updateWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void reselectionAppraisal(FtbReselectionAppraisalDTO ftbReselectionAppraisalDTO) {
        FtbCultivatePositionCourseIdentity ftbCultivatePositionCourseIdentity = ftbCultivatePositionCourseIdentityMapper.selectById(ftbReselectionAppraisalDTO.getCourseIdentityId());
        onTheJobLearningPracticalAppraisal(ftbCultivatePositionCourseIdentity.getPostLearnId(), ftbReselectionAppraisalDTO.getIdentityId());
        LambdaUpdateWrapper<FtbCultivatePositionCourseIdentity> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(FtbCultivatePositionCourseIdentity::getId, ftbReselectionAppraisalDTO.getCourseIdentityId());
        updateWrapper.set(FtbCultivatePositionCourseIdentity::getIdentityId, ftbReselectionAppraisalDTO.getIdentityId());
        ftbCultivatePositionCourseIdentityMapper.update(new FtbCultivatePositionCourseIdentity(), updateWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void retakeExam(FtbRetakeExamDTO ftbRetakeExamDTO) {
        FtbCultivatePositionCourseExam ftbCultivatePositionCourseExam = ftbCultivatePositionCourseExamMapper.selectById(ftbRetakeExamDTO.getCourseExamId());
        jobLearningExaminationJudgment(ftbCultivatePositionCourseExam.getPostLearnId(), ftbRetakeExamDTO.getExamId());
        LambdaUpdateWrapper<FtbCultivatePositionCourseExam> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(FtbCultivatePositionCourseExam::getId, ftbRetakeExamDTO.getCourseExamId());
        updateWrapper.set(FtbCultivatePositionCourseExam::getExamId, ftbRetakeExamDTO.getExamId());
        ftbCultivatePositionCourseExamMapper.update(new FtbCultivatePositionCourseExam(), updateWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletionOfOnTheJobLearningCourses(String id) {
        FtbCultivatePositionCourse ftbCultivatePositionCourse = ftbCultivatePositionCourseMapper.selectById(id);
        // 删除岗位学习课程
        LambdaUpdateWrapper<FtbCultivatePositionCourse> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.set(FtbCultivatePositionCourse::getEnabledMark, 1);
        updateWrapper.eq(SuperBaseEntity.SuperIBaseEntity::getId, id);
        ftbCultivatePositionCourseMapper.update(new FtbCultivatePositionCourse(), updateWrapper);
        // 查询绑定的课程考试
        LambdaQueryWrapper<FtbCultivatePositionCourseExam> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbCultivatePositionCourseExam::getCourseId, ftbCultivatePositionCourse.getCourseId());
        queryWrapper.eq(FtbCultivatePositionCourseExam::getEnabledMark, 0);
        queryWrapper.eq(FtbCultivatePositionCourseExam::getPostRankId, ftbCultivatePositionCourse.getPostRankId());
        queryWrapper.eq(FtbCultivatePositionCourseExam::getPostLearnId, ftbCultivatePositionCourse.getPostLearnId());
        queryWrapper.last("limit 1");
        FtbCultivatePositionCourseExam ftbCultivatePositionCourseExam = ftbCultivatePositionCourseExamMapper.selectOne(queryWrapper);
        // 级联删除考试
        LambdaUpdateWrapper<FtbCultivatePositionCourseExam> ftbCultivatePositionCourseExamLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        ftbCultivatePositionCourseExamLambdaUpdateWrapper.eq(FtbCultivatePositionCourseExam::getCourseId, ftbCultivatePositionCourse.getCourseId());
        ftbCultivatePositionCourseExamLambdaUpdateWrapper.eq(FtbCultivatePositionCourseExam::getPostRankId, ftbCultivatePositionCourse.getPostRankId());
        ftbCultivatePositionCourseExamLambdaUpdateWrapper.eq(FtbCultivatePositionCourseExam::getPostLearnId, ftbCultivatePositionCourse.getPostLearnId());
        ftbCultivatePositionCourseExamLambdaUpdateWrapper.set(FtbCultivatePositionCourseExam::getEnabledMark, 1);
        ftbCultivatePositionCourseExamMapper.update(new FtbCultivatePositionCourseExam(), ftbCultivatePositionCourseExamLambdaUpdateWrapper);
        // 级联删除实操鉴定
        LambdaUpdateWrapper<FtbCultivatePositionCourseIdentity> ftbCultivatePositionCourseIdentityLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        ftbCultivatePositionCourseIdentityLambdaUpdateWrapper.eq(FtbCultivatePositionCourseIdentity::getCourseId, ftbCultivatePositionCourse.getCourseId());
        ftbCultivatePositionCourseIdentityLambdaUpdateWrapper.eq(FtbCultivatePositionCourseIdentity::getPostRankId, ftbCultivatePositionCourse.getPostRankId());
        ftbCultivatePositionCourseIdentityLambdaUpdateWrapper.eq(FtbCultivatePositionCourseIdentity::getPostLearnId, ftbCultivatePositionCourse.getPostLearnId());
        ftbCultivatePositionCourseIdentityLambdaUpdateWrapper.set(FtbCultivatePositionCourseIdentity::getEnabledMark, 1);
        ftbCultivatePositionCourseIdentityMapper.update(new FtbCultivatePositionCourseIdentity(), ftbCultivatePositionCourseIdentityLambdaUpdateWrapper);
        // 级联删除课程证书
        LambdaUpdateWrapper<FtbCultivatePositionCourseCertificate> ftbCultivatePositionCourseCertificateLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        ftbCultivatePositionCourseCertificateLambdaUpdateWrapper.eq(FtbCultivatePositionCourseCertificate::getCourseId, ftbCultivatePositionCourse.getCourseId());
        ftbCultivatePositionCourseCertificateLambdaUpdateWrapper.eq(FtbCultivatePositionCourseCertificate::getPostRankId, ftbCultivatePositionCourse.getPostRankId());
        ftbCultivatePositionCourseCertificateLambdaUpdateWrapper.eq(FtbCultivatePositionCourseCertificate::getPostLearnId, ftbCultivatePositionCourse.getPostLearnId());
        ftbCultivatePositionCourseCertificateLambdaUpdateWrapper.set(FtbCultivatePositionCourseCertificate::getEnabledMark, 1);
        ftbCultivatePositionCourseCertificateMapper.update(new FtbCultivatePositionCourseCertificate(), ftbCultivatePositionCourseCertificateLambdaUpdateWrapper);
        // 删除课程绑定的考试
        if (Objects.nonNull(ftbCultivatePositionCourseExam)) {
            InnerExamDto innerExamDto = new InnerExamDto();
            innerExamDto.setExamId(ftbCultivatePositionCourseExam.getExamId());
            innerExamDto.setRelationRankId(ftbCultivatePositionCourseExam.getPostRankId());
            innerExamDto.setExamSource(1);
            innerExamDto.setRelationId(ftbCultivatePositionCourseExam.getId());
            ftbCultivateExamUserService.cancelPositionExam(innerExamDto);
        }
        // 删除岗位学习证书，判断是否为最后一个岗位学习课程
        if (ftbCultivatePositionCourseMapper.determineTheLastPostStudyCourse(ftbCultivatePositionCourse.getPostLearnId())
                == 0) {
            LambdaUpdateWrapper<FtbCultivatePositionCertificate> certificateLambdaQueryWrapper = Wrappers.lambdaUpdate();
            certificateLambdaQueryWrapper.eq(FtbCultivatePositionCertificate::getPostLearnId, ftbCultivatePositionCourse.getPostLearnId());
            certificateLambdaQueryWrapper.eq(FtbCultivatePositionCertificate::getPostRankId, ftbCultivatePositionCourse.getPostRankId());
            certificateLambdaQueryWrapper.set(FtbCultivatePositionCertificate::getEnableMark, 1);
            ftbCultivatePositionCertificateMapper.update(new FtbCultivatePositionCertificate(), certificateLambdaQueryWrapper);
        }
    }

    @Override
    public Page<FtbCultivatePositionLearnLevelVO> jobLevelCourseList(Page<FtbCultivatePositionLearnLevelVO> page,
                                                                     FtbCultivatePositionCourseLevelPageDTO ftbCultivatePositionCourseLevelPageDTO) {
        Page<FtbCultivatePositionLearnLevelVO> result = baseMapper.jobLevelCourseList(page, ftbCultivatePositionCourseLevelPageDTO);
        result.getRecords().forEach(ftbCultivatePositionLearnLevelVO -> {
            // 是否添加了课程考试，0已添加，1未添加
            LambdaQueryWrapper<FtbCultivatePositionCourseExam> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(FtbCultivatePositionCourseExam::getPostLearnId, ftbCultivatePositionCourseLevelPageDTO.getJobLearningId());
            queryWrapper.eq(FtbCultivatePositionCourseExam::getPostRankId, ftbCultivatePositionCourseLevelPageDTO.getPostRankId());
            queryWrapper.eq(FtbCultivatePositionCourseExam::getCourseId, ftbCultivatePositionLearnLevelVO.getCourseId());
            queryWrapper.eq(FtbCultivatePositionCourseExam::getEnabledMark, 0);
            ftbCultivatePositionLearnLevelVO.setIncludesCourseExams(1);
            ftbCultivatePositionLearnLevelVO.setContainsAssessment(1);
            ftbCultivatePositionLearnLevelVO.setContainsCourseCertificate(1);
            if (ftbCultivatePositionCourseExamMapper.selectCount(queryWrapper) > 0) {
                ftbCultivatePositionLearnLevelVO.setIncludesCourseExams(0);
            }
            // 是否添加了实操鉴定，0已添加，1未添加
            LambdaQueryWrapper<FtbCultivatePositionCourseIdentity> identityLambdaQueryWrapper = Wrappers.lambdaQuery();
            identityLambdaQueryWrapper.eq(FtbCultivatePositionCourseIdentity::getEnabledMark, 0);
            identityLambdaQueryWrapper.eq(FtbCultivatePositionCourseIdentity::getCourseId, ftbCultivatePositionLearnLevelVO.getCourseId());
            identityLambdaQueryWrapper.eq(FtbCultivatePositionCourseIdentity::getPostLearnId, ftbCultivatePositionCourseLevelPageDTO.getJobLearningId());
            identityLambdaQueryWrapper.eq(FtbCultivatePositionCourseIdentity::getPostRankId, ftbCultivatePositionCourseLevelPageDTO.getPostRankId());
            if (ftbCultivatePositionCourseIdentityMapper.selectCount(identityLambdaQueryWrapper) > 0) {
                ftbCultivatePositionLearnLevelVO.setContainsAssessment(0);
            }
            // 是否添加了课程证书，0已添加，1未添加
            LambdaQueryWrapper<FtbCultivatePositionCourseCertificate> certificateLambdaQueryWrapper = Wrappers.lambdaQuery();
            certificateLambdaQueryWrapper.eq(FtbCultivatePositionCourseCertificate::getEnabledMark, 0);
            certificateLambdaQueryWrapper.eq(FtbCultivatePositionCourseCertificate::getCourseId, ftbCultivatePositionLearnLevelVO.getCourseId());
            certificateLambdaQueryWrapper.eq(FtbCultivatePositionCourseCertificate::getPostLearnId, ftbCultivatePositionCourseLevelPageDTO.getJobLearningId());
            certificateLambdaQueryWrapper.eq(FtbCultivatePositionCourseCertificate::getPostRankId, ftbCultivatePositionCourseLevelPageDTO.getPostRankId());
            if (ftbCultivatePositionCourseCertificateMapper.selectCount(certificateLambdaQueryWrapper) > 0) {
                ftbCultivatePositionLearnLevelVO.setContainsCourseCertificate(0);
            }
        });
        return result;
    }

    @Override
    public FtbPositionCourseDropDownListVO courseDropDownList(FtbCourseDropDownListDTO ftbCourseDropDownListDTO) {
        FtbPositionCourseDropDownListVO ftbPositionCourseDropDownListVO = new FtbPositionCourseDropDownListVO();
        ftbPositionCourseDropDownListVO.setCourseId(ftbCourseDropDownListDTO.getCourseId());
        ftbPositionCourseDropDownListVO.setIdentityDelete(1);
        ftbPositionCourseDropDownListVO.setExamDelete(1);
        ftbPositionCourseDropDownListVO.setCertificateDelete(1);

        // 课程考试
        LambdaQueryWrapper<FtbCultivatePositionCourseExam> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbCultivatePositionCourseExam::getCourseId, ftbCourseDropDownListDTO.getCourseId());
        queryWrapper.eq(FtbCultivatePositionCourseExam::getPostLearnId, ftbCourseDropDownListDTO.getJobLearningId());
        queryWrapper.eq(FtbCultivatePositionCourseExam::getPostRankId, ftbCourseDropDownListDTO.getPostRankId());
        queryWrapper.eq(FtbCultivatePositionCourseExam::getEnabledMark, 0);
        FtbCultivatePositionCourseExam ftbCultivatePositionCourseExam = ftbCultivatePositionCourseExamMapper.selectOne(queryWrapper);
        if (ftbCultivatePositionCourseExam != null) {
            ftbPositionCourseDropDownListVO.setCourseExamId(ftbCultivatePositionCourseExam.getId());
            ftbPositionCourseDropDownListVO.setExamDelete(ftbCultivatePositionCourseExam.getEnabledMark());
            // 考试名
            FtbCultivateExam ftbCultivateExam = getFtbCultivateExamNameById(ftbCultivatePositionCourseExam.getExamId());
            if (ftbCultivateExam != null) {
                ftbPositionCourseDropDownListVO.setExamName(ftbCultivateExam.getExamName());
            }
        }

        // 课程实操鉴定
        LambdaQueryWrapper<FtbCultivatePositionCourseIdentity> identityLambdaQueryWrapper = Wrappers.lambdaQuery();
        identityLambdaQueryWrapper.eq(FtbCultivatePositionCourseIdentity::getCourseId, ftbCourseDropDownListDTO.getCourseId());
        identityLambdaQueryWrapper.eq(FtbCultivatePositionCourseIdentity::getPostLearnId, ftbCourseDropDownListDTO.getJobLearningId());
        identityLambdaQueryWrapper.eq(FtbCultivatePositionCourseIdentity::getPostRankId, ftbCourseDropDownListDTO.getPostRankId());
        identityLambdaQueryWrapper.eq(FtbCultivatePositionCourseIdentity::getEnabledMark, 0);
        FtbCultivatePositionCourseIdentity ftbCultivatePositionCourseIdentity = ftbCultivatePositionCourseIdentityMapper.selectOne(identityLambdaQueryWrapper);
        if (ftbCultivatePositionCourseIdentity != null) {
            ftbPositionCourseDropDownListVO.setCourseIdentityId(ftbCultivatePositionCourseIdentity.getId());
            ftbPositionCourseDropDownListVO.setIdentityDelete(ftbCultivatePositionCourseIdentity.getEnabledMark());
            // 实操鉴定名
            CultivateIdentifyTable cultivateIdentifyTable = getCultivateIdentifyNameById(ftbCultivatePositionCourseIdentity.getIdentityId());
            if (cultivateIdentifyTable != null) {
                ftbPositionCourseDropDownListVO.setIdentityName(cultivateIdentifyTable.getName());
            }
        }

        // 课程证书
        LambdaQueryWrapper<FtbCultivatePositionCourseCertificate> certificateLambdaQueryWrapper = Wrappers.lambdaQuery();
        certificateLambdaQueryWrapper.eq(FtbCultivatePositionCourseCertificate::getCourseId, ftbCourseDropDownListDTO.getCourseId());
        certificateLambdaQueryWrapper.eq(FtbCultivatePositionCourseCertificate::getPostLearnId, ftbCourseDropDownListDTO.getJobLearningId());
        certificateLambdaQueryWrapper.eq(FtbCultivatePositionCourseCertificate::getPostRankId, ftbCourseDropDownListDTO.getPostRankId());
        certificateLambdaQueryWrapper.eq(FtbCultivatePositionCourseCertificate::getEnabledMark, 0);
        certificateLambdaQueryWrapper.last("limit 1");
        FtbCultivatePositionCourseCertificate ftbCultivatePositionCourseCertificate = ftbCultivatePositionCourseCertificateMapper.selectOne(certificateLambdaQueryWrapper);
        if (ftbCultivatePositionCourseCertificate != null) {
            ftbPositionCourseDropDownListVO.setCourseCertificateId(ftbCultivatePositionCourseCertificate.getId());
            ftbPositionCourseDropDownListVO.setCertificateDelete(ftbCultivatePositionCourseCertificate.getEnabledMark());
            // 证书名
            FtbCertificateEntity ftbCertificateEntity = getFtbCertificateNameById(ftbCultivatePositionCourseCertificate.getCertificateId());
            if (ftbCertificateEntity != null) {
                ftbPositionCourseDropDownListVO.setCertificateName(ftbCertificateEntity.getName());
            }
        }
        return ftbPositionCourseDropDownListVO;
    }


    @Override
    public Page<FtbJobLearningPaginatedVO> jobLearningPaginatedList(Page<FtbJobLearningPaginatedVO> page, FtbJobLearningPaginDTO ftbJobLearningPaginDTO) {
        Page<FtbJobLearningPaginatedVO> result = baseMapper.jobLearningPaginatedList(page, ftbJobLearningPaginDTO);
        result.getRecords().forEach(ftbJobLearningPaginatedVO -> {
            ftbJobLearningPaginatedVO.setNumberOfGrades(0);
            ftbJobLearningPaginatedVO.setNumberOfStudyCourses(0L);
            ftbJobLearningPaginatedVO.setNumberOfPracticalAppraisals(0L);
            ftbJobLearningPaginatedVO.setNumberOfTestPapers(0);
            ftbJobLearningPaginatedVO.setNumberOfPromotionChannels(0);
            // 岗位名
            PositionVO positionEntity = userApiV2Util.infoPosition(ftbJobLearningPaginatedVO.getPostId(), null);
            if(positionEntity!=null) {
                ftbJobLearningPaginatedVO.setPostName(positionEntity.getFullName());
                ftbJobLearningPaginatedVO.setPostCustomId(positionEntity.getEnCode());//todo岗位id
            }
            // 学习课程数
            ftbJobLearningPaginatedVO.setNumberOfStudyCourses(ftbCultivatePositionCourseMapper.numberOfStudyCourses(ftbJobLearningPaginatedVO.getJobLearningId()));
            // 地图岗位数
            Integer numberOfMapPositions = ftbCultivatePromotionNewService
                    .getMapPostNumber(ftbJobLearningPaginatedVO.getPostId());
            ftbJobLearningPaginatedVO.setNumberOfPromotionChannels(numberOfMapPositions);
            // 实操鉴定数
            LambdaQueryWrapper<FtbCultivatePositionExamIdentify> identityLambdaQueryWrapper = Wrappers.lambdaQuery();
            identityLambdaQueryWrapper.eq(FtbCultivatePositionExamIdentify::getEnabledMark, 0);
            identityLambdaQueryWrapper.eq(FtbCultivatePositionExamIdentify::getPostLearnId, ftbJobLearningPaginatedVO.getJobLearningId());
            ftbJobLearningPaginatedVO.setNumberOfPracticalAppraisals(ftbCultivatePositionExamIdentifyMapper.selectCount(identityLambdaQueryWrapper));
            // 课程实操鉴定数
            LambdaQueryWrapper<FtbCultivatePositionCourseIdentity> courseIdentityLambdaQueryWrapper = Wrappers.lambdaQuery();
            courseIdentityLambdaQueryWrapper.eq(FtbCultivatePositionCourseIdentity::getEnabledMark, 0);
            courseIdentityLambdaQueryWrapper.eq(FtbCultivatePositionCourseIdentity::getPostLearnId, ftbJobLearningPaginatedVO.getJobLearningId());
            Long numberOfCourseIdentity = ftbCultivatePositionCourseIdentityMapper.selectCount(courseIdentityLambdaQueryWrapper);
            ftbJobLearningPaginatedVO.setNumberOfPracticalAppraisals(ftbJobLearningPaginatedVO.getNumberOfPracticalAppraisals() + numberOfCourseIdentity);
            // 试卷数
            LambdaQueryWrapper<FtbCultivatePositionExam> examLambdaQueryWrapper = Wrappers.lambdaQuery();
            examLambdaQueryWrapper.eq(FtbCultivatePositionExam::getEnabledMark, 0);
            examLambdaQueryWrapper.eq(FtbCultivatePositionExam::getPostLearnId, ftbJobLearningPaginatedVO.getJobLearningId());
            List<FtbCultivatePositionExam> ftbCultivatePositionExams = ftbCultivatePositionExamMapper.selectList(examLambdaQueryWrapper);
            // 课程考试数
            LambdaQueryWrapper<FtbCultivatePositionCourseExam> positionCourseExamLambdaQueryWrapper = Wrappers.lambdaQuery();
            positionCourseExamLambdaQueryWrapper.eq(FtbCultivatePositionCourseExam::getEnabledMark, 0);
            positionCourseExamLambdaQueryWrapper.eq(FtbCultivatePositionCourseExam::getPostLearnId, ftbJobLearningPaginatedVO.getJobLearningId());
            List<FtbCultivatePositionCourseExam> ftbCultivatePositionCourseExams = ftbCultivatePositionCourseExamMapper.selectList(positionCourseExamLambdaQueryWrapper);
            // 考试集合
            List<String> ftbCultivateExamIds = ftbCultivatePositionExams.stream()
                    .map(FtbCultivatePositionExam::getExamId).collect(Collectors.toList());
            List<String> ftbCultivateExamIdSe = ftbCultivatePositionCourseExams.stream()
                    .map(FtbCultivatePositionCourseExam::getExamId).collect(Collectors.toList());
            ftbCultivateExamIds.addAll(ftbCultivateExamIdSe);
            if (CollUtil.isNotEmpty(ftbCultivateExamIds)) {
                Map<String, PaperVo> stringPaperVoMap = ftbCultivateExamService.queryPaperInfoForExamIds(ftbCultivateExamIds);
                ftbJobLearningPaginatedVO.setNumberOfTestPapers(stringPaperVoMap.size());
            }
        });
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ActionResult deletionOfJobLearning(String postLearnId) {
        // 岗位学习表
        LambdaUpdateWrapper<FtbCultivatePosition> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(SuperBaseEntity.SuperIBaseEntity::getId, postLearnId);
        updateWrapper.set(FtbCultivatePosition::getEnabledMark, 1);
        baseMapper.update(new FtbCultivatePosition(), updateWrapper);
        // 岗位学习课程表
        LambdaUpdateWrapper<FtbCultivatePositionCourse> a = Wrappers.lambdaUpdate();
        a.set(FtbCultivatePositionCourse::getEnabledMark, 1);
        a.eq(FtbCultivatePositionCourse::getPostLearnId, postLearnId);
        ftbCultivatePositionCourseMapper.update(new FtbCultivatePositionCourse(), a);
        // 岗位实操鉴定表
        LambdaUpdateWrapper<FtbCultivatePositionExamIdentify> b = Wrappers.lambdaUpdate();
        b.set(FtbCultivatePositionExamIdentify::getEnabledMark, 1);
        b.eq(FtbCultivatePositionExamIdentify::getPostLearnId, postLearnId);
        ftbCultivatePositionExamIdentifyMapper.update(new FtbCultivatePositionExamIdentify(), b);
        // 岗位学习考试表
        LambdaUpdateWrapper<FtbCultivatePositionExam> d = Wrappers.lambdaUpdate();
        d.set(FtbCultivatePositionExam::getEnabledMark, 1);
        d.eq(FtbCultivatePositionExam::getPostLearnId, postLearnId);
        ftbCultivatePositionExamMapper.update(new FtbCultivatePositionExam(), d);
        // 学习课程考试表
        LambdaUpdateWrapper<FtbCultivatePositionCourseExam> c = Wrappers.lambdaUpdate();
        c.set(FtbCultivatePositionCourseExam::getEnabledMark, 1);
        c.eq(FtbCultivatePositionCourseExam::getPostLearnId, postLearnId);
        ftbCultivatePositionCourseExamMapper.update(new FtbCultivatePositionCourseExam(), c);
        // 学习课程实操鉴定表
        LambdaUpdateWrapper<FtbCultivatePositionCourseIdentity> e = Wrappers.lambdaUpdate();
        e.set(FtbCultivatePositionCourseIdentity::getEnabledMark, 1);
        e.eq(FtbCultivatePositionCourseIdentity::getPostLearnId, postLearnId);
        ftbCultivatePositionCourseIdentityMapper.update(new FtbCultivatePositionCourseIdentity(), e);
        // 学习课程证书表
        LambdaUpdateWrapper<FtbCultivatePositionCourseCertificate> f = Wrappers.lambdaUpdate();
        f.set(FtbCultivatePositionCourseCertificate::getEnabledMark, 1);
        f.eq(FtbCultivatePositionCourseCertificate::getPostLearnId, postLearnId);
        ftbCultivatePositionCourseCertificateMapper.update(new FtbCultivatePositionCourseCertificate(), f);

        // 学习证书表
        LambdaUpdateWrapper<FtbCultivatePositionCertificate> pc = Wrappers.lambdaUpdate();
        pc.set(FtbCultivatePositionCertificate::getEnableMark, 1);
        pc.eq(FtbCultivatePositionCertificate::getPostLearnId, postLearnId);
        ftbCultivatePositionCertificateMapper.update(new FtbCultivatePositionCertificate(), pc);
        return ActionResult.success(true);
    }

    @Override
    public FtbJobLearningConfigurationVO overviewOfJobLearningConfiguration(String postLearnId, String postId) {
        FtbJobLearningConfigurationVO result = new FtbJobLearningConfigurationVO();
        result.setPostId(postId);
        // 岗位名和职等信息
        try {
            PositionVO positionVO = userApiV2Util.infoPosition(postId, null);
            if (positionVO != null) {
                result.setPostName(positionVO.getFullName());
                result.setPostCustomId(positionVO.getEnCode());
                List<GradeVO> positionGradesList = userApiV2Util.listGrades(postId, null);
                List<FtbJobLearningConfigurationVO.GradeInformation> gradeInformations = positionGradesList.stream().map(
                        basePositionGradesEntity -> {
                            FtbJobLearningConfigurationVO.GradeInformation gradeInformation = new FtbJobLearningConfigurationVO.GradeInformation();
                            gradeInformation.setFullName(basePositionGradesEntity.getFullName());
                            gradeInformation.setGradesLevel(basePositionGradesEntity.getLinkLevel());
                            return gradeInformation;
                        }
                ).collect(Collectors.toList());
                result.setGradeDisplay(gradeInformations);
            }


        } catch (Exception ignored) {
        }
        // 学习地图变更
        FtbCultivatePromotionLevelMapVO learnMapLevels = ftbCultivatePromotionNewService.getLearnMapLevels(postId);
        result.setPromotionChannels(learnMapLevels);
        result.setNumberOfPromotionChannels(ftbCultivatePromotionNewService.getMapPostNumber(postId));
        // 学习课程
        LambdaQueryWrapper<FtbCultivatePositionCourse> learingCourseLambdaQueryWrapper = Wrappers.lambdaQuery();
        learingCourseLambdaQueryWrapper.eq(FtbCultivatePositionCourse::getEnabledMark, 0);
        learingCourseLambdaQueryWrapper.eq(FtbCultivatePositionCourse::getPostLearnId, postLearnId);
        List<FtbCultivatePositionCourse> ftbCultivatePositionCourses = ftbCultivatePositionCourseMapper.selectList(learingCourseLambdaQueryWrapper);
        List<String> strings = ftbCultivatePositionCourses.stream()
                .map(FtbCultivatePositionCourse::getCourseId).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(strings)) {
            LambdaQueryWrapper<FtbCultivateCourse> ftbCultivateCourseLambdaQueryWrapper = Wrappers.lambdaQuery();
            ftbCultivateCourseLambdaQueryWrapper.eq(FtbCultivateCourse::getEnableMark, 0);
            ftbCultivateCourseLambdaQueryWrapper.eq(FtbCultivateCourse::getIsGroundIng, 1);
            ftbCultivateCourseLambdaQueryWrapper.eq(FtbCultivateCourse::getLabel, 2);
            ftbCultivateCourseLambdaQueryWrapper.in(SuperBaseEntity.SuperIBaseEntity::getId, strings);
            List<FtbCultivateCourse> ftbCultivateCourses = ftbCultivateCourseMapper.selectList(ftbCultivateCourseLambdaQueryWrapper);
            List<FtbJobLearningConfigurationVO.LearningCourses> courses = ftbCultivateCourses.stream().map(ftbCultivateCourse -> {
                FtbJobLearningConfigurationVO.LearningCourses learningCourses = new FtbJobLearningConfigurationVO.LearningCourses();
                learningCourses.setCourseId(ftbCultivateCourse.getCourseId());
                learningCourses.setName(ftbCultivateCourse.getName());
                learningCourses.setFormat(ftbCultivateCourse.getFormat());
                return learningCourses;
            }).collect(Collectors.toList());
            result.setLearningCourses(courses);
            result.setNumberOfStudyCourses(courses.size());
        }
        // 实操鉴定表
        LambdaQueryWrapper<FtbCultivatePositionExamIdentify> identityLambdaQueryWrapper = Wrappers.lambdaQuery();
        identityLambdaQueryWrapper.eq(FtbCultivatePositionExamIdentify::getEnabledMark, 0);
        identityLambdaQueryWrapper.eq(FtbCultivatePositionExamIdentify::getPostLearnId, postLearnId);
        List<FtbJobLearningConfigurationVO.PracticalIdentification> practicalIdentifications = new ArrayList<>();
        // 岗位学习实操鉴定
        ftbCultivatePositionExamIdentifyMapper.selectList(identityLambdaQueryWrapper)
                .forEach(ftbCultivatePositionCourseIdentity -> {
                    FtbJobLearningConfigurationVO.PracticalIdentification practicalIdentification = new FtbJobLearningConfigurationVO.PracticalIdentification();
                    practicalIdentification.setType(0);
                    practicalIdentification.setId(ftbCultivatePositionCourseIdentity.getIdentifyId());

                    practicalIdentifications.add(practicalIdentification);
                });
        // 课程学习实操鉴定
        LambdaQueryWrapper<FtbCultivatePositionCourseIdentity> courseIdentityLambdaQueryWrapper = Wrappers.lambdaQuery();
        courseIdentityLambdaQueryWrapper.eq(FtbCultivatePositionCourseIdentity::getEnabledMark, 0);
        courseIdentityLambdaQueryWrapper.eq(FtbCultivatePositionCourseIdentity::getPostLearnId, postLearnId);
        List<FtbCultivatePositionCourseIdentity> ftbCultivatePositionCourseIdentities = ftbCultivatePositionCourseIdentityMapper.selectList(courseIdentityLambdaQueryWrapper);
        ftbCultivatePositionCourseIdentities.forEach(ftbCultivatePositionCourseIdentity -> {
            FtbJobLearningConfigurationVO.PracticalIdentification practicalIdentification = new FtbJobLearningConfigurationVO.PracticalIdentification();
            practicalIdentification.setType(1);
            practicalIdentification.setId(ftbCultivatePositionCourseIdentity.getIdentityId());
            practicalIdentifications.add(practicalIdentification);
        });
        if (CollUtil.isNotEmpty(practicalIdentifications)) {
            List<IdentifyTableInfoApiVo> tableListInfo = cultivateIdentifyTableService.getTableListInfo(
                    practicalIdentifications.stream()
                            .map(FtbJobLearningConfigurationVO.PracticalIdentification::getId)
                            .collect(Collectors.toList()));
            Map<String, IdentifyTableInfoApiVo> collect = tableListInfo.stream()
                    .collect(Collectors.toMap(IdentifyTableInfoApiVo::getId, k -> k, (a, b) -> a));
            practicalIdentifications.forEach(practicalIdentification -> {
                IdentifyTableInfoApiVo identifyTableInfoApiVo = collect.get(practicalIdentification.getId());
                if (Objects.nonNull(identifyTableInfoApiVo)) {
                    practicalIdentification.setIdentifyName(identifyTableInfoApiVo.getName());
                    practicalIdentification.setIdentifyId(identifyTableInfoApiVo.getRuleId());
                }
            });
            result.setPracticalIdentifications(practicalIdentifications);
        }
        result.setNumberOfPracticalAppraisals(practicalIdentifications.size());
        // 试卷
        LambdaQueryWrapper<FtbCultivatePositionExam> examLambdaQueryWrapper = Wrappers.lambdaQuery();
        examLambdaQueryWrapper.eq(FtbCultivatePositionExam::getEnabledMark, 0);
        examLambdaQueryWrapper.eq(FtbCultivatePositionExam::getPostLearnId, postLearnId);
        // 岗位考试
        List<FtbCultivatePositionExam> ftbCultivatePositionExams = ftbCultivatePositionExamMapper.selectList(examLambdaQueryWrapper);
        List<FtbJobLearningConfigurationVO.TestPaper> testPapers = new ArrayList<>();
        ftbCultivatePositionExams.forEach(ftbCultivatePositionExam -> {
            FtbJobLearningConfigurationVO.TestPaper testPaper = new FtbJobLearningConfigurationVO.TestPaper();
            testPaper.setId(ftbCultivatePositionExam.getExamId());
            testPaper.setType(0);
            testPapers.add(testPaper);
        });
        // 课程考试
        LambdaQueryWrapper<FtbCultivatePositionCourseExam> positionCourseExamLambdaQueryWrapper = Wrappers.lambdaQuery();
        positionCourseExamLambdaQueryWrapper.eq(FtbCultivatePositionCourseExam::getEnabledMark, 0);
        positionCourseExamLambdaQueryWrapper.eq(FtbCultivatePositionCourseExam::getPostLearnId, postLearnId);
        List<FtbCultivatePositionCourseExam> ftbCultivatePositionCourseExams = ftbCultivatePositionCourseExamMapper.selectList(positionCourseExamLambdaQueryWrapper);
        ftbCultivatePositionCourseExams.forEach(ftbCultivatePositionCourseExam -> {
            FtbJobLearningConfigurationVO.TestPaper testPaper = new FtbJobLearningConfigurationVO.TestPaper();
            testPaper.setId(ftbCultivatePositionCourseExam.getExamId());
            testPaper.setType(1);
            FtbCultivateCourse ftbCultivateCourse = ftbCultivateCourseMapper.selectById(ftbCultivatePositionCourseExam.getCourseId());
            if (Objects.nonNull(ftbCultivateCourse)) {
                testPaper.setCourseName(ftbCultivateCourse.getName());
            }
            testPapers.add(testPaper);
        });
        if (CollUtil.isNotEmpty(testPapers)) {
            List<String> collected = testPapers.stream().map(FtbJobLearningConfigurationVO.TestPaper::getId).collect(Collectors.toList());
            Map<String, PaperVo> stringPaperVoMap = ftbCultivateExamService.queryPaperInfoForExamIds(collected);
            testPapers.forEach(testPaper -> {
                PaperVo paperVo = stringPaperVoMap.get(testPaper.getId());
                if (Objects.nonNull(paperVo)) {
                    testPaper.setPaperName(paperVo.getName());
                    testPaper.setPaperId(paperVo.getPaperId());
                }
            });
        }
        result.setTestPapers(testPapers);
        result.setNumberOfTestPapers(testPapers.size());
        return result;
    }

    @Override
    public FtbJobLearningExamAppraisalVO jobLearningExamAppraisal(String postLearnId, String postIdID) {
        FtbJobLearningExamAppraisalVO ftbJobLearningExamAppraisalVO = new FtbJobLearningExamAppraisalVO();
        // 考试
        LambdaQueryWrapper<FtbCultivatePositionExam> examLambdaQueryWrapper = Wrappers.lambdaQuery();
        examLambdaQueryWrapper.select(FtbCultivatePositionExam::getExamId);
        examLambdaQueryWrapper.eq(FtbCultivatePositionExam::getEnabledMark, 0);
        examLambdaQueryWrapper.eq(FtbCultivatePositionExam::getPostLearnId, postLearnId);
        examLambdaQueryWrapper.eq(FtbCultivatePositionExam::getPostRankId, postIdID);
        examLambdaQueryWrapper.last("limit 1");
        FtbCultivatePositionExam ftbCultivatePositionExam = ftbCultivatePositionExamMapper.selectOne(examLambdaQueryWrapper);
        if (Objects.nonNull(ftbCultivatePositionExam)) {
            ftbJobLearningExamAppraisalVO.setExamId(ftbCultivatePositionExam.getExamId());
            // 考试名
            FtbCultivateExam ftbCultivateExam = getFtbCultivateExamNameById(ftbCultivatePositionExam.getExamId());
            if (ftbCultivateExam != null) {
                ftbJobLearningExamAppraisalVO.setExamName(ftbCultivateExam.getExamName());
                ftbJobLearningExamAppraisalVO.setExamCustomId(ftbCultivateExam.getExamId());
            }
        }
        // 实操鉴定
        LambdaQueryWrapper<FtbCultivatePositionExamIdentify> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(FtbCultivatePositionExamIdentify::getIdentifyId);
        queryWrapper.eq(FtbCultivatePositionExamIdentify::getEnabledMark, 0);
        queryWrapper.eq(FtbCultivatePositionExamIdentify::getPostLearnId, postLearnId);
        queryWrapper.eq(FtbCultivatePositionExamIdentify::getPostRankId, postIdID);
        queryWrapper.last("limit 1");
        FtbCultivatePositionExamIdentify ftbCultivatePositionExamIdent = ftbCultivatePositionExamIdentifyMapper.selectOne(queryWrapper);
        if (Objects.nonNull(ftbCultivatePositionExamIdent)) {
            ftbJobLearningExamAppraisalVO.setIdentifyId(ftbCultivatePositionExamIdent.getIdentifyId());
            // 实操鉴定名
            CultivateIdentifyTable cultivateIdentifyTable = getCultivateIdentifyNameById(ftbCultivatePositionExamIdent.getIdentifyId());
            if (cultivateIdentifyTable != null) {
                ftbJobLearningExamAppraisalVO.setPracticalIdentificationName(cultivateIdentifyTable.getName());
                ftbJobLearningExamAppraisalVO.setIdentifyCustomId(cultivateIdentifyTable.getRuleId());
            }
        }
        // 证书
        LambdaQueryWrapper<FtbCultivatePositionCertificate> certificateLambdaQueryWrapper = Wrappers.lambdaQuery();
        certificateLambdaQueryWrapper.select(FtbCultivatePositionCertificate::getCertificateId);
        certificateLambdaQueryWrapper.eq(FtbCultivatePositionCertificate::getEnableMark, 0);
        certificateLambdaQueryWrapper.eq(FtbCultivatePositionCertificate::getPostLearnId, postLearnId);
        certificateLambdaQueryWrapper.eq(FtbCultivatePositionCertificate::getPostRankId, postIdID);
        certificateLambdaQueryWrapper.last("limit 1");
        FtbCultivatePositionCertificate ftbCultivatePositionCertificate = ftbCultivatePositionCertificateMapper.selectOne(certificateLambdaQueryWrapper);
        if (Objects.nonNull(ftbCultivatePositionCertificate)) {
            ftbJobLearningExamAppraisalVO.setCertificateId(ftbCultivatePositionCertificate.getCertificateId());
            // 证书名
            FtbCertificateEntity ftbCertificateEntity = getFtbCertificateNameById(ftbCultivatePositionCertificate.getCertificateId());
            if (ftbCertificateEntity != null) {
                ftbJobLearningExamAppraisalVO.setCertificateName(ftbCertificateEntity.getName());
            }
        }
        // 岗位考试合格后才能进行鉴定,0否1是
        FtbCultivatePosition ftbCultivatePosition = this.getById(postLearnId);
        ftbJobLearningExamAppraisalVO.setQualified(ftbCultivatePosition.getQualified());
        return ftbJobLearningExamAppraisalVO;
    }

    @Override
    @Transactional
    public void postStudyExamDeleted(String postLearnId, String postRankId) {
        LambdaQueryWrapper<FtbCultivatePositionExam> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbCultivatePositionExam::getPostLearnId, postLearnId);
        queryWrapper.eq(FtbCultivatePositionExam::getPostRankId, postRankId);
        queryWrapper.eq(FtbCultivatePositionExam::getEnabledMark, 0);
        queryWrapper.last("limit 1");
        FtbCultivatePositionExam ftbCultivatePositionCourseExam = ftbCultivatePositionExamMapper.selectOne(queryWrapper);
        // 删除岗位学习考试
        LambdaUpdateWrapper<FtbCultivatePositionExam> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(FtbCultivatePositionExam::getPostLearnId, postLearnId);
        updateWrapper.eq(FtbCultivatePositionExam::getPostRankId, postRankId);
        updateWrapper.set(FtbCultivatePositionExam::getEnabledMark, 1);
        ftbCultivatePositionExamMapper.update(new FtbCultivatePositionExam(), updateWrapper);
        // 级联删除
//        InnerExamDto innerExamDto = new InnerExamDto();
//        innerExamDto.setExamId(ftbCultivatePositionCourseExam.getExamId());
//        innerExamDto.setRelationRankId(ftbCultivatePositionCourseExam.getPostRankId());
//        innerExamDto.setExamSource(2);
//        innerExamDto.setRelationId(ftbCultivatePositionCourseExam.getId());
//        ftbCultivateExamUserService.cancelPositionExam(innerExamDto);
        // 提示语回调
        ftbCultivateExamUserService.positionConfigChangeCallBack(postRankId, ftbCultivatePositionCourseExam.getExamId(), 0);
        eligibilityStatusChange(postRankId);
    }

    @Override
    @Transactional
    public void jobLearningPracticalAppraisalDelete(String postLearnId, String postRankId) {
        LambdaUpdateWrapper<FtbCultivatePositionExamIdentify> queryWrapper = Wrappers.lambdaUpdate();
        queryWrapper.eq(FtbCultivatePositionExamIdentify::getPostLearnId, postLearnId);
        queryWrapper.eq(FtbCultivatePositionExamIdentify::getPostRankId, postRankId);
        queryWrapper.set(FtbCultivatePositionExamIdentify::getEnabledMark, 1);
        ftbCultivatePositionExamIdentifyMapper.update(new FtbCultivatePositionExamIdentify(), queryWrapper);
        eligibilityStatusChange(postRankId);
    }

    @Override
    @Transactional
    public void deleteMemberGrade(String id, String userId) {
        CultivateMessageInfo cultivateMessageInfo = new CultivateMessageInfo();
        cultivateMessageInfo.setSource(1);
        cultivateMessageInfo.setState(0);
        cultivateMessageInfo.setUserId(userId);
        cultivateMessageInfo.setBusinessId(id);
        ftbCultivateMessageInfoMapper.insert(cultivateMessageInfo);
    }

    @Override
    @Transactional
    public void certificateDeletion(String postLearnId, String postRankId) {
        LambdaUpdateWrapper<FtbCultivatePositionCertificate> deleteWrapper = Wrappers.lambdaUpdate();
        deleteWrapper.eq(FtbCultivatePositionCertificate::getPostLearnId, postLearnId);
        deleteWrapper.eq(FtbCultivatePositionCertificate::getPostRankId, postRankId);
        deleteWrapper.set(FtbCultivatePositionCertificate::getEnableMark, 1);
        ftbCultivatePositionCertificateMapper.update(new FtbCultivatePositionCertificate(), deleteWrapper);
    }

    @Override
    @Transactional
    public void onTheJobLearningCertificate(FtbCultivatePositionJobLearnCertificateDTO certificateDTO) {
        FtbCultivatePositionCertificate ftbCultivatePositionCertificate = certificateDTO.convertFtbCultivatePositionCertificate(certificateDTO);
        ftbCultivatePositionCertificateMapper.insert(ftbCultivatePositionCertificate);
    }

    @Override
    @Transactional
    public void addJobLearningCourseCertificate(FtbCultivatePositionJobLearnCourseCertificateDTO certificateDTO) {
        FtbCultivatePositionCourseCertificate ftbCultivatePositionCourseCertificate = certificateDTO.convert(certificateDTO);
        ftbCultivatePositionCourseCertificateMapper.insert(ftbCultivatePositionCourseCertificate);
    }

    @Override
    @Transactional
    public void certificateCourseDeletion(String postLearnId, String courseId) {
        LambdaUpdateWrapper<FtbCultivatePositionCourseCertificate> deleteWrapper = Wrappers.lambdaUpdate();
        deleteWrapper.eq(FtbCultivatePositionCourseCertificate::getPostLearnId, postLearnId);
        deleteWrapper.eq(FtbCultivatePositionCourseCertificate::getCourseId, courseId);
        deleteWrapper.set(FtbCultivatePositionCourseCertificate::getEnabledMark, 1);
        ftbCultivatePositionCourseCertificateMapper.update(new FtbCultivatePositionCourseCertificate(), deleteWrapper);
    }

    @Override
    @Transactional
    public void changeCourseElectiveAndRequiredSwitching(FtbCultivatePositionJobLearnCourseStateSwitchDTO stateSwitchDTO) {
        // 至少有一门必修课程
        if (stateSwitchDTO.getCompulsory() == 1) {
            LambdaQueryWrapper<FtbCultivatePositionCourse> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(FtbCultivatePositionCourse::getEnabledMark, 0);
            queryWrapper.eq(FtbCultivatePositionCourse::getPostLearnId, stateSwitchDTO.getPostLearnId());
            queryWrapper.eq(FtbCultivatePositionCourse::getPostRankId, stateSwitchDTO.getPostRankId());
            queryWrapper.eq(FtbCultivatePositionCourse::getCompulsory, 0);
            queryWrapper.ne(FtbCultivatePositionCourse::getCourseId, stateSwitchDTO.getCourseId());
            List<FtbCultivatePositionCourse> ftbCultivatePositionCourses = ftbCultivatePositionCourseMapper.selectList(queryWrapper);
            if (CollectionUtil.isEmpty(ftbCultivatePositionCourses)) {
                throw new RuntimeException("至少有一门必修课程");
            }
            List<String> courseIds = ftbCultivatePositionCourses.stream().map(FtbCultivatePositionCourse::getCourseId).collect(Collectors.toList());
            QueryWrapper<FtbCultivateCourse> courseQueryWrapper = new QueryWrapper<>();
            courseQueryWrapper.lambda()
                    .in(FtbCultivateCourse::getId, courseIds);
            List<FtbCultivateCourse> courseList = ftbCultivateCourseMapper.selectList(courseQueryWrapper);
            List<FtbCultivateCourse> effectiveCourseList = courseList.stream().filter(course -> {
                return course.getIsGroundIng() == 1 && course.getLabel() == 2;
            }).collect(Collectors.toList());

            if (CollectionUtil.isEmpty(effectiveCourseList)) {
                throw new RuntimeException("至少有一门必修课程");
            }

        }
        LambdaUpdateWrapper<FtbCultivatePositionCourse> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(FtbCultivatePositionCourse::getPostLearnId, stateSwitchDTO.getPostLearnId());
        updateWrapper.eq(FtbCultivatePositionCourse::getCourseId, stateSwitchDTO.getCourseId());
        updateWrapper.eq(FtbCultivatePositionCourse::getPostRankId, stateSwitchDTO.getPostRankId());
        updateWrapper.set(FtbCultivatePositionCourse::getCompulsory, stateSwitchDTO.getCompulsory());
        ftbCultivatePositionCourseMapper.update(new FtbCultivatePositionCourse(), updateWrapper);
    }

    @Override
    @Transactional
    public void courseRetakeCertificate(FtbJobLearnCourseRetakeCertificateDTO certificateDTO) {
        LambdaUpdateWrapper<FtbCultivatePositionCourseCertificate> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(FtbCultivatePositionCourseCertificate::getId, certificateDTO.getCourseCertificateId());
        updateWrapper.eq(FtbCultivatePositionCourseCertificate::getPostRankId, certificateDTO.getPostId());
        updateWrapper.set(FtbCultivatePositionCourseCertificate::getCertificateId, certificateDTO.getCertificateId());
        ftbCultivatePositionCourseCertificateMapper.update(new FtbCultivatePositionCourseCertificate(), updateWrapper);
    }

    /**
     * 岗位学习考试判断
     *
     * @param postLearnId 岗位id
     * @param examId      考试id
     */
    private void jobLearningExaminationJudgment(String postLearnId, String examId) {
        LambdaQueryWrapper<FtbCultivatePositionExam> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbCultivatePositionExam::getEnabledMark, 0);
        queryWrapper.eq(FtbCultivatePositionExam::getPostLearnId, postLearnId);
        queryWrapper.eq(FtbCultivatePositionExam::getExamId, examId);
        if (ftbCultivatePositionExamMapper.selectCount(queryWrapper) > 0) {
            throw new RuntimeException("此岗位已经绑定了该考试了");
        }
        LambdaQueryWrapper<FtbCultivatePositionCourseExam> positionCourseExamLambdaQueryWrapper = Wrappers.lambdaQuery();
        positionCourseExamLambdaQueryWrapper.eq(FtbCultivatePositionCourseExam::getEnabledMark, 0);
        positionCourseExamLambdaQueryWrapper.eq(FtbCultivatePositionCourseExam::getPostLearnId, postLearnId);
        positionCourseExamLambdaQueryWrapper.eq(FtbCultivatePositionCourseExam::getExamId, examId);
        positionCourseExamLambdaQueryWrapper.last("limit 1");
        FtbCultivatePositionCourseExam ftbCultivatePositionCourseExam = ftbCultivatePositionCourseExamMapper.selectOne(positionCourseExamLambdaQueryWrapper);
        if (Objects.nonNull(ftbCultivatePositionCourseExam)) {
            // 校验是否下架
            LambdaQueryWrapper<FtbCultivateCourse> ftbCultivateCourseLambdaQueryWrapper = Wrappers.lambdaQuery();
            ftbCultivateCourseLambdaQueryWrapper.eq(FtbCultivateCourse::getId, ftbCultivatePositionCourseExam.getCourseId());
            ftbCultivateCourseLambdaQueryWrapper.eq(FtbCultivateCourse::getIsGroundIng, 0);
            FtbCultivateCourse ftbCultivateCourse = ftbCultivateCourseMapper.selectOne(ftbCultivateCourseLambdaQueryWrapper);
            if (Objects.nonNull(ftbCultivateCourse)) {
                throw new RuntimeException("该考试绑定的课程" + ftbCultivateCourse.getName() + "已下架，重新上架后可启用考试");
            }
            throw new RuntimeException("此岗位已经绑定了该考试了,不可重复绑定");

        }
    }

    /**
     * 岗位学习实操鉴定判断
     *
     * @param postLearnId      岗位学习 ID
     * @param identificationId 考试id
     */
    private void onTheJobLearningPracticalAppraisal(String postLearnId, String identificationId) {
        LambdaQueryWrapper<FtbCultivatePositionExamIdentify> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbCultivatePositionExamIdentify::getEnabledMark, 0);
        queryWrapper.eq(FtbCultivatePositionExamIdentify::getPostLearnId, postLearnId);
        queryWrapper.eq(FtbCultivatePositionExamIdentify::getIdentifyId, identificationId);
        if (ftbCultivatePositionExamIdentifyMapper.selectCount(queryWrapper) > 0) {
            throw new RuntimeException("此岗位已经绑定了该实操鉴定了");
        }
        LambdaQueryWrapper<FtbCultivatePositionCourseIdentity> positionCourseExamLambdaQueryWrapper = Wrappers.lambdaQuery();
        positionCourseExamLambdaQueryWrapper.eq(FtbCultivatePositionCourseIdentity::getEnabledMark, 0);
        positionCourseExamLambdaQueryWrapper.eq(FtbCultivatePositionCourseIdentity::getPostLearnId, postLearnId);
        positionCourseExamLambdaQueryWrapper.eq(FtbCultivatePositionCourseIdentity::getIdentityId, identificationId);
        if (ftbCultivatePositionCourseIdentityMapper.selectCount(positionCourseExamLambdaQueryWrapper) > 0) {
            throw new RuntimeException("此岗位已经绑定了该实操鉴定了");
        }
    }

    /**
     * 更新合格状态
     *
     * @param postId
     */
    private void eligibilityStatusChange(String postId) {
        LambdaUpdateWrapper<FtbCultivatePosition> updateQueryWrapper = Wrappers.lambdaUpdate();
        updateQueryWrapper.eq(FtbCultivatePosition::getPostId, postId);
        updateQueryWrapper.set(FtbCultivatePosition::getQualified, 0);
        this.update(new FtbCultivatePosition(), updateQueryWrapper);
    }

    /**
     * 根据证书id获取证书名称
     *
     * @param certificateId 证书id
     */
    private FtbCertificateEntity getFtbCertificateNameById(String certificateId) {
        LambdaQueryWrapper<FtbCertificateEntity> certificateEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
        certificateEntityLambdaQueryWrapper.select(FtbCertificateEntity::getName);
        certificateEntityLambdaQueryWrapper.eq(BaseEntity::getId, certificateId);
        return ftbCultivateCertificateMapper.selectOne(certificateEntityLambdaQueryWrapper);
    }

    /**
     * 根据考试id获取考试名称
     *
     * @param examId 考试id
     */
    private FtbCultivateExam getFtbCultivateExamNameById(String examId) {
        LambdaQueryWrapper<FtbCultivateExam> examLambdaQueryWrapper = Wrappers.lambdaQuery();
        examLambdaQueryWrapper.select(FtbCultivateExam::getExamName);
        examLambdaQueryWrapper.eq(SuperBaseEntity.SuperIBaseEntity::getId, examId);
        return ftbCultivateExamMapper.selectOne(examLambdaQueryWrapper);
    }

    /**
     * 根据实操鉴定id获取实操鉴定名称
     *
     * @param identityId 实操鉴定id
     */
    private CultivateIdentifyTable getCultivateIdentifyNameById(String identityId) {
        LambdaQueryWrapper<CultivateIdentifyTable> identifyTableLambdaQueryWrapper = Wrappers.lambdaQuery();
        identifyTableLambdaQueryWrapper.select(CultivateIdentifyTable::getName);
        identifyTableLambdaQueryWrapper.eq(SuperBaseEntity.SuperIBaseEntity::getId, identityId);
        return cultivateIdentifyTableMapper.selectOne(identifyTableLambdaQueryWrapper);
    }

}
