123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612 |
- package cn.com.qmth.mps.service.impl;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
- import org.apache.commons.collections4.CollectionUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.dao.DuplicateKeyException;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.transaction.interceptor.TransactionAspectSupport;
- import org.springframework.web.multipart.MultipartFile;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.qmth.boot.core.collection.PageResult;
- import com.qmth.boot.core.exception.StatusException;
- import com.qmth.boot.tools.excel.ExcelReader;
- import com.qmth.boot.tools.excel.enums.ExcelType;
- import com.qmth.boot.tools.excel.model.DataMap;
- 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.PaperDao;
- import cn.com.qmth.mps.entity.CourseEntity;
- import cn.com.qmth.mps.entity.ExamEntity;
- import cn.com.qmth.mps.entity.PaperEntity;
- import cn.com.qmth.mps.enums.ExamStatus;
- 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.PaperGroupService;
- import cn.com.qmth.mps.service.PaperService;
- import cn.com.qmth.mps.util.BatchSetDataUtil;
- import cn.com.qmth.mps.util.Calculator;
- import cn.com.qmth.mps.util.PageUtil;
- import cn.com.qmth.mps.vo.exam.ExamPaperCountVo;
- import cn.com.qmth.mps.vo.paper.GroupCountVo;
- import cn.com.qmth.mps.vo.paper.PaperInfoVo;
- import cn.com.qmth.mps.vo.paper.PaperQuery;
- import cn.com.qmth.mps.vo.paper.PaperStructInfoVo;
- import cn.com.qmth.mps.vo.paper.PaperVo;
- import cn.com.qmth.mps.vo.paper.StructDomain;
- @Service
- public class PaperServiceImpl extends ServiceImpl<PaperDao, PaperEntity> implements PaperService {
- private static final String[] SUBJECT_EXCEL_HEADER = new String[] {"科目代码", "科目名称"};
- private static final String[] SUBJECT_STRUCT_EXCEL_HEADER = new String[] {"科目代码*","科目名称","大题名称*","题目昵称","大题号(只能用小写数字)*","小题号(只能用小写数字)*","小题满分*","间隔分*","评卷分组(只能用小写数字)*","图片序号(用英文逗号分割)","双评比例(0~1)","仲裁阀值","合分策略(1-平均,2-最高,3-最低)","评卷模式(common-普通,track-轨迹)","试评数量(0-跳过试评)","选做题数量"};
- @Autowired
- private ExamService examService;
- @Autowired
- private CourseService courseService;
- @Autowired
- private PaperGroupService paperGroupService;
- @Autowired
- private PaperDetailService paperDetailService;
- @Transactional
- @Override
- public List<String> importPaper(Long examId, User user, MultipartFile file) {
- ExamEntity exam = examService.getById(examId);
- if (exam == null) {
- throw new StatusException("未找到考试批次");
- }
- if(!ExamStatus.EDIT.equals(exam.getExamStatus())) {
- throw new StatusException("考试未开放上报,不能设置结构信息或分组信息");
- }
- if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(exam.getSchoolId())) {
- throw new StatusException("没有权限");
- }
- InputStream inputStream = null;
- try {
- inputStream = file.getInputStream();
- ExcelReader reader=ExcelReader.create(ExcelType.XLSX, inputStream, 0);
- List<DataMap> lineList = reader.getDataMapList();
- if(!Arrays.equals(SUBJECT_EXCEL_HEADER,reader.getColumnNames())) {
- throw new StatusException("Excel表头错误");
- }
- if (CollectionUtils.isEmpty(lineList)) {
- throw new StatusException("Excel无内容");
- }
- if (1001 < lineList.size()) {
- throw new StatusException("数据行数不能超过1000");
- }
- List<String> failRecords = new ArrayList<>();
- List<PaperEntity> ret = new ArrayList<>();
- for (int i = 0; i < lineList.size(); i++) {
- DataMap line = lineList.get(i);
- StringBuilder msg = new StringBuilder();
- PaperEntity imp = new PaperEntity();
- imp.setTotalScore(0.0);
- imp.setObjectiveScore(0.0);
- imp.setSubjectiveScore(0.0);
- imp.setSchoolId(exam.getSchoolId());
- imp.setGroupFinish(false);
- imp.setStructFinish(false);
- imp.setExamId(examId);
- String code = trimAndNullIfBlank(line.get(SUBJECT_EXCEL_HEADER[0]));
- if (StringUtils.isBlank(code)) {
- msg.append(" 科目代码不能为空");
- } else if (code.length() > 50) {
- msg.append(" 科目代码不能超过50个字符");
- }
- String name = trimAndNullIfBlank(line.get(SUBJECT_EXCEL_HEADER[1]));
- if (StringUtils.isBlank(name)) {
- msg.append(" 科目名称不能为空");
- } else if (name.length() > 50) {
- msg.append(" 科目名称不能超过50个字符");
- }
- if (msg.length() == 0) {
- CourseEntity course = courseService.saveOrGet(exam.getSchoolId(), code, name);
- imp.setCourseId(course.getId());
- }
- if (msg.length() > 0) {
- failRecords.add(newError(i + 1, msg.toString()));
- } else {
- ret.add(imp);
- }
- }
- if (CollectionUtils.isNotEmpty(failRecords)) {
- TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
- return failRecords;
- }
- for (int i = 0; i < ret.size(); i++) {
- PaperEntity cur = ret.get(i);
- try {
- this.save(cur);
- } catch (DuplicateKeyException e) {
- // failRecords.add(newError(i + 1, "科目已存在"));
- } catch (Exception e) {
- failRecords.add(newError(i + 1, "系统异常"));
- log.error("科目导入系统异常", e);
- }
- }
- if (CollectionUtils.isNotEmpty(failRecords)) {
- TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
- }
- return failRecords;
- } catch (StatusException e) {
- throw e;
- } catch (Exception e) {
- throw new RuntimeException("系统错误", e);
- } finally {
- if (inputStream != null) {
- try {
- inputStream.close();
- } catch (IOException e) {
- }
- }
- }
- }
-
- private String trimAndNullIfBlank(String s) {
- if (StringUtils.isBlank(s)) {
- return null;
- }
- return s.trim();
- }
- private String newError(int lineNum, String msg) {
- return "第" + lineNum + "行" + msg;
- }
- @Override
- public List<ExamPaperCountVo> findPaperCount(List<Long> examIds) {
- return this.baseMapper.findPaperCount(examIds);
- }
- @Override
- public Integer findPaperCount(Long examId) {
- QueryWrapper<PaperEntity> wrapper = new QueryWrapper<>();
- LambdaQueryWrapper<PaperEntity> lw = wrapper.lambda();
- lw.eq(PaperEntity::getExamId, examId);
- return this.count(wrapper);
- }
- private PaperEntity findByExamAndCourse(Long examId, Long courseId) {
- QueryWrapper<PaperEntity> wrapper = new QueryWrapper<>();
- LambdaQueryWrapper<PaperEntity> lw = wrapper.lambda();
- lw.eq(PaperEntity::getExamId, examId);
- lw.eq(PaperEntity::getCourseId, courseId);
- return this.getOne(wrapper);
- }
- private PaperEntity saveOrGet(Long schoolId, Long examId, Long courseId) {
- PaperEntity ret = findByExamAndCourse(examId, courseId);
- if (ret != null) {
- return ret;
- }
- PaperEntity imp = new PaperEntity();
- imp.setTotalScore(0.0);
- imp.setObjectiveScore(0.0);
- imp.setSubjectiveScore(0.0);
- imp.setSchoolId(schoolId);
- imp.setGroupFinish(false);
- imp.setCourseId(courseId);
- imp.setExamId(examId);
- imp.setStructFinish(true);
- this.save(imp);
- return imp;
- }
- @Override
- public PageResult<PaperVo> page(PaperQuery query, User user) {
- if (query.getSchoolId() == null) {
- throw new StatusException("学校不能为空");
- }
- if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(query.getSchoolId())) {
- throw new StatusException("没有权限");
- }
- IPage<PaperVo> iPage = this.baseMapper.page(new Page<PaperVo>(query.getPageNumber(), query.getPageSize()),
- query);
- if (CollectionUtils.isNotEmpty(iPage.getRecords())) {
- new BatchSetDataUtil<PaperVo>() {
- @Override
- protected void setData(List<PaperVo> dataList) {
- List<Long> paperIds = dataList.stream().map(dto -> dto.getId()).distinct()
- .collect(Collectors.toList());
- List<GroupCountVo> ret = paperGroupService.findGroupCount(paperIds);
- if (ret != null && ret.size() > 0) {
- Map<Long, Integer> countMap = new HashMap<>();
- for (GroupCountVo item : ret) {
- countMap.put(item.getPaperId(), item.getGroupCount());
- }
- for (PaperVo vo : dataList) {
- vo.setGroupCount(countMap.get(vo.getId()));
- }
- }
- }
- }.setDataForBatch(iPage.getRecords(), 20);
- for (PaperVo vo : iPage.getRecords()) {
- if(vo.getGroupCount()==null) {
- vo.setGroupCount(0);
- }
- }
- }
- return PageUtil.of(iPage);
- }
- @Override
- public List<PaperVo> list(Long examId, User user) {
- ExamEntity exam = examService.getById(examId);
- if (exam == null) {
- throw new StatusException("未找到考试批次");
- }
- if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(exam.getSchoolId())) {
- throw new StatusException("没有权限");
- }
- List<PaperVo> ret=this.baseMapper.myPaperlist(examId,user.getId());
- if (CollectionUtils.isNotEmpty(ret)) {
- new BatchSetDataUtil<PaperVo>() {
- @Override
- protected void setData(List<PaperVo> dataList) {
- List<Long> paperIds = dataList.stream().map(dto -> dto.getId()).distinct()
- .collect(Collectors.toList());
- List<GroupCountVo> ret = paperGroupService.findGroupCount(paperIds);
- if (ret != null && ret.size() > 0) {
- Map<Long, Integer> countMap = new HashMap<>();
- for (GroupCountVo item : ret) {
- countMap.put(item.getPaperId(), item.getGroupCount());
- }
- for (PaperVo vo : dataList) {
- vo.setGroupCount(countMap.get(vo.getId()));
- }
- }
- }
- }.setDataForBatch(ret, 20);
- }
- return ret;
- }
- @Override
- public PaperInfoVo info(Long id, User user) {
- PaperEntity paper = this.getById(id);
- if (paper == null) {
- throw new StatusException("未找到试卷结构信息");
- }
- if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(paper.getSchoolId())) {
- throw new StatusException("没有权限");
- }
- PaperInfoVo vo = new PaperInfoVo();
- BeanUtils.copyProperties(paper, vo);
- CourseEntity course = courseService.getById(vo.getCourseId());
- vo.setCourseCode(course.getCode());
- vo.setCourseName(course.getName());
- vo.setStructInfo(paperDetailService.getStructInfo(vo.getId()));
- vo.setGroupInfo(paperGroupService.getGroupInfo(vo.getId()));
- return vo;
- }
- @Transactional
- @Override
- public List<String> importSubjectStruct(Long examId, User user, MultipartFile file) {
- ExamEntity exam = examService.getById(examId);
- if (exam == null) {
- throw new StatusException("未找到考试批次");
- }
- if(!ExamStatus.EDIT.equals(exam.getExamStatus())) {
- throw new StatusException("考试未开放上报,不能设置结构信息或分组信息");
- }
- if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(exam.getSchoolId())) {
- throw new StatusException("没有权限");
- }
- InputStream inputStream = null;
- try {
- inputStream = file.getInputStream();
- ExcelReader reader=ExcelReader.create(ExcelType.XLSX, inputStream, 1);
- List<DataMap> lineList = reader.getDataMapList();
- if(!Arrays.equals(SUBJECT_STRUCT_EXCEL_HEADER,reader.getColumnNames())) {
- throw new StatusException("Excel表头错误");
- }
- if (CollectionUtils.isEmpty(lineList)) {
- throw new StatusException("Excel无内容");
- }
- if (10001 < lineList.size()) {
- throw new StatusException("数据行数不能超过10000");
- }
- List<String> failRecords = new ArrayList<>();
- List<PaperStructInfoVo> ret = new ArrayList<>();
- for (int i = 0; i < lineList.size(); i++) {
- DataMap line = lineList.get(i);
- StringBuilder msg = new StringBuilder();
- PaperStructInfoVo imp = new PaperStructInfoVo();
- String code = trimAndNullIfBlank(line.get(SUBJECT_STRUCT_EXCEL_HEADER[0]));
- if (StringUtils.isBlank(code)) {
- msg.append(" 科目代码不能为空");
- } else if (code.length() > 50) {
- msg.append(" 科目代码不能超过50个字符");
- }
- imp.setCourseCode(code);
- String name = trimAndNullIfBlank(line.get(SUBJECT_STRUCT_EXCEL_HEADER[1]));
- if (StringUtils.isBlank(name)) {
- msg.append(" 科目名称不能为空");
- } else if (name.length() > 50) {
- msg.append(" 科目名称不能超过50个字符");
- }
- if (msg.length() == 0) {
- CourseEntity course = courseService.saveOrGet(exam.getSchoolId(), code, name);
- PaperEntity paper = saveOrGet(exam.getSchoolId(), examId, course.getId());
- imp.setPaperId(paper.getId());
- }
- String detailName = trimAndNullIfBlank(line.get(SUBJECT_STRUCT_EXCEL_HEADER[2]));
- if (StringUtils.isBlank(detailName)) {
- msg.append(" 大题名称不能为空");
- } else if (detailName.length() > 50) {
- msg.append(" 大题名称不能超过50个字符");
- }
- imp.setDetailName(detailName);
- //第4列不处理
- String detailNumber = trimAndNullIfBlank(line.get(SUBJECT_STRUCT_EXCEL_HEADER[4]));
- if (StringUtils.isBlank(detailNumber)) {
- msg.append(" 大题号不能为空");
- } else {
- try {
- int n = Integer.valueOf(detailNumber);
- if (n <= 0) {
- msg.append(" 大题号不能小于0");
- } else {
- imp.setDetailNumber(n);
- }
- } catch (Exception e) {
- msg.append(" 大题号只能是整数");
- }
- }
- String unitNumber = trimAndNullIfBlank(line.get(SUBJECT_STRUCT_EXCEL_HEADER[5]));
- if (StringUtils.isBlank(unitNumber)) {
- msg.append(" 小题号不能为空");
- } else {
- try {
- int n = Integer.valueOf(unitNumber);
- if (n <= 0) {
- msg.append(" 小题号不能小于0");
- } else {
- imp.setUnitNumber(n);
- }
- } catch (Exception e) {
- msg.append(" 小题号只能是整数");
- }
- }
- String score = trimAndNullIfBlank(line.get(SUBJECT_STRUCT_EXCEL_HEADER[6]));
- if (StringUtils.isBlank(score)) {
- msg.append(" 小题满分不能为空");
- } else {
- try {
- Double n = Double.valueOf(score);
- if (n <= 0) {
- msg.append(" 小题满分不能小于0");
- } else {
- if (score.indexOf(".")>=0&&score.indexOf(".") < score.length() - 2) {
- msg.append("小题满分只能有一位小数");
- } else {
- imp.setScore(n);
- }
- }
- } catch (Exception e) {
- msg.append(" 小题满分格式错误");
- }
- }
- String scoreStep = trimAndNullIfBlank(line.get(SUBJECT_STRUCT_EXCEL_HEADER[7]));
- if (StringUtils.isBlank(scoreStep)) {
- msg.append(" 间隔分不能为空");
- } else {
- try {
- Double n = Double.valueOf(scoreStep);
- if (n <= 0) {
- msg.append(" 间隔分不能小于0");
- } else {
- if (scoreStep.indexOf(".")>=0&&scoreStep.indexOf(".") < scoreStep.length() - 2) {
- msg.append("小间隔分只能有一位小数");
- } else {
- imp.setScoreStep(n);
- }
- }
- } catch (Exception e) {
- msg.append(" 间隔分格式错误");
- }
- }
- if (msg.length() > 0) {
- failRecords.add(newError(i + 3, msg.toString()));
- } else {
- ret.add(imp);
- }
- }
- if (CollectionUtils.isNotEmpty(failRecords)) {
- TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
- return failRecords;
- }
- this.saveStruct(ret, user, failRecords);
- if (CollectionUtils.isNotEmpty(failRecords)) {
- TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
- }
- return failRecords;
- } catch (StatusException e) {
- throw e;
- } catch (Exception e) {
- throw new RuntimeException("系统错误", e);
- } finally {
- if (inputStream != null) {
- try {
- inputStream.close();
- } catch (IOException e) {
- }
- }
- }
- }
- private void saveStruct(List<PaperStructInfoVo> cards, User user, List<String> failRecords) {
- Map<Long,String> map=new HashMap<>();
- for(PaperStructInfoVo vo:cards) {
- map.put(vo.getPaperId(), vo.getCourseCode());
- }
- List<StructDomain> ces = getBeans(cards);
- // checkStruct(ces, failRecords,map);
- if (CollectionUtils.isNotEmpty(failRecords)) {
- return;
- }
- for (StructDomain domain : ces) {
- try {
- paperDetailService.structImport(domain, user);
- } catch (StatusException e) {
- failRecords.add("科目:"+map.get(domain.getPaperId())+" "+e.getMessage());
- } catch (Exception e) {
- throw new RuntimeException("系统错误", e);
- }
- }
- }
- // private void checkStruct(List<StructDomain> ces, List<String> failRecords,Map<Long,String> courseMap) {
- // for (StructDomain card : ces) {
- // int lastDetailNum = 0;
- // for (PaperDetail detail : card.getStructInfo()) {
- // if (detail.getNumber() - lastDetailNum != 1) {
- // failRecords.add(
- // "科目:" + courseMap.get(card.getPaperId()) + ",大题号" + detail.getNumber() + "错误");
- // }
- // lastDetailNum = detail.getNumber();
- // int lastUnitNum = 0;
- // for (PaperDetailUnit unit : detail.getUnits()) {
- // if (unit.getNumber() - lastUnitNum != 1) {
- // failRecords.add("科目:" + courseMap.get(card.getPaperId()) + ",大题号:"
- // + detail.getNumber() + ",小题号" + unit.getNumber() + "错误");
- // }
- // lastUnitNum = unit.getNumber();
- // }
- // }
- // }
- // }
- private List<StructDomain> getBeans(List<PaperStructInfoVo> cards) {
- cards.sort(new Comparator<PaperStructInfoVo>() {
- @Override
- public int compare(PaperStructInfoVo o1, PaperStructInfoVo o2) {
- long c1 = o1.getPaperId();
- long c2 = o2.getPaperId();
- if (c1 < c2) {
- return -1;
- } else if (c1 > c2) {
- return 1;
- } else {
- int indx1 = o1.getDetailNumber();
- int indx2 = o2.getDetailNumber();
- if (indx1 < indx2) {
- return -1;
- } else if (indx1 > indx2) {
- return 1;
- } else {
- int u1 = o1.getUnitNumber();
- int u2 = o2.getUnitNumber();
- if (u1 < u2) {
- return -1;
- } else if (u1 > u2) {
- return 1;
- } else {
- return 0;
- }
- }
- }
- }
- });
- List<StructDomain> ces = new ArrayList<>();
- StructDomain curCard = null;
- PaperDetail curDetail = null;
- for (PaperStructInfoVo info : cards) {
- if (curCard == null || !info.getPaperId().equals(curCard.getPaperId())) {
- curCard = new StructDomain();
- curCard.setPaperId(info.getPaperId());
- curCard.setStructInfo(new ArrayList<>());
- ces.add(curCard);
- curDetail = null;
- }
- if (curDetail == null || !info.getDetailNumber().equals(curDetail.getNumber())) {
- curDetail = new PaperDetail();
- curDetail.setName(info.getDetailName());
- curDetail.setNumber(info.getDetailNumber());
- curDetail.setUnits(new ArrayList<>());
- curCard.getStructInfo().add(curDetail);
- }
- PaperDetailUnit unit = new PaperDetailUnit();
- curDetail.getUnits().add(unit);
- unit.setNumber(info.getUnitNumber());
- unit.setScore(info.getScore());
- unit.setScoreStep(info.getScoreStep());
- }
- for (StructDomain sd : ces) {
- setTotalScore(sd);
- }
- return ces;
- }
- private void setTotalScore(StructDomain domain) {
- double total = 0.0;
- for (PaperDetail detial : domain.getStructInfo()) {
- for (PaperDetailUnit unit : detial.getUnits()) {
- total = Calculator.add(total, unit.getScore(), 1);
- }
- }
- domain.setTotalScore(total);
- }
- @Override
- public List<PaperStructInfoVo> subjectiveList(PaperQuery query, User user) {
- if (query.getSchoolId() == null) {
- throw new StatusException("学校不能为空");
- }
- if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(query.getSchoolId())) {
- throw new StatusException("没有权限");
- }
- List<PaperStructInfoVo> ret = this.baseMapper.subjectiveList(query);
- if (CollectionUtils.isNotEmpty(ret)) {
- }
- return ret;
- }
- }
|