package jnpf.cultivate.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.cultivate.mapper.FtbCultivatePositionCoursePracticeMapper;
import jnpf.cultivate.service.FtbCultivatePositionCoursePracticeService;
import jnpf.model.cultivate.dto.learn.BatchCommonCountDto;
import jnpf.model.cultivate.po.position.FtbCultivatePosition;
import jnpf.model.cultivate.po.position.FtbCultivatePositionCoursePractice;
import jnpf.model.cultivate.v2.position.req.V2OtherCultivatePositionCourseForAppReq;
import jnpf.model.cultivate.v2.position.vo.FtbCultivatePositionCoursePracticeWithNameVo;
import org.springframework.stereotype.Service;

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

/**
 * 岗位学习课程关联练习表服务实现类
 *
 * @author 
 * @since 2025-01-19
 */
@Service
public class FtbCultivatePositionCoursePracticeServiceImpl extends ServiceImpl<FtbCultivatePositionCoursePracticeMapper, FtbCultivatePositionCoursePractice> implements FtbCultivatePositionCoursePracticeService {

    @Resource
    private FtbCultivatePositionCoursePracticeMapper ftbCultivatePositionCoursePracticeMapper;

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

    @Override
    public List<FtbCultivatePositionCoursePractice> listByPostLearnId(String postLearnId) {
        return ftbCultivatePositionCoursePracticeMapper.listByPostLearnId(postLearnId);
    }
    
    @Override
    public List<FtbCultivatePositionCoursePractice> listByPostLearnIdAndCourseIds(String postLearnId, List<String> courseIds) {
        if (courseIds == null || courseIds.isEmpty()) {
            return List.of();
        }
        
        return ftbCultivatePositionCoursePracticeMapper.listByPostLearnIdAndCourseIds(postLearnId, courseIds);
    }

    @Override
    public List<FtbCultivatePositionCoursePractice> queryAllPositionLearnParacticeLists(V2OtherCultivatePositionCourseForAppReq v2OtherCultivatePositionCourseForAppReq) {
        return ftbCultivatePositionCoursePracticeMapper.queryAllPositionLearnParacticeLists(v2OtherCultivatePositionCourseForAppReq);
    }

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

    /**
     * 根据岗位学习ID查询练习列表（包含技能点名称）
     *
     * @param postLearnId 岗位学习ID
     * @return 练习列表（包含技能点名称）
     */
    @Override
    public List<FtbCultivatePositionCoursePracticeWithNameVo> listByPostLearnIdWithName(String postLearnId) {
        return ftbCultivatePositionCoursePracticeMapper.listByPostLearnIdWithName(postLearnId);
    }

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