CommonController.java 9.7 KB

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