1
0

SentencePaperService.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package com.qxgmat.service.inline;
  2. import com.github.pagehelper.Page;
  3. import com.nuliji.tools.AbstractService;
  4. import com.nuliji.tools.exception.ParameterException;
  5. import com.nuliji.tools.exception.SystemException;
  6. import com.nuliji.tools.mybatis.Example;
  7. import com.qxgmat.data.constants.enums.logic.SentenceLogic;
  8. import com.qxgmat.data.dao.SentencePaperMapper;
  9. import com.qxgmat.data.dao.entity.SentencePaper;
  10. import com.qxgmat.data.dao.entity.SentenceQuestion;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.stereotype.Service;
  14. import javax.annotation.Resource;
  15. import java.util.Collection;
  16. import java.util.List;
  17. @Service
  18. public class SentencePaperService extends AbstractService {
  19. private static final Logger logger = LoggerFactory.getLogger(SentencePaperService.class);
  20. final static String formatSet = "FIND_IN_SET('%d', question_no_ids)";
  21. @Resource
  22. private SentencePaperMapper sentencePaperMapper;
  23. /**
  24. * 切换paper状态
  25. * @param ids
  26. * @param status
  27. */
  28. public void updatePaper(Collection ids, Integer status){
  29. Example example = new Example(SentencePaper.class);
  30. example.and(
  31. example.createCriteria()
  32. .andIn("id", ids)
  33. );
  34. update(sentencePaperMapper, example, SentencePaper.builder().status(status).build());
  35. }
  36. /**
  37. * 获取可用的试卷列表:单个分组逻辑
  38. * @param logic
  39. * @return
  40. */
  41. public List<SentencePaper> listByLogic(SentenceLogic logic){
  42. Example example = new Example(SentencePaper.class);
  43. example.and(
  44. example.createCriteria()
  45. .andEqualTo("logic", logic.key)
  46. .andEqualTo("status", 1)
  47. );
  48. example.orderBy("no").asc();
  49. return select(sentencePaperMapper, example);
  50. }
  51. /**
  52. * 按序号获取组卷
  53. * @param no
  54. * @param logic
  55. * @return
  56. */
  57. public SentencePaper getByNo(Integer no, SentenceLogic logic){
  58. Example example = new Example(SentencePaper.class);
  59. example.and(
  60. example.createCriteria()
  61. .andEqualTo("no", no)
  62. .andEqualTo("logic", logic.key)
  63. );
  64. return one(sentencePaperMapper, example);
  65. }
  66. /**
  67. * 生成试卷名称
  68. * @param prefixTitle
  69. * @param no
  70. * @param questionNumber
  71. * @return
  72. */
  73. public String generateTitle(String prefixTitle, Integer length, Integer no, Integer questionNumber){
  74. return String.format("%s#%d~%d", prefixTitle, (no - 1) * length + 1, (no - 1) * length + questionNumber);
  75. }
  76. public SentencePaper getLast(SentenceLogic logic){
  77. Example example = new Example(SentencePaper.class);
  78. example.and(
  79. example.createCriteria()
  80. .andEqualTo("logic", logic.key)
  81. );
  82. example.orderBy("createTime").desc();
  83. return one(sentencePaperMapper, example);
  84. }
  85. public List<SentencePaper> listByQuestion(SentenceQuestion question, SentenceLogic logic){
  86. Example example = new Example(SentencePaper.class);
  87. example.and(
  88. example.createCriteria()
  89. .andCondition(String.format(formatSet, question.getId()))
  90. );
  91. if (logic != null)
  92. example.and(
  93. example.createCriteria()
  94. .andEqualTo("logic", logic.key)
  95. );
  96. return select(sentencePaperMapper, example);
  97. }
  98. public SentencePaper add(SentencePaper paper){
  99. int result = insert(sentencePaperMapper, paper);
  100. paper = one(sentencePaperMapper, paper.getId());
  101. if(paper == null){
  102. throw new SystemException("题目添加失败");
  103. }
  104. return paper;
  105. }
  106. public SentencePaper edit(SentencePaper paper){
  107. SentencePaper in = one(sentencePaperMapper, paper.getId());
  108. if(in == null){
  109. throw new ParameterException("题目不存在");
  110. }
  111. int result = update(sentencePaperMapper, paper);
  112. return paper;
  113. }
  114. public boolean delete(Number id){
  115. SentencePaper in = one(sentencePaperMapper, id);
  116. if(in == null){
  117. throw new ParameterException("题目不存在");
  118. }
  119. int result = delete(sentencePaperMapper, id);
  120. return result > 0;
  121. }
  122. public SentencePaper get(Number id){
  123. SentencePaper in = one(sentencePaperMapper, id);
  124. if(in == null){
  125. throw new ParameterException("题目不存在");
  126. }
  127. return in;
  128. }
  129. public Page<SentencePaper> select(int page, int pageSize){
  130. return select(sentencePaperMapper, page, pageSize);
  131. }
  132. public List<SentencePaper> select(Collection ids){
  133. return select(sentencePaperMapper, ids);
  134. }
  135. }