package jnpf.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.base.entity.SuperBaseEntity;
import jnpf.cultivate.mapper.FtbCultivateCourseMapper;
import jnpf.cultivate.mapper.FtbCultivatePositionCourseExamMapper;
import jnpf.cultivate.mapper.FtbCultivatePositionExamMapper;
import jnpf.cultivate.service.FtbCultivatePositionExamCourseService;
import jnpf.cultivate.utils.UserApiV2Util;
import jnpf.model.cultivate.bo.FtbCultivateCourseBO;
import jnpf.model.cultivate.bo.FtbCultivatePositionExamBO;
import jnpf.model.cultivate.po.course.FtbCultivateCourse;
import jnpf.model.cultivate.po.position.FtbCultivatePositionCourseExam;
import jnpf.model.cultivate.po.position.FtbCultivatePositionExam;
import jnpf.permission.vo.v2.position.PositionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FtbCultivatePositionExamCourseServiceImpl implements FtbCultivatePositionExamCourseService {

    @Resource
    private FtbCultivatePositionExamMapper ftbCultivatePositionExamMapper;
    @Resource
    private FtbCultivatePositionCourseExamMapper ftbCultivatePositionCourseExamMapper;
    @Resource
    private FtbCultivateCourseMapper cultivateCourseMapper;
    @Autowired
    private UserApiV2Util userApiV2Util;


    @Override
    public FtbCultivatePositionExamBO testPaperCall(String examId, String relationPositonId, String businessId, Integer examSource) {
        FtbCultivatePositionExamBO ftbCultivatePositionExamBO = new FtbCultivatePositionExamBO();
        // 职等id
        String postRankId = null;
        // 考试来源,1岗位学习考试，2课程考试
        if (examSource == 1) {
            LambdaQueryWrapper<FtbCultivatePositionExam> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(FtbCultivatePositionExam::getExamId, examId);
            queryWrapper.eq(FtbCultivatePositionExam::getPostLearnId, relationPositonId);
            queryWrapper.eq(SuperBaseEntity.SuperIBaseEntity::getId, businessId);
            queryWrapper.last("limit 1");
            FtbCultivatePositionExam ftbCultivatePositionExam = ftbCultivatePositionExamMapper.selectOne(queryWrapper);
            postRankId = ftbCultivatePositionExam.getPostRankId();
        } else if (examSource == 2) {
            LambdaQueryWrapper<FtbCultivatePositionCourseExam> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(FtbCultivatePositionCourseExam::getExamId, examId);
            queryWrapper.eq(FtbCultivatePositionCourseExam::getPostLearnId, relationPositonId);
            queryWrapper.eq(SuperBaseEntity.SuperIBaseEntity::getId, businessId);
            queryWrapper.last("limit 1");
            FtbCultivatePositionCourseExam ftbCultivatePositionCourseExam = ftbCultivatePositionCourseExamMapper.selectOne(queryWrapper);
            postRankId = ftbCultivatePositionCourseExam.getPostRankId();
        }
        if (postRankId != null) {
            ftbCultivatePositionExamBO.setPostRankId(postRankId);
            try {
                // 职等名称
//                ActionResult<PositionGradesInfoVO> gradesInfo = positionApi.getGradesInfo(postRankId);
                PositionVO gradesInfoData = userApiV2Util.infoPosition(postRankId, null);
                if (gradesInfoData != null) {
                    // 岗位名称
                    ftbCultivatePositionExamBO.setPostName(gradesInfoData.getFullName());
                    ftbCultivatePositionExamBO.setPostId(gradesInfoData.getId());
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return ftbCultivatePositionExamBO;
    }

    @Override
    public List<FtbCultivateCourseBO> getCourseName(List<String> courseIds) {
        LambdaQueryWrapper<FtbCultivateCourse> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(SuperBaseEntity.SuperIBaseEntity::getId, courseIds);
        List<FtbCultivateCourse> ftbCultivateCourseList = cultivateCourseMapper.selectList(queryWrapper);
        return ftbCultivateCourseList.stream().map(ftbCultivateCourse -> {
            FtbCultivateCourseBO ftbCultivateCourseBO = new FtbCultivateCourseBO();
            ftbCultivateCourseBO.setCourseId(ftbCultivateCourse.getId());
            ftbCultivateCourseBO.setCourseName(ftbCultivateCourse.getName());
            return ftbCultivateCourseBO;
        }).collect(Collectors.toList());
    }
}
