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());
|
}
|
}
|
}
|