浏览代码

1.完成对前端到后端跨域问题的解决2.完成基本的销售导入代码开发3.测试导入数据2条,责任中心导入时有重复插入的问题

胡北宽 4 个月前
父节点
当前提交
65d531c66a

+ 19
- 11
src/main/java/com/example/backend/config/CorsConfig.java 查看文件

@@ -1,27 +1,35 @@
1
-package com.example.backend.config;
2
-
3 1
 import org.springframework.context.annotation.Bean;
4 2
 import org.springframework.context.annotation.Configuration;
5 3
 import org.springframework.web.cors.CorsConfiguration;
6 4
 import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
7 5
 import org.springframework.web.filter.CorsFilter;
6
+import org.springframework.web.servlet.config.annotation.CorsRegistry;
7
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
8
+
8 9
 
9 10
 @Configuration
10
-public class CorsConfig {
11
+public class CorsConfig implements WebMvcConfigurer {
12
+
13
+    @Override
14
+    public void addCorsMappings(CorsRegistry registry) {
15
+        registry.addMapping("/**")
16
+                .allowedOriginPatterns("*")
17
+                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
18
+                .allowedHeaders("*")
19
+                .allowCredentials(true)
20
+                .maxAge(3600);
21
+    }
11 22
 
12 23
     @Bean
13 24
     public CorsFilter corsFilter() {
14
-        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
15 25
         CorsConfiguration config = new CorsConfiguration();
26
+        config.addAllowedOriginPattern("*");
16 27
         config.setAllowCredentials(true);
17
-        // 设置允许的域名,*表示允许所有域名
18
-        config.addAllowedOrigin("*");
19
-        // 设置允许的请求头
20
-        config.addAllowedHeader("*");
21
-        // 设置允许的请求方法
22 28
         config.addAllowedMethod("*");
29
+        config.addAllowedHeader("*");
30
+
31
+        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
23 32
         source.registerCorsConfiguration("/**", config);
24 33
         return new CorsFilter(source);
25 34
     }
26
-
27
-}
35
+}

+ 3
- 2
src/main/java/com/example/backend/controller/contract/PurchaseContractController.java 查看文件

@@ -1,5 +1,6 @@
1 1
 package com.example.backend.controller.contract;
2 2
 
3
+import com.example.backend.service.contract.PurchaseContractService;
3 4
 import com.example.backend.service.contract.SalesContractService;
4 5
 import org.springframework.beans.factory.annotation.Autowired;
5 6
 import org.springframework.http.ResponseEntity;
