package jnpf.cultivate.service.impl;

import jnpf.model.cultivate.po.position.FtbCultivatePosition;
import jnpf.model.cultivate.v2.position.req.V2OtherCultivatePositionCourseForAppReq;
import jnpf.model.cultivate.v2.position.vo.FtbCultivatePositionCourseExamWithNameVo;
import jnpf.model.cultivate.v2.position.vo.V2AllCultivatePositionCourseExam;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.model.cultivate.dto.learn.BatchCommonCountDto;
import jnpf.model.cultivate.po.position.FtbCultivatePositionCourseExam;
import jnpf.cultivate.mapper.FtbCultivatePositionCourseExamMapper;

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

@Service
public class FtbCultivatePositionCourseExamServiceImpl extends ServiceImpl<FtbCultivatePositionCourseExamMapper, FtbCultivatePositionCourseExam> implements FtbCultivatePositionCourseExamService {

    @Resource
    private FtbCultivatePositionCourseExamMapper ftbCultivatePositionCourseExamMapper;

    @Override
    public Map<String, Long> countByPositionLearnIds(List<String> positionLearnIds) {
        if (positionLearnIds == null || positionLearnIds.isEmpty()) {
            return Map.of();
        }
        
        List<BatchCommonCountDto> result = ftbCultivatePositionCourseExamMapper.countByPositionLearnIds(positionLearnIds);
        return result.stream()
                .collect(Collectors.toMap(
                        BatchCommonCountDto::getSelectKey,
                        dto -> dto.getNum().longValue(),
                        (existing, replacement) -> existing
                ));
    }

    @Override
    public List<FtbCultivatePositionCourseExam> listByPostLearnId(String postLearnId) {
        return ftbCultivatePositionCourseExamMapper.listByPostLearnIdWithExam(postLearnId);
    }
    
    @Override
    public List<FtbCultivatePositionCourseExam> listByPostLearnIdAndCourseIds(String postLearnId, List<String> courseIds,String gradeId) {
        if (courseIds == null || courseIds.isEmpty()) {
            return List.of();
        }
        
        return ftbCultivatePositionCourseExamMapper.listByPostLearnIdAndCourseIdsWithExam(postLearnId, courseIds,gradeId);
    }

    @Override
    public List<FtbCultivatePositionCourseExam> queryAllPositionLearnExamLists(V2OtherCultivatePositionCourseForAppReq v2OtherCultivatePositionCourseForAppReq) {
        return ftbCultivatePositionCourseExamMapper.queryAllPositionLearnExamListsWithExam(v2OtherCultivatePositionCourseForAppReq);
    }

    @Override
    public List<String> queryPositionBindItem(FtbCultivatePosition cultivatePosition, String gradeId) {
        return ftbCultivatePositionCourseExamMapper.queryPositionBindItem(cultivatePosition, gradeId);
    }

    @Override
    public List<V2AllCultivatePositionCourseExam> queryAllConfigExamId(List<String> postLearnIds, String courseId, String gradeId) {
        return ftbCultivatePositionCourseExamMapper.queryAllConfigExamId(postLearnIds, courseId, gradeId);
    }

    /**
     * 根据岗位学习ID查询所有有效考试（包含考试名称）
     *
     * @param postLearnId 岗位学习ID
     * @return 考试列表（包含考试名称）
     */
    @Override
    public List<FtbCultivatePositionCourseExamWithNameVo> listByPostLearnIdWithName(String postLearnId) {
        return ftbCultivatePositionCourseExamMapper.listByPostLearnIdWithName(postLearnId);
    }

    /**
     * 根据岗位学习 ID 和课程 ID 列表查询考试（包含考试名称）
     *
     * @param postLearnId 岗位学习 ID
     * @param courseIds   课程 ID 列表
     * @return 考试列表（包含考试名称）
     */
    @Override
    public List<FtbCultivatePositionCourseExamWithNameVo> listByPostLearnIdAndCourseIdsWithName(String postLearnId, List<String> courseIds) {
        if (courseIds == null || courseIds.isEmpty()) {
            return List.of();
        }
        return ftbCultivatePositionCourseExamMapper.listByPostLearnIdAndCourseIdsWithName(postLearnId, courseIds);
    }

    /**
     * 检查考试是否被岗位学习绑定（关联岗位学习主表和课程表）
     *
     * @param examId 考试ID
     * @return true-已绑定 false-未绑定
     */
    @Override
    public Boolean checkExamBinding(String examId) {
        Integer count = ftbCultivatePositionCourseExamMapper.checkExamBinding(examId);
        return count != null && count > 0;
    }
}