|
@@ -1,5 +1,10 @@
|
|
|
package com.gis.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.poi.excel.ExcelUtil;
|
|
|
+import cn.hutool.poi.excel.sax.handler.RowHandler;
|
|
|
+import com.gis.common.exception.BaseRuntimeException;
|
|
|
import com.gis.common.util.Result;
|
|
|
import com.gis.domain.po.AnswerEntity;
|
|
|
import com.gis.domain.po.QuestionEntity;
|
|
@@ -12,8 +17,10 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
@@ -34,6 +41,10 @@ public class QuestionServiceImpl extends IBaseServiceImpl<QuestionEntity, Long>
|
|
|
@Autowired
|
|
|
AnswerMapper answerMapper;
|
|
|
|
|
|
+
|
|
|
+ private static List<List<Object>> qusetionlineList = new ArrayList<>();
|
|
|
+ private static List<List<Object>> answerlineList = new ArrayList<>();
|
|
|
+
|
|
|
@Override
|
|
|
public IBaseMapper<QuestionEntity, Long> getBaseMapper() {
|
|
|
return this.entityMapper;
|
|
@@ -112,4 +123,110 @@ public class QuestionServiceImpl extends IBaseServiceImpl<QuestionEntity, Long>
|
|
|
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result uploadExcel(MultipartFile file) {
|
|
|
+ if (file == null) {
|
|
|
+ return Result.failure("文件为空");
|
|
|
+ }
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ if (!originalFilename.endsWith(".xlsx")) {
|
|
|
+ return Result.failure("只支持.slsx格式的Excel");
|
|
|
+ }
|
|
|
+ String suffix = StringUtils.substringAfterLast(originalFilename, ".");
|
|
|
+
|
|
|
+ String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
|
|
|
+ // 目录位置
|
|
|
+ String savePath = configConstant.serverBasePath + time + "."+ suffix;
|
|
|
+ log.info("savePath: " + savePath);
|
|
|
+ try {
|
|
|
+ FileUtil.writeFromStream(file.getInputStream(), savePath);
|
|
|
+ if (!FileUtil.exist(savePath)) {
|
|
|
+ log.error("excel文件不存在: {}", savePath);
|
|
|
+ return Result.failure("excel文件不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ updateQuestion(savePath, 0);
|
|
|
+ log.info("更新问题完成");
|
|
|
+ updateAnswer(savePath, 1);
|
|
|
+ log.info("更新答案完成");
|
|
|
+ return Result.success();
|
|
|
+ } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+ log.error(e.getMessage());
|
|
|
+ throw new BaseRuntimeException("文件内容有误,请联系管理员");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateQuestion(String excelPath, Integer sheetIndex){
|
|
|
+ // reader方法的第二个参数是sheet的序号,-1表示读取所有sheet,0表示第一个sheet,依此类推
|
|
|
+ ExcelUtil.read07BySax(excelPath, sheetIndex, createRowHandler());
|
|
|
+ //去除excel中的第一行数据
|
|
|
+ qusetionlineList.remove(0);
|
|
|
+ // 清空问题表
|
|
|
+ entityMapper.truncateTableQuestion();
|
|
|
+ for (List<Object> row : qusetionlineList) {
|
|
|
+ QuestionEntity questionEntity = new QuestionEntity();
|
|
|
+ questionEntity.setId((Long)row.get(0) );
|
|
|
+ questionEntity.setTitle((String) row.get(1));
|
|
|
+ questionEntity.setType((String)row.get(2));
|
|
|
+ questionEntity.setTopicType(Integer.parseInt(row.get(3).toString()));
|
|
|
+
|
|
|
+ log.info("entity: {}", questionEntity.toString());
|
|
|
+ this.save(questionEntity);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private static RowHandler createRowHandler() {
|
|
|
+ qusetionlineList.removeAll(qusetionlineList);
|
|
|
+ return new RowHandler() {
|
|
|
+ @Override
|
|
|
+ public void handle(int sheetIndex, int rowIndex, List rowlist) {
|
|
|
+// log.info("[{}] [{}] {}", sheetIndex, rowIndex, rowlist);
|
|
|
+ qusetionlineList.add(rowlist);
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateAnswer(String excelPath, Integer sheetIndex){
|
|
|
+ // reader方法的第二个参数是sheet的序号,-1表示读取所有sheet,0表示第一个sheet,依此类推
|
|
|
+ ExcelUtil.read07BySax(excelPath, sheetIndex, answerRowHandler());
|
|
|
+ //去除excel中的第一行数据
|
|
|
+ answerlineList.remove(0);
|
|
|
+ // 清空表
|
|
|
+ entityMapper.truncateTableAnswaer();
|
|
|
+ for (List<Object> row : answerlineList) {
|
|
|
+ AnswerEntity entity = new AnswerEntity();
|
|
|
+ entity.setContent((String) row.get(0));
|
|
|
+ entity.setCorrect(Integer.parseInt(row.get(1).toString()));
|
|
|
+ entity.setSort(Integer.parseInt(row.get(2).toString()));
|
|
|
+ entity.setQuestionId((Long)row.get(3));
|
|
|
+ log.info("entity: {}", entity.toString());
|
|
|
+ answerService.save(entity);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private static RowHandler answerRowHandler() {
|
|
|
+ answerlineList.removeAll(answerlineList);
|
|
|
+ return new RowHandler() {
|
|
|
+ @Override
|
|
|
+ public void handle(int sheetIndex, int rowIndex, List rowlist) {
|
|
|
+ // rowlist: 具体没一行的数据
|
|
|
+ answerlineList.add(rowlist);
|
|
|
+ }
|
|
|
+
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|