Просмотр исходного кода

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

胡北宽 4 месяцев назад
Родитель
Сommit
65d531c66a

+ 19
- 11
src/main/java/com/example/backend/config/CorsConfig.java Просмотреть файл

1
-package com.example.backend.config;
2
-
3
 import org.springframework.context.annotation.Bean;
1
 import org.springframework.context.annotation.Bean;
4
 import org.springframework.context.annotation.Configuration;
2
 import org.springframework.context.annotation.Configuration;
5
 import org.springframework.web.cors.CorsConfiguration;
3
 import org.springframework.web.cors.CorsConfiguration;
6
 import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
4
 import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
7
 import org.springframework.web.filter.CorsFilter;
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
 @Configuration
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
     @Bean
23
     @Bean
13
     public CorsFilter corsFilter() {
24
     public CorsFilter corsFilter() {
14
-        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
15
         CorsConfiguration config = new CorsConfiguration();
25
         CorsConfiguration config = new CorsConfiguration();
26
+        config.addAllowedOriginPattern("*");
16
         config.setAllowCredentials(true);
27
         config.setAllowCredentials(true);
17
-        // 设置允许的域名,*表示允许所有域名
18
-        config.addAllowedOrigin("*");
19
-        // 设置允许的请求头
20
-        config.addAllowedHeader("*");
21
-        // 设置允许的请求方法
22
         config.addAllowedMethod("*");
28
         config.addAllowedMethod("*");
29
+        config.addAllowedHeader("*");
30
+
31
+        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
23
         source.registerCorsConfiguration("/**", config);
32
         source.registerCorsConfiguration("/**", config);
24
         return new CorsFilter(source);
33
         return new CorsFilter(source);
25
     }
34
     }
26
-
27
-}
35
+}

+ 3
- 2
src/main/java/com/example/backend/controller/contract/PurchaseContractController.java Просмотреть файл

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

+ 23
- 7
src/main/java/com/example/backend/controller/contract/SalesContractController.java Просмотреть файл

1
 package com.example.backend.controller.contract;
1
 package com.example.backend.controller.contract;
2
 
2
 
3
 import com.example.backend.service.contract.SalesContractService;
3
 import com.example.backend.service.contract.SalesContractService;
4
+import com.example.backend.util.ExcelImportResult;
4
 import org.springframework.beans.factory.annotation.Autowired;
5
 import org.springframework.beans.factory.annotation.Autowired;
5
 import org.springframework.http.ResponseEntity;
6
 import org.springframework.http.ResponseEntity;
6
 import org.springframework.web.bind.annotation.*;
7
 import org.springframework.web.bind.annotation.*;
14
  */
15
  */
15
 @RestController
16
 @RestController
16
 @RequestMapping("/salesContract")
17
 @RequestMapping("/salesContract")
18
+@CrossOrigin(origins = "*") // 添加这个注解
17
 public class SalesContractController {
19
 public class SalesContractController {
18
 
20
 
19
     @Autowired
21
     @Autowired
26
     public ResponseEntity<Map<String, Object>> importExcel(@RequestParam("file") MultipartFile file) {
28
     public ResponseEntity<Map<String, Object>> importExcel(@RequestParam("file") MultipartFile file) {
27
         Map<String, Object> result = new HashMap<>();
29
         Map<String, Object> result = new HashMap<>();
28
         try {
30
         try {
29
-            boolean success = salesContractService.importExcel(file);
30
-            if (success) {
31
+            ExcelImportResult<Void> importResult = salesContractService.importExcelSales(file);
32
+
33
+            if (importResult.isSuccess()) {
31
                 result.put("code", 200);
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
             } else {
43
             } else {
34
                 result.put("code", 500);
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
         } catch (Exception e) {
53
         } catch (Exception e) {
38
             result.put("code", 500);
54
             result.put("code", 500);
39
-            result.put("message", "导入失败:" + e.getMessage());
55
+            result.put("success", false);
56
+            result.put("message", "系统处理异常:" + e.getMessage());
40
             e.printStackTrace();
57
             e.printStackTrace();
41
         }
58
         }
42
         return ResponseEntity.ok(result);
59
         return ResponseEntity.ok(result);
43
     }
60
     }
44
-
45
-}
61
+}

+ 2
- 23
src/main/java/com/example/backend/service/contract/SalesContractService.java Просмотреть файл

1
 package com.example.backend.service.contract;
1
 package com.example.backend.service.contract;
2
 
2
 
3
 import com.example.backend.entity.contract.SalesContractEntity;
3
 import com.example.backend.entity.contract.SalesContractEntity;
4
+import com.example.backend.util.ExcelImportResult;
4
 import org.springframework.stereotype.Component;
5
 import org.springframework.stereotype.Component;
5
 import org.springframework.stereotype.Service;
6
 import org.springframework.stereotype.Service;
6
 import org.springframework.web.multipart.MultipartFile;
7
 import org.springframework.web.multipart.MultipartFile;
12
  */
13
  */
13
 
14
 
14
 public interface SalesContractService {
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
 import com.example.backend.excel.*;
4
 import com.example.backend.excel.*;
5
 import com.example.backend.mapper.contract.*;
5
 import com.example.backend.mapper.contract.*;
6
 import com.example.backend.service.contract.PurchaseContractService;
6
 import com.example.backend.service.contract.PurchaseContractService;
7
+import com.example.backend.util.ExcelUtils;
7
 import org.springframework.beans.BeanUtils;
8
 import org.springframework.beans.BeanUtils;
8
 import org.springframework.beans.factory.annotation.Autowired;
9
 import org.springframework.beans.factory.annotation.Autowired;
9
 import org.springframework.stereotype.Service;
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
-package com.example.backend.excel;
1
+package com.example.backend.util;
2
 
2
 
3
 import java.time.LocalDateTime;
3
 import java.time.LocalDateTime;
4
 import java.time.format.DateTimeFormatter;
4
 import java.time.format.DateTimeFormatter;

+ 57
- 0
src/main/java/com/example/backend/util/ExcelImportResult.java Просмотреть файл

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

src/main/java/com/example/backend/excel/ExcelUtils.java → src/main/java/com/example/backend/util/ExcelUtils.java Просмотреть файл

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

+ 42
- 0
src/main/java/com/example/backend/util/ResponseUtil.java Просмотреть файл

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
+}

Загрузка…
Отмена
Сохранить