package jnpf.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jnpf.model.cultivate.po.position.FtbCultivatePosition;
import jnpf.model.cultivate.v2.position.vo.FtbCultivatePositionCourseIdentityWithNameVo;
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.FtbCultivatePositionCourseIdentity;
import jnpf.cultivate.mapper.FtbCultivatePositionCourseIdentityMapper;

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

@Service
public class FtbCultivatePositionCourseIdentityServiceImpl extends ServiceImpl<FtbCultivatePositionCourseIdentityMapper, FtbCultivatePositionCourseIdentity> implements FtbCultivatePositionCourseIdentityService {

    @Resource
    private FtbCultivatePositionCourseIdentityMapper ftbCultivatePositionCourseIdentityMapper;

    @Override
    public Map<String, Long> countByPositionLearnIds(List<String> positionLearnIds) {
        if (positionLearnIds == null || positionLearnIds.isEmpty()) {
            return Map.of();
        }

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

    @Override
    public List<FtbCultivatePositionCourseIdentity> listByPostLearnId(String postLearnId) {
        return ftbCultivatePositionCourseIdentityMapper.listByPostLearnId(postLearnId);
    }

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

    @Override
    public List<FtbCultivatePositionCourseIdentity> queryAllPositionLearnIdentityLists() {
        return ftbCultivatePositionCourseIdentityMapper.queryAllPositionLearnIdentityLists();
    }

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

    /**
     * 根据岗位学习ID查询鉴定列表（包含鉴定名称）
     *
     * @param postLearnId 岗位学习ID
     * @return 鉴定列表（包含鉴定名称）
     */
    @Override
    public List<FtbCultivatePositionCourseIdentityWithNameVo> listByPostLearnIdWithName(String postLearnId) {
        return ftbCultivatePositionCourseIdentityMapper.listByPostLearnIdWithName(postLearnId);
    }

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