package com.qxgmat.service;

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.ExaminationPaperMapper;
import com.qxgmat.data.dao.entity.ExaminationPaper;
import com.qxgmat.data.dao.entity.Question;
import com.qxgmat.data.dao.entity.UserPaper;
import com.qxgmat.service.inline.QuestionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

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

    @Resource
    private ExaminationPaperMapper examinationPaperMapper;

    @Resource
    private QuestionService questionService;

    // 完成一次
    public boolean finished(UserPaper userPaper){

        return true;
    }

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

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

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

    public ExaminationPaper get(Number id){
        ExaminationPaper in = one(examinationPaperMapper, id);

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

    public Page<ExaminationPaper> select(int page, int pageSize){
        return select(examinationPaperMapper, page, pageSize);
    }

    public List<ExaminationPaper> select(Collection ids){
        return select(examinationPaperMapper, ids);
    }

}
