CompanyController.java
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.aigeo.company.controller;
import com.aigeo.common.enums.CompanyStatus;
import com.aigeo.company.entity.Company;
import com.aigeo.company.service.CompanyService;
import com.aigeo.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 公司控制器
*/
@Slf4j
@RestController
@RequestMapping("/api/companies")
@Tag(name = "公司管理", description = "公司相关接口")
public class CompanyController {
@Autowired
private CompanyService companyService;
@PostMapping
@Operation(summary = "创建公司", description = "创建新的公司")
public Result<Company> createCompany(@RequestBody Company company) {
try {
Company savedCompany = companyService.save(company);
return Result.success("公司创建成功", savedCompany);
} catch (Exception e) {
log.error("创建公司失败", e);
return Result.error("公司创建失败");
}
}
@GetMapping("/{id}")
@Operation(summary = "获取公司详情", description = "根据ID获取公司详情")
public Result<Company> getCompanyById(@PathVariable Integer id) {
try {
return companyService.findById(id)
.map(company -> Result.success("查询成功", company))
.orElse(Result.error("公司不存在"));
} catch (Exception e) {
log.error("获取公司详情失败, id: {}", id, e);
return Result.error("查询失败");
}
}
@GetMapping
@Operation(summary = "获取公司列表", description = "获取所有公司列表")
public Result<List<Company>> getAllCompanies() {
try {
List<Company> companies = companyService.findAll();
return Result.success("查询成功", companies);
} catch (Exception e) {
log.error("获取公司列表失败", e);
return Result.error("查询失败");
}
}
@GetMapping("/status/{status}")
@Operation(summary = "根据状态获取公司列表", description = "根据状态获取公司列表")
public Result<List<Company>> getCompaniesByStatus(@PathVariable String status) {
try {
CompanyStatus companyStatus = CompanyStatus.fromCode(status);
List<Company> companies = companyService.findByStatus(companyStatus);
return Result.success("查询成功", companies);
} catch (Exception e) {
log.error("根据状态获取公司列表失败, status: {}", status, e);
return Result.error("查询失败");
}
}
@GetMapping("/search")
@Operation(summary = "根据名称获取公司列表", description = "根据名称获取公司列表")
public Result<List<Company>> getCompaniesByName(@RequestParam String name) {
try {
List<Company> companies = companyService.findByName(name);
return Result.success("查询成功", companies);
} catch (Exception e) {
log.error("根据名称获取公司列表失败, name: {}", name, e);
return Result.error("查询失败");
}
}
@PutMapping("/{id}")
@Operation(summary = "更新公司", description = "更新公司信息")
public Result<Company> updateCompany(@PathVariable Integer id, @RequestBody Company company) {
try {
if (!companyService.findById(id).isPresent()) {
return Result.error("公司不存在");
}
company.setId(id);
Company updatedCompany = companyService.save(company);
return Result.success("公司更新成功", updatedCompany);
} catch (Exception e) {
log.error("更新公司失败, id: {}", id, e);
return Result.error("公司更新失败");
}
}
@DeleteMapping("/{id}")
@Operation(summary = "删除公司", description = "删除指定ID的公司")
public Result<String> deleteCompany(@PathVariable Integer id) {
try {
if (!companyService.findById(id).isPresent()) {
return Result.error("公司不存在");
}
companyService.deleteById(id);
return Result.success("公司删除成功");
} catch (Exception e) {
log.error("删除公司失败, id: {}", id, e);
return Result.error("公司删除失败");
}
}
}