package com.baiyue.lastleg.appointmentplanning; import com.baiyue.common.core.api.ApiResponse; import com.baiyue.common.core.api.PageResult; import com.baiyue.common.security.AuthenticatedUser; import org.springframework.http.HttpStatus; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import jakarta.validation.Valid; import org.springframework.web.server.ResponseStatusException; @RestController @RequestMapping("/api/platform/v1/last-leg/appointment-planning") public class AppointmentPlanningController { private final AppointmentCargoService service; private final AppointmentFollowupService followups; private final AppointmentSourceService sources; private final AppointmentCoverageService coverage; public AppointmentPlanningController(AppointmentCargoService service, AppointmentFollowupService followups, AppointmentSourceService sources, AppointmentCoverageService coverage) { this.service = service; this.followups = followups; this.sources = sources; this.coverage = coverage; } public record Resolved(AppointmentSourceService.Source source, AppointmentCoverageService.Coverage coverage) { } @GetMapping("/sources/resolve") @PreAuthorize("hasAuthority('platform:appointment:list') and hasAuthority('platform:overseas-warehouse:last-mile-order:list')") public ApiResponse resolve(@AuthenticationPrincipal AuthenticatedUser user, @RequestParam String sourceKey, @RequestParam String customerId, @RequestParam(required=false) String destinationKey) { requirePlatform(user); var source = sources.resolve(sourceKey,customerId,destinationKey); var result = coverage.query(java.util.List.of(new AppointmentCoverageService.SourceRef(source.customerId(),source.sourceKey(),source.destinationKey()))); return ApiResponse.success(new Resolved(source,result.get(0))); } @GetMapping("/cargo") @PreAuthorize("hasAuthority('platform:appointment:list') and hasAuthority('platform:overseas-warehouse:last-mile-order:list')") public ApiResponse> cargo(@AuthenticationPrincipal AuthenticatedUser user, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "20") int size, @RequestParam(required = false) String warehouseId, @RequestParam(required = false) String destination, @RequestParam(required = false) String keyword) { requirePlatform(user); return ApiResponse.success(service.page(page, size, warehouseId, destination, keyword)); } @GetMapping("/sources/{sourceKey}/follow-ups") @PreAuthorize("hasAuthority('platform:appointment:list') and hasAuthority('platform:overseas-warehouse:last-mile-order:list')") public ApiResponse history(@AuthenticationPrincipal AuthenticatedUser user, @PathVariable String sourceKey, @RequestParam String customerId, @RequestParam(defaultValue="1") int page, @RequestParam(defaultValue="20") int size) { requirePlatform(user); return ApiResponse.success(followups.history(sourceKey,customerId,page,size)); } @PostMapping("/sources/{sourceKey}/follow-ups") @PreAuthorize("hasAuthority('platform:appointment:list') and hasAuthority('platform:overseas-warehouse:last-mile-order:list') and hasAuthority('platform:appointment:followup')") public ApiResponse followup(@AuthenticationPrincipal AuthenticatedUser user, @PathVariable String sourceKey, @RequestParam String customerId, @Valid @RequestBody AppointmentFollowupService.Save request) { requirePlatform(user); return ApiResponse.success(followups.save(sourceKey,customerId,request,user.userId().toString(),user.displayName()==null ? user.username() : user.displayName())); } private static void requirePlatform(AuthenticatedUser user) { if (user == null || user.userId()==null) throw new ResponseStatusException(HttpStatus.UNAUTHORIZED); if (!"PLATFORM".equals(user.appCode()) || !Long.valueOf(0).equals(user.tenantId())) throw new ResponseStatusException(HttpStatus.FORBIDDEN); } }