This commit is contained in:
even 2025-06-06 12:21:10 +08:00
commit 90cb7a430f
3 changed files with 62 additions and 59 deletions

View File

@ -12,6 +12,7 @@ import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@ -53,11 +54,11 @@ public class ReportController {
@PostMapping(path="/downLoadReport")
public CommonResult<String> downLoadReport(@RequestBody @NotNull @Valid BaseIdReq req) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
public CommonResult<ResponseEntity<byte[]>> downLoadReport(@RequestBody @NotNull @Valid BaseIdReq req) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
String xmTraceId = reportService.downLoadReport(req);
ResponseEntity<byte[]> fileInputStream = reportService.downLoadReport(req);
return CommonResult.success(xmTraceId);
return CommonResult.success(fileInputStream);
}

View File

@ -6,6 +6,7 @@ import cd.casic.ci.process.process.dataObject.base.BaseIdPageReq;
import cd.casic.ci.process.process.dataObject.base.BaseIdReq;
import cd.casic.framework.commons.pojo.PageResult;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
@ -23,5 +24,5 @@ public interface ReportService{
ReportResp deleteReport(@Valid ReportDeleteReq req) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException;
String downLoadReport(@Valid BaseIdReq req) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException;
ResponseEntity<byte[]> downLoadReport(@Valid BaseIdReq req) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException;
}

View File

