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.nuliji.tools.mybatis.Example;
import com.qxgmat.data.dao.SentenceQuestionMapper;
import com.qxgmat.data.dao.entity.Manager;
import com.qxgmat.data.dao.entity.Question;
import com.qxgmat.data.dao.entity.SentenceQuestion;
import com.qxgmat.data.dao.entity.UserQuestion;
import com.qxgmat.data.relation.SentenceQuestionRelationMapper;
import com.qxgmat.data.relation.entity.SentenceQuestionRelation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

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

    @Resource
    private SentenceQuestionMapper sentenceQuestionMapper;

    @Resource
    private QuestionService questionService;

    @Resource
    private SentenceQuestionRelationMapper sentenceQuestionRelationMapper;

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

    /**
     * 根据长难句题目关系，获取完整题目：列表
     * @param p
     * @return
     */
    public List<SentenceQuestionRelation> relation(List<SentenceQuestion> p){
        List<SentenceQuestionRelation> relationList = Transform.convert(p, SentenceQuestionRelation.class);

        Collection questionIds = Transform.getIds(p, SentenceQuestion.class, "questionId");
        List<Question> questions = questionService.select(questionIds);
        Transform.combine(relationList, questions, SentenceQuestionRelation.class, "questionId", "question", Question.class, "id");
        return relationList;
    }

    /**
     * 根据长难句题目关系，获取完整题目：单个
     * @param p
     * @return
     */
    public SentenceQuestionRelation relation(SentenceQuestion p){
        SentenceQuestionRelation relation = Transform.convert(p, SentenceQuestionRelation.class);

        Question question = questionService.get(p.getQuestionId());
        relation.setQuestion(question);
        return relation;
    }

    /**
     * 根据序号查询
     * @param no
     * @return
     */
    public SentenceQuestion getByNo(Integer no){
        Example example = new Example(SentenceQuestion.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("no", no)
        );
        return one(sentenceQuestionMapper, example);
    }

    public SentenceQuestion add(SentenceQuestion question){
        int result = insert(sentenceQuestionMapper, question);
        question = one(sentenceQuestionMapper, question.getId());
        if(question == null){
            throw new SystemException("题目添加失败");
        }
        return question;
    }

    public SentenceQuestion edit(SentenceQuestion question){
        SentenceQuestion in = one(sentenceQuestionMapper, question.getId());
        if(in == null){
            throw new ParameterException("题目不存在");
        }
        int result = update(sentenceQuestionMapper, question);
        return question;
    }

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

    public SentenceQuestion get(Number id){
        SentenceQuestion in = one(sentenceQuestionMapper, id);

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

    public Page<SentenceQuestion> select(int page, int pageSize){
        return select(sentenceQuestionMapper, page, pageSize);
    }

    public List<SentenceQuestion> select(Collection ids){
        return select(sentenceQuestionMapper, ids);
    }

}
