| | |
| | | */ |
| | | @PostMapping("/importExcel") |
| | | public ResponseEntity<?> importEngineer(@RequestBody Map<String, Object> body) { |
| | | // 初始化返回结果和关键标记 |
| | | Map<String, Object> errorResponse = new HashMap<>(); |
| | | boolean mesSuccess = false; |
| | | String engineeringId = null; |
| | | // 1. 校验 |
| | | Object rowsObj = body.get("excelRows"); |
| | | if (!(rowsObj instanceof List)) { |
| | | return ResponseEntity.badRequest().body("excelRows 必须是数组"); |
| | |
| | | return ResponseEntity.badRequest().body("excelRows 不能为空"); |
| | | } |
| | | |
| | | Map<String, Object> payload = glassInfoService.buildEngineerImportPayload(excelRows); |
| | | log.info("构建的 MES 导入数据: {}", payload); |
| | | try { |
| | | // 2. 构建MES导入数据 |
| | | Map<String, Object> payload = glassInfoService.buildEngineerImportPayload(excelRows); |
| | | log.info("构建的 MES 导入数据: {}", payload); |
| | | |
| | | // 从payload中提取工程号(payload中使用的是engineerId) |
| | | String engineeringId = (String) payload.get("engineerId"); |
| | | if (engineeringId == null || engineeringId.isEmpty()) { |
| | | // 如果payload中没有engineerId,尝试从glassInfolList中获取 |
| | | @SuppressWarnings("unchecked") |
| | | List<Map<String, Object>> glassInfoList = (List<Map<String, Object>>) payload.get("glassInfolList"); |
| | | if (glassInfoList != null && !glassInfoList.isEmpty()) { |
| | | Object firstEngineerId = glassInfoList.get(0).get("engineerId"); |
| | | if (firstEngineerId != null) { |
| | | engineeringId = firstEngineerId.toString(); |
| | | // 3. 提取工程号 |
| | | engineeringId = (String) payload.get("engineerId"); |
| | | if (engineeringId == null || engineeringId.isEmpty()) { |
| | | @SuppressWarnings("unchecked") |
| | | List<Map<String, Object>> glassInfoList = (List<Map<String, Object>>) payload.get("glassInfolList"); |
| | | if (glassInfoList != null && !glassInfoList.isEmpty()) { |
| | | Object firstEngineerId = glassInfoList.get(0).get("engineerId"); |
| | | if (firstEngineerId != null) { |
| | | engineeringId = firstEngineerId.toString(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | String mesEngineeringImportUrl = glassInfoService.getMesEngineeringImportUrl(); |
| | | |
| | | try { |
| | | // 4. 调用MES接口 |
| | | String mesEngineeringImportUrl = glassInfoService.getMesEngineeringImportUrl(); |
| | | ResponseEntity<Map> mesResp = restTemplate.postForEntity(mesEngineeringImportUrl, payload, Map.class); |
| | | Map<String, Object> mesBody = mesResp.getBody(); |
| | | |
| | | // 检查MES响应是否真正成功(不仅检查HTTP状态码,还要检查响应体中的code字段) |
| | | boolean mesSuccess = false; |
| | | |
| | | // 5. 检查MES响应是否真正成功(优化后的判断逻辑,增加异常捕获) |
| | | if (mesResp.getStatusCode().is2xxSuccessful() && mesBody != null) { |
| | | Object codeObj = mesBody.get("code"); |
| | | if (codeObj != null) { |
| | | int code = codeObj instanceof Number ? ((Number) codeObj).intValue() : |
| | | Integer.parseInt(String.valueOf(codeObj)); |
| | | // MES成功通常返回code=200或0 |
| | | mesSuccess = (code == 200 || code == 0); |
| | | try { |
| | | // 安全转换code为整数,避免类型转换异常 |
| | | int code = codeObj instanceof Number ? ((Number) codeObj).intValue() : |
| | | Integer.parseInt(String.valueOf(codeObj).trim()); |
| | | // MES成功通常返回code=200或0 |
| | | mesSuccess = (code == 200 || code == 0); |
| | | } catch (NumberFormatException e) { |
| | | log.warn("MES响应code字段不是有效数字:{}", codeObj, e); |
| | | } |
| | | } else { |
| | | // 如果没有code字段,认为HTTP 2xx就是成功 |
| | | // 没有code字段,认为HTTP 2xx就是成功 |
| | | mesSuccess = true; |
| | | } |
| | | } |
| | | |
| | | // 只有MES导入真正成功时,才保存玻璃信息到本地数据库,并关联engineering_id |
| | | if (mesSuccess && engineeringId != null) { |
| | | try { |
| | | glassInfoService.saveGlassInfosFromExcel(excelRows, engineeringId); |
| | | log.info("MES导入成功,已保存玻璃信息到本地数据库,工程号: {}", engineeringId); |
| | | } catch (Exception e) { |
| | | log.error("MES导入成功,但保存玻璃信息到本地数据库失败: engineeringId={}", engineeringId, e); |
| | | // 即使保存失败,也返回MES的成功响应,但记录错误日志 |
| | | } |
| | | } else { |
| | | log.warn("MES导入未成功,不保存玻璃信息到本地数据库: engineeringId={}, mesSuccess={}", engineeringId, mesSuccess); |
| | | // HTTP状态码不是2xx或响应体为空,设为失败 |
| | | mesSuccess = false; |
| | | log.warn("MES接口返回非2xx状态码或空响应体,状态码:{}", mesResp.getStatusCode()); |
| | | } |
| | | |
| | | // 直接返回 MES 的响应,让前端根据响应体中的 code 字段判断是否成功 |
| | | |
| | | // 6. 只有MES导入真正成功且工程号不为空时,才保存到本地数据库 |
| | | if (mesSuccess && engineeringId != null && !engineeringId.isEmpty()) { |
| | | // 先保存工程号 |
| | | engineeringSequenceService.saveEngineeringId(new Date(), engineeringId); |
| | | // 再保存玻璃信息 |
| | | glassInfoService.saveGlassInfosFromExcel(excelRows, engineeringId); |
| | | log.info("MES导入成功,已保存工程号和玻璃信息到本地数据库,工程号: {}", engineeringId); |
| | | } else { |
| | | log.warn("MES导入未成功,不保存工程号和玻璃信息到本地数据库: engineeringId={}, mesSuccess={}", engineeringId, mesSuccess); |
| | | } |
| | | |
| | | // 7. 返回MES的响应 |
| | | return ResponseEntity.status(mesResp.getStatusCode()).body(mesBody); |
| | | } catch (org.springframework.web.client.ResourceAccessException e) { |
| | | // 连接超时或无法连接 |
| | | log.error("转发 MES 导入接口失败(连接问题) url={}, error={}", mesEngineeringImportUrl, e.getMessage(), e); |
| | | Map<String, Object> errorResponse = new java.util.HashMap<>(); |
| | | log.error("转发 MES 导入接口失败(连接问题) url={}, error={}", glassInfoService.getMesEngineeringImportUrl(), e.getMessage(), e); |
| | | 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<>(); |
| | | log.error("转发 MES 导入接口失败 url={}, error={}", glassInfoService.getMesEngineeringImportUrl(), e.getMessage(), e); |
| | | errorResponse.put("code", 500); |
| | | errorResponse.put("message", "转发 MES 失败: " + e.getMessage()); |
| | | errorResponse.put("data", false); |