package jnpf.cultivate.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.cultivate.mapper.FtbCultivatePositionCourseMapper;
import jnpf.cultivate.service.FtbCultivatePositionCourseService;
import jnpf.model.cultivate.dto.learn.BatchCommonCountDto;
import jnpf.model.cultivate.po.position.FtbCultivatePosition;
import jnpf.model.cultivate.po.position.FtbCultivatePositionCourse;
import jnpf.model.cultivate.v2.course.vo.app.AppCourseSimpleVo;
import jnpf.model.cultivate.v2.enums.PositionBusinessSourceEnum;
import jnpf.model.cultivate.v2.position.req.V2OtherCultivatePositionCourseForAppReq;
import jnpf.model.cultivate.v2.position.vo.CultivatePositionSimpleVo;
import jnpf.model.cultivate.v2.position.vo.FtbCultivatePositionCourseWithNameVo;
import jnpf.model.cultivate.v2.position.vo.V2CultivatePositionDetailForApp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

@Service
public class FtbCultivatePositionCourseServiceImpl extends ServiceImpl<FtbCultivatePositionCourseMapper, FtbCultivatePositionCourse> implements FtbCultivatePositionCourseService {

    @Resource
    private FtbCultivatePositionCourseMapper ftbCultivatePositionCourseMapper;

    /**
     * @param positionLearnIds 岗位学习 id 列表
     * @return 数量
     */
    @Override
    public Map<String, Long> countByPositionLearnIds(List<String> positionLearnIds) {
        if (positionLearnIds == null || positionLearnIds.isEmpty()) {
            return Map.of();
        }

        List<BatchCommonCountDto> result = ftbCultivatePositionCourseMapper.countByPositionLearnIds(positionLearnIds);
        return result.stream()
                .collect(Collectors.toMap(
                        BatchCommonCountDto::getSelectKey,
                        dto -> dto.getNum().longValue(),
                        (existing, replacement) -> existing
                ));
    }

    /**
     * 根据岗位学习 ID 查询所有有效课程
     *
     * @param postLearnId 岗位学习 ID
     * @param courseId    课程 ID
     * @return 课程列表
     */
    @Override
    public List<FtbCultivatePositionCourseWithNameVo> listByPostLearnId(String postLearnId, String courseId) {
        return ftbCultivatePositionCourseMapper.listByPostLearnId(postLearnId, courseId);
    }

    /**
     * 根据岗位学习 ID 和职级 ID 查询所有有效课程
     *
     * @param postLearnId 岗位学习 ID
     * @param gradeId     职级 ID
     * @param courseId    课程 ID
     * @return 课程列表
     */
    @Override
    public List<FtbCultivatePositionCourseWithNameVo> listByPostLearnIdAndGradeId(String postLearnId, String gradeId, String courseId) {
        return ftbCultivatePositionCourseMapper.listByPostLearnIdAndGradeId(postLearnId, gradeId, courseId);
    }

    /**
     * 查询所有岗位学习
     *
     * @param courseName 课程名称
     * @return 岗位学习列表
     */
    @Override
    public List<CultivatePositionSimpleVo> queryAllPositionLearn(String courseName) {
        return ftbCultivatePositionCourseMapper.queryAllPositionLearn(courseName);
    }

    /**
     * 查询所有岗位学习课程列表
     *
     * @param dto 请求参数
     * @return 岗位学习课程列表
     */
    @Override
    public List<AppCourseSimpleVo> queryAllPositionLearnCourseLists(V2OtherCultivatePositionCourseForAppReq dto) {
        List<AppCourseSimpleVo> list = ftbCultivatePositionCourseMapper.queryAllPositionLearnCourseLists(dto);
        if (CollUtil.isEmpty(list)) {
            return list;
        }

        list.forEach(course -> course.setBusinessSource(PositionBusinessSourceEnum.POST_LEARNING));
        return list;
    }

