package com.qxgmat.service.inline;

import com.github.pagehelper.Page;
import com.nuliji.tools.AbstractService;
import com.nuliji.tools.Transform;
import com.nuliji.tools.exception.ParameterException;
import com.nuliji.tools.exception.SystemException;
import com.qxgmat.data.dao.QuestionMapper;
import com.qxgmat.data.dao.entity.Question;
import com.qxgmat.data.dao.entity.UserQuestion;
import com.qxgmat.data.inline.PaperStat;
import com.qxgmat.data.relation.QuestionRelationMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class QuestionService extends AbstractService {
    private static final Logger logger = LoggerFactory.getLogger(QuestionService.class);

    @Resource
    private QuestionMapper questionMapper;

    @Resource
    private QuestionRelationMapper questionRelationMapper;

    /**
     * 根据题目获取总试卷统计信息
     * @param questionNoList
     * @return
     */
    public PaperStat statPaper(List<Question> questionNoList){
        PaperStat stat = new PaperStat();
        Integer totalTime = 0;
        Integer totalNumber = 0;
        Integer totalCorrect = 0;
        for(Question question : questionNoList){
            totalTime += question.getTotalTime();
            totalNumber += question.getTotalNumber();
            totalCorrect += question.getTotalCorrect();
        }
        stat.setTotalCorrect(totalCorrect);
        stat.setTotalNumber(totalNumber);
        stat.setTotalTime(totalTime);
        return stat;
    }

    /**
     * 根据试卷分组获取统计信息
     * @param questionIdsMap
     * @return
     */
    public Map<Integer, PaperStat> statPaperMap(Map<Integer, Integer[]> questionIdsMap){
        Map<Integer, PaperStat> relationMap = new HashMap<>();
        List<Integer> ids = new ArrayList<>();
        for(Integer[] questionIds : questionIdsMap.values()){
            ids.addAll(Arrays.stream(questionIds).collect(Collectors.toList()));
        }
        List<Question> questionList = select(ids);
        Map questionNoMap = Transform.getMap(questionList, Question.class, "id");
        List<Question> l = new ArrayList<>();
        for(Integer k: questionIdsMap.keySet()){
            l.clear();
            for (Integer questionNoId : questionIdsMap.get(k)){
                l.add((Question)questionNoMap.get(questionNoId));
            }
            relationMap.put(k, statPaper(l));
        }

        return relationMap;
    }

    /**
     * 累加做题记录到questionNo
     * @param question
     */
    public void accumulation(UserQuestion question){
        questionRelationMapper.accumulation(question.getQuestionId(), 1, question.getTime(), question.getIsCorrect());
    }

    public Question add(Question question){
        // 按实际更新更改更新时间
        if (!question.getQxContent().isEmpty()){
            question.setQxTime(new Date());
        }
        if (!question.getOfficialContent().isEmpty()){
            question.setOfficialTime(new Date());
        }
        if (question.getAssociationContent().length > 0){
            question.setAssociationTime(new Date());
        }
        int result = insert(questionMapper, question);
        question = one(questionMapper, question.getId());
        if(question == null){
            throw new SystemException("题目添加失败");
        }
        return question;
    }

    public Question edit(Question question){
        Question in = one(questionMapper, question.getId());
        if(in == null){
            throw new ParameterException("题目不存在");
        }
        // 按实际更新更改更新时间
        if (!in.getQxContent().equals(question.getQxContent())){
            question.setQxTime(new Date());
        }
        if (!in.getOfficialContent().equals(question.getOfficialContent())){
            question.setOfficialTime(new Date());
        }
        if (in.getAssociationContent().length != question.getAssociationContent().length){
            question.setAssociationTime(new Date());
        }
        int result = update(questionMapper, question);
        return question;
    }

    public boolean delete(Number id){
        Question in = one(questionMapper, id);
        if(in == null){
            throw new ParameterException("题目不存在");
        }
        int result = delete(questionMapper, id);
        return result > 0;
    }

    public Question get(Number id){
        Question in = one(questionMapper, id);

        if(in == null){
            throw new ParameterException("题目不存在");
        }
        return in;
    }

    public Page<Question> select(int page, int pageSize){
        return select(questionMapper, page, pageSize);
    }

    public List<Question> select(Collection ids){
        return select(questionMapper, ids);
    }

}
