package com.zksy.web.controller.cockpit; import com.zksy.cockpit.foundation.CockpitErrorCategory; import com.zksy.cockpit.foundation.CockpitErrorPayload; import com.zksy.cockpit.geometry.CockpitFacilityGeometryService; import com.zksy.common.core.domain.AjaxResult; import com.zksy.common.utils.uuid.IdUtils; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import java.time.Clock; import java.time.ZoneId; import java.time.format.DateTimeFormatter; @RestController public class CockpitFacilityGeometryController { private final CockpitFacilityGeometryService service; private final Clock clock; public CockpitFacilityGeometryController(CockpitFacilityGeometryService service, Clock clock) { this.service = service; this.clock = clock; } @GetMapping("/api/cockpit/v1/infrastructure/geometry") public ResponseEntity geometry() { return ResponseEntity.ok(AjaxResult.success(service.load())); } @ExceptionHandler(RuntimeException.class) public ResponseEntity handleReadFailure(HttpServletRequest request, RuntimeException exception) { // Includes transaction begin/commit failures thrown outside service.load()'s body. org.slf4j.LoggerFactory.getLogger(CockpitFacilityGeometryController.class) .error("Facility geometry read failed", exception); String requestId = request.getHeader("X-Request-Id"); if (requestId == null || requestId.trim().isEmpty()) { requestId = IdUtils.fastSimpleUUID(); } CockpitErrorPayload payload = new CockpitErrorPayload(CockpitErrorCategory.SERVER_ERROR, "facility geometry source unavailable", requestId, DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(clock.instant().atZone(ZoneId.of("Asia/Shanghai")))); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(new AjaxResult(500, payload.getMessage(), payload)); } }