PaperDetailServiceImpl.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package cn.com.qmth.mps.service.impl;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import org.apache.commons.collections4.CollectionUtils;
  7. import org.apache.commons.lang3.StringUtils;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Service;
  10. import org.springframework.transaction.annotation.Transactional;
  11. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  12. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  13. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  14. import com.qmth.boot.core.exception.StatusException;
  15. import cn.com.qmth.mps.bean.PaperDetail;
  16. import cn.com.qmth.mps.bean.PaperDetailUnit;
  17. import cn.com.qmth.mps.bean.User;
  18. import cn.com.qmth.mps.dao.PaperDetailDao;
  19. import cn.com.qmth.mps.entity.PaperDetailEntity;
  20. import cn.com.qmth.mps.entity.PaperEntity;
  21. import cn.com.qmth.mps.enums.Role;
  22. import cn.com.qmth.mps.service.PaperDetailService;
  23. import cn.com.qmth.mps.service.PaperDetailUnitService;
  24. import cn.com.qmth.mps.service.PaperGroupService;
  25. import cn.com.qmth.mps.service.PaperService;
  26. import cn.com.qmth.mps.util.Calculator;
  27. import cn.com.qmth.mps.vo.paper.StructDomain;
  28. @Service
  29. public class PaperDetailServiceImpl extends ServiceImpl<PaperDetailDao, PaperDetailEntity>
  30. implements PaperDetailService {
  31. @Autowired
  32. private PaperGroupService paperGroupService;
  33. @Autowired
  34. private PaperDetailUnitService paperDetailUnitService;
  35. @Autowired
  36. private PaperService paperService;
  37. @Override
  38. public List<PaperDetail> getStructInfo(Long paperId) {
  39. List<PaperDetail> ret = new ArrayList<>();
  40. List<PaperDetailEntity> es = getByPaperId(paperId);
  41. if (CollectionUtils.isEmpty(es)) {
  42. return ret;
  43. }
  44. Map<Long,PaperDetail> map=new HashMap<>();
  45. for(PaperDetailEntity e:es) {
  46. PaperDetail vo=new PaperDetail();
  47. vo.setName(e.getName());
  48. vo.setNumber(e.getNumber());
  49. map.put(e.getId(), vo);
  50. ret.add(vo);
  51. }
  52. Map<Long,List<PaperDetailUnit>> units=paperDetailUnitService.getStructInfo(paperId);
  53. if (units==null||units.size()==0) {
  54. return ret;
  55. }
  56. for(Long key:units.keySet()) {
  57. map.get(key).setUnits(units.get(key));
  58. }
  59. return ret;
  60. }
  61. @Override
  62. public List<PaperDetailEntity> getByPaperId(Long paperId) {
  63. QueryWrapper<PaperDetailEntity> wrapper = new QueryWrapper<>();
  64. LambdaQueryWrapper<PaperDetailEntity> lw = wrapper.lambda();
  65. lw.eq(PaperDetailEntity::getPaperId, paperId);
  66. return this.list(wrapper);
  67. }
  68. @Transactional
  69. @Override
  70. public void structSave(StructDomain domain, User user) {
  71. if (domain.getPaperId() == null) {
  72. throw new StatusException("试卷结构ID不能为空");
  73. }
  74. if (domain.getTotalScore() == null) {
  75. throw new StatusException("试卷满分不能为空");
  76. }
  77. if (domain.getTotalScore()<=0) {
  78. throw new StatusException("试卷满分必须大于0");
  79. }
  80. String total=domain.getTotalScore().toString();
  81. if (total.indexOf(".")<total.length()-2) {
  82. throw new StatusException("试卷满分只能有一位小数");
  83. }
  84. if (CollectionUtils.isEmpty(domain.getStructInfo())) {
  85. throw new StatusException("大题信息不能为空");
  86. }
  87. for (PaperDetail u : domain.getStructInfo()) {
  88. if (u.getNumber() == null || StringUtils.isBlank(u.getName())) {
  89. throw new StatusException("大题号、大题名称不能为空");
  90. }
  91. if (CollectionUtils.isEmpty(u.getUnits())) {
  92. throw new StatusException("小题信息不能为空");
  93. }
  94. for (PaperDetailUnit unit : u.getUnits()) {
  95. if (unit.getNumber()== null ||unit.getScore()==null||unit.getScoreStep()==null) {
  96. throw new StatusException("小题号、小题满分、给分间隔不能为空");
  97. }
  98. if (unit.getNumber()<=0) {
  99. throw new StatusException("小题号必须大于0");
  100. }
  101. String score=unit.getScore().toString();
  102. if (score.indexOf(".")<score.length()-2) {
  103. throw new StatusException("小题满分只能有一位小数");
  104. }
  105. String scoreStep=unit.getScoreStep().toString();
  106. if (scoreStep.indexOf(".")<scoreStep.length()-2) {
  107. throw new StatusException("给分间隔只能有一位小数");
  108. }
  109. }
  110. }
  111. PaperEntity paper = paperService.getById(domain.getPaperId());
  112. if (paper == null) {
  113. throw new StatusException("未找到试卷结构");
  114. }
  115. if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(paper.getSchoolId())) {
  116. throw new StatusException("没有权限");
  117. }
  118. if (paper.getStructFinish()) {
  119. throw new StatusException("试卷结构已提交,不能暂存");
  120. }
  121. clearPaperSruct(domain.getPaperId());
  122. paper.setTotalScore(domain.getTotalScore());
  123. paper.setSubjectiveScore(domain.getTotalScore());
  124. paperService.updateById(paper);
  125. for(PaperDetail pd:domain.getStructInfo()) {
  126. PaperDetailEntity e=new PaperDetailEntity();
  127. e.setName(pd.getName());
  128. e.setNumber(pd.getNumber());
  129. e.setPaperId(domain.getPaperId());
  130. this.save(e);
  131. paperDetailUnitService.saveUnit(e,pd.getUnits());
  132. }
  133. }
  134. private void clearPaperSruct(Long paperId) {
  135. clearByPaperId(paperId);
  136. paperDetailUnitService.clearByPaperId(paperId);
  137. }
  138. private void clearByPaperId(Long paperId) {
  139. QueryWrapper<PaperDetailEntity> wrapper = new QueryWrapper<>();
  140. LambdaQueryWrapper<PaperDetailEntity> lw = wrapper.lambda();
  141. lw.eq(PaperDetailEntity::getPaperId, paperId);
  142. this.remove(wrapper);
  143. }
  144. @Transactional
  145. @Override
  146. public void structSubmit(StructDomain domain, User user) {
  147. if (domain.getPaperId() == null) {
  148. throw new StatusException("试卷结构ID不能为空");
  149. }
  150. if (domain.getTotalScore() == null) {
  151. throw new StatusException("试卷满分不能为空");
  152. }
  153. if (domain.getTotalScore()<=0) {
  154. throw new StatusException("试卷满分必须大于0");
  155. }
  156. String total=domain.getTotalScore().toString();
  157. if (total.indexOf(".")<total.length()-2) {
  158. throw new StatusException("试卷满分只能有一位小数");
  159. }
  160. if (CollectionUtils.isEmpty(domain.getStructInfo())) {
  161. throw new StatusException("大题信息不能为空");
  162. }
  163. for (PaperDetail u : domain.getStructInfo()) {
  164. if (u.getNumber() == null || StringUtils.isBlank(u.getName())) {
  165. throw new StatusException("大题号、大题名称不能为空");
  166. }
  167. if (CollectionUtils.isEmpty(u.getUnits())) {
  168. throw new StatusException("小题信息不能为空");
  169. }
  170. for (PaperDetailUnit unit : u.getUnits()) {
  171. if (unit.getNumber()== null ||unit.getScore()==null||unit.getScoreStep()==null) {
  172. throw new StatusException("小题号、小题满分、给分间隔不能为空");
  173. }
  174. if (unit.getNumber()<=0) {
  175. throw new StatusException("小题号必须大于0");
  176. }
  177. String score=unit.getScore().toString();
  178. if (score.indexOf(".")<score.length()-2) {
  179. throw new StatusException("小题满分只能有一位小数");
  180. }
  181. String scoreStep=unit.getScoreStep().toString();
  182. if (scoreStep.indexOf(".")<scoreStep.length()-2) {
  183. throw new StatusException("给分间隔只能有一位小数");
  184. }
  185. }
  186. }
  187. checkTotalScore(domain);
  188. PaperEntity paper = paperService.getById(domain.getPaperId());
  189. if (paper == null) {
  190. throw new StatusException("未找到试卷结构");
  191. }
  192. if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(paper.getSchoolId())) {
  193. throw new StatusException("没有权限");
  194. }
  195. if (paperGroupService.existsGroup(domain.getPaperId())) {
  196. throw new StatusException("请先删除分组信息再提交试卷结构");
  197. }
  198. clearPaperSruct(domain.getPaperId());
  199. paper.setTotalScore(domain.getTotalScore());
  200. paper.setSubjectiveScore(domain.getTotalScore());
  201. paper.setStructFinish(true);
  202. paperService.updateById(paper);
  203. for(PaperDetail pd:domain.getStructInfo()) {
  204. PaperDetailEntity e=new PaperDetailEntity();
  205. e.setName(pd.getName());
  206. e.setNumber(pd.getNumber());
  207. e.setPaperId(domain.getPaperId());
  208. this.save(e);
  209. paperDetailUnitService.saveUnit(e,pd.getUnits());
  210. }
  211. }
  212. public void checkTotalScore(StructDomain domain) {
  213. double total=0.0;
  214. for(PaperDetail detial:domain.getStructInfo()) {
  215. for(PaperDetailUnit unit:detial.getUnits()) {
  216. total=Calculator.add(total, unit.getScore(),1);
  217. }
  218. }
  219. if(total!=domain.getTotalScore()) {
  220. throw new StatusException("试卷满分与小题分不一致");
  221. }
  222. }
  223. }