Authored by 王涛

Merge branch 'master-500-dev-lzc' into 'master-500-dev'

导入导出

1.【无】导入导出联调

See merge request !101
@@ -102,6 +102,9 @@ public class TokenFilter implements Filter { @@ -102,6 +102,9 @@ public class TokenFilter implements Filter {
102 102
103 //获取token 103 //获取token
104 String token = request.getHeader("Authorization"); 104 String token = request.getHeader("Authorization");
  105 + if (token == null){
  106 + token = request.getParameter("Authorization");
  107 + }
105 if (StringUtils.isBlank(token)) { 108 if (StringUtils.isBlank(token)) {
106 error(response); 109 error(response);
107 return; 110 return;
@@ -257,7 +257,7 @@ public class DataSetServiceImpl implements DataSetService { @@ -257,7 +257,7 @@ public class DataSetServiceImpl implements DataSetService {
257 } else { 257 } else {
258 dataSource = dataSourceService.selectOne("source_code", dataSetDto.getSourceCode()); 258 dataSource = dataSourceService.selectOne("source_code", dataSetDto.getSourceCode());
259 } 259 }
260 - 260 + dataSource.setSourceCode(dataSetDto.getSourceCode());
261 log.info("数据源详情:{}", dataSource); 261 log.info("数据源详情:{}", dataSource);
262 262
263 //3.参数替换 263 //3.参数替换
@@ -303,6 +303,7 @@ public class DataSetServiceImpl implements DataSetService { @@ -303,6 +303,7 @@ public class DataSetServiceImpl implements DataSetService {
303 dataSource = new DataSource(); 303 dataSource = new DataSource();
304 dataSource.setSourceConfig(dynSentence); 304 dataSource.setSourceConfig(dynSentence);
305 dataSource.setSourceType(JdbcConstants.HTTP); 305 dataSource.setSourceType(JdbcConstants.HTTP);
  306 + dataSource.setSourceCode(dto.getSourceCode());
306 String body = JSONObject.parseObject(dynSentence).getString("body"); 307 String body = JSONObject.parseObject(dynSentence).getString("body");
307 if (StringUtils.isNotBlank(body)) { 308 if (StringUtils.isNotBlank(body)) {
308 dynSentence = body; 309 dynSentence = body;
@@ -37,8 +37,8 @@ import org.springframework.web.client.RestTemplate; @@ -37,8 +37,8 @@ import org.springframework.web.client.RestTemplate;
37 import javax.annotation.Resource; 37 import javax.annotation.Resource;
38 import java.sql.*; 38 import java.sql.*;
39 import java.time.LocalDateTime; 39 import java.time.LocalDateTime;
40 -import java.util.*;  
41 import java.util.Date; 40 import java.util.Date;
  41 +import java.util.*;
42 42
43 /** 43 /**
44 * @author Raod 44 * @author Raod
@@ -304,6 +304,20 @@ public class DataSourceServiceImpl implements DataSourceService { @@ -304,6 +304,20 @@ public class DataSourceServiceImpl implements DataSourceService {
304 public List<JSONObject> executeHttp(DataSourceDto dto) { 304 public List<JSONObject> executeHttp(DataSourceDto dto) {
305 analysisHttpConfig(dto); 305 analysisHttpConfig(dto);
306 String apiUrl = dto.getApiUrl(); 306 String apiUrl = dto.getApiUrl();
  307 + String sourceCode = dto.getSourceCode();
  308 + DataSource dataSource = dataSourceMapper.selectOne(new LambdaQueryWrapper<DataSource>()
  309 + .eq(DataSource::getSourceCode, sourceCode));
  310 + String sourceConfig = dataSource.getSourceConfig();
  311 + JSONObject jsonObject = JSONObject.parseObject(sourceConfig);
  312 + String gateway = jsonObject.getString("apiUrl");
  313 + if (apiUrl.indexOf("/") == 0) {
  314 + apiUrl = apiUrl.substring(1);
  315 + }
  316 + if (gateway.lastIndexOf("/") != gateway.length() - 1) {
  317 + gateway = gateway + "/";
  318 + }
  319 + apiUrl = gateway + apiUrl;
  320 +
307 HttpMethod httpMethod = HttpMethod.valueOf(dto.getMethod()); 321 HttpMethod httpMethod = HttpMethod.valueOf(dto.getMethod());
308 if (Objects.equals(HttpMethod.GET, httpMethod)) { 322 if (Objects.equals(HttpMethod.GET, httpMethod)) {
309 Map<String, Object> contextData = dto.getContextData(); 323 Map<String, Object> contextData = dto.getContextData();
@@ -323,12 +337,12 @@ public class DataSourceServiceImpl implements DataSourceService { @@ -323,12 +337,12 @@ public class DataSourceServiceImpl implements DataSourceService {
323 ResponseEntity<Object> exchange; 337 ResponseEntity<Object> exchange;
324 List<JSONObject> result = new ArrayList<>(); 338 List<JSONObject> result = new ArrayList<>();
325 try { 339 try {
326 - log.info("== 发送请求:\n\t\t apiUrl:{}\n\t\t httpMethod:{}\n\t\t entity:{}\n\t\t ",apiUrl, httpMethod, dto.getDynSentence()); 340 + log.info("== 发送请求:\n\t\t apiUrl:{}\n\t\t httpMethod:{}\n\t\t entity:{}\n\t\t ", apiUrl, httpMethod, dto.getDynSentence());
327 exchange = restTemplate.exchange(apiUrl, httpMethod, entity, Object.class); 341 exchange = restTemplate.exchange(apiUrl, httpMethod, entity, Object.class);
328 } catch (HttpClientErrorException error) { 342 } catch (HttpClientErrorException error) {
329 int rawStatusCode = error.getRawStatusCode(); 343 int rawStatusCode = error.getRawStatusCode();
330 // 401 接口请求无权限 344 // 401 接口请求无权限
331 - JSONObject jsonObject = new JSONObject(); 345 + jsonObject = new JSONObject();
332 jsonObject.put("code", rawStatusCode); 346 jsonObject.put("code", rawStatusCode);
333 jsonObject.put("data", error.getMessage()); 347 jsonObject.put("data", error.getMessage());
334 result.add(jsonObject); 348 result.add(jsonObject);
1 package com.anjiplus.template.gaea.business.modules.export.controller; 1 package com.anjiplus.template.gaea.business.modules.export.controller;
2 2
3 import com.alibaba.fastjson.JSON; 3 import com.alibaba.fastjson.JSON;
  4 +import com.alibaba.fastjson.JSONArray;
4 import com.alibaba.fastjson.JSONObject; 5 import com.alibaba.fastjson.JSONObject;
5 import com.alibaba.fastjson.serializer.SerializerFeature; 6 import com.alibaba.fastjson.serializer.SerializerFeature;
  7 +import com.anji.plus.gaea.bean.ResponseBean;
6 import com.anjiplus.template.gaea.business.modules.export.service.impl.ExportServiceImpl; 8 import com.anjiplus.template.gaea.business.modules.export.service.impl.ExportServiceImpl;
7 import com.anjiplus.template.gaea.business.modules.report.dao.entity.Report; 9 import com.anjiplus.template.gaea.business.modules.report.dao.entity.Report;
8 -import org.springframework.web.bind.annotation.GetMapping;  
9 -import org.springframework.web.bind.annotation.RequestMapping;  
10 -import org.springframework.web.bind.annotation.RequestParam;  
11 -import org.springframework.web.bind.annotation.RestController; 10 +import org.springframework.web.bind.annotation.*;
12 import org.springframework.web.multipart.MultipartFile; 11 import org.springframework.web.multipart.MultipartFile;
13 12
14 import javax.servlet.http.HttpServletResponse; 13 import javax.servlet.http.HttpServletResponse;
15 import java.io.IOException; 14 import java.io.IOException;
16 -import java.io.InputStream;  
17 -import java.io.InputStreamReader; 15 +import java.net.URLEncoder;
18 import java.nio.charset.StandardCharsets; 16 import java.nio.charset.StandardCharsets;
19 import java.util.List; 17 import java.util.List;
20 18
@@ -35,9 +33,17 @@ public class ExportController { @@ -35,9 +33,17 @@ public class ExportController {
35 } 33 }
36 34
37 @GetMapping("/export") 35 @GetMapping("/export")
38 - public void export(HttpServletResponse response, @RequestParam List<String> reportCodeList) { 36 + public void export(HttpServletResponse response, @RequestParam List<String> reportIdList,
  37 + @RequestParam String type) {
  38 + if (reportIdList == null || reportIdList.size() == 0) {
  39 + return;
  40 + }
  41 + List<String> reportCodeList = exportService.getReportCodeList(reportIdList);
  42 + if (reportCodeList == null || reportCodeList.size() == 0) {
  43 + return;
  44 + }
39 List<Report> export = exportService.export(reportCodeList); 45 List<Report> export = exportService.export(reportCodeList);
40 - exportJson(response, export, "AJ大屏报表"); 46 + exportJson(response, export, "大屏报表");
41 } 47 }
42 48
43 private void exportJson(HttpServletResponse response, List<Report> export, String fileName) { 49 private void exportJson(HttpServletResponse response, List<Report> export, String fileName) {
@@ -46,7 +52,10 @@ public class ExportController { @@ -46,7 +52,10 @@ public class ExportController {
46 SerializerFeature.PrettyFormat, 52 SerializerFeature.PrettyFormat,
47 SerializerFeature.WriteMapNullValue, 53 SerializerFeature.WriteMapNullValue,
48 SerializerFeature.WriteDateUseDateFormat); 54 SerializerFeature.WriteDateUseDateFormat);
  55 +
49 response.setContentType("application/octet-stream"); 56 response.setContentType("application/octet-stream");
  57 + fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());
  58 + response.setCharacterEncoding("UTF-8");
50 response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".json"); 59 response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".json");
51 response.getOutputStream().write(jsonString.getBytes(StandardCharsets.UTF_8)); 60 response.getOutputStream().write(jsonString.getBytes(StandardCharsets.UTF_8));
52 } catch (Exception e) { 61 } catch (Exception e) {
@@ -54,11 +63,24 @@ public class ExportController { @@ -54,11 +63,24 @@ public class ExportController {
54 } 63 }
55 } 64 }
56 65
57 - @RequestMapping("/import")  
58 - public void importE(MultipartFile file) throws IOException { 66 + @PostMapping("/import")
  67 + public ResponseBean importE(@RequestBody JSONArray jsonArray) throws IOException {
  68 + if (jsonArray == null || jsonArray.size() == 0) {
  69 + return ResponseBean.builder().build();
  70 + }
  71 + String s = JSONObject.toJSONString(jsonArray);
  72 + List<Report> reportList = JSONObject.parseArray(s, Report.class);
  73 + exportService.importE(reportList);
  74 + return ResponseBean.builder().build();
  75 + }
  76 +
  77 + @PostMapping("/read")
  78 + public ResponseBean read(MultipartFile file) throws IOException {
  79 + if (file == null) {
  80 + return null;
  81 + }
59 byte[] bytes = file.getBytes(); 82 byte[] bytes = file.getBytes();
60 Object parse = JSONObject.parse(bytes); 83 Object parse = JSONObject.parse(bytes);
61 - List<Report> reportList = JSONObject.parseArray(JSONObject.toJSONString(parse), Report.class);  
62 - exportService.importE(reportList); 84 + return ResponseBean.builder().data(JSONObject.parseArray(JSONObject.toJSONString(parse), Report.class)).build();
63 } 85 }
64 } 86 }
@@ -10,6 +10,9 @@ import java.util.List; @@ -10,6 +10,9 @@ import java.util.List;
10 * @date 2022-06-21 11:03 10 * @date 2022-06-21 11:03
11 */ 11 */
12 public interface ExportService { 12 public interface ExportService {
  13 +
  14 + List<String> getReportCodeList(List<String> reportIdList);
  15 +
13 List<Report> export(List<String> reportCodeList); 16 List<Report> export(List<String> reportCodeList);
14 17
15 void importE(List<Report> reportList); 18 void importE(List<Report> reportList);
@@ -18,6 +18,7 @@ import com.anjiplus.template.gaea.business.modules.report.service.ReportService; @@ -18,6 +18,7 @@ import com.anjiplus.template.gaea.business.modules.report.service.ReportService;
18 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; 18 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
19 import org.apache.commons.lang3.StringUtils; 19 import org.apache.commons.lang3.StringUtils;
20 import org.springframework.stereotype.Service; 20 import org.springframework.stereotype.Service;
  21 +import org.springframework.transaction.annotation.Transactional;
21 22
22 import java.util.ArrayList; 23 import java.util.ArrayList;
23 import java.util.List; 24 import java.util.List;
@@ -51,6 +52,12 @@ public class ExportServiceImpl implements ExportService { @@ -51,6 +52,12 @@ public class ExportServiceImpl implements ExportService {
51 } 52 }
52 53
53 @Override 54 @Override
  55 + public List<String> getReportCodeList(List<String> reportIdList) {
  56 + List<Report> reportList = reportService.getMapper().selectBatchIds(reportIdList);
  57 + return reportList.stream().map(Report::getReportCode).collect(Collectors.toList());
  58 + }
  59 +
  60 + @Override
54 public List<Report> export(List<String> reportCodeList) { 61 public List<Report> export(List<String> reportCodeList) {
55 //大屏 62 //大屏
56 List<Report> reportList = reportService.list(new LambdaQueryWrapper<Report>() 63 List<Report> reportList = reportService.list(new LambdaQueryWrapper<Report>()
@@ -70,10 +77,19 @@ public class ExportServiceImpl implements ExportService { @@ -70,10 +77,19 @@ public class ExportServiceImpl implements ExportService {
70 reportDashboardWidgetList.stream().filter(item -> item.getData().contains("setCode")).forEach(item -> { 77 reportDashboardWidgetList.stream().filter(item -> item.getData().contains("setCode")).forEach(item -> {
71 String data = item.getData(); 78 String data = item.getData();
72 int startIndex = data.indexOf("setCode"); 79 int startIndex = data.indexOf("setCode");
73 - int endIndex = data.indexOf(",", startIndex);  
74 - String setCode = data.substring(startIndex + 10, endIndex - 1);  
75 - setCodeList.add(setCode);  
76 - item.setSetCode(setCode); 80 + while (startIndex != -1) {
  81 + int endIndex = data.indexOf(",", startIndex);
  82 + String setCode = data.substring(startIndex + 10, endIndex - 1);
  83 + setCodeList.add(setCode);
  84 + item.setSetCode(setCode);
  85 + if (endIndex + 2 <= data.length()) {
  86 + data = data.substring(endIndex + 2);
  87 + } else {
  88 + data = "";
  89 + }
  90 + startIndex = data.indexOf("setCode");
  91 + }
  92 + ;
77 }); 93 });
78 if (setCodeList.size() == 0) { 94 if (setCodeList.size() == 0) {
79 return reportList; 95 return reportList;
@@ -109,8 +125,11 @@ public class ExportServiceImpl implements ExportService { @@ -109,8 +125,11 @@ public class ExportServiceImpl implements ExportService {
109 //组合组件接口 125 //组合组件接口
110 Map<String, List<DataSet>> dataSetMap = dataSetList.stream().collect(Collectors.groupingBy(DataSet::getSetCode)); 126 Map<String, List<DataSet>> dataSetMap = dataSetList.stream().collect(Collectors.groupingBy(DataSet::getSetCode));
111 reportDashboardWidgetList.stream().filter(item -> item.getData().contains("setCode")).forEach(reportDashboardWidget -> { 127 reportDashboardWidgetList.stream().filter(item -> item.getData().contains("setCode")).forEach(reportDashboardWidget -> {
112 - if (dataSetMap.get(reportDashboardWidget.getSetCode()) != null) {  
113 - reportDashboardWidget.getDataSetList().addAll(dataSetMap.get(reportDashboardWidget.getSetCode())); 128 + String data = reportDashboardWidget.getData();
  129 + for (Map.Entry<String, List<DataSet>> map : dataSetMap.entrySet()) {
  130 + if (data.contains("\"setCode\":\"" + map.getKey() + "\"")) {
  131 + reportDashboardWidget.getDataSetList().addAll(dataSetMap.get(map.getKey()));
  132 + }
114 } 133 }
115 }); 134 });
116 //组合接口参数转换数据源 135 //组合接口参数转换数据源
@@ -132,6 +151,7 @@ public class ExportServiceImpl implements ExportService { @@ -132,6 +151,7 @@ public class ExportServiceImpl implements ExportService {
132 } 151 }
133 152
134 @Override 153 @Override
  154 + @Transactional
135 public void importE(List<Report> reportList) { 155 public void importE(List<Report> reportList) {
136 156
137 //新增大屏 157 //新增大屏
@@ -144,7 +164,7 @@ public class ExportServiceImpl implements ExportService { @@ -144,7 +164,7 @@ public class ExportServiceImpl implements ExportService {
144 164
145 //新增大屏配置 165 //新增大屏配置
146 List<ReportDashboard> reportDashboardList = new ArrayList<>(); 166 List<ReportDashboard> reportDashboardList = new ArrayList<>();
147 - reportList.stream().map(Report::getReportDashboardList).forEach(reportDashboardList::addAll); 167 + reportList.stream().map(Report::getReportDashboardList).distinct().forEach(reportDashboardList::addAll);
148 if (reportDashboardList.size() != 0) { 168 if (reportDashboardList.size() != 0) {
149 reportDashboardService.delete(new LambdaQueryWrapper<ReportDashboard>().in(ReportDashboard::getReportCode, reportCodeList)); 169 reportDashboardService.delete(new LambdaQueryWrapper<ReportDashboard>().in(ReportDashboard::getReportCode, reportCodeList));
150 reportDashboardService.insertBatch(reportDashboardList); 170 reportDashboardService.insertBatch(reportDashboardList);
@@ -152,7 +172,7 @@ public class ExportServiceImpl implements ExportService { @@ -152,7 +172,7 @@ public class ExportServiceImpl implements ExportService {
152 172
153 //新增大屏组件 173 //新增大屏组件
154 List<ReportDashboardWidget> reportDashboardWidgetList = new ArrayList<>(); 174 List<ReportDashboardWidget> reportDashboardWidgetList = new ArrayList<>();
155 - reportList.stream().map(Report::getReportDashboardWidgetList).forEach(reportDashboardWidgetList::addAll); 175 + reportList.stream().map(Report::getReportDashboardWidgetList).distinct().forEach(reportDashboardWidgetList::addAll);
156 if (reportDashboardWidgetList.size() == 0) { 176 if (reportDashboardWidgetList.size() == 0) {
157 return; 177 return;
158 } 178 }
@@ -162,7 +182,7 @@ public class ExportServiceImpl implements ExportService { @@ -162,7 +182,7 @@ public class ExportServiceImpl implements ExportService {
162 //新增数据集 182 //新增数据集
163 List<String> setCodeList = reportDashboardWidgetList.stream().map(ReportDashboardWidget::getSetCode).distinct().collect(Collectors.toList()); 183 List<String> setCodeList = reportDashboardWidgetList.stream().map(ReportDashboardWidget::getSetCode).distinct().collect(Collectors.toList());
164 List<DataSet> dataSetList = new ArrayList<>(); 184 List<DataSet> dataSetList = new ArrayList<>();
165 - reportDashboardWidgetList.stream().map(ReportDashboardWidget::getDataSetList).forEach(dataSetList::addAll); 185 + reportDashboardWidgetList.stream().map(ReportDashboardWidget::getDataSetList).distinct().forEach(dataSetList::addAll);
166 if (dataSetList.size() != 0) { 186 if (dataSetList.size() != 0) {
167 dataSetService.delete(new LambdaQueryWrapper<DataSet>().in(DataSet::getSetCode, setCodeList)); 187 dataSetService.delete(new LambdaQueryWrapper<DataSet>().in(DataSet::getSetCode, setCodeList));
168 for (DataSet dataSet : dataSetList) { 188 for (DataSet dataSet : dataSetList) {
@@ -175,7 +195,7 @@ public class ExportServiceImpl implements ExportService { @@ -175,7 +195,7 @@ public class ExportServiceImpl implements ExportService {
175 195
176 //新增数据集参数 196 //新增数据集参数
177 List<DataSetParam> dataSetParamList = new ArrayList<>(); 197 List<DataSetParam> dataSetParamList = new ArrayList<>();
178 - dataSetList.stream().map(DataSet::getDataSetParamList).forEach(dataSetParamList::addAll); 198 + dataSetList.stream().map(DataSet::getDataSetParamList).distinct().forEach(dataSetParamList::addAll);
179 if (dataSetParamList.size() != 0) { 199 if (dataSetParamList.size() != 0) {
180 dataSetParamService.delete(new LambdaQueryWrapper<DataSetParam>().in(DataSetParam::getSetCode, setCodeList)); 200 dataSetParamService.delete(new LambdaQueryWrapper<DataSetParam>().in(DataSetParam::getSetCode, setCodeList));
181 dataSetParamService.insertBatch(dataSetParamList); 201 dataSetParamService.insertBatch(dataSetParamList);
@@ -183,7 +203,7 @@ public class ExportServiceImpl implements ExportService { @@ -183,7 +203,7 @@ public class ExportServiceImpl implements ExportService {
183 203
184 //新增数据源转换 204 //新增数据源转换
185 List<DataSetTransform> dataSetTransformList = new ArrayList<>(); 205 List<DataSetTransform> dataSetTransformList = new ArrayList<>();
186 - dataSetList.stream().map(DataSet::getDataSetTransformList).forEach(dataSetTransformList::addAll); 206 + dataSetList.stream().map(DataSet::getDataSetTransformList).distinct().forEach(dataSetTransformList::addAll);
187 if (dataSetTransformList.size() != 0) { 207 if (dataSetTransformList.size() != 0) {
188 dataSetTransformService.delete(new LambdaQueryWrapper<DataSetTransform>().in(DataSetTransform::getSetCode, setCodeList)); 208 dataSetTransformService.delete(new LambdaQueryWrapper<DataSetTransform>().in(DataSetTransform::getSetCode, setCodeList));
189 dataSetTransformService.insertBatch(dataSetTransformList); 209 dataSetTransformService.insertBatch(dataSetTransformList);