@ -9,15 +9,11 @@ import cd.casic.ci.process.process.service.report.ReportService;
import cd.casic.framework.commons.exception.ServiceException;
import cd.casic.framework.commons.exception.enums.GlobalErrorCodeConstants;
import cd.casic.framework.commons.pojo.PageResult;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;
import java.security.KeyManagementException;
@ -39,15 +35,16 @@ import static cd.casic.ci.process.engine.worker.base.HttpWorker.getRestTemplateW
@Slf4j
public class ReportServiceImpl implements ReportService {
public static final String PATH = "/openapi/v1/asset/report/downLoadReport/batch";
@Override
public PageResult<ReportResp> findReportList(BaseIdPageReq req) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
RestTemplate restTemplate = getRestTemplateWithoutSANCheck();
String reportFindUrl = ConstantContextHolder.getScaIp() + "/openapi/v1/asset/report/list";
MultiValueMap<String, Object> body = buildFindRequestBody(req);
String body = buildFindRequestBody(req);
HttpHeaders headers = createHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("OpenApiUserToken", ConstantContextHolder.getScaToken());
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
HttpEntity<String> requestEntity = new HttpEntity<>(body, headers);
log.info("报告分页查询接口:" + reportFindUrl);
JSONObject response = restTemplate.postForObject(reportFindUrl, requestEntity, JSONObject.class);
@ -72,12 +69,11 @@ public class ReportServiceImpl implements ReportService {
@Override
public ReportResp deleteReport(ReportDeleteReq req) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
RestTemplate restTemplate = getRestTemplateWithoutSANCheck();
String reportDeleteUrl = ConstantContextHolder.getScaIp() + "/openapi/v1/asset/report/delet";
MultiValueMap<String, Object> body = buildDeleteRequestBody(req);
String reportDeleteUrl = ConstantContextHolder.getScaIp() + "/openapi/v1/asset/report/delete";
String body = buildDeleteRequestBody(req);
HttpHeaders headers = createHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("OpenApiUserToken", ConstantContextHolder.getScaToken());
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
HttpEntity<String> requestEntity = new HttpEntity<>(body, headers);
log.info("报告删除接口:" + reportDeleteUrl);
JSONObject response = restTemplate.postForObject(reportDeleteUrl, requestEntity, JSONObject.class);
@ -99,27 +95,28 @@ public class ReportServiceImpl implements ReportService {
@Override
public String downLoadReport(BaseIdReq req) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
public ResponseEntity<byte[]> downLoadReport(BaseIdReq req) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
RestTemplate restTemplate = getRestTemplateWithoutSANCheck();
String reportDeleteUrl = ConstantContextHolder.getScaIp() + "/openapi/v1/asset/report/downLoadReport/batch";
MultiValueMap<String, Object> body = buildDownloadRequestBody(req);
String reportDeleteUrl = ConstantContextHolder.getScaIp() + PATH;
String body = buildDownloadRequestBody(req);
HttpHeaders headers = createHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("OpenApiUserToken", ConstantContextHolder.getScaToken());
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
HttpEntity<String> requestEntity = new HttpEntity<>(body, headers);
log.info("报告下载接口:" + reportDeleteUrl);
JSONObject response = restTemplate.postForObject(reportDeleteUrl, requestEntity, JSONObject.class);
String message = response.getString("message");
if (message.equals("success")) {
ReportResp reportResp = new ReportResp();
ResponseEntity<byte[]> responseEntity = restTemplate.postForEntity(reportDeleteUrl, requestEntity, byte[].class);
if (responseEntity.getStatusCode() == HttpStatus.OK && responseEntity.hasBody()) {
log.info("===============报告下载接口成功=================");
return response.getString("xmTraceId");
} else {
// 构建返回给前端的 ResponseEntity<byte[]>
return ResponseEntity.ok()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=report.bin")
.body(responseEntity.getBody());
}else {
log.error("===============报告下载接口失败=================");
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"删除报告失败");
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), "删除报告失败");
}
}
@ -132,40 +129,43 @@ public class ReportServiceImpl implements ReportService {
return headers;
}
private MultiValueMap<String, Object> buildFindRequestBody(BaseIdPageReq req) {
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
if (!ObjectUtils.isEmpty(req.getProjectId())){
body.add("projectId", req.getProjectId());
private String buildFindRequestBody(BaseIdPageReq req) {
JSONObject json = new JSONObject();
if (req.getProjectId() != null) {
json.put("projectId", req.getProjectId());
}
body.add("pageNum", req.getPageNo());
body.add("pageSize", req.getPageSize());
return body;
json.put("pageNum", req.getPageNo());
json.put("pageSize", req.getPageSize());
return json.toJSONString();
}
private MultiValueMap<String, Object> buildDeleteRequestBody(ReportDeleteReq req) {
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("endDate", req.getEndDate());
body.add("name", req.getName());
body.add("projectIdList", req.getProjectIdList());
body.add("reportId", req.getReportId());
body.add("sourceCode", req.getSourceCode());
body.add("startDate", req.getStartDate());
body.add("status", req.getStatus());
body.add("type", req.getType());
body.add("use", req.getUse());
return body;
private String buildDeleteRequestBody(ReportDeleteReq req) {
JSONObject json = new JSONObject();
json.put("endDate", req.getEndDate());
json.put("name", req.getName());
json.put("projectIdList", req.getProjectIdList());
json.put("reportId", req.getReportId());
json.put("sourceCode", req.getSourceCode());
json.put("startDate", req.getStartDate());
json.put("status", req.getStatus());
json.put("type", req.getType());
json.put("use", req.getUse());
return json.toJSONString();
}
private MultiValueMap<String, Object> buildDownloadRequestBody(BaseIdReq req) {
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("reportIds", req.getReportIds());
return body;
private String buildDownloadRequestBody(BaseIdReq req) {
JSONObject json = new JSONObject();
json.put("reportIds", req.getReportIds());
return json.toJSONString();
}
private void setReportFindResp(JSONObject data, String xmTraceId, List<ReportResp> respList) {
data.getJSONArray("records").forEach(item -> {
JSONObject report = (JSONObject) item;
JSONArray records = data.getJSONArray("records");
for (int i = 0; i < records.size(); i++) {
JSONObject report = records.getJSONObject(i);
ReportResp reportResp = new ReportResp();
reportResp.setId(report.getInteger("id"));
@ -183,13 +183,14 @@ public class ReportServiceImpl implements ReportService {
reportResp.setXmTraceId(xmTraceId);
List<Integer> typeList = new ArrayList<>();
report.getJSONArray("type").forEach(o->{
typeList.add((Integer) o);
});
JSONArray typeArray = report.getJSONArray("type");
for (int j = 0; j < typeArray.size(); j++) {
typeList.add(typeArray.getInteger(j));
}
reportResp.setType(typeList);
respList.add(reportResp);
});
}
}