石墨消解仪后端服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.6 KiB

  1. package com.iflytop.gd.app.controller;
  2. import com.iflytop.gd.common.cmd.CommandHandler;
  3. import com.iflytop.gd.app.service.cmd.CommandHandlerRegistry;
  4. import com.iflytop.gd.app.model.dto.CmdDTO;
  5. import com.iflytop.gd.app.service.exceptions.UnSupportCommandException;
  6. import com.iflytop.gd.common.result.Result;
  7. import io.swagger.v3.oas.annotations.Operation;
  8. import io.swagger.v3.oas.annotations.tags.Tag;
  9. import lombok.RequiredArgsConstructor;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.springframework.web.bind.annotation.PostMapping;
  12. import org.springframework.web.bind.annotation.RequestBody;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RestController;
  15. @Tag(name = "前端业务指令")
  16. @RestController
  17. @RequestMapping("/api/cmd")
  18. @RequiredArgsConstructor
  19. @Slf4j
  20. public class CmdController {
  21. private final CommandHandlerRegistry registry;
  22. @Operation(summary = "前端统一调用一个接口")
  23. @PostMapping
  24. public Result<?> controlMethod(@RequestBody CmdDTO cmdDTO) {
  25. String commandName = cmdDTO.getCommand();
  26. try {
  27. CommandHandler commandHandler = registry.getCommandHandler(commandName);
  28. log.info("业务指令开始执行");
  29. commandHandler.handle(cmdDTO);
  30. } catch (UnSupportCommandException exception) {
  31. log.error("未找到对应的业务指令");
  32. return Result.failed(exception.getMessage());
  33. } catch (Exception e) {
  34. log.error("执行业务指令发生异常: {}", cmdDTO, e);
  35. return Result.failed(e.getMessage());
  36. }
  37. return Result.success();
  38. }
  39. }