|
@@ -1,19 +1,33 @@
|
|
|
package cn.com.qmth.print.manage.service.impl;
|
|
|
|
|
|
-import org.apache.commons.lang.StringUtils;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
import cn.com.qmth.print.manage.dao.ExamStudentDao;
|
|
|
+import cn.com.qmth.print.manage.dto.StudentDTO;
|
|
|
import cn.com.qmth.print.manage.entity.ExamStudentEntity;
|
|
|
import cn.com.qmth.print.manage.enums.GroupType;
|
|
|
import cn.com.qmth.print.manage.service.ExamStudentService;
|
|
|
-
|
|
|
+import cn.com.qmth.print.manage.service.query.ExamStudentQuery;
|
|
|
+import cn.com.qmth.print.manage.utils.excel.ExcelError;
|
|
|
+import cn.com.qmth.print.manage.utils.excel.ExcelReader;
|
|
|
+import cn.com.qmth.print.manage.utils.excel.ExcelReaderHandle;
|
|
|
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.exception.StatusException;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
+import java.util.StringJoiner;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
+import java.util.concurrent.atomic.AtomicLong;
|
|
|
|
|
|
@Service
|
|
|
public class ExamStudentServiceImpl extends ServiceImpl<ExamStudentDao, ExamStudentEntity> implements
|
|
@@ -63,4 +77,97 @@ public class ExamStudentServiceImpl extends ServiceImpl<ExamStudentDao, ExamStud
|
|
|
queryWrapper.lambda().eq(ExamStudentEntity::getExamId, examId);
|
|
|
return this.list(queryWrapper);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IPage<ExamStudentEntity> pageStudent(ExamStudentQuery query) {
|
|
|
+ QueryWrapper<ExamStudentEntity> queryWrapper = new QueryWrapper<>();
|
|
|
+ if (query.getExamId() == null) {
|
|
|
+ throw new StatusException("请选择批次");
|
|
|
+ }
|
|
|
+ queryWrapper.lambda().eq(ExamStudentEntity::getExamId, query.getExamId());
|
|
|
+ if (StringUtils.isNotBlank(query.getExamNumber())) {
|
|
|
+ queryWrapper.lambda().eq(ExamStudentEntity::getExamNumber, query.getExamNumber());
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(query.getCourseCode())) {
|
|
|
+ queryWrapper.lambda().eq(ExamStudentEntity::getCourseCode, query.getCourseCode());
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(query.getExamSite())) {
|
|
|
+ queryWrapper.lambda().eq(ExamStudentEntity::getExamSite, query.getExamSite());
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(query.getExamRoom())) {
|
|
|
+ queryWrapper.lambda().eq(ExamStudentEntity::getExamRoom, query.getExamRoom());
|
|
|
+ }
|
|
|
+ Page page = new Page(query.getPageNumber(), query.getPageSize());
|
|
|
+ return this.page(page, queryWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public List<ExcelError> importStudents(Long examId, MultipartFile file) throws IOException {
|
|
|
+ List<ExamStudentEntity> studentList = new ArrayList<>();
|
|
|
+ ExcelReader excelReader = new ExcelReader(StudentDTO.class);
|
|
|
+ AtomicLong aLong = new AtomicLong(1L);
|
|
|
+ List<ExcelError> excelErrors = excelReader.reader(file.getInputStream(), obj -> {
|
|
|
+ try {
|
|
|
+ StudentDTO dto = (StudentDTO) obj;
|
|
|
+ ExamStudentEntity examStudentEntity = new ExamStudentEntity();
|
|
|
+ examStudentEntity.setExamId(examId);
|
|
|
+ examStudentEntity.setExamNumber(dto.getExamNumber());
|
|
|
+ examStudentEntity.setStudentCode(dto.getStudentCode());
|
|
|
+ examStudentEntity.setName(dto.getName());
|
|
|
+ examStudentEntity.setCourseCode(dto.getCourseCode().concat("_").concat(dto.getCourseName()));
|
|
|
+ examStudentEntity.setExamSite(dto.getExamSite());
|
|
|
+ examStudentEntity.setExamRoom(dto.getExamRoom());
|
|
|
+ examStudentEntity.setSortNo(aLong.getAndIncrement());
|
|
|
+ studentList.add(examStudentEntity);
|
|
|
+ return null;
|
|
|
+ } catch (RuntimeException e) {
|
|
|
+
|
|
|
+ TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
|
|
+ ExcelError excelError = new ExcelError();
|
|
|
+ excelError.setExcelErrorType(e.getMessage());
|
|
|
+ return excelError;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ String errors = errorsString(excelErrors);
|
|
|
+ if (errors.length() > 0) {
|
|
|
+ throw new StatusException(errors);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(studentList)) {
|
|
|
+ List<ExamStudentEntity> data = new ArrayList<>();
|
|
|
+ for (ExamStudentEntity stu : studentList) {
|
|
|
+ if (data.size() == 2000) {
|
|
|
+ this.saveBatch(data);
|
|
|
+ data.clear();
|
|
|
+ }
|
|
|
+ data.add(stu);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!data.isEmpty()) {
|
|
|
+ this.saveBatch(data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return excelErrors;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 拼接导入异常信息
|
|
|
+ *
|
|
|
+ * @param excelErrors
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String errorsString(List<ExcelError> excelErrors) {
|
|
|
+ StringJoiner sj = new StringJoiner(";");
|
|
|
+ if (!excelErrors.isEmpty() && excelErrors.size() > 0) {
|
|
|
+ int forint = excelErrors.size() < 10 ? excelErrors.size() : 9;
|
|
|
+ for (int i = 0; i < forint; i++) {
|
|
|
+ ExcelError excelError = excelErrors.get(i);
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ sb.append("第").append(excelError.getRow()).append("行,").append(excelError.getExcelErrorType());
|
|
|
+ sj.add(sb.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sj.toString();
|
|
|
+ }
|
|
|
}
|