package cn.reghao.tnb.content.app.exam.service;

import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.tnb.common.db.KeyValue;
import cn.reghao.tnb.content.app.exam.db.mapper.PaperMapper;
import cn.reghao.tnb.content.app.exam.db.mapper.QuestionMapper;
import cn.reghao.tnb.content.app.exam.db.mapper.SubjectMapper;
import cn.reghao.tnb.content.app.exam.model.vo.SubjectInfo;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2024-08-24 23:23:54
 */
@Service
public class SubjectService {
    private final SubjectMapper subjectMapper;
    private final QuestionMapper questionMapper;
    private final PaperMapper paperMapper;

    public SubjectService(SubjectMapper subjectMapper, QuestionMapper questionMapper, PaperMapper paperMapper) {
        this.subjectMapper = subjectMapper;
        this.questionMapper = questionMapper;
        this.paperMapper = paperMapper;
    }

    public PageList<SubjectInfo> getSubjectInfos(int pageNumber, int pageSize) {
        List<SubjectInfo> list = subjectMapper.findAll().stream().map(subject -> {
            /*int id = subject.getId();
            List<GroupCount> list1 = questionMapper.findGroupBySubjectId(id);
            list1.forEach(groupCount -> {
                int type = Integer.parseInt(groupCount.getId());
                int total = groupCount.getTotal();
            });
            int paperCount = paperMapper.countBySubjectId(id);*/
            return new SubjectInfo(subject);
        }).collect(Collectors.toList());
        return PageList.pageList(pageNumber, pageSize, list.size(), list);
    }

    public List<KeyValue> getKeyValues() {
        return subjectMapper.findAll().stream().map(subject -> {
            int id = subject.getId();
            String name = subject.getName();
            return new KeyValue(id, name);
        }).collect(Collectors.toList());
    }
}
