From a7f8ecbb90bf738f4e29e7270c91f61cb9e85e47 Mon Sep 17 00:00:00 2001 From: zgn <1215577830@qq.com> Date: Sun, 27 Jun 2021 14:55:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=89=B9=E9=87=8F=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E7=BB=83=E4=B9=A0=E3=80=81=E8=80=83=E8=AF=95=E5=B9=B6?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zgn/fazhi/controller/ExcelController.java | 93 +++++++++-- .../listener/BeforeExaminationListener.java | 152 +++++++++++++++++- .../zgn/fazhi/listener/BeforeListener.java | 54 +++++-- .../listener/ExaminationDataListener.java | 79 ++++++++- .../com/zgn/fazhi/pojo/po/Examination.java | 7 +- .../zgn/fazhi/pojo/po/ExaminationAnswer.java | 2 +- .../service/ExaminationAnswerService.java | 10 ++ .../zgn/fazhi/service/ExaminationService.java | 9 ++ .../impl/ExaminationAnswerServiceImpl.java | 29 ++++ .../service/impl/ExaminationServiceImpl.java | 59 ++++++- 10 files changed, 456 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/zgn/fazhi/controller/ExcelController.java b/src/main/java/com/zgn/fazhi/controller/ExcelController.java index 5975efe..86e9277 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 3aa187f..a8ad400 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 74a35e6..9d7b2e1 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 4c75b52..1c81048 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 6d8624d..52a0ec0 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,10 +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("考试题目") + private String title; + } diff --git a/src/main/java/com/zgn/fazhi/pojo/po/ExaminationAnswer.java b/src/main/java/com/zgn/fazhi/pojo/po/ExaminationAnswer.java index 2bb4c93..afff93a 100644 --- a/src/main/java/com/zgn/fazhi/pojo/po/ExaminationAnswer.java +++ b/src/main/java/com/zgn/fazhi/pojo/po/ExaminationAnswer.java @@ -29,7 +29,7 @@ public class ExaminationAnswer implements Serializable { private Integer id; @ApiModelProperty(value = "选项内容") - private Integer option; + private String answerOption; @ApiModelProperty(value = "考题id") private Integer eid; diff --git a/src/main/java/com/zgn/fazhi/service/ExaminationAnswerService.java b/src/main/java/com/zgn/fazhi/service/ExaminationAnswerService.java index 1f1d1a4..640bcce 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 1df5556..c891856 100644 --- a/src/main/java/com/zgn/fazhi/service/ExaminationService.java +++ b/src/main/java/com/zgn/fazhi/service/ExaminationService.java @@ -3,6 +3,9 @@ package com.zgn.fazhi.service; import com.zgn.fazhi.pojo.po.Examination; import com.baomidou.mybatisplus.extension.service.IService; +import java.util.List; +import java.util.Map; + /** *

* 服务类 @@ -13,4 +16,10 @@ import com.baomidou.mybatisplus.extension.service.IService; */ public interface ExaminationService extends IService { + /** + * 批量插入考题 + * @param list 考题数组 + */ + void addExaminationList(List> list); + } 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 0a61597..a5b2d54 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 e031eaf..de8aaf6 100644 --- a/src/main/java/com/zgn/fazhi/service/impl/ExaminationServiceImpl.java +++ b/src/main/java/com/zgn/fazhi/service/impl/ExaminationServiceImpl.java @@ -2,19 +2,66 @@ package com.zgn.fazhi.service.impl; import com.zgn.fazhi.pojo.po.Examination; import com.zgn.fazhi.mapper.ExaminationMapper; +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 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; /** - *

- * 服务实现类 - *

- * - * @author wt - * @since 2021-06-25 + * 考题 + * @author ydw */ @Service public class ExaminationServiceImpl extends ServiceImpl implements ExaminationService { + private static final Logger LOGGER = LoggerFactory.getLogger(ExaminationServiceImpl.class); + + @Autowired + private ExaminationMapper examinationMapper; + + @Autowired + private ExaminationAnswerService examinationAnswerService; + + /** + * 批量插入考题 + * @param list 考题数组 + */ + @Override + @Transactional(rollbackFor = Exception.class) + public void addExaminationList(List> 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); + + } + + } + } -- Gitee