package com.mwt.oes.service.impl;

import com.mwt.oes.dao.PaperMapper;
import com.mwt.oes.dao.PaperQueMapper;
import com.mwt.oes.dao.ProgramingLanguageMapper;
import com.mwt.oes.domain.Paper;
import com.mwt.oes.domain.PaperExample;
import com.mwt.oes.domain.PaperQue;
import com.mwt.oes.domain.ProgramingLanguage;
import com.mwt.oes.service.StudentHomeService;
import com.mwt.oes.service.StudentPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentPaperServiceImpl implements StudentPaperService {

    @Autowired
    PaperMapper paperMapper;

    @Autowired
    PaperQueMapper paperQueMapper;

    @Autowired
    ProgramingLanguageMapper languageMapper;

    @Autowired
    StudentHomeService studentHomeService;

    /**
     * 根据状态获取当前可考试卷
     * @param paperStatus
     * @return
     */
    @Override
    public List<Map<String, Object>> getCurrentPapersList(Integer paperStatus) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<Paper> paperList = paperMapper.selectCurrentPaperList(paperStatus);
        for (Paper paper : paperList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", paperList.indexOf(paper) + 1);
            map.put("paperId", paper.getPaperId());
            map.put("paperName", paper.getPaperName());

            //添加分类信息
            map.put("langId", paper.getLangId());
            ProgramingLanguage programingLanguage = languageMapper.selectByPrimaryKey(paper.getLangId());
            map.put("langName", programingLanguage.getLangName());
            map.put("langImgSrc", programingLanguage.getLangImgSrc());

            map.put("paperDuration", paper.getPaperDuration());
            map.put("paperDifficulty", paper.getPaperDifficulty());
            map.put("paperAttention", paper.getPaperAttention());
            map.put("paperType", paper.getPaperType());
            map.put("singleScore", paper.getSingleScore());
            map.put("multipleScore", paper.getMultipleScore());
            map.put("judgeScore", paper.getJudgeScore());
            map.put("fillScore", paper.getFillScore());

            //通过试题数量算总分数
            Map<String, Integer> queNum = studentHomeService.getPaperQueNumByPaperId(paper.getPaperId());
            int totalScore = queNum.get("singleNum")*paper.getSingleScore()
                    + queNum.get("multipleNum")*paper.getMultipleScore()
                    + queNum.get("judgeNum")*paper.getJudgeScore()
                    + queNum.get("fillNum")*paper.getFillScore();
            map.put("totalScore", totalScore);
            map.put("singleNum", queNum.get("singleNum"));
            map.put("multipleNum", queNum.get("multipleNum"));
            map.put("judgeNum", queNum.get("judgeNum"));
            map.put("fillNum", queNum.get("fillNum"));
            map.put("totalNum", queNum.get("totalNum"));
            map.put("participateNum", paper.getParticipateNum());
            map.put("paperCreateTime", paper.getPaperCreateTime());
            map.put("startTime", paper.getStartTime());
            map.put("endTime", paper.getEndTime());
            map.put("paperStatus", paper.getPaperStatus());
            resultList.add(map);
        }

        return resultList;
    }

    /**
     * 通过 ID 获取试卷信息
     * @param paperId
     * @return
     */
    @Override
    public Paper getPaperInfoByPaperId(Integer paperId) {
        Paper paper = paperMapper.selectByPrimaryKey(paperId);
        return paper;
    }

    /**
     * 通过 ID 获取试卷各题目数量
     * @param paperId
     * @return
     */
    @Override
    public Map<String, Integer> getPaperQueNumByPaperId(Integer paperId) {
        Map<String, Integer> result = new HashMap<>();
        List<PaperQue> list = paperQueMapper.selectByPaperId(paperId);

        if(list != null){
            Long singleNum = list.stream().filter(f->(f.getSingleId() != null)).count();
            Long multipleNum = list.stream().filter(f->(f.getMultipleId() != null)).count();
            Long judgeNum = list.stream().filter(f->(f.getJudgeId() != null)).count();
            Long fillNum = list.stream().filter(f->(f.getFillId() != null)).count();

            result.put("totalNum",list.size());
            result.put("singleNum",singleNum.intValue());
            result.put("multipleNum",multipleNum.intValue());
            result.put("judgeNum",judgeNum.intValue());
            result.put("fillNum",fillNum.intValue());
        }

        return result;
    }
}
