package cn.com.qmth.stmms.ms.marking.assembler;

import cn.com.qmth.stmms.ms.core.domain.MarkStage;
import cn.com.qmth.stmms.ms.core.domain.Paper;
import cn.com.qmth.stmms.ms.core.domain.task.MarkTask;
import cn.com.qmth.stmms.ms.core.repository.MarkTaskRepo;
import cn.com.qmth.stmms.ms.core.vo.Subject;
import cn.com.qmth.stmms.ms.marking.dto.MarkQuestionStatDTO;
import cn.com.qmth.stmms.ms.marking.dto.QuestionStatDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.com.qmth.stmms.ms.core.repository.LevelRepo;
import cn.com.qmth.stmms.ms.core.repository.PaperRepo;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by zhengmin on 2016/10/14.
 */
@Component
public class QuestionStatAssembler {

    @Autowired
    private LevelRepo levelRepo;

    @Autowired
    private PaperRepo paperRepo;

    @Autowired
    private MarkTaskRepo markTaskRepo;

    public QuestionStatDTO toDTO(Object[] qStats) {
        return this.assemble(qStats);
    }

    public QuestionStatDTO toDTOWithMarkers(Object[] qStats) {
        QuestionStatDTO qpDTO = this.assemble(qStats);
        if (qpDTO != null) {

        }
        return qpDTO;
    }

    private QuestionStatDTO assemble(Object[] qStats) {
        QuestionStatDTO qpDTO = null;
        if (qStats != null) {
            qpDTO = new QuestionStatDTO();
            qpDTO.setId(qStats[0]);
            qpDTO.setName(qStats[1]);
            qpDTO.setLeftCount(qStats[2]);
            qpDTO.setTotalCount(qStats[3]);
            qpDTO.setAreaCode(qStats[4]);
        }
        return qpDTO;
    }

    public QuestionStatDTO toProgressDTO(Object[] qStats) {
        QuestionStatDTO qpDTO = null;
        if (qStats != null) {
            qpDTO = new QuestionStatDTO();
            qpDTO.setAreaCode(qStats[0]);
            qpDTO.setAreaName(qStats[1]);
            qpDTO.setName(qStats[2]);
            qpDTO.setLeftCount(qStats[3]);
            qpDTO.setTotalCount(qStats[4]);
        }
        return qpDTO;
    }


    public MarkQuestionStatDTO toMarkProgressDTO(Object[] qStats, Long workId, Subject subject) {
//        List<Paper> papers = paperRepo.findByWorkIdAndSubject(workId, subject);
//        long shift = papers.stream().filter(m->m.isShift()).count();
//        long shiftScore = papers.stream().filter(m->m.isShiftScore()).count();
        List<MarkTask> tasks = markTaskRepo.findByWorkIdAndSubjectAndMarkerIdAndStage(workId, subject, Long.valueOf(qStats[1].toString()), MarkStage.LEVEL);
        long shift = tasks.stream().filter(m -> m.getPaper().isShift() && Objects.isNull(m.getResult())).count();
        List<MarkTask> tasks1 = markTaskRepo.findByWorkIdAndSubjectAndMarkerIdAndStage(workId, subject, Long.valueOf(qStats[1].toString()), MarkStage.SCORE);
        long shiftScore = tasks1.stream().filter(m -> m.getPaper().isShiftScore() && Objects.isNull(m.getResult())).count();
        //所有打回
        long rejectCount = tasks.stream().filter(m->m.isRejected()).count();
        MarkQuestionStatDTO qpDTO = null;
        if (qStats != null) {
            qpDTO = new MarkQuestionStatDTO();
            qpDTO.setId(qStats[0]);
            qpDTO.setLoginName(qStats[2]);
            qpDTO.setMarkerName(qStats[3]);
            qpDTO.setName(qStats[4]);
            qpDTO.setLeftCount(qStats[5]);
            qpDTO.setTotalCount(qStats[6]);
            qpDTO.setRejectedCount(rejectCount);
            qpDTO.setShiftCount(shift);
            qpDTO.setShiftScoreCount(shiftScore);
            qpDTO.setAreaCode(qStats[8]);
        }
        return qpDTO;
    }
}
