package com.ruoyi.test.seriver.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.test.domain.Answer;
import com.ruoyi.test.domain.Vo.AnswerVo;
import com.ruoyi.test.mapper.AnswerMapper;
import com.ruoyi.test.seriver.IAnswerService;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnswerServiceImpl extends ServiceImpl<AnswerMapper, Answer> implements IAnswerService {

    @Autowired
    private AnswerMapper answerMapper;

    @Override
    public int answer(Answer answer) {
        return answerMapper.answer(answer);
    }

    @Override
    public Answer selectByTestId(Long senderId,Long testid) {
        LambdaQueryWrapper<Answer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Answer::getTestid,testid);
        queryWrapper.eq(Answer::getSenderId,senderId);
        queryWrapper.orderByDesc(Answer::getFinishtime).last("LIMIT 1");
        return answerMapper.selectOne(queryWrapper);
    }

    @Override
    public List<Answer> selectBySenderId(Long senderId) {
        return answerMapper.selectBySender(senderId);
    }

    @Override
    public Boolean selectBySenderIdAndTestId(Long senderId, Long testid) {
        LambdaQueryWrapper<Answer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Answer::getSenderId,senderId)
                .eq(Answer::getTestid,testid);
        if(answerMapper.selectCount(queryWrapper)>0){
            return true;
        }else{
            return false;
        }
    }
}
