package com.qmth.distributed.print.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qmth.distributed.print.business.bean.dto.CourseDimensionDto;
import com.qmth.distributed.print.business.entity.ObeCourseRequirementDimension;
import com.qmth.distributed.print.business.mapper.ObeCourseRequirementDimensionMapper;
import com.qmth.distributed.print.business.service.ObeCourseDimensionService;
import com.qmth.distributed.print.business.service.ObeCourseRequirementDimensionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 课程大纲指标与知识点关联表 服务实现类
 * </p>
 *
 * @author CaoZixuan
 * @since 2024-06-07
 */
@Service
public class ObeCourseRequirementDimensionServiceImpl extends ServiceImpl<ObeCourseRequirementDimensionMapper, ObeCourseRequirementDimension> implements ObeCourseRequirementDimensionService {

    @Resource
    private ObeCourseDimensionService obeCourseDimensionService;

    @Override
    public List<CourseDimensionDto> findByOutlineIdAndRequirementId(Long obeCourseOutlineId,
            Long obeCultureProgramRequirementId) {
        List<CourseDimensionDto> result = new ArrayList<>();
        List<Long> dimensionIdList = this.list(new QueryWrapper<ObeCourseRequirementDimension>().lambda()
                        .eq(ObeCourseRequirementDimension::getObeCourseOutlineId, obeCourseOutlineId)
                        .eq(ObeCourseRequirementDimension::getObeCultureProgramRequirementId, obeCultureProgramRequirementId))
                .stream().map(ObeCourseRequirementDimension::getObeCourseDimensionId).distinct()
                .collect(Collectors.toList());

        if (CollectionUtils.isNotEmpty(dimensionIdList)) {
            result = obeCourseDimensionService.listByIds(dimensionIdList).stream().flatMap(e -> {
                CourseDimensionDto courseDimensionDto = new CourseDimensionDto();
                courseDimensionDto.setId(e.getId());
                courseDimensionDto.setCode(e.getCode());
                courseDimensionDto.setName(e.getName());
                return Stream.of(courseDimensionDto);
            }).collect(Collectors.toList());
        }
        return result;
    }
}
