huang
7 天以前 f13ba9e05f653bc3083c4d17fe8658e67054131e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.mes.device.controller;
 
import com.mes.device.service.GlassInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
 
import java.util.List;
import java.util.Map;
 
/**
 * @author :huang
 * @date :2025-12-08
 * 工程导入转发接口(接收前端 Excel 行数据,后端组装后转发 MES)
 */
@Slf4j
@RestController
@RequestMapping("excel")
@RequiredArgsConstructor
public class GlassInfoImportController {
 
    private final GlassInfoService glassInfoService;
    private final RestTemplate restTemplate = new RestTemplate();
 
    @Value("${mes.engineering.import-url}")
    private String mesEngineeringImportUrl;
 
    /**
     * 导入工程
     * 前端入参示例:
     * {
     *   "excelRows": [
     *     {"glassId":"GL001","width":"1000","height":"2000","thickness":"5","quantity":"2","orderNumber":"NG25082101","filmsId":"白玻"}
     *   ]
     * }
     */
    @PostMapping("/importExcel")
    public ResponseEntity<?> importEngineer(@RequestBody Map<String, Object> body) {
        Object rowsObj = body.get("excelRows");
        if (!(rowsObj instanceof List)) {
            return ResponseEntity.badRequest().body("excelRows 必须是数组");
        }
        @SuppressWarnings("unchecked")
        List<Map<String, Object>> excelRows = (List<Map<String, Object>>) rowsObj;
        if (CollectionUtils.isEmpty(excelRows)) {
            return ResponseEntity.badRequest().body("excelRows 不能为空");
        }
 
        Map<String, Object> payload = glassInfoService.buildEngineerImportPayload(excelRows);
        log.info("构建的 MES 导入数据: {}", payload);
 
        try {
            ResponseEntity<Map> mesResp = restTemplate.postForEntity(mesEngineeringImportUrl, payload, Map.class);
            return ResponseEntity.status(mesResp.getStatusCode()).body(mesResp.getBody());
        } catch (Exception e) {
            log.error("转发 MES 导入接口失败 url={}, error={}", mesEngineeringImportUrl, e.getMessage(), e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("转发 MES 失败: " + e.getMessage());
        }
    }
}