您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

109 行
5.8KB

  1. package com.example.webapi.controller;
  2. import com.example.webapi.dto.TransportCompanyQueryDTO;
  3. import com.example.webapi.entity.TransportCompany;
  4. import com.example.webapi.service.TransportCompanyService;
  5. import com.example.webapi.dto.ApiResponse;
  6. import io.swagger.annotations.Api;
  7. import io.swagger.annotations.ApiOperation;
  8. import io.swagger.annotations.ApiParam;
  9. import io.swagger.v3.oas.annotations.media.Content;
  10. import io.swagger.v3.oas.annotations.media.Schema;
  11. import io.swagger.v3.oas.annotations.responses.ApiResponses;
  12. import io.swagger.v3.oas.annotations.tags.Tag;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.data.domain.Page;
  15. import org.springframework.web.bind.annotation.*;
  16. import java.util.Optional;
  17. @Api(tags = "运输公司管理")
  18. @Tag(name = "运输公司管理", description = "提供运输公司的增删改查接口")
  19. @RestController
  20. @RequestMapping("/transport-company")
  21. public class TransportCompanyController {
  22. @Autowired
  23. private TransportCompanyService service;
  24. @ApiOperation(value = "条件查询运输公司(分页)", notes = "根据提供的查询条件分页查询运输公司")
  25. @ApiResponses(value = {
  26. @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "查询成功",
  27. content = @Content(mediaType = "application/json",
  28. schema = @Schema(implementation = com.example.webapi.dto.ApiResponse.class))),
  29. @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
  30. })
  31. @PostMapping("/page/conditions")
  32. public ApiResponse<Page<TransportCompany>> pageByConditions(
  33. @ApiParam(value = "查询条件DTO,包含分页信息和过滤条件", required = true)
  34. @RequestBody TransportCompanyQueryDTO queryDTO) {
  35. return ApiResponse.success(service.findByConditions(queryDTO));
  36. }
  37. @ApiOperation(value = "根据ID查询运输公司", notes = "根据ID获取单条运输公司详情")
  38. @ApiResponses(value = {
  39. @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "查询成功",
  40. content = @Content(mediaType = "application/json",
  41. schema = @Schema(implementation = com.example.webapi.dto.ApiResponse.class))),
  42. @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "记录不存在"),
  43. @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
  44. })
  45. @GetMapping("/{id}")
  46. public ApiResponse<TransportCompany> getById(
  47. @ApiParam(value = "运输公司ID", required = true, example = "1")
  48. @PathVariable Integer id) {
  49. Optional<TransportCompany> result = service.findById(id);
  50. return result.map(ApiResponse::success).orElseGet(() -> ApiResponse.error("未找到该运输公司"));
  51. }
  52. @ApiOperation(value = "新增运输公司", notes = "创建新的运输公司")
  53. @ApiResponses(value = {
  54. @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "创建成功",
  55. content = @Content(mediaType = "application/json",
  56. schema = @Schema(implementation = com.example.webapi.dto.ApiResponse.class))),
  57. @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "请求参数错误"),
  58. @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
  59. })
  60. @PostMapping
  61. public ApiResponse<TransportCompany> create(
  62. @ApiParam(value = "运输公司实体", required = true)
  63. @RequestBody TransportCompany entity) {
  64. return ApiResponse.success(service.save(entity));
  65. }
  66. @ApiOperation(value = "更新运输公司", notes = "根据ID更新运输公司信息")
  67. @ApiResponses(value = {
  68. @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "更新成功",
  69. content = @Content(mediaType = "application/json",
  70. schema = @Schema(implementation = com.example.webapi.dto.ApiResponse.class))),
  71. @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "请求参数错误"),
  72. @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "记录不存在"),
  73. @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
  74. })
  75. @PutMapping("/{id}")
  76. public ApiResponse<TransportCompany> update(
  77. @ApiParam(value = "运输公司ID", required = true, example = "1")
  78. @PathVariable Integer id,
  79. @ApiParam(value = "更新后的运输公司实体", required = true)
  80. @RequestBody TransportCompany entity) {
  81. entity.setId(id);
  82. return ApiResponse.success(service.save(entity));
  83. }
  84. @ApiOperation(value = "删除运输公司", notes = "根据ID删除运输公司")
  85. @ApiResponses(value = {
  86. @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "删除成功",
  87. content = @Content(mediaType = "application/json",
  88. schema = @Schema(implementation = com.example.webapi.dto.ApiResponse.class))),
  89. @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "记录不存在"),
  90. @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
  91. })
  92. @DeleteMapping("/{id}")
  93. public ApiResponse<Void> delete(
  94. @ApiParam(value = "运输公司ID", required = true, example = "1")
  95. @PathVariable Integer id) {
  96. service.deleteById(id);
  97. return ApiResponse.success();
  98. }
  99. }