Merge remote-tracking branch 'origin/master'

hehai
pd 4 years ago
commit ccc324d142
  1. 7
      src/main/java/com/msdw/tms/api/QuestionsControllerApi.java
  2. 6
      src/main/java/com/msdw/tms/common/utils/Constant.java
  3. 19
      src/main/java/com/msdw/tms/common/utils/DownloadUtils.java
  4. 53
      src/main/java/com/msdw/tms/common/utils/poi/ExcelExportUtil.java
  5. 19
      src/main/java/com/msdw/tms/config/CorsConfig.java
  6. 6
      src/main/java/com/msdw/tms/controller/QuestionsController.java
  7. 2
      src/main/java/com/msdw/tms/entity/QuestionsEntity.java
  8. 2
      src/main/java/com/msdw/tms/entity/vo/QuestionsDetailVO.java
  9. 2
      src/main/java/com/msdw/tms/entity/vo/QuestionsVO.java
  10. 3
      src/main/java/com/msdw/tms/service/QuestionsService.java
  11. 25
      src/main/java/com/msdw/tms/service/impl/QuestionsServiceImpl.java
  12. 2
      src/main/resources/application.yml
  13. BIN
      src/main/resources/excel-template/试题导入模板.xlsx
  14. 9
      src/test/java/com/msdw/tms/TmsApplicationTests.java

@ -82,4 +82,11 @@ public interface QuestionsControllerApi {
*/ */
@ApiOperation(value = "抽题测评", notes = "抽题测评") @ApiOperation(value = "抽题测评", notes = "抽题测评")
R evaluation (); R evaluation ();
/**
* 通过excel批量导出
*/
@ApiOperation(value = "通过excel批量导出", notes = "通过excel批量导出")
void exportQuestion(HttpServletResponse response) throws Exception;
} }

