package com.mes.device.controller;
|
|
import com.mes.device.service.GlassInfoService;
|
import lombok.RequiredArgsConstructor;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.http.HttpStatus;
|
import org.springframework.http.ResponseEntity;
|
import org.springframework.util.CollectionUtils;
|
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
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();
|
|
|
/**
|
* 导入工程
|
* 前端入参示例:
|
* {
|
* "excelRows": [
|
* {"glassId":"GL001","width":"1000","height":"2000","thickness":"5","quantity":"2","flowCardId":"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);
|
|
String mesEngineeringImportUrl = glassInfoService.getMesEngineeringImportUrl();
|
|
try {
|
ResponseEntity<Map> mesResp = restTemplate.postForEntity(mesEngineeringImportUrl, payload, Map.class);
|
// 直接返回 MES 的响应,让前端根据响应体中的 code 字段判断是否成功
|
return ResponseEntity.status(mesResp.getStatusCode()).body(mesResp.getBody());
|
} catch (org.springframework.web.client.ResourceAccessException e) {
|
// 连接超时或无法连接
|
log.error("转发 MES 导入接口失败(连接问题) url={}, error={}", mesEngineeringImportUrl, e.getMessage(), e);
|
Map<String, Object> errorResponse = new java.util.HashMap<>();
|
errorResponse.put("code", 500);
|
errorResponse.put("message", "无法连接到 MES 接口,请检查网络连接或联系管理员");
|
errorResponse.put("data", false);
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
|
} catch (Exception e) {
|
// 其他异常
|
log.error("转发 MES 导入接口失败 url={}, error={}", mesEngineeringImportUrl, e.getMessage(), e);
|
Map<String, Object> errorResponse = new java.util.HashMap<>();
|
errorResponse.put("code", 500);
|
errorResponse.put("message", "转发 MES 失败: " + e.getMessage());
|
errorResponse.put("data", false);
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
|
}
|
}
|
}
|