ReportDataController.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package jnpf.controller;
  2. import cn.hutool.core.codec.Base64;
  3. import cn.hutool.http.HttpRequest;
  4. import cn.hutool.http.Method;
  5. import io.swagger.v3.oas.annotations.Operation;
  6. import io.swagger.v3.oas.annotations.Parameter;
  7. import io.swagger.v3.oas.annotations.Parameters;
  8. import io.swagger.v3.oas.annotations.tags.Tag;
  9. import jnpf.base.ActionResult;
  10. import jnpf.constant.FileTypeConstant;
  11. import jnpf.entity.FileParameter;
  12. import jnpf.enums.ImageEnum;
  13. import jnpf.model.report.ReportPagination;
  14. import jnpf.model.report.UploaderVO;
  15. import jnpf.service.ReportVersionService;
  16. import jnpf.univer.model.UniverPreview;
  17. import jnpf.util.*;
  18. import org.dromara.x.file.storage.core.FileInfo;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.http.MediaType;
  21. import org.springframework.web.bind.annotation.*;
  22. import org.springframework.web.multipart.MultipartFile;
  23. import java.io.IOException;
  24. import java.util.Map;
  25. import java.util.Objects;
  26. import java.util.regex.Matcher;
  27. import java.util.regex.Pattern;
  28. /**
  29. * @author :JNPF开发平台组
  30. * @version: V3.1.0
  31. * @copyright 引迈信息技术有限公司
  32. * @date :2024/5/11 下午4:35
  33. */
  34. @Tag(name = "报表数据", description = "Report")
  35. @RestController
  36. @RequestMapping("/api/Report/data")
  37. public class ReportDataController {
  38. @Autowired
  39. private ReportVersionService reportVersionService;
  40. @Operation(summary = "预览")
  41. @Parameters({
  42. @Parameter(name = "id", description = "模板id")
  43. })
  44. @PostMapping("/{id}/preview")
  45. public ActionResult preview(@PathVariable String id, @RequestBody Map<String, Object> params) {
  46. ReportPagination pagination = JsonUtil.getJsonToBean(params, ReportPagination.class);
  47. UniverPreview preview = reportVersionService.preview(id, pagination, params);
  48. return ActionResult.success(preview);
  49. }
  50. @Operation(summary = "预览")
  51. @Parameters({
  52. @Parameter(name = "id", description = "模板id")
  53. })
  54. @PostMapping("/{id}/previewTemplate")
  55. public ActionResult previewTemplate(@PathVariable String id, @RequestBody Map<String, Object> params) {
  56. ReportPagination pagination = JsonUtil.getJsonToBean(params, ReportPagination.class);
  57. UniverPreview preview = reportVersionService.previewTemplate(id, pagination, params);
  58. return ActionResult.success(preview);
  59. }
  60. @Operation(summary = "上传图片")
  61. @PostMapping(value = "/upload/file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  62. public ActionResult upload(@RequestPart("file") MultipartFile file) {
  63. UploaderVO vo = new UploaderVO();
  64. try {
  65. String fileName = RandomUtil.uuId() + ".jpeg";
  66. String url = "/api/Report/data/Download?name=" + fileName + "&encryption=";
  67. FileInfo fileInfo = FileUploadUtils.uploadFile(new FileParameter(FileTypeConstant.ANNEX, fileName), file);
  68. vo.setName(fileInfo.getFilename());
  69. vo.setUrl(UploaderUtil.uploaderFile(url, fileInfo.getFilename() + "#" + FileTypeConstant.ANNEX));
  70. } catch (Exception e) {
  71. }
  72. return ActionResult.success(vo);
  73. }
  74. @Operation(summary = "远端接口下载图片")
  75. @PostMapping(value = "/downImg")
  76. public ActionResult upload(@RequestBody ReportPagination pagination) {
  77. UploaderVO vo = new UploaderVO();
  78. try {
  79. byte[] bytes = null;
  80. String imgValue = pagination.getImgValue();
  81. String imgType = pagination.getImgType();
  82. if (StringUtil.isNotEmpty(imgValue)) {
  83. if (Objects.equals(ImageEnum.BASE64.name(), imgType)) {
  84. String regex = "data:image/\\w+;base64,";
  85. String base64Img = imgValue;
  86. Pattern pattern = Pattern.compile(regex);
  87. Matcher matcher = pattern.matcher(imgValue);
  88. if (matcher.find()) {
  89. base64Img = imgValue.replace(matcher.group(),"");
  90. }
  91. bytes = Base64.decode(base64Img);
  92. } else {
  93. HttpRequest request = HttpRequest.of(imgValue).method(Method.GET);
  94. bytes = request.execute().bodyBytes();
  95. }
  96. }
  97. if (bytes != null && bytes.length > 0) {
  98. String fileName = RandomUtil.uuId() + ".jpeg";
  99. String url = "/api/Report/data/Download?name=" + fileName + "&encryption=";
  100. FileInfo fileInfo = FileUploadUtils.uploadFile(new FileParameter(FileTypeConstant.ANNEX, fileName), bytes);
  101. vo.setName(fileInfo.getFilename());
  102. vo.setUrl(UploaderUtil.uploaderFile(url, fileInfo.getFilename() + "#" + FileTypeConstant.ANNEX));
  103. }
  104. } catch (Exception e) {
  105. }
  106. return ActionResult.success(vo);
  107. }
  108. @Operation(summary = "上传excel")
  109. @PostMapping(value = "/ImportExcel", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  110. public ActionResult importExcel(@RequestPart("file") MultipartFile multipartFile) throws IOException {
  111. UniverPreview preview = reportVersionService.importExcel(multipartFile);
  112. return ActionResult.success(preview);
  113. }
  114. @Operation(summary = "下载excel")
  115. @Parameters({
  116. @Parameter(name = "id", description = "模板id")
  117. })
  118. @PostMapping("/{id}/DownExcel")
  119. public ActionResult down(@PathVariable String id, @RequestBody Map<String, Object> params) {
  120. ReportPagination pagination = JsonUtil.getJsonToBean(params, ReportPagination.class);
  121. pagination.setId(id);
  122. UploaderVO vo = reportVersionService.downExcel(pagination);
  123. return ActionResult.success(vo);
  124. }
  125. @NoDataSourceBind()
  126. @Operation(summary = "下载文件")
  127. @GetMapping("/Download")
  128. public void downExcel(String encryption, String name) {
  129. String fileNameAll = DesUtil.aesDecode(encryption).replaceAll("\n", "");
  130. if (!StringUtil.isEmpty(fileNameAll)) {
  131. String[] data = fileNameAll.split("#");
  132. String fileName = data.length > 1 ? data[1] : "";
  133. String type = data.length > 2 ? data[2] : "";
  134. String typePath = FilePathUtil.getFilePath(type.toLowerCase());
  135. FileUploadUtils.downloadFile(new FileParameter(typePath, fileName), inputStream -> {
  136. if (fileName.endsWith(".jpeg")) {
  137. FileDownloadUtil.flushFile(inputStream, name);
  138. } else {
  139. FileDownloadUtil.outFile(inputStream, name);
  140. }
  141. });
  142. }
  143. }
  144. }