@ -218,17 +218,17 @@ public class Constant {
/** /**
* 是否删除 * 是否删除0使用1删除默认0使用
*/ */
public enum IsDel { public enum IsDel {
/** /**
* 删除 * 删除
*/ */
DEL(0, "删除"), DEL(1, "删除"),
/** /**
* 不删除 * 不删除
*/ */
NOT_DEL(1, "不删除"); NOT_DEL(0, "不删除");
private Integer type; //类型 private Integer type; //类型
private String desc; //描述 private String desc; //描述

@ -0,0 +1,19 @@
package com.msdw.tms.common.utils;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class DownloadUtils {
public void download(ByteArrayOutputStream byteArrayOutputStream, HttpServletResponse response, String returnName) throws IOException {
response.setContentType("application/octet-stream");
returnName = response.encodeURL(new String(returnName.getBytes(), "iso8859-1")); //保存的文件名,必须和页面编码一致,否则乱码
response.addHeader("content-disposition", "attachment;filename=" + returnName);
response.setContentLength(byteArrayOutputStream.size());
ServletOutputStream outputstream = response.getOutputStream(); //取得输出流
byteArrayOutputStream.writeTo(outputstream); //写到输出流
byteArrayOutputStream.close(); //关闭
outputstream.flush(); //刷数据
}
}

@ -1,10 +1,7 @@
package com.msdw.tms.common.utils.poi; package com.msdw.tms.common.utils.poi;
import lombok.Data; import lombok.Data;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -77,7 +74,53 @@ public class ExcelExportUtil<T> {
workbook.write(response.getOutputStream()); workbook.write(response.getOutputStream());
} }
public CellStyle[] getTemplateStyles(Row row) { /**
* 基于注解导出
* 参数
* response
* objs数据
* fileName生成的文件名
*/
// public void export(HttpServletResponse response, List<T> objs, String fileName) throws Exception {
//
// //1.根据模板创建工作簿
// Workbook workbook = new XSSFWorkbook(); //2007版本
//
// fileName = URLEncoder.encode(fileName, "UTF-8");
//
// Sheet sheet = workbook.createSheet(fileName);
//
// //3.提取公共的样式
// // CellStyle[] styles = getTemplateStyles(sheet.getRow(styleIndex));
// //4.根据数据创建每一行和每一个单元格的数据2
// AtomicInteger datasAi = new AtomicInteger(rowIndex); //数字
// for (T t : objs) {
// //datasAi.getAndIncrement() :获取数字,并++ i++
// Row row = sheet.createRow(datasAi.getAndIncrement());
// for (int i = 0; i < styles.length; i++) {
// Cell cell = row.createCell(i);
// cell.setCellStyle(styles[i]);
// for (Field field : fields) {
// if (field.isAnnotationPresent(ExcelAttribute.class)) {
// field.setAccessible(true);
// ExcelAttribute ea = field.getAnnotation(ExcelAttribute.class);
// if (i == ea.sort()) {
// if (field.get(t) != null) {
// cell.setCellValue(field.get(t).toString());
// }
// }
// }
// }
// }
// }
//
// response.setContentType("application/octet-stream");
// response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes("ISO8859-1")));
// response.setHeader("filename", fileName);
// workbook.write(response.getOutputStream());
// }
CellStyle[] getTemplateStyles(Row row) {
CellStyle[] styles = new CellStyle[row.getLastCellNum()]; CellStyle[] styles = new CellStyle[row.getLastCellNum()];
for (int i = 0; i < row.getLastCellNum(); i++) { for (int i = 0; i < row.getLastCellNum(); i++) {
styles[i] = row.getCell(i).getCellStyle(); styles[i] = row.getCell(i).getCellStyle();

@ -0,0 +1,19 @@
package com.msdw.tms.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
.allowCredentials(true)
.maxAge(3600)
.allowedHeaders("*");
}
}

@ -149,4 +149,10 @@ public class QuestionsController implements QuestionsControllerApi {
return R.ok().put("data", evaluation); return R.ok().put("data", evaluation);
} }
@Override
@GetMapping("/export")
public void exportQuestion(HttpServletResponse response) throws Exception {
questionsService.exportQuestion(response);
}
} }

@ -71,7 +71,7 @@ public class QuestionsEntity implements Serializable {
*/ */
private Integer isEnable; private Integer isEnable;
/** /**
* 是否删除1使用0删除默认1使用 * 是否删除0使用1删除默认0使用
*/ */
private Integer isDel; private Integer isDel;
/** /**

@ -89,7 +89,7 @@ public class QuestionsDetailVO implements Serializable {
*/ */
private Integer isEnable; private Integer isEnable;
/** /**
* 是否删除1使用0删除默认1使用 * 是否删除0使用1删除默认0使用
*/ */
private Integer isDel; private Integer isDel;
/** /**

@ -67,7 +67,7 @@ public class QuestionsVO implements Serializable {
*/ */
private Integer isEnable; private Integer isEnable;
/** /**
* 是否删除1使用0删除默认1使用 * 是否删除0使用1删除默认0使用
*/ */
private Integer isDel; private Integer isDel;
/** /**

@ -9,6 +9,7 @@ import com.msdw.tms.entity.request.QuestionsQueryRequest;
import com.msdw.tms.entity.request.QuestionsUpdateRequest; import com.msdw.tms.entity.request.QuestionsUpdateRequest;
import com.msdw.tms.entity.vo.EvaluationVO; import com.msdw.tms.entity.vo.EvaluationVO;
import com.msdw.tms.entity.vo.QuestionsDetailVO; import com.msdw.tms.entity.vo.QuestionsDetailVO;
import com.msdw.tms.entity.vo.QuestionsVO;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -44,5 +45,7 @@ public interface QuestionsService extends IService<QuestionsEntity> {
void downloadFiles(HttpServletResponse response) throws IOException; void downloadFiles(HttpServletResponse response) throws IOException;
EvaluationVO evaluation(); EvaluationVO evaluation();
void exportQuestion(HttpServletResponse response) throws Exception;
} }

@ -8,6 +8,7 @@ import com.msdw.tms.common.utils.Constant;
import com.msdw.tms.common.utils.FilesResult; import com.msdw.tms.common.utils.FilesResult;
import com.msdw.tms.common.utils.PageUtils; import com.msdw.tms.common.utils.PageUtils;
import com.msdw.tms.common.utils.Query; import com.msdw.tms.common.utils.Query;
import com.msdw.tms.common.utils.poi.ExcelExportUtil;
import com.msdw.tms.common.utils.poi.ExcelImportUtil; import com.msdw.tms.common.utils.poi.ExcelImportUtil;
import com.msdw.tms.dao.QuestionsDao; import com.msdw.tms.dao.QuestionsDao;
import com.msdw.tms.entity.QuestionsEntity; import com.msdw.tms.entity.QuestionsEntity;
@ -27,12 +28,14 @@ import com.msdw.tms.service.QuestionsService;
import com.msdw.tms.service.XlsxTemplateService; import com.msdw.tms.service.XlsxTemplateService;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@ -278,7 +281,8 @@ public class QuestionsServiceImpl extends ServiceImpl<QuestionsDao, QuestionsEnt
@Transactional @Transactional
public boolean importQuestion(MultipartFile file) throws IOException { public boolean importQuestion(MultipartFile file) throws IOException {
// 使用excel导入工具类获取试题数据列表 // 使用excel导入工具类获取试题数据列表
List<QuestionsImportRequest> list = new ExcelImportUtil(QuestionsImportRequest.class).readExcel(file.getInputStream(), Constant.STARTING_ROW, Constant.STARTING_CELL); List<QuestionsImportRequest> list = new ExcelImportUtil(QuestionsImportRequest.class)
.readExcel(file.getInputStream(), Constant.STARTING_ROW, Constant.STARTING_CELL);
if (list == null || list.size() == 0) { if (list == null || list.size() == 0) {
ExceptionCast.cast(CommonCode.INVALID_PARAM); ExceptionCast.cast(CommonCode.INVALID_PARAM);
@ -413,6 +417,25 @@ public class QuestionsServiceImpl extends ServiceImpl<QuestionsDao, QuestionsEnt
return evaluation; return evaluation;
} }
@Override
public void exportQuestion(HttpServletResponse response) throws Exception {
//1.构造数据
List<QuestionsImportRequest> list = this.list().stream().map(item -> {
QuestionsImportRequest importRequest = new QuestionsImportRequest();
BeanUtils.copyProperties(item, importRequest);
return importRequest;
}).collect(Collectors.toList());
//2.加载模板流数据
org.springframework.core.io.Resource resource = new ClassPathResource("excel-template/试题导入模板.xlsx");
FileInputStream fis = new FileInputStream(resource.getFile());
//3、通过工具类下载文件
new ExcelExportUtil(QuestionsImportRequest.class, Constant.STARTING_ROW, Constant.STARTING_CELL).
export(response, fis, list, "试题表.xlsx");
}
private Set<QuestionsEntity> getRandomList(int len, List<QuestionsEntity> list) { private Set<QuestionsEntity> getRandomList(int len, List<QuestionsEntity> list) {
Set<QuestionsEntity> set = new HashSet<>(); Set<QuestionsEntity> set = new HashSet<>();
Random random = new Random(); Random random = new Random();

@ -1,6 +1,6 @@
spring: spring:
datasource: datasource:
url: jdbc:mysql://www.liuwanr.cn:3306/msdw_tms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai url: jdbc:mysql://www.liuwanr.cn:3306/huoran?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: super username: super
password: huoran888 password: huoran888
driver-class-name: com.mysql.jdbc.Driver driver-class-name: com.mysql.jdbc.Driver

@ -3,13 +3,18 @@ package com.msdw.tms;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
@SpringBootTest @SpringBootTest
class TmsApplicationTests { class TmsApplicationTests {
@Test @Test
void contextLoads() { void contextLoads() throws UnsupportedEncodingException {
System.out.println(URLDecoder.decode("%E8%AF%95%E9%A2%98%E8%A1%A8", "UTF-8"));
printMsg(1, 2, 3, "哈哈哈"); //printMsg(1, 2, 3, "哈哈哈");
// ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------
// QuestionsVO questionsVO = new QuestionsVO(); // QuestionsVO questionsVO = new QuestionsVO();

Loading…
Cancel
Save