CommonController.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package cn.cslg.pas.controller;
  2. import cn.cslg.pas.common.core.base.Constants;
  3. import cn.cslg.pas.common.utils.*;
  4. import cn.cslg.pas.domain.SystemDict;
  5. import cn.cslg.pas.domain.SystemDictAssociate;
  6. import cn.cslg.pas.service.AreaService;
  7. import cn.cslg.pas.service.SystemDictAssociateService;
  8. import cn.cslg.pas.service.SystemDictService;
  9. import cn.hutool.core.collection.CollUtil;
  10. import cn.hutool.core.date.DateUtil;
  11. import cn.hutool.core.io.FileUtil;
  12. import cn.hutool.core.io.IoUtil;
  13. import cn.hutool.core.io.resource.ClassPathResource;
  14. import cn.hutool.core.io.resource.ResourceUtil;
  15. import cn.hutool.core.util.IdUtil;
  16. import cn.hutool.core.util.NumberUtil;
  17. import cn.hutool.poi.excel.ExcelUtil;
  18. import cn.hutool.poi.excel.ExcelWriter;
  19. import io.swagger.v3.oas.annotations.Operation;
  20. import io.swagger.v3.oas.annotations.tags.Tag;
  21. import lombok.RequiredArgsConstructor;
  22. import org.springframework.context.annotation.Lazy;
  23. import org.springframework.core.io.FileSystemResource;
  24. import org.springframework.http.HttpHeaders;
  25. import org.springframework.http.HttpStatus;
  26. import org.springframework.http.ResponseEntity;
  27. import org.springframework.web.bind.annotation.*;
  28. import org.springframework.web.multipart.MultipartFile;
  29. import javax.servlet.ServletOutputStream;
  30. import javax.servlet.http.HttpServletRequest;
  31. import javax.servlet.http.HttpServletResponse;
  32. import java.io.*;
  33. import java.net.URLEncoder;
  34. import java.util.*;
  35. import java.util.stream.Collectors;
  36. @Tag(name = "通用功能")
  37. @CrossOrigin
  38. @RestController
  39. @RequestMapping(Constants.API_VERSION_V2 + "/common")
  40. @RequiredArgsConstructor(onConstructor_ = {@Lazy})
  41. public class CommonController {
  42. private final CacheUtils cacheUtils;
  43. private final AreaService areaService;
  44. private final SystemDictService systemDictService;
  45. private final SystemDictAssociateService systemDictAssociateService;
  46. private final FileUtils fileUtils;
  47. @GetMapping("area")
  48. @Operation(summary = "获取区域")
  49. public String getAreaList() {
  50. return Response.success(areaService.getAreaList());
  51. }
  52. @GetMapping("data")
  53. @Operation(summary = "通用数据")
  54. public Object getCommonData(String keys) {
  55. List<String> typeList = StringUtils.changeStringToString(keys, ",");
  56. List<SystemDict> systemDictList = systemDictService.getSystemDictListByType(typeList);
  57. Map<String, Object> map = new HashMap<>();
  58. for (String type : typeList) {
  59. map.put(type, systemDictList.stream().filter(item -> item.getType().equals(type)).collect(Collectors.toList()));
  60. }
  61. return Response.success(map);
  62. }
  63. @GetMapping("getDictTreeByParentDictValue")
  64. @Operation(summary = "获取字典项之间的级联关系")
  65. public String getDictTreeByParentDictValue(@RequestParam(value = "value") List<String> value, String type, Integer flag) {
  66. List<SystemDictAssociate> systemDictAssociates = systemDictAssociateService.getDictTreeByParentDictValue(value, type, flag);
  67. return Response.success(systemDictAssociates);
  68. }
  69. @GetMapping("static")
  70. @Operation(summary = "获取静态文件")
  71. public String getStaticFile(String path, String type, HttpServletResponse response, HttpServletRequest request) {
  72. String str = ResourceUtil.readUtf8Str(new ClassPathResource("/static/" + path).getPath());
  73. return Response.success(JsonUtils.jsonToMap(str));
  74. }
  75. @PostMapping("export")
  76. @Operation(summary = "导出分析结果")
  77. public void export(@RequestBody Map<String, Object> count, Integer type, String xAxis, HttpServletResponse response, HttpServletRequest request) {
  78. try {
  79. ExcelWriter writer;
  80. if (type != 3) {
  81. List<List<String>> rows = CollUtil.newArrayList(this.getRowsData(count, type, xAxis));
  82. writer = ExcelUtil.getWriter();
  83. writer.write(rows, true);
  84. } else {
  85. Set<String> rs = new HashSet<>(count.keySet());
  86. writer = new ExcelWriter();
  87. int i = 0;
  88. for (String key : rs) {
  89. writer.setSheet(i);
  90. writer.renameSheet(i, key);
  91. List<List<String>> rows = CollUtil.newArrayList(this.getRowsData(JsonUtils.jsonToMap(count.get(key).toString()), 1, xAxis));
  92. writer.write(rows, true);
  93. i++;
  94. }
  95. }
  96. response.setContentType("application/vnd.ms-excel;charset=utf-8");
  97. response.setHeader("Content-Disposition", "attachment;filename=" + DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xls");
  98. ServletOutputStream out = response.getOutputStream();
  99. writer.flush(out, true);
  100. writer.close();
  101. IoUtil.close(out);
  102. } catch (Exception e) {
  103. // e.printStackTrace();
  104. }
  105. }
  106. private List<List<String>> getRowsData(Map<String, Object> count, Integer type, String xAxis) {
  107. List<List<String>> row = new ArrayList<>();
  108. switch (type) {
  109. case 1:
  110. row.add(CollUtil.newArrayList(xAxis, "数量"));
  111. for (String key : count.keySet()) {
  112. row.add(CollUtil.newArrayList(key, this.getValue(count.get(key))));
  113. }
  114. break;
  115. case 2:
  116. Set<String> cols = new HashSet<>();
  117. for (String x : count.keySet()) {
  118. Map<String, Object> map = JsonUtils.objectToMap(count.get(x));
  119. cols.addAll(map.keySet());
  120. }
  121. List<String> cs = new ArrayList<>();
  122. cs.add(xAxis);
  123. cs.addAll(cols);
  124. row.add(CollUtil.newArrayList(cs));
  125. for (String x : count.keySet()) {
  126. List<String> rs = new ArrayList<>();
  127. Map<String, Object> map = JsonUtils.objectToMap(count.get(x));
  128. rs.add(x);
  129. for (String y : cols) {
  130. rs.add(this.getValue(map.get(y)));
  131. }
  132. row.add(CollUtil.newArrayList(rs));
  133. }
  134. break;
  135. }
  136. return row;
  137. }
  138. private String getValue(Object value) {
  139. if (StringUtils.isNotNull(value)) {
  140. return NumberUtil.roundStr(value.toString(), 0);
  141. } else {
  142. return "0";
  143. }
  144. }
  145. @GetMapping("download")
  146. @Operation(summary = "下载文件")
  147. public ResponseEntity<FileSystemResource> downloadSystemFile2(String url) throws IOException {
  148. File file = new File(fileUtils.getSystemPath() + url);
  149. HttpHeaders headers = new HttpHeaders();
  150. String fileName = DateUtils.getNowTimeFormat(DateUtils.YYYYMMDDHHMMSS) + "." + FileUtil.extName(file.getName());
  151. headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
  152. HttpStatus status = HttpStatus.OK;
  153. return new ResponseEntity<>(new FileSystemResource(file), headers, status);
  154. }
  155. @PostMapping("/upload/chunks")
  156. @Operation(summary = "上传分片")
  157. public String uploadChunks(MultipartFile file, String md5, Integer index) throws IOException {
  158. String tempPath = fileUtils.getTempPath(md5);
  159. File tempDirectory = new File(tempPath);
  160. if (!tempDirectory.exists()) {
  161. tempDirectory.mkdir();
  162. }
  163. String savePath = tempPath + FileUtils.FILE_SEPARATOR + md5 + "-" + index;
  164. file.transferTo(new File(savePath));
  165. return Response.success(true);
  166. }
  167. @PostMapping("/upload/chunks/merge")
  168. @Operation(summary = "合并分片")
  169. public String uploadChunksMerge(String md5, String fileName) {
  170. String tempPath = fileUtils.getTempPath(md5);
  171. List<File> fileList = FileUtil.loopFiles(tempPath);
  172. String tempFileName = IdUtil.simpleUUID() + "." + FileUtil.extName(fileName);
  173. String saveTempPath = fileUtils.getTempPath(tempFileName);
  174. String saveUrl = fileUtils.getDirectory(tempFileName);
  175. String savePath = fileUtils.getSystemPath(saveUrl);
  176. try (RandomAccessFile raf = new RandomAccessFile(new File(saveTempPath), "rw")) {
  177. for (int i = 0; i < fileList.size(); i++) {
  178. RandomAccessFile reader = new RandomAccessFile(new File(tempPath + FileUtils.FILE_SEPARATOR + md5 + "-" + i), "r");
  179. byte[] b = new byte[1024];
  180. int n = 0;
  181. while ((n = reader.read(b)) != -1) {
  182. raf.write(b, 0, n);
  183. }
  184. reader.close();
  185. }
  186. } catch (Exception e) {
  187. e.printStackTrace();
  188. }
  189. FileUtil.copy(saveTempPath, savePath, true);
  190. FileUtil.del(tempPath);
  191. FileUtil.del(saveTempPath);
  192. return Response.success(saveUrl);
  193. }
  194. }