package com.ynfy.buss.exam.repository.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynfy.buss.exam.question.dto.QuestionDTO;
import com.ynfy.buss.exam.question.enums.QuestionType;
import com.ynfy.buss.exam.question.service.IQuestionService;
import com.ynfy.buss.exam.repository.entity.Repository;
import com.ynfy.buss.exam.repository.mapper.RepositoryMapper;
import com.ynfy.buss.exam.repository.service.IRepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: repository
 * @Author: jeecg-boot
 * @Date: 2023-02-22
 * @Version: V1.0
 */
@Service
public class RepositoryServiceImpl extends ServiceImpl<RepositoryMapper, Repository> implements IRepositoryService {

    @Lazy
    @Autowired
    public IQuestionService questionService;

    /**
     * 题库获取各类型题目数
     *
     * @param list
     */
    @Override
    public void countQuestionNumByType(List<Repository> list) {
        if (!CollectionUtils.isEmpty(list)) {
            List<String> idList = list.stream().map(Repository::getId).collect(Collectors.toList());
            List<QuestionDTO> questionDTOList = questionService.countQuestionType(idList);
            Map<String, List<QuestionDTO>> map = questionDTOList.stream().collect(Collectors
                    .groupingBy(QuestionDTO::getRepositoryId, Collectors.toList()));
            list.forEach(l -> {
                List<QuestionDTO> dtoList = map.get(l.getId());
                if (!CollectionUtils.isEmpty(dtoList)) {
                    l.setNum(dtoList.stream().mapToInt(QuestionDTO::getNum).sum());
                    StringBuilder str = new StringBuilder();
                    int index = 0;
                    for (QuestionDTO dto : dtoList) {
                        if (dto.getType().equals(4)||dto.getType().equals(6)) {
                        //组合简答暂时不显示
//                            System.out.println("不拼接");
//                            System.out.println(QuestionType.getByCode(dto.getType()).getValue());
                        }else {
                            str.append(QuestionType.getByCode(dto.getType()).getValue()).append(dto.getNum()).append("题");
                            if (index < dtoList.size() - 1) {
                                str.append("，");
                            }
                        }
                        index++;
                    }
//                    System.out.println("yyz打印："+str.toString());
                    l.setQuestionRemark(str.toString());
                }
            });
        }
    }
}
