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.nuliji.tools.mybatis.Example; import com.qxgmat.data.constants.enums.logic.SentenceLogic; import com.qxgmat.data.dao.SentencePaperMapper; import com.qxgmat.data.dao.entity.SentencePaper; import com.qxgmat.data.dao.entity.SentenceQuestion; 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 SentencePaperService extends AbstractService { private static final Logger logger = LoggerFactory.getLogger(SentencePaperService.class); final static String formatSet = "FIND_IN_SET('%d', question_no_ids)"; @Resource private SentencePaperMapper sentencePaperMapper; /** * 切换paper状态 * @param ids * @param status */ public void updatePaper(Collection ids, Integer status){ Example example = new Example(SentencePaper.class); example.and( example.createCriteria() .andIn("id", ids) ); update(sentencePaperMapper, example, SentencePaper.builder().status(status).build()); } /** * 获取可用的试卷列表:单个分组逻辑 * @param logic * @return */ public List listByLogic(SentenceLogic logic){ Example example = new Example(SentencePaper.class); example.and( example.createCriteria() .andEqualTo("logic", logic.key) .andEqualTo("status", 1) ); example.orderBy("no").asc(); return select(sentencePaperMapper, example); } /** * 按序号获取组卷 * @param no * @param logic * @return */ public SentencePaper getByNo(Integer no, SentenceLogic logic){ Example example = new Example(SentencePaper.class); example.and( example.createCriteria() .andEqualTo("no", no) .andEqualTo("logic", logic.key) ); return one(sentencePaperMapper, example); } /** * 生成试卷名称 * @param prefixTitle * @param no * @param questionNumber * @return */ public String generateTitle(String prefixTitle, Integer length, Integer no, Integer questionNumber){ return String.format("%s#%d~%d", prefixTitle, (no - 1) * length + 1, (no - 1) * length + questionNumber); } public SentencePaper getLast(SentenceLogic logic){ Example example = new Example(SentencePaper.class); example.and( example.createCriteria() .andEqualTo("logic", logic.key) ); example.orderBy("createTime").desc(); return one(sentencePaperMapper, example); } public List listByQuestion(SentenceQuestion question, SentenceLogic logic){ Example example = new Example(SentencePaper.class); example.and( example.createCriteria() .andCondition(String.format(formatSet, question.getId())) ); if (logic != null) example.and( example.createCriteria() .andEqualTo("logic", logic.key) ); return select(sentencePaperMapper, example); } public SentencePaper add(SentencePaper paper){ int result = insert(sentencePaperMapper, paper); paper = one(sentencePaperMapper, paper.getId()); if(paper == null){ throw new SystemException("题目添加失败"); } return paper; } public SentencePaper edit(SentencePaper paper){ SentencePaper in = one(sentencePaperMapper, paper.getId()); if(in == null){ throw new ParameterException("题目不存在"); } int result = update(sentencePaperMapper, paper); return paper; } public boolean delete(Number id){ SentencePaper in = one(sentencePaperMapper, id); if(in == null){ throw new ParameterException("题目不存在"); } int result = delete(sentencePaperMapper, id); return result > 0; } public SentencePaper get(Number id){ SentencePaper in = one(sentencePaperMapper, id); if(in == null){ throw new ParameterException("题目不存在"); } return in; } public Page select(int page, int pageSize){ return select(sentencePaperMapper, page, pageSize); } public List select(Collection ids){ return select(sentencePaperMapper, ids); } }