浏览代码

加入期末成绩模版导入

wangliang 1 年之前
父节点
当前提交
487798c5de

+ 12 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/entity/TCScoreNormal.java

@@ -71,6 +71,18 @@ public class TCScoreNormal implements Serializable {
 
     }
 
+    public TCScoreNormal(Long examId, String courseCode, String courseName, String paperNumber, String paperType, Long userId) {
+        this.id = SystemConstant.getDbUuid();
+        this.examId = examId;
+        this.courseCode = courseCode;
+        this.courseName = courseName;
+        this.paperNumber = paperNumber;
+        this.paperType = paperType;
+        this.enable = true;
+        this.createId = userId;
+        this.createTime = System.currentTimeMillis();
+    }
+
     public TCScoreNormal(Long examId, String courseCode, String courseName, String paperNumber, String paperType, String name, String examNumber, String scoreNormal, Long userId) {
         this.id = SystemConstant.getDbUuid();
         this.examId = examId;

+ 4 - 2
distributed-print/install/mysql/upgrade/3.3.1.sql

@@ -150,7 +150,8 @@ CREATE TABLE `t_c_score_end_exam` (
                                       `create_time` bigint DEFAULT NULL COMMENT '创建时间',
                                       `update_id` bigint DEFAULT NULL COMMENT '更新人id',
                                       `update_time` bigint DEFAULT NULL COMMENT '更新时间',
-                                      PRIMARY KEY (`id`)
+                                      PRIMARY KEY (`id`),
+                                      UNIQUE KEY `t_c_score_end_exam_unique` (`exam_id`,`course_code`,`course_name`,`paper_number`,`paper_type`,`name`,`exam_number`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='期末考试成绩表';
 
 DROP TABLE IF EXISTS `t_c_score_normal`;
@@ -169,7 +170,8 @@ CREATE TABLE `t_c_score_normal` (
                                     `create_time` bigint DEFAULT NULL COMMENT '创建时间',
                                     `update_id` bigint DEFAULT NULL COMMENT '更新人id',
                                     `update_time` bigint DEFAULT NULL COMMENT '更新时间',
-                                    PRIMARY KEY (`id`)
+                                    PRIMARY KEY (`id`),
+                                    UNIQUE KEY `t_c_score_normal_unique` (`exam_id`,`course_code`,`course_name`,`paper_number`,`paper_type`,`name`,`exam_number`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='平常作业成绩表';
 
 DROP TABLE IF EXISTS `t_r_basic_info`;

+ 74 - 6
distributed-print/src/main/java/com/qmth/distributed/print/api/TCScoreNormalController.java

@@ -1,15 +1,21 @@
 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.baomidou.mybatisplus.extension.plugins.pagination.Page;
 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.result.EditResult;
 import com.qmth.distributed.print.business.entity.TCScoreNormal;
 import com.qmth.distributed.print.business.service.TCScoreNormalService;
 import com.qmth.teachcloud.common.annotation.OperationLogDetail;
 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.util.JacksonUtil;
 import com.qmth.teachcloud.common.util.ResultUtil;
@@ -20,7 +26,9 @@ import io.swagger.annotations.*;
 import org.apache.commons.codec.digest.DigestUtils;
 import org.slf4j.Logger;
 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.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
@@ -105,30 +113,90 @@ public class TCScoreNormalController {
         MarkPaper markPaper = markPaperService.getByExamIdAndPaperNumberAndPaperType(examId, paperNumber, paperType);
         Objects.requireNonNull(markPaper, "未找到科目信息");
 
-        List<Map<String, String>> list = null;
+        Map<String, String> messageMap = new LinkedHashMap<>();
         try {
             StringJoiner errorData = new StringJoiner("");
-            list = EasyExcel.read(file.getInputStream()).headRowNumber(1).sheet(0).doReadSync();
+            StringJoiner successData = new StringJoiner("");
+            List<Map<String, String>> list = EasyExcel.read(file.getInputStream()).headRowNumber(1).sheet(0).doReadSync();
+
+            SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
             List<TCScoreNormal> tcScoreNormalList = new ArrayList<>(list.size());
             log.info("list:{}", JacksonUtil.parseJson(list));
             Map<String, String> headMap = list.get(0);
             log.info("headMap:{}", JacksonUtil.parseJson(headMap));
+            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表头第二行为姓名");
+                }
+            }
+
             for (int i = 1; i < list.size(); i++) {
                 Map<String, String> objectMap = list.get(i);
+                boolean error = false;
+                JSONArray jsonArray = new JSONArray();
+                TCScoreNormal tcScoreNormal = new TCScoreNormal(examId, courseCode, markPaper.getCourseName(), paperNumber, paperType, sysUser.getId());
                 for (Map.Entry<String, String> entry : objectMap.entrySet()) {
-                    if (Objects.isNull(entry) || Objects.equals(entry, "")) {
-                        errorData.add("第" + i + "行").add(headMap.get(entry.getKey()) + "为空\n");
+                    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(), "学号")) {
+                            tcScoreNormal.setExamNumber(entry.getValue());
+                        } else if (Objects.equals(head.trim(), "姓名")) {
+                            tcScoreNormal.setName(entry.getValue());
+                        } else {
+                            jsonObject.put("name", headMap.get(entry.getKey()));
+                            jsonObject.put("score", entry.getValue());
+                            jsonArray.add(jsonObject);
+                        }
                     }
                 }
+                if (jsonArray.size() > 0) {
+                    tcScoreNormal.setScoreNormal(jsonArray.toJSONString());
+                }
+                if (!error) {
+                    tcScoreNormalList.add(tcScoreNormal);
+                }
                 log.info("objectMap:{}", JacksonUtil.parseJson(objectMap));
+                log.info("jsonArray:{}", JacksonUtil.parseJson(jsonArray));
+            }
+            if (!CollectionUtils.isEmpty(tcScoreNormalList)) {
+                successData.add("共导入" + tcScoreNormalList.size() + "条数据");
+
+                QueryWrapper<TCScoreNormal> tcScoreNormalQueryWrapper = new QueryWrapper<>();
+                tcScoreNormalQueryWrapper.lambda().eq(TCScoreNormal::getExamId, examId)
+                        .eq(TCScoreNormal::getCourseCode, courseCode)
+                        .eq(TCScoreNormal::getCourseName, markPaper.getCourseName())
+                        .eq(TCScoreNormal::getPaperNumber, paperNumber)
+                        .eq(TCScoreNormal::getPaperType, paperType);
+                tcScoreNormalService.remove(tcScoreNormalQueryWrapper);
+                tcScoreNormalService.saveBatch(tcScoreNormalList);
             }
+            messageMap.put("正确信息", successData.length() > 0 ? successData.toString() : "无");
+            messageMap.put("错误信息", errorData.length() > 0 ? errorData.toString() : "无");
         } catch (Exception e) {
-            e.printStackTrace();
+            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(list);
+        return ResultUtil.ok(messageMap);
     }
 
     @ApiOperation(value = "平时成绩列表")

+ 5 - 1
teachcloud-common/src/main/java/com/qmth/teachcloud/common/enums/FieldUniqueEnum.java

@@ -37,7 +37,11 @@ public enum FieldUniqueEnum {
 
     role_group_roleId_memberId_idx("角色成员数据重复,同角色其它用户已绑定"),
 
-    unique_ticket_number_idx("学校准考证号");
+    unique_ticket_number_idx("学校准考证号"),
+
+    t_c_score_normal_unique("考生"),
+
+    t_c_score_end_exam_unique("考生");
 
     private String title;