PaperGroupServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package cn.com.qmth.mps.service.impl;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.LinkedHashSet;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Set;
  9. import org.apache.commons.collections4.CollectionUtils;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Service;
  12. import org.springframework.transaction.annotation.Transactional;
  13. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  14. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  15. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  16. import com.qmth.boot.core.exception.StatusException;
  17. import cn.com.qmth.mps.bean.PaperDetail;
  18. import cn.com.qmth.mps.bean.PaperDetailUnit;
  19. import cn.com.qmth.mps.bean.PaperGroup;
  20. import cn.com.qmth.mps.bean.PaperGroupUnit;
  21. import cn.com.qmth.mps.bean.User;
  22. import cn.com.qmth.mps.dao.PaperGroupDao;
  23. import cn.com.qmth.mps.entity.PaperDetailEntity;
  24. import cn.com.qmth.mps.entity.PaperEntity;
  25. import cn.com.qmth.mps.entity.PaperGroupEntity;
  26. import cn.com.qmth.mps.enums.Role;
  27. import cn.com.qmth.mps.service.CourseService;
  28. import cn.com.qmth.mps.service.ExamService;
  29. import cn.com.qmth.mps.service.PaperDetailService;
  30. import cn.com.qmth.mps.service.PaperDetailUnitService;
  31. import cn.com.qmth.mps.service.PaperGroupService;
  32. import cn.com.qmth.mps.service.PaperGroupUnitService;
  33. import cn.com.qmth.mps.service.PaperService;
  34. import cn.com.qmth.mps.vo.paper.GroupCountVo;
  35. import cn.com.qmth.mps.vo.paper.GroupInfoVo;
  36. import cn.com.qmth.mps.vo.paper.GroupVo;
  37. import cn.com.qmth.mps.vo.paper.PaperGroupDomain;
  38. @Service
  39. public class PaperGroupServiceImpl extends ServiceImpl<PaperGroupDao, PaperGroupEntity> implements PaperGroupService {
  40. @Autowired
  41. private PaperGroupUnitService paperGroupUnitService;
  42. @Autowired
  43. private PaperService paperService;
  44. @Autowired
  45. private PaperDetailService paperDetailService;
  46. @Autowired
  47. private PaperDetailUnitService paperDetailUnitService;
  48. @Autowired
  49. private CourseService courseService;
  50. @Autowired
  51. private ExamService examService;
  52. @Override
  53. public List<GroupCountVo> findGroupCount(List<Long> paperIds) {
  54. return this.baseMapper.findGroupCount(paperIds);
  55. }
  56. @Override
  57. public List<PaperGroup> getGroupInfo(Long paperId) {
  58. List<PaperGroup> ret = new ArrayList<>();
  59. List<PaperGroupEntity> es = getByPaperId(paperId);
  60. if (CollectionUtils.isEmpty(es)) {
  61. return ret;
  62. }
  63. Map<Long, PaperGroup> map = new HashMap<>();
  64. for (PaperGroupEntity e : es) {
  65. PaperGroup vo = new PaperGroup();
  66. vo.setNumber(e.getNumber());
  67. map.put(e.getId(), vo);
  68. ret.add(vo);
  69. }
  70. Map<Long, List<PaperGroupUnit>> units = paperGroupUnitService.getGroupInfo(paperId);
  71. if (units == null || units.size() == 0) {
  72. return ret;
  73. }
  74. for (Long key : units.keySet()) {
  75. map.get(key).setGroupUnits(units.get(key));
  76. }
  77. return ret;
  78. }
  79. private List<PaperGroupEntity> getByPaperId(Long paperId) {
  80. QueryWrapper<PaperGroupEntity> wrapper = new QueryWrapper<>();
  81. LambdaQueryWrapper<PaperGroupEntity> lw = wrapper.lambda();
  82. lw.eq(PaperGroupEntity::getPaperId, paperId);
  83. return this.list(wrapper);
  84. }
  85. @Override
  86. public List<GroupVo> groupList(Long paperId, User user) {
  87. PaperEntity paper = paperService.getById(paperId);
  88. if (paper == null) {
  89. throw new StatusException("未找到试卷结构");
  90. }
  91. if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(paper.getSchoolId())) {
  92. throw new StatusException("没有权限");
  93. }
  94. List<GroupVo> ret = new ArrayList<>();
  95. List<PaperGroupEntity> es = getByPaperId(paperId);
  96. if (CollectionUtils.isEmpty(es)) {
  97. return ret;
  98. }
  99. Map<Long, GroupVo> map = new HashMap<>();
  100. for (PaperGroupEntity e : es) {
  101. GroupVo vo = new GroupVo();
  102. vo.setNumber(e.getNumber());
  103. vo.setGroupId(e.getId());
  104. map.put(e.getId(), vo);
  105. ret.add(vo);
  106. }
  107. Map<Long, List<PaperGroupUnit>> units = paperGroupUnitService.getGroupInfo(paperId);
  108. List<PaperDetailEntity> des = paperDetailService.getByPaperId(paperId);
  109. Map<Integer, PaperDetailEntity> detailMap = new HashMap<>();
  110. for (PaperDetailEntity de : des) {
  111. detailMap.put(de.getNumber(), de);
  112. }
  113. for (GroupVo vo : ret) {
  114. List<PaperGroupUnit> unit = units.get(vo.getGroupId());
  115. if (CollectionUtils.isNotEmpty(unit)) {
  116. Set<Integer> set = new LinkedHashSet<>();
  117. for (PaperGroupUnit u : unit) {
  118. set.add(u.getDetailNumber());
  119. }
  120. StringBuilder numSb = new StringBuilder();
  121. StringBuilder nameSb = new StringBuilder();
  122. for (Integer num : set) {
  123. numSb.append(num).append(",");
  124. nameSb.append(detailMap.get(num).getName()).append(",");
  125. }
  126. vo.setDetailNumbers(numSb.substring(0, numSb.length() - 1));
  127. vo.setDetailNames(nameSb.substring(0, nameSb.length() - 1));
  128. }
  129. }
  130. return ret;
  131. }
  132. @Override
  133. public GroupInfoVo groupInfo(Long paperId, Integer groupNumber, User user) {
  134. PaperEntity paper = paperService.getById(paperId);
  135. if (paper == null) {
  136. throw new StatusException("未找到试卷结构");
  137. }
  138. if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(paper.getSchoolId())) {
  139. throw new StatusException("没有权限");
  140. }
  141. PaperGroupEntity e = this.getByPaperIdAndNumber(paperId, groupNumber);
  142. if (e == null) {
  143. return null;
  144. }
  145. GroupInfoVo vo = new GroupInfoVo();
  146. vo.setNumber(e.getNumber());
  147. List<PaperDetail> pds=paperDetailService.getStructInfo(paperId);
  148. Map<Long, List<PaperGroupUnit>> units = paperGroupUnitService.getGroupInfo(paperId);
  149. vo.setStructInfo(getGroupInfo(pds,units.get(e.getId())));
  150. vo.setCourseName(courseService.getById(paper.getCourseId()).getName());
  151. return vo;
  152. }
  153. private List<PaperDetail> getGroupInfo(List<PaperDetail> pds,List<PaperGroupUnit> units){
  154. List<PaperDetail> ret=new ArrayList<>();
  155. Set<Integer> detailSet=new HashSet<>();
  156. Set<String> unitSet=new HashSet<>();
  157. for(PaperGroupUnit unit:units) {
  158. detailSet.add(unit.getDetailNumber());
  159. unitSet.add(unit.getDetailNumber()+"-"+unit.getDetailUnitNumber());
  160. }
  161. for(PaperDetail pd:pds) {
  162. if(detailSet.contains(pd.getNumber())) {
  163. ret.add(pd);
  164. List<PaperDetailUnit> us=new ArrayList<>();
  165. for(PaperDetailUnit u:pd.getUnits()) {
  166. if(unitSet.contains(pd.getNumber()+"-"+u.getNumber())) {
  167. us.add(u);
  168. }
  169. }
  170. pd.setUnits(us);
  171. }
  172. }
  173. return ret;
  174. }
  175. private PaperGroupEntity getByPaperIdAndNumber(Long paperId, Integer groupNumber) {
  176. QueryWrapper<PaperGroupEntity> wrapper = new QueryWrapper<>();
  177. LambdaQueryWrapper<PaperGroupEntity> lw = wrapper.lambda();
  178. lw.eq(PaperGroupEntity::getPaperId, paperId);
  179. lw.eq(PaperGroupEntity::getNumber, groupNumber);
  180. return this.getOne(wrapper);
  181. }
  182. @Transactional
  183. @Override
  184. public void groupSave(PaperGroupDomain domain, User user) {
  185. if (domain.getPaperId() == null) {
  186. throw new StatusException("试卷结构ID不能为空");
  187. }
  188. if (domain.getNumber() == null) {
  189. throw new StatusException("分组号不能为空");
  190. }
  191. if (CollectionUtils.isEmpty(domain.getGroupUnits())) {
  192. throw new StatusException("分组信息不能为空");
  193. }
  194. for (PaperGroupUnit u : domain.getGroupUnits()) {
  195. if (u.getDetailNumber() == null || u.getDetailUnitNumber() == null) {
  196. throw new StatusException("大题号、小题号不能为空");
  197. }
  198. }
  199. PaperEntity paper = paperService.getById(domain.getPaperId());
  200. if (paper == null) {
  201. throw new StatusException("未找到试卷结构");
  202. }
  203. examService.checkExamStatus(paper.getExamId());
  204. if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(paper.getSchoolId())) {
  205. throw new StatusException("没有权限");
  206. }
  207. if (!paper.getStructFinish()) {
  208. throw new StatusException("试卷结构未提交,不能设置分组");
  209. }
  210. PaperGroupEntity g;
  211. if(domain.getGroupId()!=null) {
  212. g=this.getById(domain.getGroupId());
  213. paperGroupUnitService.removeByGroupId(domain.getGroupId());
  214. }else {
  215. g = new PaperGroupEntity();
  216. g.setPaperId(domain.getPaperId());
  217. }
  218. g.setNumber(domain.getNumber());
  219. this.saveOrUpdate(g);
  220. List<PaperDetail> pds = paperDetailService.getStructInfo(domain.getPaperId());
  221. if (CollectionUtils.isEmpty(pds)) {
  222. throw new StatusException("试卷结构小题信息为空");
  223. }
  224. checkStruct(pds, domain.getGroupUnits());
  225. paperGroupUnitService.saveUnit(g, domain.getGroupUnits());
  226. if (paperGroupUnitService.countByPaperId(domain.getPaperId())
  227. .equals(paperDetailUnitService.countByPaperId(domain.getPaperId()))) {
  228. paper.setGroupFinish(true);
  229. }else {
  230. paper.setGroupFinish(false);
  231. }
  232. paperService.updateById(paper);
  233. }
  234. private void checkStruct(List<PaperDetail> pds, List<PaperGroupUnit> groupUnits) {
  235. for (PaperGroupUnit groupUnit : groupUnits) {
  236. checkStructExists(pds, groupUnit);
  237. }
  238. }
  239. private void checkStructExists(List<PaperDetail> pds, PaperGroupUnit groupUnit) {
  240. for (PaperDetail pd : pds) {
  241. if (pd.getNumber().equals(groupUnit.getDetailNumber())) {
  242. for (PaperDetailUnit u : pd.getUnits()) {
  243. if (u.getNumber().equals(groupUnit.getDetailUnitNumber())) {
  244. return;
  245. }
  246. }
  247. }
  248. }
  249. throw new StatusException(
  250. "试卷结构中大题号:" + groupUnit.getDetailNumber() + " 小题号:" + groupUnit.getDetailUnitNumber() + "不存在");
  251. }
  252. @Transactional
  253. @Override
  254. public void groupDelete(Long groupId, User user) {
  255. PaperGroupEntity group=this.getById(groupId);
  256. if (group == null) {
  257. throw new StatusException("未找到分组信息");
  258. }
  259. PaperEntity paper = paperService.getById(group.getPaperId());
  260. if (paper == null) {
  261. throw new StatusException("未找到试卷结构");
  262. }
  263. examService.checkExamStatus(paper.getExamId());
  264. if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(paper.getSchoolId())) {
  265. throw new StatusException("没有权限");
  266. }
  267. this.removeById(groupId);
  268. paperGroupUnitService.removeByGroupId(groupId);
  269. paper.setGroupFinish(false);
  270. paperService.updateById(paper);
  271. }
  272. @Override
  273. public Boolean existsGroup(Long paperId) {
  274. QueryWrapper<PaperGroupEntity> wrapper = new QueryWrapper<>();
  275. LambdaQueryWrapper<PaperGroupEntity> lw = wrapper.lambda();
  276. lw.eq(PaperGroupEntity::getPaperId, paperId);
  277. return this.count(wrapper)>0;
  278. }
  279. @Transactional
  280. @Override
  281. public void groupClear(Long paperId, User user) {
  282. PaperEntity paper = paperService.getById(paperId);
  283. if (paper == null) {
  284. throw new StatusException("未找到试卷结构");
  285. }
  286. examService.checkExamStatus(paper.getExamId());
  287. if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(paper.getSchoolId())) {
  288. throw new StatusException("没有权限");
  289. }
  290. paperGroupUnitService.clearByPaperId(paperId);
  291. clearByPaperId(paperId);
  292. paper.setGroupFinish(false);
  293. paperService.updateById(paper);
  294. }
  295. private void clearByPaperId(Long paperId) {
  296. QueryWrapper<PaperGroupEntity> wrapper = new QueryWrapper<>();
  297. LambdaQueryWrapper<PaperGroupEntity> lw = wrapper.lambda();
  298. lw.eq(PaperGroupEntity::getPaperId, paperId);
  299. this.remove(wrapper);
  300. }
  301. }