package com.iflytop.gd.app.controller; import com.iflytop.gd.common.cmd.CommandHandler; import com.iflytop.gd.app.service.cmd.CommandHandlerRegistry; import com.iflytop.gd.app.model.dto.CmdDTO; import com.iflytop.gd.app.service.exceptions.UnSupportCommandException; import com.iflytop.gd.common.result.Result; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; 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; @Tag(name = "前端业务指令") @RestController @RequestMapping("/api/cmd") @RequiredArgsConstructor @Slf4j public class CmdController { private final CommandHandlerRegistry registry; @Operation(summary = "前端统一调用一个接口") @PostMapping public Result controlMethod(@RequestBody CmdDTO cmdDTO) { String commandName = cmdDTO.getCommand(); try { CommandHandler commandHandler = registry.getCommandHandler(commandName); log.info("业务指令开始执行"); commandHandler.handle(cmdDTO); } catch (UnSupportCommandException exception) { log.error("未找到对应的业务指令"); return Result.failed(exception.getMessage()); } catch (Exception e) { log.error("执行业务指令发生异常: {}", cmdDTO, e); return Result.failed(e.getMessage()); } return Result.success(); } }