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.FtbCultivateLearnTaskAssignmentMapper;
import jnpf.cultivate.service.FtbCultivateLearnTaskAssignmentService;
import jnpf.model.cultivate.dto.learn.BatchCommonCountDto;
import jnpf.model.cultivate.po.learn.FtbCultivateLearnTaskAssignment;
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 FtbCultivateLearnTaskAssignmentServiceImpl extends ServiceImpl<FtbCultivateLearnTaskAssignmentMapper, FtbCultivateLearnTaskAssignment> implements FtbCultivateLearnTaskAssignmentService {

    @Resource
    private FtbCultivateLearnTaskAssignmentMapper ftbCultivateLearnTaskAssignmentMapper;

    /**
     * 批量查询统计任务的学习人数
     * @param taskIds 任务id
     * @param status  0-查询全部  1-查询已经完成的
     * @return
     */
    @Override
    public Map<String, Integer> groupCountNum(List<String> taskIds, Integer status) {
        Map<String, Integer> ret = new HashMap<>();
        if(CollUtil.isEmpty(taskIds)){
            return ret;
        }
        List<BatchCommonCountDto> list = ftbCultivateLearnTaskAssignmentMapper.groupCountNum(taskIds, status);
        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<FtbCultivateLearnTaskAssignment>> groupListAssignment(ArrayList<String> taskIds) {
        Map<String, List<FtbCultivateLearnTaskAssignment>> ret = new HashMap<>();
        if(CollUtil.isEmpty(taskIds)){
            return ret;
        }
        List<FtbCultivateLearnTaskAssignment> list = ftbCultivateLearnTaskAssignmentMapper.groupListAssignment(taskIds);
        if (CollectionUtil.isEmpty(list)) {
            return ret;
        }
        for (FtbCultivateLearnTaskAssignment dto : list) {
            List<FtbCultivateLearnTaskAssignment> ftbCultivateLearnTaskAssignments = ret.get(dto.getTaskId());
            if (CollUtil.isEmpty(ftbCultivateLearnTaskAssignments)) {
                ftbCultivateLearnTaskAssignments = new ArrayList<>();
            }
            ftbCultivateLearnTaskAssignments.add(dto);
            ret.put(dto.getTaskId(), ftbCultivateLearnTaskAssignments);
        }
        return ret;
    }

}