    /**
     * 根据岗位学习 ID、年级 ID 和考试 ID 查询培养岗位课程列表
     *
     * @param positionLearnId 岗位学习 ID
     * @param gradeId         职级 id
     * @param examId          考试 ID
     * @return FtbCultivatePositionCourse 对象列表
     */
    @Override
    public List<FtbCultivatePositionCourse> listByPostLearnIdAndExamId(String positionLearnId, String gradeId, String examId) {
        return ftbCultivatePositionCourseMapper.listByPostLearnIdAndExamId(positionLearnId, gradeId, examId);
    }

    /**
     * 根据岗位学习 ID、年级 ID 和实践 ID 查询培养岗位课程列表
     *
     * @param positionLearnId 岗位学习 ID
     * @param gradeId         职级
     * @param practiceId      实践 ID
     * @return FtbCultivatePositionCourse 对象列表
     */
    @Override
    public List<FtbCultivatePositionCourse> listByPostLearnIdAndPracticeId(String positionLearnId, String gradeId, String practiceId) {
        return ftbCultivatePositionCourseMapper.listByPostLearnIdAndPracticeId(positionLearnId, gradeId, practiceId);
    }

    /**
     * 根据岗位学习 ID、年级 ID 和身份 ID 查询培养岗位课程列表
     *
     * @param positionLearnId 岗位学习 ID
     * @param gradeId         职级 iD
     * @param identityId      鉴定 iD
     * @return FtbCultivatePositionCourse 对象列表
     */
    @Override
    public List<FtbCultivatePositionCourse> listByPostLearnIdAndIdentityId(String positionLearnId, String gradeId, String identityId) {
        return ftbCultivatePositionCourseMapper.listByPostLearnIdAndIdentityId(positionLearnId, gradeId, identityId);
    }

    /**
     * 根据岗位学习 ID、年级 ID 查询培养岗位课程列表
     *
     * @param positionLearnId 岗位学习 ID
     * @param gradeId         职级 iD
     * @return FtbCultivatePositionCourse 对象列表
     */
    @Override
    public List<FtbCultivatePositionCourse> queryAllPositionCourseAndLearnIdAndGradeId(String positionLearnId, String gradeId) {
        return ftbCultivatePositionCourseMapper.queryAllPositionCourseAndLearnIdAndGradeId(positionLearnId, gradeId);
    }

    @Override
    public List<FtbCultivatePositionCourseWithNameVo> listPositionCourseList(FtbCultivatePosition existingPosition, V2CultivatePositionDetailForApp req) {
        return ftbCultivatePositionCourseMapper.listPositionCourseList(existingPosition, req);
    }

    @Override
    public List<FtbCultivatePositionCourse> listAllCourseByPostLearnId(String positionLearnId, String postId, String gradeId, Integer compulsory) {
        LambdaQueryWrapper<FtbCultivatePositionCourse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FtbCultivatePositionCourse::getPostLearnId, positionLearnId);
        wrapper.eq(FtbCultivatePositionCourse::getPostRankId, postId);
        wrapper.eq(StringUtils.isNotEmpty(gradeId), FtbCultivatePositionCourse::getGradeId, gradeId);
        wrapper.eq(compulsory != null, FtbCultivatePositionCourse::getCompulsory, compulsory);
        wrapper.eq(FtbCultivatePositionCourse::getEnabledMark, 0); // 只查询有效的记录
        wrapper.orderByAsc(FtbCultivatePositionCourse::getSortCode);
        return list(wrapper);
    }

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

    /**
     * 根据岗位学习ID查询所有有效课程（包含课程名称）
     *
     * @param postLearnId 岗位学习ID
     * @param courseId    课程ID
     * @return 课程列表（包含课程名称）
     */
    @Override
    public List<FtbCultivatePositionCourseWithNameVo> listByPostLearnIdWithName(String postLearnId, String courseId) {
        return ftbCultivatePositionCourseMapper.listByPostLearnIdWithName(postLearnId, courseId);
    }

}