123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- package cn.com.qmth.mps.service.impl;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.LinkedHashSet;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import org.apache.commons.collections4.CollectionUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.qmth.boot.core.exception.StatusException;
- import cn.com.qmth.mps.bean.PaperDetail;
- import cn.com.qmth.mps.bean.PaperDetailUnit;
- import cn.com.qmth.mps.bean.PaperGroup;
- import cn.com.qmth.mps.bean.PaperGroupUnit;
- import cn.com.qmth.mps.bean.User;
- import cn.com.qmth.mps.dao.PaperGroupDao;
- import cn.com.qmth.mps.entity.PaperDetailEntity;
- import cn.com.qmth.mps.entity.PaperEntity;
- import cn.com.qmth.mps.entity.PaperGroupEntity;
- import cn.com.qmth.mps.enums.Role;
- import cn.com.qmth.mps.service.CourseService;
- import cn.com.qmth.mps.service.ExamService;
- import cn.com.qmth.mps.service.PaperDetailService;
- import cn.com.qmth.mps.service.PaperDetailUnitService;
- import cn.com.qmth.mps.service.PaperGroupService;
- import cn.com.qmth.mps.service.PaperGroupUnitService;
- import cn.com.qmth.mps.service.PaperService;
- import cn.com.qmth.mps.vo.paper.GroupCountVo;
- import cn.com.qmth.mps.vo.paper.GroupInfoVo;
- import cn.com.qmth.mps.vo.paper.GroupVo;
- import cn.com.qmth.mps.vo.paper.PaperGroupDomain;
- @Service
- public class PaperGroupServiceImpl extends ServiceImpl<PaperGroupDao, PaperGroupEntity> implements PaperGroupService {
- @Autowired
- private PaperGroupUnitService paperGroupUnitService;
- @Autowired
- private PaperService paperService;
- @Autowired
- private PaperDetailService paperDetailService;
- @Autowired
- private PaperDetailUnitService paperDetailUnitService;
-
- @Autowired
- private CourseService courseService;
- @Autowired
- private ExamService examService;
- @Override
- public List<GroupCountVo> findGroupCount(List<Long> paperIds) {
- return this.baseMapper.findGroupCount(paperIds);
- }
- @Override
- public List<PaperGroup> getGroupInfo(Long paperId) {
- List<PaperGroup> ret = new ArrayList<>();
- List<PaperGroupEntity> es = getByPaperId(paperId);
- if (CollectionUtils.isEmpty(es)) {
- return ret;
- }
- Map<Long, PaperGroup> map = new HashMap<>();
- for (PaperGroupEntity e : es) {
- PaperGroup vo = new PaperGroup();
- vo.setNumber(e.getNumber());
- map.put(e.getId(), vo);
- ret.add(vo);
- }
- Map<Long, List<PaperGroupUnit>> units = paperGroupUnitService.getGroupInfo(paperId);
- if (units == null || units.size() == 0) {
- return ret;
- }
- for (Long key : units.keySet()) {
- map.get(key).setGroupUnits(units.get(key));
- }
- return ret;
- }
- private List<PaperGroupEntity> getByPaperId(Long paperId) {
- QueryWrapper<PaperGroupEntity> wrapper = new QueryWrapper<>();
- LambdaQueryWrapper<PaperGroupEntity> lw = wrapper.lambda();
- lw.eq(PaperGroupEntity::getPaperId, paperId);
- return this.list(wrapper);
- }
- @Override
- public List<GroupVo> groupList(Long paperId, User user) {
- PaperEntity paper = paperService.getById(paperId);
- if (paper == null) {
- throw new StatusException("未找到试卷结构");
- }
- if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(paper.getSchoolId())) {
- throw new StatusException("没有权限");
- }
- List<GroupVo> ret = new ArrayList<>();
- List<PaperGroupEntity> es = getByPaperId(paperId);
- if (CollectionUtils.isEmpty(es)) {
- return ret;
- }
- Map<Long, GroupVo> map = new HashMap<>();
- for (PaperGroupEntity e : es) {
- GroupVo vo = new GroupVo();
- vo.setNumber(e.getNumber());
- vo.setGroupId(e.getId());
- map.put(e.getId(), vo);
- ret.add(vo);
- }
- Map<Long, List<PaperGroupUnit>> units = paperGroupUnitService.getGroupInfo(paperId);
- List<PaperDetailEntity> des = paperDetailService.getByPaperId(paperId);
- Map<Integer, PaperDetailEntity> detailMap = new HashMap<>();
- for (PaperDetailEntity de : des) {
- detailMap.put(de.getNumber(), de);
- }
- for (GroupVo vo : ret) {
- List<PaperGroupUnit> unit = units.get(vo.getGroupId());
- if (CollectionUtils.isNotEmpty(unit)) {
- Set<Integer> set = new LinkedHashSet<>();
- for (PaperGroupUnit u : unit) {
- set.add(u.getDetailNumber());
- }
- StringBuilder numSb = new StringBuilder();
- StringBuilder nameSb = new StringBuilder();
- for (Integer num : set) {
- numSb.append(num).append(",");
- nameSb.append(detailMap.get(num).getName()).append(",");
- }
- vo.setDetailNumbers(numSb.substring(0, numSb.length() - 1));
- vo.setDetailNames(nameSb.substring(0, nameSb.length() - 1));
- }
- }
- return ret;
- }
- @Override
- public GroupInfoVo groupInfo(Long paperId, Integer groupNumber, User user) {
- PaperEntity paper = paperService.getById(paperId);
- if (paper == null) {
- throw new StatusException("未找到试卷结构");
- }
- if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(paper.getSchoolId())) {
- throw new StatusException("没有权限");
- }
- PaperGroupEntity e = this.getByPaperIdAndNumber(paperId, groupNumber);
- if (e == null) {
- return null;
- }
- GroupInfoVo vo = new GroupInfoVo();
- vo.setNumber(e.getNumber());
- List<PaperDetail> pds=paperDetailService.getStructInfo(paperId);
- Map<Long, List<PaperGroupUnit>> units = paperGroupUnitService.getGroupInfo(paperId);
- vo.setStructInfo(getGroupInfo(pds,units.get(e.getId())));
- vo.setCourseName(courseService.getById(paper.getCourseId()).getName());
- return vo;
- }
-
- private List<PaperDetail> getGroupInfo(List<PaperDetail> pds,List<PaperGroupUnit> units){
- List<PaperDetail> ret=new ArrayList<>();
- Set<Integer> detailSet=new HashSet<>();
- Set<String> unitSet=new HashSet<>();
- for(PaperGroupUnit unit:units) {
- detailSet.add(unit.getDetailNumber());
- unitSet.add(unit.getDetailNumber()+"-"+unit.getDetailUnitNumber());
- }
- for(PaperDetail pd:pds) {
- if(detailSet.contains(pd.getNumber())) {
- ret.add(pd);
- List<PaperDetailUnit> us=new ArrayList<>();
- for(PaperDetailUnit u:pd.getUnits()) {
- if(unitSet.contains(pd.getNumber()+"-"+u.getNumber())) {
- us.add(u);
- }
- }
- pd.setUnits(us);
- }
- }
- return ret;
- }
- private PaperGroupEntity getByPaperIdAndNumber(Long paperId, Integer groupNumber) {
- QueryWrapper<PaperGroupEntity> wrapper = new QueryWrapper<>();
- LambdaQueryWrapper<PaperGroupEntity> lw = wrapper.lambda();
- lw.eq(PaperGroupEntity::getPaperId, paperId);
- lw.eq(PaperGroupEntity::getNumber, groupNumber);
- return this.getOne(wrapper);
- }
- @Transactional
- @Override
- public void groupSave(PaperGroupDomain domain, User user) {
- if (domain.getPaperId() == null) {
- throw new StatusException("试卷结构ID不能为空");
- }
- if (domain.getNumber() == null) {
- throw new StatusException("分组号不能为空");
- }
- if (CollectionUtils.isEmpty(domain.getGroupUnits())) {
- throw new StatusException("分组信息不能为空");
- }
- for (PaperGroupUnit u : domain.getGroupUnits()) {
- if (u.getDetailNumber() == null || u.getDetailUnitNumber() == null) {
- throw new StatusException("大题号、小题号不能为空");
- }
- }
- PaperEntity paper = paperService.getById(domain.getPaperId());
- if (paper == null) {
- throw new StatusException("未找到试卷结构");
- }
- examService.checkExamStatus(paper.getExamId());
- if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(paper.getSchoolId())) {
- throw new StatusException("没有权限");
- }
- if (!paper.getStructFinish()) {
- throw new StatusException("试卷结构未提交,不能设置分组");
- }
- PaperGroupEntity g;
- if(domain.getGroupId()!=null) {
- g=this.getById(domain.getGroupId());
- paperGroupUnitService.removeByGroupId(domain.getGroupId());
- }else {
- g = new PaperGroupEntity();
- g.setPaperId(domain.getPaperId());
- }
- g.setNumber(domain.getNumber());
- this.saveOrUpdate(g);
- List<PaperDetail> pds = paperDetailService.getStructInfo(domain.getPaperId());
- if (CollectionUtils.isEmpty(pds)) {
- throw new StatusException("试卷结构小题信息为空");
- }
- checkStruct(pds, domain.getGroupUnits());
- paperGroupUnitService.saveUnit(g, domain.getGroupUnits());
- if (paperGroupUnitService.countByPaperId(domain.getPaperId())
- .equals(paperDetailUnitService.countByPaperId(domain.getPaperId()))) {
- paper.setGroupFinish(true);
- }else {
- paper.setGroupFinish(false);
- }
- paperService.updateById(paper);
- }
- private void checkStruct(List<PaperDetail> pds, List<PaperGroupUnit> groupUnits) {
- for (PaperGroupUnit groupUnit : groupUnits) {
- checkStructExists(pds, groupUnit);
- }
- }
- private void checkStructExists(List<PaperDetail> pds, PaperGroupUnit groupUnit) {
- for (PaperDetail pd : pds) {
- if (pd.getNumber().equals(groupUnit.getDetailNumber())) {
- for (PaperDetailUnit u : pd.getUnits()) {
- if (u.getNumber().equals(groupUnit.getDetailUnitNumber())) {
- return;
- }
- }
- }
- }
- throw new StatusException(
- "试卷结构中大题号:" + groupUnit.getDetailNumber() + " 小题号:" + groupUnit.getDetailUnitNumber() + "不存在");
- }
- @Transactional
- @Override
- public void groupDelete(Long groupId, User user) {
- PaperGroupEntity group=this.getById(groupId);
- if (group == null) {
- throw new StatusException("未找到分组信息");
- }
- PaperEntity paper = paperService.getById(group.getPaperId());
- if (paper == null) {
- throw new StatusException("未找到试卷结构");
- }
- examService.checkExamStatus(paper.getExamId());
- if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(paper.getSchoolId())) {
- throw new StatusException("没有权限");
- }
- this.removeById(groupId);
- paperGroupUnitService.removeByGroupId(groupId);
- paper.setGroupFinish(false);
- paperService.updateById(paper);
- }
- @Override
- public Boolean existsGroup(Long paperId) {
- QueryWrapper<PaperGroupEntity> wrapper = new QueryWrapper<>();
- LambdaQueryWrapper<PaperGroupEntity> lw = wrapper.lambda();
- lw.eq(PaperGroupEntity::getPaperId, paperId);
- return this.count(wrapper)>0;
- }
- @Transactional
- @Override
- public void groupClear(Long paperId, User user) {
- PaperEntity paper = paperService.getById(paperId);
- if (paper == null) {
- throw new StatusException("未找到试卷结构");
- }
- examService.checkExamStatus(paper.getExamId());
- if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(paper.getSchoolId())) {
- throw new StatusException("没有权限");
- }
- paperGroupUnitService.clearByPaperId(paperId);
- clearByPaperId(paperId);
- paper.setGroupFinish(false);
- paperService.updateById(paper);
- }
- private void clearByPaperId(Long paperId) {
- QueryWrapper<PaperGroupEntity> wrapper = new QueryWrapper<>();
- LambdaQueryWrapper<PaperGroupEntity> lw = wrapper.lambda();
- lw.eq(PaperGroupEntity::getPaperId, paperId);
- this.remove(wrapper);
- }
- }
|