InvSourceController.java 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package com.ydtech.modules.inv.controller;
  2. import com.ydtech.core.page.HttpResult;
  3. import com.ydtech.modules.inv.model.excel.InvSourceExcel;
  4. import com.ydtech.modules.inv.model.po.InvSource;
  5. import com.ydtech.modules.inv.service.InvSourceService;
  6. import com.ydtech.modules.inv.utils.EasyExcelUtils;
  7. import io.swagger.annotations.Api;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.web.bind.annotation.*;
  10. import java.util.Arrays;
  11. /**
  12. * @author jianlong
  13. * @date 2024-01-20 10:38
  14. */
  15. @Api(tags = "来源表", value = "来源表")
  16. @RequestMapping("Source")
  17. @RestController
  18. public class InvSourceController {
  19. @Autowired
  20. private InvSourceService invSourceService;
  21. private static String concatenateWithSlash(String[] array) {
  22. StringBuilder result = new StringBuilder();
  23. for (String segment : array) {
  24. result.append(segment).append("/");
  25. }
  26. // Remove the trailing "/"
  27. if (result.length() > 0) {
  28. result.setLength(result.length() - 1);
  29. }
  30. return result.toString();
  31. }
  32. private static int findIndex(String[] array, String target) {
  33. for (int i = 0; i < array.length; i++) {
  34. // Use the indexOf method to check for the target string
  35. if (array[i].indexOf(target) != -1) {
  36. return i;
  37. }
  38. }
  39. return -1; // Return -1 if the element is not found
  40. }
  41. @GetMapping("excel")
  42. public HttpResult readExcel(@RequestParam("file") String file ) {
  43. String[] pathSegments = file.split("/");
  44. int index = findIndex(pathSegments,"upload");
  45. String[] subarray = Arrays.copyOfRange(pathSegments, index+1, pathSegments.length);
  46. String filename = concatenateWithSlash(subarray);
  47. return invSourceService.readExcel(filename);
  48. }
  49. }