|
@@ -0,0 +1,344 @@
|
|
|
+package cn.com.qmth.am.service.impl;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
+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.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+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.am.bean.ImageSlice;
|
|
|
+import cn.com.qmth.am.bean.ImportResult;
|
|
|
+import cn.com.qmth.am.config.SysProperty;
|
|
|
+import cn.com.qmth.am.dao.QuestionDao;
|
|
|
+import cn.com.qmth.am.entity.QuestionEntity;
|
|
|
+import cn.com.qmth.am.service.QuestionService;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class QuestionServiceImpl extends ServiceImpl<QuestionDao, QuestionEntity> implements QuestionService {
|
|
|
+
|
|
|
+ private static final String[] EXCEL_HEADER = new String[] { "考试ID", "科目代码", "大题号", "小题号", "满分", "试题内容", "试题答案",
|
|
|
+ "作答页码", "作答坐标" };
|
|
|
+ @Autowired
|
|
|
+ private SysProperty sysProperty;
|
|
|
+ @Autowired
|
|
|
+ private QuestionService questionService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void importQuestion() {
|
|
|
+ File dir = new File(sysProperty.getDataDir() + "/question-import");
|
|
|
+ File[] fs = dir.listFiles();
|
|
|
+ if (fs == null || fs.length == 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (File file : fs) {
|
|
|
+ if (!file.isFile() || !file.getName().endsWith(".xlsx")) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ InputStream inputStream = null;
|
|
|
+ ImportResult ret = null;
|
|
|
+ try {
|
|
|
+ inputStream = new FileInputStream(file);
|
|
|
+ ret = questionService.disposeFile(inputStream);
|
|
|
+ } catch (Exception e) {
|
|
|
+ String errMsg;
|
|
|
+ if (e instanceof FileNotFoundException) {
|
|
|
+ errMsg = "未找到文件:" + file.getAbsolutePath();
|
|
|
+ } else {
|
|
|
+ errMsg = "系统错误:" + e.getMessage();
|
|
|
+ }
|
|
|
+ ret = new ImportResult(errMsg);
|
|
|
+ } finally {
|
|
|
+ if (inputStream != null) {
|
|
|
+ try {
|
|
|
+ inputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ moveFile(dir, file, ret);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void moveFile(File dir, File file, ImportResult ret) {
|
|
|
+ try {
|
|
|
+ boolean succss = CollectionUtils.isEmpty(ret.getErrMsg());
|
|
|
+ if (succss) {
|
|
|
+ File sucDir = new File(dir.getAbsoluteFile() + "/success/");
|
|
|
+ if (!sucDir.exists()) {
|
|
|
+ sucDir.mkdir();
|
|
|
+ }
|
|
|
+ File targetFile = new File(sucDir.getAbsoluteFile() + "/" + file.getName());
|
|
|
+ if (targetFile.exists()) {
|
|
|
+ targetFile.delete();
|
|
|
+ }
|
|
|
+ FileUtils.copyFile(file, targetFile);
|
|
|
+ file.delete();
|
|
|
+ String fname = file.getName().substring(0, file.getName().lastIndexOf("."));
|
|
|
+ File msgFile = new File(sucDir.getAbsoluteFile() + "/" + fname + "_info.txt");
|
|
|
+ if (msgFile.exists()) {
|
|
|
+ msgFile.delete();
|
|
|
+ }
|
|
|
+ FileUtils.write(msgFile, ret.getCountInfo(), "UFT-8");
|
|
|
+ } else {
|
|
|
+ File sucDir = new File(dir.getAbsoluteFile() + "/failed/");
|
|
|
+ if (!sucDir.exists()) {
|
|
|
+ sucDir.mkdir();
|
|
|
+ }
|
|
|
+ File targetFile = new File(sucDir.getAbsoluteFile() + "/" + file.getName());
|
|
|
+ if (targetFile.exists()) {
|
|
|
+ targetFile.delete();
|
|
|
+ }
|
|
|
+ FileUtils.copyFile(file, targetFile);
|
|
|
+ file.delete();
|
|
|
+ String fname = file.getName().substring(0, file.getName().lastIndexOf("."));
|
|
|
+ File msgFile = new File(sucDir.getAbsoluteFile() + "/" + fname + "_info.txt");
|
|
|
+ if (msgFile.exists()) {
|
|
|
+ msgFile.delete();
|
|
|
+ }
|
|
|
+ FileUtils.writeLines(msgFile, ret.getErrMsg(), "UFT-8");
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new StatusException("文件处理出错", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private String errorMsg(int lineNum, String msg) {
|
|
|
+ return "第" + lineNum + "行 " + msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String trimAndNullIfBlank(String s) {
|
|
|
+ if (StringUtils.isBlank(s)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return s.trim();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public ImportResult disposeFile(InputStream inputStream) {
|
|
|
+ List<DataMap> lineList = null;
|
|
|
+ ExcelReader reader = ExcelReader.create(ExcelType.XLSX, inputStream, 0);
|
|
|
+ try {
|
|
|
+ lineList = reader.getDataMapList();
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new StatusException("Excel 解析失败");
|
|
|
+ }
|
|
|
+ if (!Arrays.equals(EXCEL_HEADER, reader.getColumnNames())) {
|
|
|
+ throw new StatusException("Excel表头错误");
|
|
|
+ }
|
|
|
+ if (CollectionUtils.isEmpty(lineList)) {
|
|
|
+ throw new StatusException("Excel无内容");
|
|
|
+ }
|
|
|
+ if (100001 < lineList.size()) {
|
|
|
+ throw new StatusException("数据行数不能超过100000");
|
|
|
+ }
|
|
|
+ List<QuestionEntity> ss = new ArrayList<>();
|
|
|
+ ImportResult ret = new ImportResult();
|
|
|
+ List<String> failRecords = new ArrayList<>();
|
|
|
+ ret.setErrMsg(failRecords);
|
|
|
+ for (int i = 0; i < lineList.size(); i++) {
|
|
|
+ DataMap line = lineList.get(i);
|
|
|
+
|
|
|
+ StringBuilder msg = new StringBuilder();
|
|
|
+
|
|
|
+ QuestionEntity imp = new QuestionEntity();
|
|
|
+ String examId = trimAndNullIfBlank(line.get(EXCEL_HEADER[0]));
|
|
|
+ if (StringUtils.isBlank(examId)) {
|
|
|
+ msg.append(" 考试ID不能为空");
|
|
|
+ } else if (examId.length() > 20) {
|
|
|
+ msg.append(" 考试ID不能超过20个字符");
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ Long examIdVal = Long.parseLong(examId);
|
|
|
+ imp.setExamId(examIdVal);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ msg.append(" 考试ID只能是数字");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String subjectCode = trimAndNullIfBlank(line.get(EXCEL_HEADER[1]));
|
|
|
+ if (StringUtils.isBlank(subjectCode)) {
|
|
|
+ msg.append(" 科目代码不能为空");
|
|
|
+ } else if (subjectCode.length() > 100) {
|
|
|
+ msg.append(" 科目代码不能超过100个字符");
|
|
|
+ }
|
|
|
+ imp.setSubjectCode(subjectCode);
|
|
|
+
|
|
|
+ String mainNum = trimAndNullIfBlank(line.get(EXCEL_HEADER[2]));
|
|
|
+ if (StringUtils.isBlank(mainNum)) {
|
|
|
+ msg.append(" 大题号不能为空");
|
|
|
+ } else if (mainNum.length() > 10) {
|
|
|
+ msg.append(" 大题号不能超过10个字符");
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ Integer mainNumVal = Integer.parseInt(mainNum);
|
|
|
+ if (mainNumVal <= 0) {
|
|
|
+ msg.append(" 大题号必须大于0");
|
|
|
+ }
|
|
|
+ imp.setMainNumber(mainNumVal);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ msg.append(" 大题号格式错误");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String subNum = trimAndNullIfBlank(line.get(EXCEL_HEADER[3]));
|
|
|
+ if (StringUtils.isBlank(subNum)) {
|
|
|
+ msg.append(" 小题号不能为空");
|
|
|
+ } else if (subNum.length() > 10) {
|
|
|
+ msg.append(" 小题号不能超过10个字符");
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ Integer subNumVal = Integer.parseInt(subNum);
|
|
|
+ if (subNumVal <= 0) {
|
|
|
+ msg.append(" 小题号必须大于0");
|
|
|
+ }
|
|
|
+ imp.setSubNumber(subNumVal);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ msg.append(" 小题号格式错误");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String fullScore = trimAndNullIfBlank(line.get(EXCEL_HEADER[4]));
|
|
|
+ if (StringUtils.isBlank(fullScore)) {
|
|
|
+ msg.append(" 满分不能为空");
|
|
|
+ } else if (fullScore.length() > 10) {
|
|
|
+ msg.append(" 满分不能超过10个字符");
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ Double fullScoreVal = Double.parseDouble(fullScore);
|
|
|
+ if (fullScoreVal <= 0) {
|
|
|
+ msg.append(" 满分必须大于0");
|
|
|
+ }
|
|
|
+ imp.setFullScore(fullScoreVal);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ msg.append(" 满分格式错误");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String content = trimAndNullIfBlank(line.get(EXCEL_HEADER[5]));
|
|
|
+ if (StringUtils.isBlank(content)) {
|
|
|
+ msg.append(" 试题内容不能为空");
|
|
|
+ }
|
|
|
+ imp.setContent(content);
|
|
|
+
|
|
|
+ String answer = trimAndNullIfBlank(line.get(EXCEL_HEADER[6]));
|
|
|
+ if (StringUtils.isBlank(answer)) {
|
|
|
+ msg.append(" 试题答案不能为空");
|
|
|
+ }
|
|
|
+ imp.setAnswer(answer);
|
|
|
+
|
|
|
+ String pageIndex = trimAndNullIfBlank(line.get(EXCEL_HEADER[7]));
|
|
|
+ if (StringUtils.isBlank(pageIndex)) {
|
|
|
+ msg.append(" 作答页码不能为空");
|
|
|
+ } else if (pageIndex.length() > 10) {
|
|
|
+ msg.append(" 作答页码不能超过10个字符");
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ Integer pageIndexVal = Integer.parseInt(pageIndex);
|
|
|
+ if (pageIndexVal <= 0) {
|
|
|
+ msg.append(" 作答页码必须大于0");
|
|
|
+ }
|
|
|
+ imp.setPageIndex(pageIndexVal);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ msg.append(" 作答页码格式错误");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String imageSlice = trimAndNullIfBlank(line.get(EXCEL_HEADER[8]));
|
|
|
+ if (StringUtils.isBlank(imageSlice)) {
|
|
|
+ msg.append(" 作答坐标不能为空");
|
|
|
+ } else if (imageSlice.length() > 1000) {
|
|
|
+ msg.append(" 作答坐标不能超过1000个字符");
|
|
|
+ } else {
|
|
|
+ ImageSlice val=getImageSlice(imageSlice);
|
|
|
+ if(val==null) {
|
|
|
+ msg.append(" 作答坐标格式有误");
|
|
|
+ }else {
|
|
|
+ imp.setImageSlice(val);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (msg.length() > 0) {
|
|
|
+ failRecords.add(errorMsg(i + 1, msg.toString()));
|
|
|
+ } else {
|
|
|
+ ss.add(imp);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (CollectionUtils.isNotEmpty(failRecords)) {
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ int addCount = saveQuestionBatch(ss);
|
|
|
+ ret.setCountInfo("新增数量:" + addCount);
|
|
|
+ } catch (Exception e) {
|
|
|
+ failRecords.add("系统错误:" + e.getMessage());
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ private ImageSlice getImageSlice(String s) {
|
|
|
+ if (StringUtils.isBlank(s)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ ImageSlice ret = JSONObject.parseObject(s, ImageSlice.class);
|
|
|
+ if (ret.getH() == null || ret.getI() == null || ret.getW() == null || ret.getX() == null
|
|
|
+ || ret.getY() == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private int saveQuestionBatch(List<QuestionEntity> ss) {
|
|
|
+ if (CollectionUtils.isEmpty(ss)) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ List<QuestionEntity> all = this.list();
|
|
|
+ Set<String> set = new HashSet<>();
|
|
|
+ if (CollectionUtils.isNotEmpty(all)) {
|
|
|
+ for (QuestionEntity s : all) {
|
|
|
+ String key = s.getExamId() + "-" + s.getSubjectCode() + "-" + s.getMainNumber()+"-"+s.getSubNumber();
|
|
|
+ set.add(key);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<QuestionEntity> adds = new ArrayList<>();
|
|
|
+ for (QuestionEntity s : ss) {
|
|
|
+ String key = s.getExamId() + "-" + s.getSubjectCode() + "-" + s.getMainNumber()+"-"+s.getSubNumber();
|
|
|
+ if (!set.contains(key)) {
|
|
|
+ adds.add(s);
|
|
|
+ } else {
|
|
|
+ set.add(key);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (CollectionUtils.isNotEmpty(adds)) {
|
|
|
+ saveBatch(adds);
|
|
|
+ }
|
|
|
+ return adds.size();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|