123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- package cn.com.qmth.mps.service.impl;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.apache.commons.collections4.CollectionUtils;
- import org.apache.commons.lang3.StringUtils;
- 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.User;
- import cn.com.qmth.mps.dao.PaperDetailDao;
- import cn.com.qmth.mps.entity.PaperDetailEntity;
- import cn.com.qmth.mps.entity.PaperEntity;
- import cn.com.qmth.mps.enums.Role;
- 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.PaperService;
- import cn.com.qmth.mps.util.Calculator;
- import cn.com.qmth.mps.vo.paper.StructDomain;
- @Service
- public class PaperDetailServiceImpl extends ServiceImpl<PaperDetailDao, PaperDetailEntity>
- implements PaperDetailService {
- @Autowired
- private PaperGroupService paperGroupService;
-
- @Autowired
- private PaperDetailUnitService paperDetailUnitService;
- @Autowired
- private PaperService paperService;
- @Override
- public List<PaperDetail> getStructInfo(Long paperId) {
- List<PaperDetail> ret = new ArrayList<>();
- List<PaperDetailEntity> es = getByPaperId(paperId);
- if (CollectionUtils.isEmpty(es)) {
- return ret;
- }
- Map<Long,PaperDetail> map=new HashMap<>();
- for(PaperDetailEntity e:es) {
- PaperDetail vo=new PaperDetail();
- vo.setName(e.getName());
- vo.setNumber(e.getNumber());
- map.put(e.getId(), vo);
- ret.add(vo);
- }
- Map<Long,List<PaperDetailUnit>> units=paperDetailUnitService.getStructInfo(paperId);
- if (units==null||units.size()==0) {
- return ret;
- }
- for(Long key:units.keySet()) {
- map.get(key).setUnits(units.get(key));
- }
- return ret;
- }
- @Override
- public List<PaperDetailEntity> getByPaperId(Long paperId) {
- QueryWrapper<PaperDetailEntity> wrapper = new QueryWrapper<>();
- LambdaQueryWrapper<PaperDetailEntity> lw = wrapper.lambda();
- lw.eq(PaperDetailEntity::getPaperId, paperId);
- return this.list(wrapper);
- }
- @Transactional
- @Override
- public void structSave(StructDomain domain, User user) {
- if (domain.getPaperId() == null) {
- throw new StatusException("试卷结构ID不能为空");
- }
- if (domain.getTotalScore() == null) {
- throw new StatusException("试卷满分不能为空");
- }
- if (domain.getTotalScore()<=0) {
- throw new StatusException("试卷满分必须大于0");
- }
- String total=domain.getTotalScore().toString();
- if (total.indexOf(".")<total.length()-2) {
- throw new StatusException("试卷满分只能有一位小数");
- }
- if (CollectionUtils.isEmpty(domain.getStructInfo())) {
- throw new StatusException("大题信息不能为空");
- }
- for (PaperDetail u : domain.getStructInfo()) {
- if (u.getNumber() == null || StringUtils.isBlank(u.getName())) {
- throw new StatusException("大题号、大题名称不能为空");
- }
- if (CollectionUtils.isEmpty(u.getUnits())) {
- throw new StatusException("小题信息不能为空");
- }
- for (PaperDetailUnit unit : u.getUnits()) {
- if (unit.getNumber()== null ||unit.getScore()==null||unit.getScoreStep()==null) {
- throw new StatusException("小题号、小题满分、给分间隔不能为空");
- }
- if (unit.getNumber()<=0) {
- throw new StatusException("小题号必须大于0");
- }
- String score=unit.getScore().toString();
- if (score.indexOf(".")<score.length()-2) {
- throw new StatusException("小题满分只能有一位小数");
- }
- String scoreStep=unit.getScoreStep().toString();
- if (scoreStep.indexOf(".")<scoreStep.length()-2) {
- throw new StatusException("给分间隔只能有一位小数");
- }
- }
- }
- PaperEntity paper = paperService.getById(domain.getPaperId());
- if (paper == null) {
- throw new StatusException("未找到试卷结构");
- }
- if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(paper.getSchoolId())) {
- throw new StatusException("没有权限");
- }
- if (paper.getStructFinish()) {
- throw new StatusException("试卷结构已提交,不能暂存");
- }
- clearPaperSruct(domain.getPaperId());
- paper.setTotalScore(domain.getTotalScore());
- paper.setSubjectiveScore(domain.getTotalScore());
- paperService.updateById(paper);
- for(PaperDetail pd:domain.getStructInfo()) {
- PaperDetailEntity e=new PaperDetailEntity();
- e.setName(pd.getName());
- e.setNumber(pd.getNumber());
- e.setPaperId(domain.getPaperId());
- this.save(e);
- paperDetailUnitService.saveUnit(e,pd.getUnits());
- }
- }
-
- private void clearPaperSruct(Long paperId) {
- clearByPaperId(paperId);
- paperDetailUnitService.clearByPaperId(paperId);
- }
-
- private void clearByPaperId(Long paperId) {
- QueryWrapper<PaperDetailEntity> wrapper = new QueryWrapper<>();
- LambdaQueryWrapper<PaperDetailEntity> lw = wrapper.lambda();
- lw.eq(PaperDetailEntity::getPaperId, paperId);
- this.remove(wrapper);
- }
-
- @Transactional
- @Override
- public void structSubmit(StructDomain domain, User user) {
- if (domain.getPaperId() == null) {
- throw new StatusException("试卷结构ID不能为空");
- }
- if (domain.getTotalScore() == null) {
- throw new StatusException("试卷满分不能为空");
- }
- if (domain.getTotalScore()<=0) {
- throw new StatusException("试卷满分必须大于0");
- }
- String total=domain.getTotalScore().toString();
- if (total.indexOf(".")<total.length()-2) {
- throw new StatusException("试卷满分只能有一位小数");
- }
- if (CollectionUtils.isEmpty(domain.getStructInfo())) {
- throw new StatusException("大题信息不能为空");
- }
- for (PaperDetail u : domain.getStructInfo()) {
- if (u.getNumber() == null || StringUtils.isBlank(u.getName())) {
- throw new StatusException("大题号、大题名称不能为空");
- }
- if (CollectionUtils.isEmpty(u.getUnits())) {
- throw new StatusException("小题信息不能为空");
- }
- for (PaperDetailUnit unit : u.getUnits()) {
- if (unit.getNumber()== null ||unit.getScore()==null||unit.getScoreStep()==null) {
- throw new StatusException("小题号、小题满分、给分间隔不能为空");
- }
- if (unit.getNumber()<=0) {
- throw new StatusException("小题号必须大于0");
- }
- String score=unit.getScore().toString();
- if (score.indexOf(".")<score.length()-2) {
- throw new StatusException("小题满分只能有一位小数");
- }
- String scoreStep=unit.getScoreStep().toString();
- if (scoreStep.indexOf(".")<scoreStep.length()-2) {
- throw new StatusException("给分间隔只能有一位小数");
- }
- }
- }
- checkTotalScore(domain);
- PaperEntity paper = paperService.getById(domain.getPaperId());
- if (paper == null) {
- throw new StatusException("未找到试卷结构");
- }
- if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(paper.getSchoolId())) {
- throw new StatusException("没有权限");
- }
- if (paperGroupService.existsGroup(domain.getPaperId())) {
- throw new StatusException("请先删除分组信息再提交试卷结构");
- }
- clearPaperSruct(domain.getPaperId());
- paper.setTotalScore(domain.getTotalScore());
- paper.setSubjectiveScore(domain.getTotalScore());
- paper.setStructFinish(true);
- paperService.updateById(paper);
- for(PaperDetail pd:domain.getStructInfo()) {
- PaperDetailEntity e=new PaperDetailEntity();
- e.setName(pd.getName());
- e.setNumber(pd.getNumber());
- e.setPaperId(domain.getPaperId());
- this.save(e);
- paperDetailUnitService.saveUnit(e,pd.getUnits());
- }
- }
-
- public void checkTotalScore(StructDomain domain) {
- double total=0.0;
- for(PaperDetail detial:domain.getStructInfo()) {
- for(PaperDetailUnit unit:detial.getUnits()) {
- total=Calculator.add(total, unit.getScore(),1);
- }
- }
- if(total!=domain.getTotalScore()) {
- throw new StatusException("试卷满分与小题分不一致");
- }
- }
- }
|