|
@@ -1,16 +1,32 @@
|
|
package com.qmth.distributed.print.api;
|
|
package com.qmth.distributed.print.api;
|
|
|
|
|
|
|
|
+import com.alibaba.excel.EasyExcel;
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.qmth.boot.api.constant.ApiConstant;
|
|
import com.qmth.boot.api.constant.ApiConstant;
|
|
|
|
+import com.qmth.boot.api.exception.ApiException;
|
|
import com.qmth.distributed.print.business.bean.excel.ExcelField;
|
|
import com.qmth.distributed.print.business.bean.excel.ExcelField;
|
|
import com.qmth.distributed.print.business.bean.result.EditResult;
|
|
import com.qmth.distributed.print.business.bean.result.EditResult;
|
|
|
|
+import com.qmth.distributed.print.business.entity.TCScoreEndExam;
|
|
|
|
+import com.qmth.distributed.print.business.service.TCScoreEndExamService;
|
|
import com.qmth.distributed.print.business.service.TCScoreNormalService;
|
|
import com.qmth.distributed.print.business.service.TCScoreNormalService;
|
|
import com.qmth.teachcloud.common.annotation.OperationLogDetail;
|
|
import com.qmth.teachcloud.common.annotation.OperationLogDetail;
|
|
import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
|
|
+import com.qmth.teachcloud.common.entity.SysUser;
|
|
|
|
+import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
|
|
|
|
+import com.qmth.teachcloud.common.enums.FieldUniqueEnum;
|
|
import com.qmth.teachcloud.common.enums.log.CustomizedOperationTypeEnum;
|
|
import com.qmth.teachcloud.common.enums.log.CustomizedOperationTypeEnum;
|
|
|
|
+import com.qmth.teachcloud.common.util.JacksonUtil;
|
|
import com.qmth.teachcloud.common.util.ResultUtil;
|
|
import com.qmth.teachcloud.common.util.ResultUtil;
|
|
|
|
+import com.qmth.teachcloud.common.util.ServletUtil;
|
|
|
|
+import com.qmth.teachcloud.mark.entity.MarkPaper;
|
|
import io.swagger.annotations.*;
|
|
import io.swagger.annotations.*;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.dao.DuplicateKeyException;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
@@ -18,8 +34,7 @@ import javax.annotation.Resource;
|
|
import javax.validation.constraints.Max;
|
|
import javax.validation.constraints.Max;
|
|
import javax.validation.constraints.Min;
|
|
import javax.validation.constraints.Min;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.List;
|
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
/**
|
|
* <p>
|
|
* <p>
|
|
@@ -38,6 +53,9 @@ public class TCScoreEndExamController {
|
|
@Resource
|
|
@Resource
|
|
TCScoreNormalService tcScoreNormalService;
|
|
TCScoreNormalService tcScoreNormalService;
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
+ TCScoreEndExamService tcScoreEndExamService;
|
|
|
|
+
|
|
@ApiOperation(value = "导入期末成绩-模板下载")
|
|
@ApiOperation(value = "导入期末成绩-模板下载")
|
|
@RequestMapping(value = "/score/end_exam/template_download", method = RequestMethod.POST)
|
|
@RequestMapping(value = "/score/end_exam/template_download", method = RequestMethod.POST)
|
|
@OperationLogDetail(customizedOperationType = CustomizedOperationTypeEnum.EXPORT)
|
|
@OperationLogDetail(customizedOperationType = CustomizedOperationTypeEnum.EXPORT)
|
|
@@ -62,12 +80,102 @@ public class TCScoreEndExamController {
|
|
@RequestMapping(value = "/score/end_exam/import", method = RequestMethod.POST)
|
|
@RequestMapping(value = "/score/end_exam/import", method = RequestMethod.POST)
|
|
@OperationLogDetail(customizedOperationType = CustomizedOperationTypeEnum.IMPORT)
|
|
@OperationLogDetail(customizedOperationType = CustomizedOperationTypeEnum.IMPORT)
|
|
@ApiResponses({@ApiResponse(code = 200, message = "导入成功", response = EditResult.class)})
|
|
@ApiResponses({@ApiResponse(code = 200, message = "导入成功", response = EditResult.class)})
|
|
|
|
+ @Transactional
|
|
public Object scoreEndExamImport(@ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file,
|
|
public Object scoreEndExamImport(@ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file,
|
|
@ApiParam(value = "考试id", required = true) @RequestParam Long examId,
|
|
@ApiParam(value = "考试id", required = true) @RequestParam Long examId,
|
|
@ApiParam(value = "科目编码", required = true) @RequestParam String courseCode,
|
|
@ApiParam(value = "科目编码", required = true) @RequestParam String courseCode,
|
|
@ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber,
|
|
@ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber,
|
|
@ApiParam(value = "试卷类型", required = true) @RequestParam String paperType) throws IOException {
|
|
@ApiParam(value = "试卷类型", required = true) @RequestParam String paperType) throws IOException {
|
|
- return ResultUtil.ok(true);
|
|
|
|
|
|
+ log.debug("导入Excel开始...");
|
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
|
+ MarkPaper markPaper = tcScoreNormalService.importExcelVaild(file, examId, paperNumber, paperType);
|
|
|
|
+
|
|
|
|
+ Map<String, String> messageMap = new LinkedHashMap<>();
|
|
|
|
+ try {
|
|
|
|
+ StringJoiner errorData = new StringJoiner("");
|
|
|
|
+ StringJoiner successData = new StringJoiner("");
|
|
|
|
+ List<Map<String, String>> list = EasyExcel.read(file.getInputStream()).headRowNumber(1).sheet(0).doReadSync();
|
|
|
|
+ log.info("list:{}", JacksonUtil.parseJson(list));
|
|
|
|
+
|
|
|
|
+ SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
|
|
|
|
+ List<TCScoreEndExam> tcScoreEndExamList = new ArrayList<>(list.size());
|
|
|
|
+ Map<String, String> headMap = list.get(0);
|
|
|
|
+ for (Map.Entry<String, String> entry : headMap.entrySet()) {
|
|
|
|
+ if (Objects.equals(entry.getKey(), 0) && Objects.nonNull(entry.getValue()) &&
|
|
|
|
+ !Objects.equals(entry.getValue().trim(), "学号")) {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("excel表头第一行为学号");
|
|
|
|
+ } else if (Objects.equals(entry.getKey(), 1) && Objects.nonNull(entry.getValue()) &&
|
|
|
|
+ !Objects.equals(entry.getValue().trim(), "姓名")) {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("excel表头第二行为姓名");
|
|
|
|
+ } else if (Objects.equals(entry.getKey(), 2) && Objects.nonNull(entry.getValue()) &&
|
|
|
|
+ !Objects.equals(entry.getValue().trim(), "成绩")) {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("excel表头第三行为成绩");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (int i = 1; i < list.size(); i++) {
|
|
|
|
+ Map<String, String> objectMap = list.get(i);
|
|
|
|
+ boolean error = false;
|
|
|
|
+ JSONArray jsonArray = new JSONArray();
|
|
|
|
+ TCScoreEndExam tcScoreEndExam = new TCScoreEndExam(examId, courseCode, markPaper.getCourseName(), paperNumber, paperType, sysUser.getId());
|
|
|
|
+ for (Map.Entry<String, String> entry : objectMap.entrySet()) {
|
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
|
+ if (Objects.isNull(entry.getValue()) || Objects.equals(entry.getValue().trim(), "")) {
|
|
|
|
+ errorData.add("excel第" + i + "行[").add(headMap.get(entry.getKey()) + "]为空;").add("\r\n");
|
|
|
|
+ error = true;
|
|
|
|
+ } else {
|
|
|
|
+ String head = headMap.get(entry.getKey());
|
|
|
|
+ Objects.requireNonNull(head, "表头信息异常");
|
|
|
|
+ if (Objects.equals(head.trim(), "学号")) {
|
|
|
|
+ tcScoreEndExam.setExamNumber(entry.getValue());
|
|
|
|
+ } else if (Objects.equals(head.trim(), "姓名")) {
|
|
|
|
+ tcScoreEndExam.setName(entry.getValue());
|
|
|
|
+ } else if (Objects.equals(head.trim(), "成绩")) {
|
|
|
|
+ tcScoreEndExam.setScore(Double.valueOf(entry.getValue()));
|
|
|
|
+ } else {
|
|
|
|
+ jsonObject.put("name", headMap.get(entry.getKey()));
|
|
|
|
+ jsonObject.put("score", entry.getValue());
|
|
|
|
+ jsonArray.add(jsonObject);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (jsonArray.size() > 0) {
|
|
|
|
+ tcScoreEndExam.setScoreDetail(jsonArray.toJSONString());
|
|
|
|
+ }
|
|
|
|
+ if (!error) {
|
|
|
|
+ tcScoreEndExamList.add(tcScoreEndExam);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (!CollectionUtils.isEmpty(tcScoreEndExamList)) {
|
|
|
|
+ successData.add("共导入" + tcScoreEndExamList.size() + "条数据");
|
|
|
|
+
|
|
|
|
+ QueryWrapper<TCScoreEndExam> tcScoreEndExamQueryWrapper = new QueryWrapper<>();
|
|
|
|
+ tcScoreEndExamQueryWrapper.lambda().eq(TCScoreEndExam::getExamId, examId)
|
|
|
|
+ .eq(TCScoreEndExam::getCourseCode, courseCode)
|
|
|
|
+ .eq(TCScoreEndExam::getCourseName, markPaper.getCourseName())
|
|
|
|
+ .eq(TCScoreEndExam::getPaperNumber, paperNumber)
|
|
|
|
+ .eq(TCScoreEndExam::getPaperType, paperType);
|
|
|
|
+ tcScoreEndExamService.remove(tcScoreEndExamQueryWrapper);
|
|
|
|
+ tcScoreEndExamService.saveBatch(tcScoreEndExamList);
|
|
|
|
+ }
|
|
|
|
+ messageMap.put("正确信息", successData.length() > 0 ? successData.toString() : "无");
|
|
|
|
+ messageMap.put("错误信息", errorData.length() > 0 ? errorData.toString() : "无");
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error(SystemConstant.LOG_ERROR, e);
|
|
|
|
+ if (e instanceof DuplicateKeyException) {
|
|
|
|
+ String errorColumn = e.getCause().toString();
|
|
|
|
+ String columnStr = errorColumn.substring(errorColumn.lastIndexOf("key") + 3, errorColumn.length()).replaceAll("'", "");
|
|
|
|
+ throw ExceptionResultEnum.SQL_ERROR.exception("[" + FieldUniqueEnum.convertToTitle(columnStr) + "]数据不允许重复插入");
|
|
|
|
+ } else if (e instanceof ApiException) {
|
|
|
|
+ ResultUtil.error((ApiException) e, ((ApiException) e).getCode(), e.getMessage());
|
|
|
|
+ } else {
|
|
|
|
+ ResultUtil.error(e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ log.debug("导入Excel结束...");
|
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
|
+ log.info("============耗时{}秒============:", (end - start) / 1000);
|
|
|
|
+ return ResultUtil.ok(messageMap);
|
|
}
|
|
}
|
|
|
|
|
|
@ApiOperation(value = "期末成绩列表")
|
|
@ApiOperation(value = "期末成绩列表")
|