|
@@ -0,0 +1,222 @@
|
|
|
+package cn.com.qmth.scancloud.tools.service.impl;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+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.scancloud.tools.config.SysProperty;
|
|
|
+import cn.com.qmth.scancloud.tools.enums.TaskType;
|
|
|
+import cn.com.qmth.scancloud.tools.model.Struct;
|
|
|
+import cn.com.qmth.scancloud.tools.service.AbstractTask;
|
|
|
+import cn.com.qmth.scancloud.tools.service.CommonService;
|
|
|
+import cn.com.qmth.scancloud.tools.utils.HttpHelper;
|
|
|
+import cn.com.qmth.scancloud.tools.utils.JsonHelper;
|
|
|
+import cn.com.qmth.scancloud.tools.utils.StatusException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 缺考导入
|
|
|
+ */
|
|
|
+public class StructImportTask extends AbstractTask {
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(StructImportTask.class);
|
|
|
+
|
|
|
+ private static final String[] EXCEL_HEADER = new String[] { "科目代码", "条形码值", "是否客观题", "大题名称", "大题号", "小题号", "小题满分" };
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected String getTaskName() {
|
|
|
+ return TaskType.STRUCT_IMPORT.getTitle();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void execute() {
|
|
|
+ Long examId = SysProperty.EXAM_ID;
|
|
|
+ if (examId == null) {
|
|
|
+ throw new StatusException("【scan.tool.examId】未配置!");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (CommonService.findExam(examId) == null) {
|
|
|
+ throw new StatusException("当前考试不存在!examId = " + examId);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Struct> structs = readData(examId);
|
|
|
+ String url = SysProperty.SCAN_SERVER_URL + "/api/tool/import/cet/absent";// TODO
|
|
|
+ String json = JsonHelper.toJson(structs);
|
|
|
+ HttpHelper.post(url, json);
|
|
|
+ log.info("处理完毕");
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<Struct> readData(Long examId) {
|
|
|
+ if (StringUtils.isBlank(SysProperty.STRUCT_IMPORT)) {
|
|
|
+ throw new StatusException("【scan.tool.struct-import】未配置!");
|
|
|
+ }
|
|
|
+ File file = new File(SysProperty.DATA_DIR + "/" + SysProperty.STRUCT_IMPORT);
|
|
|
+ if (!file.exists()) {
|
|
|
+ throw new StatusException("文件不存在:" + SysProperty.STRUCT_IMPORT);
|
|
|
+ }
|
|
|
+ InputStream inputStream = null;
|
|
|
+ try {
|
|
|
+ inputStream = new FileInputStream(file);
|
|
|
+ ExcelReader reader = ExcelReader.create(ExcelType.XLSX, inputStream, 0);
|
|
|
+ List<DataMap> lineList = reader.getDataMapList();
|
|
|
+ if (!Arrays.equals(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<Struct> ret = new ArrayList<>();
|
|
|
+ for (int i = 0; i < lineList.size(); i++) {
|
|
|
+ DataMap line = lineList.get(i);
|
|
|
+
|
|
|
+ StringBuilder msg = new StringBuilder();
|
|
|
+
|
|
|
+ Struct imp = new Struct();
|
|
|
+ imp.setExamId(examId);
|
|
|
+ String code = trimAndNullIfBlank(line.getValue(0));
|
|
|
+ if (StringUtils.isBlank(code)) {
|
|
|
+ msg.append(" 科目代码不能为空");
|
|
|
+ } else if (code.length() > 50) {
|
|
|
+ msg.append(" 科目代码不能超过50个字符");
|
|
|
+ }
|
|
|
+ imp.setSubjectCode(code);
|
|
|
+
|
|
|
+ String paperType = trimAndNullIfBlank(line.getValue(1));
|
|
|
+ if (StringUtils.isBlank(paperType)) {
|
|
|
+ msg.append(" 条形码值不能为空");
|
|
|
+ } else if (paperType.length() > 50) {
|
|
|
+ msg.append(" 条形码值不能超过50个字符");
|
|
|
+ }
|
|
|
+ imp.setPaperType(paperType);
|
|
|
+
|
|
|
+ String objective = trimAndNullIfBlank(line.getValue(2));
|
|
|
+ if (StringUtils.isBlank(objective)) {
|
|
|
+ msg.append(" 是否客观题不能为空");
|
|
|
+ } else {
|
|
|
+ if (!"是".equals(objective) && !"否".equals(objective)) {
|
|
|
+ msg.append(" 是否客观题填写错误");
|
|
|
+ } else {
|
|
|
+ if ("是".equals(objective)) {
|
|
|
+ imp.setObjective(true);
|
|
|
+ } else {
|
|
|
+ imp.setObjective(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String detailName = trimAndNullIfBlank(line.getValue(3));
|
|
|
+ if (StringUtils.isBlank(detailName)) {
|
|
|
+ msg.append(" 大题名称不能为空");
|
|
|
+ } else if (detailName.length() > 50) {
|
|
|
+ msg.append(" 大题名称不能超过50个字符");
|
|
|
+ }
|
|
|
+ imp.setMainTitle(detailName);
|
|
|
+
|
|
|
+ String detailNumber = trimAndNullIfBlank(line.getValue(4));
|
|
|
+ if (StringUtils.isBlank(detailNumber)) {
|
|
|
+ msg.append(" 大题号不能为空");
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ int n = Integer.valueOf(detailNumber);
|
|
|
+ if (n <= 0) {
|
|
|
+ msg.append(" 大题号不能小于0");
|
|
|
+ } else {
|
|
|
+ imp.setMainNumber(n);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ msg.append(" 大题号只能是整数");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ String unitNumber = trimAndNullIfBlank(line.getValue(5));
|
|
|
+ if (StringUtils.isBlank(unitNumber)) {
|
|
|
+ msg.append(" 小题号不能为空");
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ int n = Integer.valueOf(unitNumber);
|
|
|
+ if (n <= 0) {
|
|
|
+ msg.append(" 小题号不能小于0");
|
|
|
+ } else {
|
|
|
+ imp.setSubNumber(n + "");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ msg.append(" 小题号只能是整数");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ String score = trimAndNullIfBlank(line.getValue(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.setTotalScore(n);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ msg.append(" 小题满分格式错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (msg.length() > 0) {
|
|
|
+ failRecords.add(newError(i + 1, msg.toString()));
|
|
|
+ } else {
|
|
|
+ ret.add(imp);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ if (CollectionUtils.isNotEmpty(failRecords)) {
|
|
|
+ for (String err : failRecords) {
|
|
|
+ log.error(err);
|
|
|
+ }
|
|
|
+ throw new StatusException("请检查填写信息");
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+ } 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;
|
|
|
+ }
|
|
|
+}
|