Authored by zhichao

导入导出

1.【无】导入导出开发
... ... @@ -2,10 +2,15 @@
package com.anjiplus.template.gaea.business.modules.dashboardwidget.dao.entity;
import com.anji.plus.gaea.curd.entity.GaeaBaseEntity;
import com.anjiplus.template.gaea.business.modules.dataset.dao.entity.DataSet;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
/**
* @description 大屏看板数据渲染 entity
* @author Raod
... ... @@ -46,5 +51,9 @@ public class ReportDashboardWidget extends GaeaBaseEntity {
@ApiModelProperty(value = "排序,图层的概念")
private Long sort;
@TableField(exist = false)
private String setCode;
@TableField(exist = false)
private List<DataSet> dataSetList = new ArrayList<>();
}
... ...
... ... @@ -4,10 +4,17 @@ package com.anjiplus.template.gaea.business.modules.dataset.dao.entity;
import com.anji.plus.gaea.annotation.Unique;
import com.anji.plus.gaea.curd.entity.GaeaBaseEntity;
import com.anjiplus.template.gaea.business.code.ResponseCode;
import com.anjiplus.template.gaea.business.modules.datasetparam.dao.entity.DataSetParam;
import com.anjiplus.template.gaea.business.modules.datasettransform.dao.entity.DataSetTransform;
import com.anjiplus.template.gaea.business.modules.datasource.dao.entity.DataSource;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
/**
* @description 数据集 entity
* @author Raod
... ... @@ -44,5 +51,15 @@ public class DataSet extends GaeaBaseEntity {
@ApiModelProperty(value = "0--未删除 1--已删除 DIC_NAME=DELETE_FLAG")
private Integer deleteFlag;
@TableField(exist = false)
List<DataSetTransform> dataSetTransformList = new ArrayList<>();
@TableField(exist = false)
List<DataSetParam> dataSetParamList = new ArrayList<>();
@TableField(exist = false)
List<DataSource> dataSourceList = new ArrayList<>();
@TableField(exist = false)
private String sourceCodeChange;
}
... ...
package com.anjiplus.template.gaea.business.modules.export.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.anjiplus.template.gaea.business.modules.export.service.impl.ExportServiceImpl;
import com.anjiplus.template.gaea.business.modules.report.dao.entity.Report;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
/**
* 导出
*
* @author lzc
* @version 1.0
* @date 2022-06-21 11:02
*/
@RestController
public class ExportController {
private final ExportServiceImpl exportService;
public ExportController(ExportServiceImpl exportService) {
this.exportService = exportService;
}
@GetMapping("/export")
public void export(HttpServletResponse response, @RequestParam List<String> reportCodeList) {
List<Report> export = exportService.export(reportCodeList);
exportJson(response, export, "AJ大屏报表");
}
private void exportJson(HttpServletResponse response, List<Report> export, String fileName) {
try {
String jsonString = JSON.toJSONString(export,
SerializerFeature.PrettyFormat,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteDateUseDateFormat);
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".json");
response.getOutputStream().write(jsonString.getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
throw new RuntimeException("导出错误");
}
}
@RequestMapping("/import")
public void importE(MultipartFile file) throws IOException {
byte[] bytes = file.getBytes();
Object parse = JSONObject.parse(bytes);
List<Report> reportList = JSONObject.parseArray(JSONObject.toJSONString(parse), Report.class);
exportService.importE(reportList);
}
}
... ...
package com.anjiplus.template.gaea.business.modules.export.dao;
/**
* @author lzc
* @version 1.0
* @date 2022-06-21 11:21
*/
public interface ExportMapper {
}
... ...
package com.anjiplus.template.gaea.business.modules.export.service;
import com.anjiplus.template.gaea.business.modules.report.dao.entity.Report;
import java.util.List;
/**
* @author lzc
* @version 1.0
* @date 2022-06-21 11:03
*/
public interface ExportService {
List<Report> export(List<String> reportCodeList);
void importE(List<Report> reportList);
}
... ...
package com.anjiplus.template.gaea.business.modules.export.service.impl;
import com.anjiplus.template.gaea.business.modules.dashboard.dao.entity.ReportDashboard;
import com.anjiplus.template.gaea.business.modules.dashboard.service.ReportDashboardService;
import com.anjiplus.template.gaea.business.modules.dashboardwidget.dao.entity.ReportDashboardWidget;
import com.anjiplus.template.gaea.business.modules.dashboardwidget.service.ReportDashboardWidgetService;
import com.anjiplus.template.gaea.business.modules.dataset.dao.entity.DataSet;
import com.anjiplus.template.gaea.business.modules.dataset.service.DataSetService;
import com.anjiplus.template.gaea.business.modules.datasetparam.dao.entity.DataSetParam;
import com.anjiplus.template.gaea.business.modules.datasetparam.service.DataSetParamService;
import com.anjiplus.template.gaea.business.modules.datasettransform.dao.entity.DataSetTransform;
import com.anjiplus.template.gaea.business.modules.datasettransform.service.DataSetTransformService;
import com.anjiplus.template.gaea.business.modules.datasource.dao.entity.DataSource;
import com.anjiplus.template.gaea.business.modules.datasource.service.DataSourceService;
import com.anjiplus.template.gaea.business.modules.export.service.ExportService;
import com.anjiplus.template.gaea.business.modules.report.dao.entity.Report;
import com.anjiplus.template.gaea.business.modules.report.service.ReportService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author lzc
* @version 1.0
* @date 2022-06-21 11:03
*/
@Service
public class ExportServiceImpl implements ExportService {
private final ReportService reportService;
private final ReportDashboardService reportDashboardService;
private final ReportDashboardWidgetService reportDashboardWidgetService;
private final DataSetService dataSetService;
private final DataSetParamService dataSetParamService;
private final DataSetTransformService dataSetTransformService;
private final DataSourceService dataSourceService;
public ExportServiceImpl(ReportService reportService, ReportDashboardService reportDashboardService, ReportDashboardWidgetService reportDashboardWidgetService, DataSetService dataSetService, DataSetParamService dataSetParamService, DataSetTransformService dataSetTransformService, DataSourceService dataSourceService) {
this.reportService = reportService;
this.reportDashboardService = reportDashboardService;
this.reportDashboardWidgetService = reportDashboardWidgetService;
this.dataSetService = dataSetService;
this.dataSetParamService = dataSetParamService;
this.dataSetTransformService = dataSetTransformService;
this.dataSourceService = dataSourceService;
}
@Override
public List<Report> export(List<String> reportCodeList) {
//大屏
List<Report> reportList = reportService.list(new LambdaQueryWrapper<Report>()
.in(Report::getReportCode, reportCodeList));
if (reportList == null || reportList.size() == 0) {
return reportList;
}
//大屏配置
reportCodeList = reportList.stream().map(Report::getReportCode).collect(Collectors.toList());
List<ReportDashboard> reportDashboardList = reportDashboardService.list(new LambdaQueryWrapper<ReportDashboard>()
.in(ReportDashboard::getReportCode, reportCodeList));
//大屏组件
List<ReportDashboardWidget> reportDashboardWidgetList = reportDashboardWidgetService.list(new LambdaQueryWrapper<ReportDashboardWidget>()
.in(ReportDashboardWidget::getReportCode, reportCodeList));
//组件中的接口地址
List<String> setCodeList = new ArrayList<>();
reportDashboardWidgetList.stream().filter(item -> item.getData().contains("setCode")).forEach(item -> {
String data = item.getData();
int startIndex = data.indexOf("setCode");
int endIndex = data.indexOf(",", startIndex);
String setCode = data.substring(startIndex + 10, endIndex - 1);
setCodeList.add(setCode);
item.setSetCode(setCode);
});
if (setCodeList.size() == 0) {
return reportList;
}
//接口地址
List<DataSet> dataSetList = dataSetService.list(new LambdaQueryWrapper<DataSet>()
.in(DataSet::getSetCode, setCodeList));
if (dataSetList == null || dataSetList.size() == 0) {
return reportList;
}
//接口参数
List<String> setCodes = dataSetList.stream().map(DataSet::getSetCode).collect(Collectors.toList());
List<DataSetParam> dataSetParamList = dataSetParamService.list(new LambdaQueryWrapper<DataSetParam>()
.in(DataSetParam::getSetCode, setCodes));
//数据转换
List<DataSetTransform> dataSetTransformList = dataSetTransformService.list(new LambdaQueryWrapper<DataSetTransform>()
.in(DataSetTransform::getSetCode, setCodes));
//数据源
List<String> sourceCodeList = dataSetList.stream().map(DataSet::getSourceCode).distinct().collect(Collectors.toList());
List<DataSource> dataSourceList = dataSourceService.list(new LambdaQueryWrapper<DataSource>()
.in(DataSource::getSourceCode, sourceCodeList));
//组合大屏组件
Map<String, List<ReportDashboard>> reportDashboardMap = reportDashboardList.stream().collect(Collectors.groupingBy(ReportDashboard::getReportCode));
Map<String, List<ReportDashboardWidget>> reportDashboardWidgetMap = reportDashboardWidgetList.stream().collect(Collectors.groupingBy(ReportDashboardWidget::getReportCode));
for (Report report : reportList) {
if (reportDashboardMap.get(report.getReportCode()) != null) {
report.setReportDashboardList(reportDashboardMap.get(report.getReportCode()));
}
if (reportDashboardWidgetMap.get(report.getReportCode()) != null) {
report.setReportDashboardWidgetList(reportDashboardWidgetMap.get(report.getReportCode()));
}
}
//组合组件接口
Map<String, List<DataSet>> dataSetMap = dataSetList.stream().collect(Collectors.groupingBy(DataSet::getSetCode));
reportDashboardWidgetList.stream().filter(item -> item.getData().contains("setCode")).forEach(reportDashboardWidget -> {
if (dataSetMap.get(reportDashboardWidget.getSetCode()) != null) {
reportDashboardWidget.getDataSetList().addAll(dataSetMap.get(reportDashboardWidget.getSetCode()));
}
});
//组合接口参数转换数据源
Map<String, List<DataSetTransform>> dataSetTransformMap = dataSetTransformList.stream().collect(Collectors.groupingBy(DataSetTransform::getSetCode));
Map<String, List<DataSetParam>> dataSetParamMap = dataSetParamList.stream().collect(Collectors.groupingBy(DataSetParam::getSetCode));
Map<String, List<DataSource>> dataSourceMap = dataSourceList.stream().collect(Collectors.groupingBy(DataSource::getSourceCode));
for (DataSet dataSet : dataSetList) {
if (dataSetTransformMap.get(dataSet.getSetCode()) != null) {
dataSet.getDataSetTransformList().addAll(dataSetTransformMap.get(dataSet.getSetCode()));
}
if (dataSetParamMap.get(dataSet.getSetCode()) != null) {
dataSet.getDataSetParamList().addAll(dataSetParamMap.get(dataSet.getSetCode()));
}
if (dataSourceMap.get(dataSet.getSourceCode()) != null) {
dataSet.getDataSourceList().addAll(dataSourceMap.get(dataSet.getSourceCode()));
}
}
return reportList;
}
@Override
public void importE(List<Report> reportList) {
//新增大屏
if (reportList == null || reportList.size() == 0) {
return;
}
List<String> reportCodeList = reportList.stream().map(Report::getReportCode).collect(Collectors.toList());
reportService.delete(new LambdaQueryWrapper<Report>().in(Report::getReportCode, reportCodeList));
reportService.insertBatch(reportList);
//新增大屏配置
List<ReportDashboard> reportDashboardList = new ArrayList<>();
reportList.stream().map(Report::getReportDashboardList).forEach(reportDashboardList::addAll);
if (reportDashboardList.size() != 0) {
reportDashboardService.delete(new LambdaQueryWrapper<ReportDashboard>().in(ReportDashboard::getReportCode, reportCodeList));
reportDashboardService.insertBatch(reportDashboardList);
}
//新增大屏组件
List<ReportDashboardWidget> reportDashboardWidgetList = new ArrayList<>();
reportList.stream().map(Report::getReportDashboardWidgetList).forEach(reportDashboardWidgetList::addAll);
if (reportDashboardWidgetList.size() == 0) {
return;
}
reportDashboardWidgetService.delete(new LambdaQueryWrapper<ReportDashboardWidget>().in(ReportDashboardWidget::getReportCode, reportCodeList));
reportDashboardWidgetService.insertBatch(reportDashboardWidgetList);
//新增数据集
List<String> setCodeList = reportDashboardWidgetList.stream().map(ReportDashboardWidget::getSetCode).distinct().collect(Collectors.toList());
List<DataSet> dataSetList = new ArrayList<>();
reportDashboardWidgetList.stream().map(ReportDashboardWidget::getDataSetList).forEach(dataSetList::addAll);
if (dataSetList.size() != 0) {
dataSetService.delete(new LambdaQueryWrapper<DataSet>().in(DataSet::getSetCode, setCodeList));
for (DataSet dataSet : dataSetList) {
if (StringUtils.isNotBlank(dataSet.getSourceCodeChange())) {
dataSet.setSourceCode(dataSet.getSourceCodeChange());
}
}
dataSetService.insertBatch(dataSetList);
}
//新增数据集参数
List<DataSetParam> dataSetParamList = new ArrayList<>();
dataSetList.stream().map(DataSet::getDataSetParamList).forEach(dataSetParamList::addAll);
if (dataSetParamList.size() != 0) {
dataSetParamService.delete(new LambdaQueryWrapper<DataSetParam>().in(DataSetParam::getSetCode, setCodeList));
dataSetParamService.insertBatch(dataSetParamList);
}
//新增数据源转换
List<DataSetTransform> dataSetTransformList = new ArrayList<>();
dataSetList.stream().map(DataSet::getDataSetTransformList).forEach(dataSetTransformList::addAll);
if (dataSetTransformList.size() != 0) {
dataSetTransformService.delete(new LambdaQueryWrapper<DataSetTransform>().in(DataSetTransform::getSetCode, setCodeList));
dataSetTransformService.insertBatch(dataSetTransformList);
}
}
}
... ...
... ... @@ -3,10 +3,15 @@ package com.anjiplus.template.gaea.business.modules.report.dao.entity;
import com.anji.plus.gaea.annotation.Unique;
import com.anji.plus.gaea.curd.entity.GaeaBaseEntity;
import com.anjiplus.template.gaea.business.code.ResponseCode;
import com.anjiplus.template.gaea.business.modules.dashboard.dao.entity.ReportDashboard;
import com.anjiplus.template.gaea.business.modules.dashboardwidget.dao.entity.ReportDashboardWidget;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
/**
* TODO
*
... ... @@ -41,4 +46,10 @@ public class Report extends GaeaBaseEntity {
@ApiModelProperty(value = "0--未删除 1--已删除 DIC_NAME=DELETE_FLAG")
private Integer deleteFlag;
@TableField(exist = false)
private List<ReportDashboard> reportDashboardList;
@TableField(exist = false)
private List<ReportDashboardWidget> reportDashboardWidgetList;
}
... ...