@@ -20,7 +21,7 @@ import java.util.Map;
20 21
 public class PurchaseContractController {
21 22
 
22 23
     @Autowired
23
-    private SalesContractService salesContractService;
24
+    private PurchaseContractService purchaseContractService;
24 25
 
25 26
     /**
26 27
      * 导入Excel文件
@@ -29,7 +30,7 @@ public class PurchaseContractController {
29 30
     public ResponseEntity<Map<String, Object>> importExcel(@RequestParam("file") MultipartFile file) {
30 31
         Map<String, Object> result = new HashMap<>();
31 32
         try {
32
-            boolean success = salesContractService.importExcel(file);
33
+            boolean success = purchaseContractService.importExcel(file);
33 34
             if (success) {
34 35
                 result.put("code", 200);
35 36
                 result.put("message", "导入成功");

+ 23
- 7
src/main/java/com/example/backend/controller/contract/SalesContractController.java 查看文件

@@ -1,6 +1,7 @@
1 1
 package com.example.backend.controller.contract;
2 2
 
3 3
 import com.example.backend.service.contract.SalesContractService;
4
+import com.example.backend.util.ExcelImportResult;
4 5
 import org.springframework.beans.factory.annotation.Autowired;
5 6
 import org.springframework.http.ResponseEntity;
6 7
 import org.springframework.web.bind.annotation.*;
@@ -14,6 +15,7 @@ import java.util.Map;
14 15
  */
15 16
 @RestController
16 17
 @RequestMapping("/salesContract")
18
+@CrossOrigin(origins = "*") // 添加这个注解
17 19
 public class SalesContractController {
18 20
 
19 21
     @Autowired
@@ -26,20 +28,34 @@ public class SalesContractController {
26 28
     public ResponseEntity<Map<String, Object>> importExcel(@RequestParam("file") MultipartFile file) {
27 29
         Map<String, Object> result = new HashMap<>();
28 30
         try {
29
-            boolean success = salesContractService.importExcel(file);
30
-            if (success) {
31
+            ExcelImportResult<Void> importResult = salesContractService.importExcelSales(file);
32
+
33
+            if (importResult.isSuccess()) {
31 34
                 result.put("code", 200);
32
-                result.put("message", "导入成功");
35
+                result.put("success", true);
36
+                result.put("message", importResult.getMessage());
37
+                // 添加统计信息
38
+                if (importResult.getTotalCount() != null) {
39
+                    result.put("totalCount", importResult.getTotalCount());
40
+                    result.put("successCount", importResult.getSuccessCount());
41
+                    result.put("errorCount", importResult.getErrorCount());
42
+                }
33 43
             } else {
34 44
                 result.put("code", 500);
35
-                result.put("message", "导入失败");
45
+                result.put("success", false);
46
+                result.put("message", importResult.getMessage());
47
+                // 添加详细的错误信息
48
+                if (importResult.getErrors() != null && !importResult.getErrors().isEmpty()) {
49
+                    result.put("errors", importResult.getErrors());
50
+                    result.put("errorCount", importResult.getErrors().size());
51
+                }
36 52
             }
37 53
         } catch (Exception e) {
38 54
             result.put("code", 500);
39
-            result.put("message", "导入失败:" + e.getMessage());
55
+            result.put("success", false);
56
+            result.put("message", "系统处理异常:" + e.getMessage());
40 57
             e.printStackTrace();
41 58
         }
42 59
         return ResponseEntity.ok(result);
43 60
     }
44
-
45
-}
61
+}

+ 2
- 23
src/main/java/com/example/backend/service/contract/SalesContractService.java 查看文件

@@ -1,6 +1,7 @@
1 1
 package com.example.backend.service.contract;
2 2
 
3 3
 import com.example.backend.entity.contract.SalesContractEntity;
4
+import com.example.backend.util.ExcelImportResult;
4 5
 import org.springframework.stereotype.Component;
5 6
 import org.springframework.stereotype.Service;
6 7
 import org.springframework.web.multipart.MultipartFile;
@@ -12,29 +13,7 @@ import java.util.List;
12 13
  */
13 14
 
14 15
 public interface SalesContractService {
16
+    ExcelImportResult<Void> importExcelSales(MultipartFile file);
15 17
 
16
-    /**
17
-     * 导入Excel文件
18
-     * @param file Excel文件
19
-     * @return 导入结果
20
-     */
21
-    boolean importExcel(MultipartFile file);
22
-
23
-    /**
24
-     * 保存合同及明细
25
-     * @param contract 销售合同
26
-     * @param products 产品明细
27
-     * @param deliveries 交货明细
28
-     * @param payments 付款明细
29
-     * @param qualities 质量保证明细
30
-     * @param terms 其他条款
31
-     * @return 保存结果
32
-     */
33
-//    boolean saveContractWithDetails(SalesContractEntity contract,
34
-//                                    List<?> products,
35
-//                                    List<?> deliveries,
36
-//                                    List<?> payments,
37
-//                                    List<?> qualities,
38
-//                                    List<?> terms);
39 18
 
40 19
 }

+ 1
- 0
src/main/java/com/example/backend/service/contract/impl/PurchaseContractServiceImpl.java 查看文件

@@ -4,6 +4,7 @@ import com.example.backend.entity.contract.*;
4 4
 import com.example.backend.excel.*;
5 5
 import com.example.backend.mapper.contract.*;
6 6
 import com.example.backend.service.contract.PurchaseContractService;
7
+import com.example.backend.util.ExcelUtils;
7 8
 import org.springframework.beans.BeanUtils;
8 9
 import org.springframework.beans.factory.annotation.Autowired;
9 10
 import org.springframework.stereotype.Service;

+ 1673
- 760
src/main/java/com/example/backend/service/contract/impl/SalesContractServiceImpl.java
文件差异内容过多而无法显示
查看文件


src/main/java/com/example/backend/excel/DateUtils.java → src/main/java/com/example/backend/util/DateUtils.java 查看文件

@@ -1,4 +1,4 @@
1
-package com.example.backend.excel;
1
+package com.example.backend.util;
2 2
 
3 3
 import java.time.LocalDateTime;
4 4
 import java.time.format.DateTimeFormatter;

+ 57
- 0
src/main/java/com/example/backend/util/ExcelImportResult.java 查看文件

@@ -0,0 +1,57 @@
1
+package com.example.backend.util;
2
+
3
+import lombok.Data;
4
+import java.util.List;
5
+
6
+@Data
7
+public class ExcelImportResult<T> {
8
+    
9
+    private boolean success;
10
+    private String message;
11
+    private List<String> errors;
12
+    private T data;
13
+    private Integer totalCount;
14
+    private Integer successCount;
15
+    private Integer errorCount;
16
+    
17
+    public ExcelImportResult() {}
18
+    
19
+    // 成功结果
20
+    public static <T> ExcelImportResult<T> success(String message) {
21
+        return success(message, null);
22
+    }
23
+    
24
+    public static <T> ExcelImportResult<T> success(String message, T data) {
25
+        ExcelImportResult<T> result = new ExcelImportResult<>();
26
+        result.setSuccess(true);
27
+        result.setMessage(message);
28
+        result.setData(data);
29
+        return result;
30
+    }
31
+    
32
+    public static <T> ExcelImportResult<T> success(String message, Integer totalCount, Integer successCount) {
33
+        ExcelImportResult<T> result = new ExcelImportResult<>();
34
+        result.setSuccess(true);
35
+        result.setMessage(message);
36
+        result.setTotalCount(totalCount);
37
+        result.setSuccessCount(successCount);
38
+        result.setErrorCount(totalCount - successCount);
39
+        return result;
40
+    }
41
+    
42
+    // 失败结果
43
+    public static <T> ExcelImportResult<T> error(String message) {
44
+        return error(message, null);
45
+    }
46
+    
47
+    public static <T> ExcelImportResult<T> error(String message, List<String> errors) {
48
+        ExcelImportResult<T> result = new ExcelImportResult<>();
49
+        result.setSuccess(false);
50
+        result.setMessage(message);
51
+        result.setErrors(errors);
52
+        if (errors != null) {
53
+            result.setErrorCount(errors.size());
54
+        }
55
+        return result;
56
+    }
57
+}

src/main/java/com/example/backend/excel/ExcelUtil.java → src/main/java/com/example/backend/util/ExcelUtil.java 查看文件

@@ -1,9 +1,10 @@
1
-package com.example.backend.excel;
1
+package com.example.backend.util;
2 2
 
3 3
 import com.alibaba.excel.EasyExcel;
4 4
 import com.alibaba.excel.context.AnalysisContext;
5 5
 import com.alibaba.excel.event.AnalysisEventListener;
6 6
 import com.alibaba.excel.metadata.data.ReadCellData;
7
+import com.example.backend.excel.*;
7 8
 import org.springframework.web.multipart.MultipartFile;
8 9
 
9 10
 import java.io.IOException;

src/main/java/com/example/backend/excel/ExcelUtils.java → src/main/java/com/example/backend/util/ExcelUtils.java 查看文件

@@ -1,10 +1,11 @@
1
-package com.example.backend.excel;
1
+package com.example.backend.util;
2 2
 
3 3
 import com.alibaba.excel.EasyExcel;
4 4
 import com.alibaba.excel.context.AnalysisContext;
5 5
 import com.alibaba.excel.event.AnalysisEventListener;
6 6
 import com.alibaba.excel.metadata.data.CellData;
7 7
 import com.alibaba.excel.metadata.data.ReadCellData;
8
+import com.example.backend.excel.*;
8 9
 import org.springframework.web.multipart.MultipartFile;
9 10
 
10 11
 import java.io.IOException;

+ 42
- 0
src/main/java/com/example/backend/util/ResponseUtil.java 查看文件

@@ -0,0 +1,42 @@
1
+package com.example.backend.util;
2
+
3
+import org.springframework.http.HttpStatus;
4
+import org.springframework.http.ResponseEntity;
5
+
6
+import java.util.List;
7
+
8
+public class ResponseUtil {
9
+    
10
+    /**
11
+     * 成功响应
12
+     */
13
+    public static <T> ResponseEntity<ExcelImportResult<T>> success(String message) {
14
+        return ResponseEntity.ok(ExcelImportResult.success(message));
15
+    }
16
+    
17
+    public static <T> ResponseEntity<ExcelImportResult<T>> success(String message, T data) {
18
+        return ResponseEntity.ok(ExcelImportResult.success(message, data));
19
+    }
20
+    
21
+    public static <T> ResponseEntity<ExcelImportResult<T>> success(String message, Integer totalCount, Integer successCount) {
22
+        return ResponseEntity.ok(ExcelImportResult.success(message, totalCount, successCount));
23
+    }
24
+    
25
+    /**
26
+     * 失败响应
27
+     */
28
+    public static <T> ResponseEntity<ExcelImportResult<T>> error(String message) {
29
+        return ResponseEntity.status(HttpStatus.BAD_REQUEST)
30
+                .body(ExcelImportResult.error(message));
31
+    }
32
+    
33
+    public static <T> ResponseEntity<ExcelImportResult<T>> error(String message, List<String> errors) {
34
+        return ResponseEntity.status(HttpStatus.BAD_REQUEST)
35
+                .body(ExcelImportResult.error(message, errors));
36
+    }
37
+    
38
+    public static <T> ResponseEntity<ExcelImportResult<T>> error(HttpStatus status, String message, List<String> errors) {
39
+        return ResponseEntity.status(status)
40
+                .body(ExcelImportResult.error(message, errors));
41
+    }
42
+}

正在加载...
取消
保存