package com.qxgmat.service.inline;

import com.github.pagehelper.Page;
import com.nuliji.tools.AbstractService;
import com.nuliji.tools.exception.ParameterException;
import com.nuliji.tools.exception.SystemException;
import com.qxgmat.data.dao.UserPaperMapper;
import com.qxgmat.data.dao.UserPaperQuestionMapper;
import com.qxgmat.data.dao.entity.UserPaper;
import com.qxgmat.data.dao.entity.UserPaperQuestion;
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 UserPaperQuestionService extends AbstractService {
    private static final Logger logger = LoggerFactory.getLogger(UserPaperQuestionService.class);

    @Resource
    private UserPaperQuestionMapper userPaperQuestionMapper;

    public UserPaperQuestion add(UserPaperQuestion paper){
        int result = insert(userPaperQuestionMapper, paper);
        paper = one(userPaperQuestionMapper, paper.getId());
        if(paper == null){
            throw new SystemException("组卷题目添加失败");
        }
        return paper;
    }

    public UserPaperQuestion edit(UserPaperQuestion paper){
        UserPaperQuestion in = one(userPaperQuestionMapper, paper.getId());
        if(in == null){
            throw new ParameterException("组卷题目不存在");
        }
        int result = update(userPaperQuestionMapper, paper);
        return paper;
    }

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

    public UserPaperQuestion get(Number id){
        UserPaperQuestion in = one(userPaperQuestionMapper, id);

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

    public Page<UserPaperQuestion> select(int page, int pageSize){
        return select(userPaperQuestionMapper, page, pageSize);
    }

    public List<UserPaperQuestion> select(Collection ids){
        return select(userPaperQuestionMapper, ids);
    }

}
