Commit 74bf0437 authored by 高滢's avatar 高滢

导出方案

parent 260ca1ca
package com.ruoyi.domain.vo;
import com.ruoyi.common.annotation.Excel;
import lombok.Data;
@Data
public class SystemReviewExportVO {
@Excel(name = "标准章节号")
private String chapter;
@Excel(name = "标准要求")
private String reviewStandard;
@Excel(name = "审查要点")
private String reviewKeyPoint;
@Excel(name = "审查细则")
private String reviewDetails;
@Excel(name = "审查场景")
private String reviewScene;
}
......@@ -2,8 +2,11 @@ package com.ruoyi.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.domain.TTestPlanRecord;
import com.ruoyi.domain.vo.SystemReviewExportVO;
import com.ruoyi.web.request.SystemReviewExportRequest;
import com.ruoyi.web.request.TestPlanRecordGetInfoRequest;
import com.ruoyi.web.request.TestPlanRecordPageRequest;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
......@@ -25,6 +28,8 @@ public interface TTestPlanRecordMapper extends BaseMapper<TTestPlanRecord> {
public int updateTestPlanRecord(TestPlanRecordGetInfoRequest testPlanRecordGetInfoRequest);
public List<SystemReviewExportVO> systemReviewExport(@Param("standardId") Long standardId, @Param("type") String type);
}
......
package com.ruoyi.web;
import com.alibaba.fastjson2.JSONObject;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelMultipleSheetsUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.ReviewStandard;
import com.ruoyi.domain.TTestPlanRecord;
......@@ -15,11 +17,11 @@ import com.ruoyi.mapper.TTestPlanRecordMapper;
import com.ruoyi.service.TTestPlanRecordService;
import com.ruoyi.service.impl.ReviewStandardServiceImpl;
import com.ruoyi.web.request.PlanDetailStandardRequest;
import com.ruoyi.web.request.SystemReviewExportRequest;
import com.ruoyi.web.request.TestPlanRecordGetInfoRequest;
import com.ruoyi.web.request.TestPlanRecordPageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import okhttp3.*;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
......@@ -31,14 +33,14 @@ import org.springframework.web.bind.annotation.RequestBody;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
@Api(tags = "检测方案记录")
@RestController
@RequestMapping("/Plan/Record")
public class TestPlanRecordController extends BaseController{
private static final String XLSX_SUFFIX = ".xlsx";
@Autowired
private TTestPlanRecordService tTestPlanRecordService;
......@@ -72,7 +74,6 @@ public class TestPlanRecordController extends BaseController{
@Tags({@Tag(key = "param", value = "arg[0]"), @Tag(key = "result", value = "returnedObj")})
@RequestMapping(method = RequestMethod.POST, value = "/standard")
public R<List<ReviewStandard>> selectStandarDetails(@Validated @RequestBody PlanDetailStandardRequest planDetailStandardRequest){
System.out.println("planDetailStandardRequest = " + planDetailStandardRequest);
List<ReviewStandard> tree = reviewStandardService.findTree(planDetailStandardRequest.getInspectionStandardId(), planDetailStandardRequest.getType());
return R.ok(tree);
}
......@@ -129,4 +130,39 @@ public class TestPlanRecordController extends BaseController{
util.exportExcel( respon, list, "用例编号");
// return R.ok();
}
@RequestMapping(method = RequestMethod.POST, value = "/exportTo")
public void exportPlanRecordTo(HttpServletResponse respon, @Validated @RequestBody SystemReviewExportRequest systemReviewExportRequest) throws IOException{
List inspectionTtem = systemReviewExportRequest.getInspectionTtem();
Map<String, Object> result = new HashMap<>();
Boolean isPage = true;
inspectionTtem.forEach(item->{
if("sraif".equals(item)){
result.put(isPage ? "体系审查" : "sheet1名字", tTestPlanRecordMapper.systemReviewExport(systemReviewExportRequest.getInspectionStandardId(), "system") );
}
if ("grfis".equals(item)){
result.put(isPage ? "车型审查" : "sheet1名字", tTestPlanRecordMapper.systemReviewExport(systemReviewExportRequest.getInspectionStandardId(), "car") );
}
if ("trfis".equals(item)){
String params = JSON.toJSONString(systemReviewExportRequest.getCarExperimentRequest());
List<ItemsByUseCaseVO> listUseCase = new ArrayList<>();
try {
listUseCase = tTestPlanRecordService.getCarTestDetail(params);
} catch (IOException e) {
throw new RuntimeException(e);
}
List<RecaordUseCasesVO> list = new ArrayList<>();
listUseCase.forEach(itemS->{
itemS.getUseCases().forEach(data->{
list.add(data);
});
});
result.put(isPage ? "车型实验" : "sheet1名字", list );
}
});
ExcelMultipleSheetsUtil.excelMultipleSheets(result, "Excel数据" + DateUtil.format(new Date(), "yyyyMMddHHmmss"), XLSX_SUFFIX, respon);
}
}
package com.ruoyi.web.request;
import lombok.Data;
import java.util.List;
@Data
public class CarExperimentRequest {
private String regulationName;
private List<String> matchUseCases;
}
package com.ruoyi.web.request;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
/**
* @author GaoYing
*/
@Data
@ApiModel("导出参数")
public class SystemReviewExportRequest {
@ApiModelProperty("检验项目")
private List<String> inspectionTtem;
// @ApiModelProperty("用例编号")
// private List<String> useCaseNo;
//
// @ApiModelProperty("标准")
// private String inspectionStandardName;
@ApiModelProperty("标准ID")
private Long inspectionStandardId;
@ApiModelProperty("车型实验json")
private CarExperimentRequest carExperimentRequest;
}
......@@ -78,4 +78,13 @@
</trim>
where id = #{id}
</update>
<select id="systemReviewExport" resultType="com.ruoyi.domain.vo.SystemReviewExportVO">
select trs.chapter,trs.text as reviewStandard,trk.text as reviewKeyPoint,trd.text as reviewDetails, trsc.text as reviewScene from t_standard ts
LEFT JOIN t_review_standard trs on trs.standard_id = ts.id
LEFT JOIN t_review_keypoint trk on trk.review_standard_id = trs.id
LEFT JOIN t_review_details trd on trd.review_keypoint_id = trk.id
LEFT JOIN t_review_scene trsc on trsc.review_details_id = trd.id
WHERE standard_id = #{standardId} AND type = #{type}
</select>
</mapper>
package com.ruoyi.common.utils.poi;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.utils.DictUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;
import static com.ruoyi.common.core.text.CharsetKit.UTF_8;
public class ExcelMultipleSheetsUtil {
/**
* 导出excel:可多个sheet页
*
* @param data 数据:Map 集合【key == 每一个sheet页的名称,value == sheet页数据】
* @param excelFileName excel文件名
* @param suffixName 后缀名
* @param response 响应
* @throws IOException 异常
*/
public static void excelMultipleSheets(Map<String, Object> data, String excelFileName, String suffixName, HttpServletResponse response) throws IOException {
// 创建工作簿
try (Workbook workbook = new XSSFWorkbook()) {
for (Map.Entry<String, Object> entry : data.entrySet()) {
String sheetName = entry.getKey();
Object sheetData = entry.getValue();
Sheet sheet = workbook.createSheet(sheetName);
if (ObjectUtil.isNotEmpty(sheetData)) {
createSheetWithData(sheet, sheetData);
}
}
setResponseHeader(response, excelFileName, suffixName);
// 写出文件
workbook.write(response.getOutputStream());
}
}
/**
* 创建表单并填充数据
*
* @param sheet 表单
* @param data 数据
*/
private static void createSheetWithData(Sheet sheet, Object data) {
if (data instanceof List) {
createSheetWithListData(sheet, (List<?>) data);
} else {
createSheetWithObjectData(sheet, data);
}
}
/**
* 创建列表类型数据对应的Excel表单
*
* @param sheet 表单
* @param dataList 数据列表
*/
private static void createSheetWithListData(Sheet sheet, List<?> dataList) {
if (CollUtil.isNotEmpty(dataList)) {
Object firstItem = dataList.get(0);
createHeaderRow(sheet, firstItem.getClass());
int rowIndex = 1;
for (Object item : dataList) {
createDataRow(sheet, item, rowIndex++);
}
}
}
/**
* 创建对象类型数据对应的Excel表单
*
* @param sheet 表单
* @param data 数据
*/
private static void createSheetWithObjectData(Sheet sheet, Object data) {
createHeaderRow(sheet, data.getClass());
createDataRow(sheet, data, 1);
}
/**
* 创建表头行
*
* @param sheet 表单
* @param clazz 数据类
*/
private static void createHeaderRow(Sheet sheet, Class<?> clazz) {
// 创建单元格样式
CellStyle headerCellStyle = createCellStyle(sheet.getWorkbook());
// 创建标题行
Row headerRow = sheet.createRow(0);
Field[] fields = clazz.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
createHeaderCell(sheet, headerCellStyle, fields, headerRow, i);
}
}
/**
* 创建数据行
*
* @param sheet 表单
* @param data 数据
* @param rowIndex 行号
*/
private static void createDataRow(Sheet sheet, Object data, int rowIndex) {
// 创建单元格样式
CellStyle dataCellStyle = createCellStyle(sheet.getWorkbook());
// 创建数据行
Row dataRow = sheet.createRow(rowIndex);
Field[] fields = data.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
createDataCell(dataCellStyle, fields, dataRow, i, data);
}
}
/**
* 创建单元格样式
*
* @param workbook 工作簿
* @return 单元格样式
*/
private static CellStyle createCellStyle(Workbook workbook) {
CellStyle cellStyle = workbook.createCellStyle();
// 设置 水平和垂直 居中对齐
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
// 设置 上 下 左 右 边框及颜色
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
// 设置字体
Font dataFont = workbook.createFont();
dataFont.setFontName("Arial");
dataFont.setFontHeightInPoints((short) 10);
cellStyle.setFont(dataFont);
return cellStyle;
}
/**
* 创建Excel表头单元格
*
* @param sheet 表单
* @param headerCellStyle 单元格样式
* @param fields 字段
* @param headerRow 标题行
* @param i 序号
*/
private static void createHeaderCell(Sheet sheet, CellStyle headerCellStyle, Field[] fields, Row headerRow, int i) {
// 默认宽度
double width = 16;
Excel excelAnnotation = fields[i].getAnnotation(Excel.class);
if (excelAnnotation != null && !ObjectUtil.isEmpty(excelAnnotation.width())) {
width = excelAnnotation.width();
}
// 设置宽度
sheet.setColumnWidth(i, (int) ((width + 0.72) * 256));
if (excelAnnotation != null) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(excelAnnotation.name());
cell.setCellStyle(headerCellStyle);
}
}
/**
* 创建Excel数据单元格
*
* @param dataCellStyle 单元格样式
* @param fields 字段
* @param dataRow 数据行
* @param i 序号
* @param data 数据
*/
private static void createDataCell(CellStyle dataCellStyle, Field[] fields, Row dataRow, int i, Object data) {
Cell cell = dataRow.createCell(i);
cell.setCellStyle(dataCellStyle);
try {
fields[i].setAccessible(true);
Object value = fields[i].get(data);
handleAnnotationAndSetValue(cell, fields[i], value);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
/**
* 处理注解并设置单元格值
*
* @param cell 单元格
* @param field 字段
* @param value 值
*/
private static void handleAnnotationAndSetValue(Cell cell, Field field, Object value) {
if (field.isAnnotationPresent(Excel.class) && field.getAnnotation(Excel.class).dictType().length() > 0) {
value = DictUtils.getDictLabel(field.getAnnotation(Excel.class).dictType(), String.valueOf(value));
}
if (field.isAnnotationPresent(Excel.class) && StrUtil.isNotEmpty(field.getAnnotation(Excel.class).dateFormat())) {
value = DateUtil.format(Convert.convert(Date.class, value), field.getAnnotation(Excel.class).dateFormat());
}
cell.setCellValue(ObjectUtil.isEmpty(value) ? null : value.toString());
}
/**
* 设置响应头
*
* @param response 响应
* @param excelFileName 文件名
* @param suffixName 后缀名
* @throws UnsupportedEncodingException 编码异常
*/
private static void setResponseHeader(HttpServletResponse response, String excelFileName, String suffixName) throws UnsupportedEncodingException {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(excelFileName + suffixName, UTF_8));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment