diff --git a/src/main/java/com/zgn/fazhi/controller/ExcelController.java b/src/main/java/com/zgn/fazhi/controller/ExcelController.java index 5975efe8654e42edbbe15883c50d44a4e20957b7..86e927727115b60978718284ddbe1b723107d558 100644 --- a/src/main/java/com/zgn/fazhi/controller/ExcelController.java +++ b/src/main/java/com/zgn/fazhi/controller/ExcelController.java @@ -2,12 +2,16 @@ package com.zgn.fazhi.controller; import com.alibaba.excel.EasyExcel; import com.zgn.fazhi.factory.ResultFactory; +import com.zgn.fazhi.listener.BeforeExaminationListener; import com.zgn.fazhi.listener.BeforeListener; +import com.zgn.fazhi.listener.ExaminationDataListener; import com.zgn.fazhi.listener.PracticeDataListener; import com.zgn.fazhi.mapper.PracticeAnswerMapper; import com.zgn.fazhi.mapper.PracticeMapper; +import com.zgn.fazhi.pojo.po.Examination; import com.zgn.fazhi.pojo.po.SubjectType; import com.zgn.fazhi.result.Result; +import com.zgn.fazhi.service.ExaminationService; import com.zgn.fazhi.service.GradeService; import com.zgn.fazhi.service.PracticeService; import com.zgn.fazhi.service.SubjectTypeService; @@ -16,7 +20,11 @@ import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.RedisConnectionFailureException; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RestController; @@ -26,11 +34,15 @@ import java.io.IOException; /** * 导入导出Excel + * * @author ydw */ @RestController public class ExcelController { + private static final Logger LOGGER = LoggerFactory.getLogger(ExcelController.class); + + @Autowired private RedisUtil redisUtil; @@ -43,38 +55,91 @@ public class ExcelController { @Autowired private PracticeService practiceService; + @Autowired + private ExaminationService examinationService; + @Autowired + private RedisTemplate redisTemplate; - @PostMapping(value = "/upload", consumes = "multipart/*", headers = "content-type=multipart/form-data") - @ApiOperation("上传文件") + + @PostMapping(value = "/api/uploadPractice", consumes = "multipart/*", headers = "content-type=multipart/form-data") + @ApiOperation("上传练习题excel") @ApiImplicitParams({@ApiImplicitParam(paramType = "form", dataType = "_file", name = "上传文件", required = true)}) - public Result file(@ApiParam(value = "上传文件", required = true) @RequestPart("file") MultipartFile files) throws IOException { - if (files.isEmpty()){ + public Result practiceFile(@ApiParam(value = "上传练习题", required = true) @RequestPart("file") MultipartFile files) throws IOException { + + + // 判断文件是否为空 + if (files.isEmpty()) { System.out.println("文件为空"); return ResultFactory.buildFailResult("文件为空"); - }else { - -// EasyExcel.read(files.getInputStream(), new NoModelDataListener()).headRowNumber(1).sheet().doRead(); - EasyExcel.read(files.getInputStream(),new BeforeListener(gradeService, subjectTypeService, redisUtil)).headRowNumber(1).sheet().doRead(); - if (redisUtil.lGetListSize("picture") != 0){ + } else { + try { + System.out.println("执行redis"); + redisTemplate.opsForValue().get("skr"); + } catch (RedisConnectionFailureException e) { + LOGGER.error("插入练习题出现连接异常: " + e.toString()); + return ResultFactory.buildFailResult("连接redis出现异常,请联系运维!"); + } + EasyExcel.read(files.getInputStream(), new BeforeListener(gradeService, subjectTypeService, redisUtil)).headRowNumber(1).sheet().doRead(); + if (isError()) { try { - return ResultFactory.buildResult(400,"数据有误",redisUtil.lGet("picture",0,-1)); + return ResultFactory.buildResult(402, "数据有误", redisUtil.lGet("testList", 0, -1)); }finally { - if (redisUtil.hasKey("picture")){ + if (redisUtil.hasKey("testList")) { System.out.println("清除Redis缓存"); - redisUtil.del("picture"); + redisUtil.del("testList"); } } } // 如果数据无误,写入数据库 EasyExcel.read(files.getInputStream(), new PracticeDataListener(practiceService)).headRowNumber(1).sheet().doRead(); + System.out.println("文件不为空"); + return ResultFactory.buildSuccessResult("数据无误,导入成功"); + + + } + + } + + @ApiOperation("上传考试试题excel") + @PostMapping(value = "/api/uploadExamination", consumes = "multipart/*", headers = "content-type=multipart/form-data") + @ApiImplicitParams({@ApiImplicitParam(paramType = "form", dataType = "_file", name = "上传文件", required = true)}) + public Result examinationFile(@ApiParam(value = "上传考题", required = true) @RequestPart("file") MultipartFile files) throws IOException { + // 判断文件是否为空 + if (files.isEmpty()) { + System.out.println("文件为空"); + return ResultFactory.buildFailResult("文件为空"); + } else { + try { + System.out.println("执行redis"); + redisTemplate.opsForValue().get("skr"); + } catch (RedisConnectionFailureException e) { + LOGGER.error("插入练习题出现连接异常: " + e.toString()); + return ResultFactory.buildFailResult("连接redis出现异常,请联系运维!"); + } + // 校验考试数据 + EasyExcel.read(files.getInputStream(), new BeforeExaminationListener(subjectTypeService, redisUtil)).headRowNumber(1).sheet().doRead(); + if (isError()) { + try { + return ResultFactory.buildResult(402, "数据有误", redisUtil.lGet("testList", 0, -1)); + }finally { + if (redisUtil.hasKey("testList")) { + System.out.println("清除Redis缓存"); + redisUtil.del("testList"); + } + } + } + // 如果数据无误,写入数据库 + EasyExcel.read(files.getInputStream(), new ExaminationDataListener(examinationService)).headRowNumber(1).sheet().doRead(); System.out.println("文件不为空"); return ResultFactory.buildSuccessResult("数据无误,导入成功"); -// return ResultFactory.buildSuccessResult("调用接口成功"); } + } - }; + private boolean isError() { + return redisUtil.lGetListSize("testList") != 0; + } } diff --git a/src/main/java/com/zgn/fazhi/listener/BeforeExaminationListener.java b/src/main/java/com/zgn/fazhi/listener/BeforeExaminationListener.java index 3aa187fa0393c51a5607a5bf31e8065197fc2381..a8ad40091fe0bc364298ace1cfbf5073dfcd976b 100644 --- a/src/main/java/com/zgn/fazhi/listener/BeforeExaminationListener.java +++ b/src/main/java/com/zgn/fazhi/listener/BeforeExaminationListener.java @@ -1,4 +1,154 @@ package com.zgn.fazhi.listener; -public class BeforeExaminationListener { +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.event.AnalysisEventListener; +import com.zgn.fazhi.pojo.po.Grade; +import com.zgn.fazhi.pojo.po.SubjectType; +import com.zgn.fazhi.service.GradeService; +import com.zgn.fazhi.service.SubjectTypeService; +import com.zgn.fazhi.utils.RedisUtil; +import lombok.SneakyThrows; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.util.StringUtils; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 检测excel表格是否正确 + * + * @author ydw + */ + +public class BeforeExaminationListener extends AnalysisEventListener> { + + private SubjectTypeService subjectTypeService; + + private RedisUtil redisUtil; + + private static final Logger LOGGER = LoggerFactory.getLogger(BeforeExaminationListener.class); + /** + * 每隔10条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 + */ + private static final int BATCH_COUNT = 10; + Map headMap = new HashMap<>(); + List> list = new ArrayList>(); + Integer pdid; + + /** + * 构造方法,由于当前类无法被spring托管,所以使用注入方式注入service + * @param subjectTypeService 查询题型service + * @param redisUtil redis工具类 + */ + public BeforeExaminationListener(SubjectTypeService subjectTypeService, RedisUtil redisUtil) { + this.subjectTypeService = subjectTypeService; + this.redisUtil = redisUtil; + } + + + /** + * 检查每一条数据的合法性 + */ + @SneakyThrows + @Override + public void invoke(Map data, AnalysisContext context) { + LOGGER.info("解析到一条数据:{}", data); + // 每5条记录校验一次合法性 + if (list.size() >= BATCH_COUNT) { + checkList(list); + // 存储完成清理 list + list.clear(); + } + list.add(data); + + } + + @SneakyThrows + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + checkList(list); + System.out.println("打印头" + headMap); + LOGGER.info("所有的数据存储完毕!" + list.size()); +// List objects = new ArrayList<>(); + + } + + /** + * 检查数据合法性 + * @param list 数据数组 + */ + private void checkList(List> list) { + System.out.println("校验中 : "+ list); + // 从数据库中查询所有的题型和等级,校验是否合法 + List subjectTypes = subjectTypeService.getAllSubjectType();; + for (SubjectType subjectType : subjectTypes) { + if ("判断题".equals(subjectType.getName())){ + pdid = subjectType.getId(); + } + } + // 遍历该5条数据 + out:for (Map map : list) { + + int isNull = 0; + + nullOut: + for (int i = 0; i < map.size(); i++) { + if (map.get(i) == null) { + isNull = 1; + continue nullOut; + } + } + // 如果map中为null则校验异常 + if (isNull == 1){ + redisUtil.lSet("testList", map); + LOGGER.info("缓存已有: " + redisUtil.lGet("testList", 0, -1)); + continue out; + } + + // 如果考题为判断题且长度大于6则不合法 + if (Integer.valueOf(map.get(1)).equals(pdid)){ + if (map.size()>6){ + redisUtil.lSet("testList", map); + LOGGER.info("缓存已有: " + redisUtil.lGet("testList", 0, -1)); + continue out; + } + } + + // 判断是否是数据库已有题型 + int isType = 0; + for (SubjectType subjectType : subjectTypes) { + if (!(StringUtils.isEmpty(subjectType) && map.isEmpty())) { + if (subjectType.getId().toString().equals(map.get(1))) { + // 如果在题型范围内 + isType = 1; + } + } + } + if (isType == 0){ + // 如果不在题型范围内,将该数据放入redis缓存中,用list + string + LOGGER.info(map.toString()); + redisUtil.lSet("testList", map); + LOGGER.info("缓存已有: " + redisUtil.lGet("testList", 0, -1)); + continue out; + } + } + + + } + + /** + * 读取表头数据 + * 这里会一行行的返回头 + * 监听器只需要重写这个方法就可以读取到头信息 + * + * @param headMap 表头map + */ + @Override + public void invokeHeadMap(Map headMap, AnalysisContext context) { + this.headMap = headMap; + } + } diff --git a/src/main/java/com/zgn/fazhi/listener/BeforeListener.java b/src/main/java/com/zgn/fazhi/listener/BeforeListener.java index 74a35e671294e7aad396dbb0dcb13b3e5b840a0f..9d7b2e1b6716841fd21c5923db9ead9113097481 100644 --- a/src/main/java/com/zgn/fazhi/listener/BeforeListener.java +++ b/src/main/java/com/zgn/fazhi/listener/BeforeListener.java @@ -41,12 +41,20 @@ public class BeforeListener extends AnalysisEventListener> List> list = new ArrayList>(); Integer pdid; + /** + * 构造方法,由于当前类无法被spring托管,所以使用注入方式注入service + * + * @param gradeService 查询年级service + * @param subjectTypeService 查询题型service + * @param redisUtil redis工具类 + */ public BeforeListener(GradeService gradeService, SubjectTypeService subjectTypeService, RedisUtil redisUtil) { this.gradeService = gradeService; this.subjectTypeService = subjectTypeService; this.redisUtil = redisUtil; } + /** * 检查每一条数据的合法性 */ @@ -77,25 +85,43 @@ public class BeforeListener extends AnalysisEventListener> /** * 检查数据合法性 + * * @param list 数据数组 */ private void checkList(List> list) { - System.out.println("校验中 : "+ list); + System.out.println("校验中 : " + list); // 从数据库中查询所有的题型和等级,校验是否合法 - List subjectTypes = subjectTypeService.getAllSubjectType();; + List subjectTypes = subjectTypeService.getAllSubjectType(); List allGrade = gradeService.getAllGrade(); for (SubjectType subjectType : subjectTypes) { - if ("判断题".equals(subjectType.getName())){ + if ("判断题".equals(subjectType.getName())) { pdid = subjectType.getId(); } } // 遍历该5条数据 - out:for (Map map : list) { + out: + for (Map map : list) { + int isNull = 0; + +// nullOut: +// for (int i = 0; i < map.size(); i++) { +// if (map.get(i) == null) { +// isNull = 1; +// continue nullOut; +// } +// } +// // 如果map中为null则校验异常 +// if (isNull == 1){ +// redisUtil.lSet("testList", map); +// LOGGER.info("缓存已有: " + redisUtil.lGet("testList", 0, -1)); +// continue out; +// } + // 如果为判断题且长度大于7则不合法 - if (Integer.valueOf(map.get(1)).equals(pdid)){ - if (map.size()>7){ - redisUtil.lSet("picture", map); - LOGGER.info("缓存已有: " + redisUtil.lGet("picture", 0, -1)); + if (Integer.valueOf(map.get(1)).equals(pdid)) { + if (map.size() > 7) { + redisUtil.lSet("testList", map); + LOGGER.info("缓存已有: " + redisUtil.lGet("testList", 0, -1)); continue out; } } @@ -110,11 +136,11 @@ public class BeforeListener extends AnalysisEventListener> } } } - if (isType == 0){ + if (isType == 0) { // 如果不在题型范围内,将该数据放入redis缓存中,用list + string LOGGER.info(map.toString()); - redisUtil.lSet("picture", map); - LOGGER.info("缓存已有: " + redisUtil.lGet("picture", 0, -1)); + redisUtil.lSet("testList", map); + LOGGER.info("缓存已有: " + redisUtil.lGet("testList", 0, -1)); continue out; } isType = 0; @@ -126,11 +152,11 @@ public class BeforeListener extends AnalysisEventListener> } } } - if (isType == 0){ + if (isType == 0) { // 如果不在题型范围内,将该数据放入redis缓存中,用list + string LOGGER.info(map.toString()); - redisUtil.lSet("picture", map); - LOGGER.info("缓存已有: " + redisUtil.lGet("picture", 0, -1)); + redisUtil.lSet("testList", map); + LOGGER.info("缓存已有: " + redisUtil.lGet("testList", 0, -1)); continue out; } } diff --git a/src/main/java/com/zgn/fazhi/listener/ExaminationDataListener.java b/src/main/java/com/zgn/fazhi/listener/ExaminationDataListener.java index 4c75b52217ed6a111d4bc20ad5df205b5692dfc5..1c81048081ebf2ae55cb7015956ac8f0303fff81 100644 --- a/src/main/java/com/zgn/fazhi/listener/ExaminationDataListener.java +++ b/src/main/java/com/zgn/fazhi/listener/ExaminationDataListener.java @@ -1,4 +1,81 @@ package com.zgn.fazhi.listener; -public class ExaminationDataListener { +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.zgn.fazhi.pojo.vo.PracticeExcels; +import com.zgn.fazhi.service.ExaminationService; +import com.zgn.fazhi.service.PracticeService; +import lombok.SneakyThrows; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.event.AnalysisEventListener; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * 直接用map接收数据 + * + * @author ydw + */ +public class ExaminationDataListener extends AnalysisEventListener> { + private static final Logger LOGGER = LoggerFactory.getLogger(ExaminationDataListener.class); + + private ExaminationService examinationService; + + /** + * 每隔10条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 + */ + private static final int BATCH_COUNT = 10; + Map headMap = new HashMap<>(); + List> list = new ArrayList>(); + + @SneakyThrows + @Override + public void invoke(Map data, AnalysisContext context) { + LOGGER.info("解析到一条数据:{}", data); + // 每5条记录刷新一次 + if (list.size() >= BATCH_COUNT) { + saveData(list); + // 存储完成清理 list + list.clear(); + } + list.add(data); + } + + public ExaminationDataListener(ExaminationService examinationService) { + this.examinationService = examinationService; + } + + @SneakyThrows + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + saveData(list); + System.out.println("打印头"+headMap); + LOGGER.info("所有的数据存储完毕!"+list.size()); + + } + + /** + * 加上存储数据库 + */ + private void saveData(List> list) { + LOGGER.info("{}条数据,开始存储数据库!长度为", list.size()); + examinationService.addExaminationList(list); + } + + /** + * 读取表头数据 + * 这里会一行行的返回头 + * 监听器只需要重写这个方法就可以读取到头信息 + * + * @param headMap 表头map + */ + @Override + public void invokeHeadMap(Map headMap, AnalysisContext context) { + this.headMap = headMap; + } + } diff --git a/src/main/java/com/zgn/fazhi/pojo/po/Examination.java b/src/main/java/com/zgn/fazhi/pojo/po/Examination.java index 63446129372268c37d802d36895a368b700462ff..52a0ec0e72b43a54655f684a1103f591786207de 100644 --- a/src/main/java/com/zgn/fazhi/pojo/po/Examination.java +++ b/src/main/java/com/zgn/fazhi/pojo/po/Examination.java @@ -7,6 +7,8 @@ import java.time.LocalDateTime; import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.annotation.TableField; import java.io.Serializable; +import java.util.Date; + import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @@ -42,11 +44,13 @@ public class Examination implements Serializable { @ApiModelProperty(value = "导入时间") @TableField(fill = FieldFill.INSERT) - private LocalDateTime createTime; + private Date createTime; @ApiModelProperty(value = "随机组题时,是否会进入筛选") private Integer deleted; - @ApiModelProperty(value = "题目") + @ApiModelProperty("考试题目") private String title; + + } diff --git a/src/main/java/com/zgn/fazhi/service/ExaminationAnswerService.java b/src/main/java/com/zgn/fazhi/service/ExaminationAnswerService.java index 1f1d1a40a5be61abd7bf4e7de0c2b4cb78728d19..640bcceb77bf2ef7ff5fbe4b970dc24b95088d66 100644 --- a/src/main/java/com/zgn/fazhi/service/ExaminationAnswerService.java +++ b/src/main/java/com/zgn/fazhi/service/ExaminationAnswerService.java @@ -3,6 +3,9 @@ package com.zgn.fazhi.service; import com.zgn.fazhi.pojo.po.ExaminationAnswer; import com.baomidou.mybatisplus.extension.service.IService; +import java.util.List; +import java.util.Map; + /** *

* 服务类 @@ -13,4 +16,11 @@ import com.baomidou.mybatisplus.extension.service.IService; */ public interface ExaminationAnswerService extends IService { + /** + * 通过id批量插入答案 + * @param map 考题答案数据 + * @param id 考题id + */ + void addAnswerListById(Map map, int id); + } diff --git a/src/main/java/com/zgn/fazhi/service/ExaminationService.java b/src/main/java/com/zgn/fazhi/service/ExaminationService.java index 2dd005f8a4c31b22ca3f2259dad6262e1557e443..c33aa17ed2d610821a40e3953ed734373eb6284d 100644 --- a/src/main/java/com/zgn/fazhi/service/ExaminationService.java +++ b/src/main/java/com/zgn/fazhi/service/ExaminationService.java @@ -7,6 +7,9 @@ import com.zgn.fazhi.result.Result; import java.io.Serializable; import java.util.List; +import java.util.List; +import java.util.Map; + /** *

* 服务类 @@ -17,6 +20,12 @@ import java.util.List; */ public interface ExaminationService extends IService { + /** + * 批量插入考题 + * @param list 考题数组 + */ + void addExaminationList(List> list); + List selectByType(int id); List selectAllPaper(); diff --git a/src/main/java/com/zgn/fazhi/service/impl/ExaminationAnswerServiceImpl.java b/src/main/java/com/zgn/fazhi/service/impl/ExaminationAnswerServiceImpl.java index 0a61597844085e06e0edb6a1ccabd65eebafd42d..a5b2d54d1427d0d252f6e11dc7f8dbfe15938fe8 100644 --- a/src/main/java/com/zgn/fazhi/service/impl/ExaminationAnswerServiceImpl.java +++ b/src/main/java/com/zgn/fazhi/service/impl/ExaminationAnswerServiceImpl.java @@ -2,10 +2,17 @@ package com.zgn.fazhi.service.impl; import com.zgn.fazhi.pojo.po.ExaminationAnswer; import com.zgn.fazhi.mapper.ExaminationAnswerMapper; +import com.zgn.fazhi.pojo.po.PracticeAnswer; import com.zgn.fazhi.service.ExaminationAnswerService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.List; +import java.util.Map; + /** *

* 服务实现类 @@ -17,4 +24,26 @@ import org.springframework.stereotype.Service; @Service public class ExaminationAnswerServiceImpl extends ServiceImpl implements ExaminationAnswerService { + private static final Logger LOGGER = LoggerFactory.getLogger(ExaminationAnswerServiceImpl.class); + + @Autowired + private ExaminationAnswerMapper examinationAnswerMapper; + + /** + * 通过id批量插入答案 + * @param map 考题答案数据 + * @param id 考题id + */ + @Override + public void addAnswerListById(Map map, int id) { + LOGGER.info("开始插入答案 id = " + id); + for (int i = 4; i < map.size(); i++) { + ExaminationAnswer examinationAnswer = new ExaminationAnswer(); + examinationAnswer.setEid(id); + examinationAnswer.setAnswerOption(map.get(i)); + examinationAnswerMapper.insert(examinationAnswer); + LOGGER.info("完成插入答案 : " + examinationAnswer); + } + } + } diff --git a/src/main/java/com/zgn/fazhi/service/impl/ExaminationServiceImpl.java b/src/main/java/com/zgn/fazhi/service/impl/ExaminationServiceImpl.java index 66d8a0fb9864db55afebf40ce1dd5c3877a44ace..5eae28044b5250b28ea67d2c2bbb51aeed542f45 100644 --- a/src/main/java/com/zgn/fazhi/service/impl/ExaminationServiceImpl.java +++ b/src/main/java/com/zgn/fazhi/service/impl/ExaminationServiceImpl.java @@ -4,27 +4,40 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.zgn.fazhi.factory.ResultFactory; import com.zgn.fazhi.pojo.po.Examination; import com.zgn.fazhi.mapper.ExaminationMapper; -import com.zgn.fazhi.result.Result; +import com.zgn.fazhi.pojo.po.Practice; +import com.zgn.fazhi.service.ExaminationAnswerService; import com.zgn.fazhi.service.ExaminationService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import com.zgn.fazhi.result.Result; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Date; +import java.util.List; +import java.util.Map; import java.util.List; /** - *

- * 服务实现类 - *

- * - * @author wt - * @since 2021-06-25 + * 考题 + * @author ydw */ @Service public class ExaminationServiceImpl extends ServiceImpl implements ExaminationService { @Autowired ExaminationMapper examinationMapper; + private static final Logger LOGGER = LoggerFactory.getLogger(ExaminationServiceImpl.class); + + @Autowired + private ExaminationMapper examinationMapper; + + @Autowired + private ExaminationAnswerService examinationAnswerService; + //根据类型查找试卷 @Override public List selectByType(int id) { @@ -61,6 +74,39 @@ public class ExaminationServiceImpl extends ServiceImpl> list) { + LOGGER.info("开始导入数据库,size = " + list.size()); + for (Map map : list) { + Examination examination = new Examination(); + // 题目 + examination.setTitle(map.get(0)); + // 题型 + examination.setTypeId(Integer.valueOf(map.get(1))); + // 标准答案 + examination.setAnswer(map.get(2)); + // 答案解析 + examination.setAnalysis(map.get(3)); + // 插入日期 + examination.setCreateTime(new Date()); + LOGGER.info("封装完成一个练习题 : " + examination); + + // 封装后返回主键id + examinationMapper.insert(examination); + int id = examination.getId(); + + // 通过题目id插入答案 + examinationAnswerService.addAnswerListById(map,id); + + } + + } + //根据Id修改试卷 @Override public Result updatePaperById(Examination examination) {