package jnpf.cultivate.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.cultivate.mapper.FtbCultivateLearnTaskCourseMapper;
import jnpf.cultivate.service.FtbCultivateLearnTaskCourseService;
import jnpf.model.cultivate.dto.learn.BatchCommonCountDto;
import jnpf.model.cultivate.po.learn.FtbCultivateLearnTaskCourse;
import jnpf.model.cultivate.v2.task.vo.FtbCultivateLearnTaskCourseVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: peng.hao
 * @create: 2024/9/9:11:05
 */
@Service
public class FtbCultivateLearnTaskCourseServiceImpl extends ServiceImpl<FtbCultivateLearnTaskCourseMapper, FtbCultivateLearnTaskCourse> implements FtbCultivateLearnTaskCourseService {

    @Resource
    private FtbCultivateLearnTaskCourseMapper ftbCultivateLearnTaskCourseMapper;

    /**
     * 批量查询任务的课程数量
     *
     * @param taskIds 任务id集合
     * @return
     */
    @Override
    public Map<String, Integer> groupCountNum(List<String> taskIds) {
        Map<String, Integer> ret = new HashMap<>();
        List<BatchCommonCountDto> list = ftbCultivateLearnTaskCourseMapper.groupCountNum(taskIds);
        if (CollectionUtil.isEmpty(list)) {
            return ret;
        }
        for (BatchCommonCountDto batchCommonCountDto : list) {
            ret.put(batchCommonCountDto.getSelectKey(), batchCommonCountDto.getNum());
        }
        return ret;
    }

    /**
     * 批量查询任务的课程列表
     *
     * @param taskIds 任务ID集合
     * @return
     */
    @Override
    public Map<String, List<FtbCultivateLearnTaskCourse>> groupListTaskCourse(ArrayList<String> taskIds) {
        Map<String, List<FtbCultivateLearnTaskCourse>> ret = new HashMap<>();
        if (CollUtil.isEmpty(taskIds)) {
            return ret;
        }
        List<FtbCultivateLearnTaskCourse> list = ftbCultivateLearnTaskCourseMapper.groupListTaskCourse(taskIds);
        if (CollectionUtil.isEmpty(list)) {
            return ret;
        }
        for (FtbCultivateLearnTaskCourse dto : list) {
            List<FtbCultivateLearnTaskCourse> ftbCultivateLearnTaskAssignments = ret.get(dto.getTaskId());
            if (CollUtil.isEmpty(ftbCultivateLearnTaskAssignments)) {
                ftbCultivateLearnTaskAssignments = new ArrayList<>();
            }
            ftbCultivateLearnTaskAssignments.add(dto);
            ret.put(dto.getTaskId(), ftbCultivateLearnTaskAssignments);
        }
        return ret;
    }

    @Override
    public List<FtbCultivateLearnTaskCourseVo> listByTaskId(String taskId, Integer required) {
//        是否必修 (0 是 1 否)
        return ftbCultivateLearnTaskCourseMapper.listByTaskId(taskId, required);
    }

    @Override
    public List<FtbCultivateLearnTaskCourseVo> listByTaskIdAndPhaseId(String taskId, String phaseId) {
        return ftbCultivateLearnTaskCourseMapper.listByTaskIdAndPhaseId(taskId, phaseId);
    }
}