CompanyController.java
13.5 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package com.aigeo.company.controller;
import com.aigeo.company.entity.Company;
import com.aigeo.company.service.CompanyService;
import com.aigeo.common.result.Result;
import com.aigeo.common.result.ResultCode;
import com.aigeo.common.exception.BusinessException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
/**
* 公司管理控制器
*
* @author AIGEO Team
* @since 1.0.0
*/
@Tag(name = "公司管理", description = "公司信息管理接口,支持公司的创建、查询、更新和删除操作")
@RestController
@RequestMapping("/api/companies")
@RequiredArgsConstructor
@Slf4j
public class CompanyController {
private final CompanyService companyService;
@Operation(
summary = "分页查询公司列表",
description = "支持按公司名称、状态等条件分页查询公司列表,默认按创建时间倒序排列"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "查询成功"),
@ApiResponse(responseCode = "400", description = "请求参数错误"),
@ApiResponse(responseCode = "500", description = "服务器内部错误")
})
@GetMapping("/list")
public Result<Page<Company>> listCompanies(
@Parameter(description = "页码,从0开始", example = "0")
@RequestParam(defaultValue = "0") @Min(0) int page,
@Parameter(description = "页大小,默认10条", example = "10")
@RequestParam(defaultValue = "10") @Min(1) @Max(100) int size,
@Parameter(description = "公司名称(模糊查询)", example = "AIGEO")
@RequestParam(required = false) String name,
@Parameter(description = "公司状态(ACTIVE/INACTIVE/SUSPENDED)")
@RequestParam(required = false) String status,
@Parameter(description = "套餐类型(TRIAL/BASIC/PREMIUM/ENTERPRISE)")
@RequestParam(required = false) String planType
) {
try {
Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt"));
Page<Company> companies = companyService.searchCompanies(name, status, planType, pageable);
return Result.success("查询成功", companies);
} catch (Exception e) {
log.error("分页查询公司列表失败", e);
return Result.error("查询失败");
}
}
@Operation(
summary = "获取所有公司(简化版)",
description = "获取所有公司的基本信息,用于下拉选择等场景"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "查询成功"),
@ApiResponse(responseCode = "500", description = "服务器内部错误")
})
@GetMapping
public Result<List<Company>> getAllCompanies() {
try {
List<Company> companies = companyService.getAllCompanies();
return Result.success("查询成功", companies);
} catch (Exception e) {
log.error("获取所有公司失败", e);
return Result.error("查询失败");
}
}
@Operation(
summary = "根据ID查询公司详情",
description = "通过公司ID获取公司的详细信息"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "查询成功"),
@ApiResponse(responseCode = "404", description = "公司不存在"),
@ApiResponse(responseCode = "500", description = "服务器内部错误")
})
@GetMapping("/{id}")
public Result<Company> getCompanyById(
@Parameter(description = "公司ID", required = true, example = "1")
@PathVariable @NotNull Integer id
) {
try {
Company company = companyService.getCompanyById(id);
return Result.success("查询成功", company);
} catch (BusinessException e) {
log.warn("根据ID查询公司失败, id: {}, error: {}", id, e.getMessage());
return Result.error(e.getResultCode(), e.getMessage());
} catch (Exception e) {
log.error("根据ID查询公司详情失败, id: {}", id, e);
return Result.error("查询失败");
}
}
@Operation(
summary = "根据子域名查询公司",
description = "通过公司子域名获取公司信息,用于多租户识别"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "查询成功"),
@ApiResponse(responseCode = "404", description = "公司不存在"),
@ApiResponse(responseCode = "500", description = "服务器内部错误")
})
@GetMapping("/subdomain/{subdomain}")
public Result<Company> getCompanyBySubdomain(
@Parameter(description = "公司子域名", required = true, example = "aigeo")
@PathVariable @NotNull String subdomain
) {
try {
Optional<Company> companyOpt = companyService.getCompanyBySubdomain(subdomain);
if (companyOpt.isPresent()) {
return Result.success("查询成功", companyOpt.get());
} else {
return Result.error(ResultCode.COMPANY_NOT_FOUND);
}
} catch (Exception e) {
log.error("根据子域名查询公司失败, subdomain: {}", subdomain, e);
return Result.error("查询失败");
}
}
@Operation(
summary = "查询活跃公司列表",
description = "获取所有状态为活跃的公司列表"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "查询成功"),
@ApiResponse(responseCode = "500", description = "服务器内部错误")
})
@GetMapping("/active")
public Result<List<Company>> getActiveCompanies() {
try {
List<Company> companies = companyService.getActiveCompanies();
return Result.success("查询成功", companies);
} catch (Exception e) {
log.error("查询活跃公司列表失败", e);
return Result.error("查询失败");
}
}
@Operation(
summary = "创建新公司",
description = "创建一个新的公司记录,包含基本信息和配置"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "创建成功"),
@ApiResponse(responseCode = "400", description = "请求参数错误"),
@ApiResponse(responseCode = "409", description = "公司子域名已存在"),
@ApiResponse(responseCode = "500", description = "服务器内部错误")
})
@PostMapping
public Result<Company> createCompany(
@Parameter(description = "公司信息", required = true)
@Valid @RequestBody Company company
) {
try {
// 检查子域名是否已存在
if (companyService.existsBySubdomain(company.getSubdomain())) {
return Result.error(ResultCode.DATA_DUPLICATE, "子域名已存在");
}
Company savedCompany = companyService.saveCompany(company);
log.info("成功创建公司: id={}, name={}", savedCompany.getId(), savedCompany.getName());
return Result.success("创建成功", savedCompany);
} catch (Exception e) {
log.error("创建公司失败", e);
return Result.error("创建失败");
}
}
@Operation(
summary = "更新公司信息",
description = "根据ID更新公司的详细信息"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "更新成功"),
@ApiResponse(responseCode = "400", description = "请求参数错误"),
@ApiResponse(responseCode = "404", description = "公司不存在"),
@ApiResponse(responseCode = "500", description = "服务器内部错误")
})
@PutMapping("/{id}")
public Result<Company> updateCompany(
@Parameter(description = "公司ID", required = true, example = "1")
@PathVariable @NotNull Integer id,
@Parameter(description = "更新的公司信息", required = true)
@Valid @RequestBody Company companyDetails
) {
try {
Company existingCompany = companyService.getCompanyById(id);
// 更新字段
existingCompany.setName(companyDetails.getName());
existingCompany.setSubdomain(companyDetails.getSubdomain());
existingCompany.setPlanType(companyDetails.getPlanType());
existingCompany.setMaxUsers(companyDetails.getMaxUsers());
existingCompany.setMaxArticlesPerMonth(companyDetails.getMaxArticlesPerMonth());
existingCompany.setStatus(companyDetails.getStatus());
existingCompany.setTrialExpiryDate(companyDetails.getTrialExpiryDate());
existingCompany.setDefaultSettings(companyDetails.getDefaultSettings());
Company savedCompany = companyService.saveCompany(existingCompany);
log.info("成功更新公司: id={}, name={}", savedCompany.getId(), savedCompany.getName());
return Result.success("更新成功", savedCompany);
} catch (BusinessException e) {
log.warn("更新公司失败, id: {}, error: {}", id, e.getMessage());
return Result.error(e.getResultCode(), e.getMessage());
} catch (Exception e) {
log.error("更新公司失败, id: {}", id, e);
return Result.error("更新失败");
}
}
@Operation(
summary = "批量更新公司状态",
description = "批量更新多个公司的状态"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "更新成功"),
@ApiResponse(responseCode = "400", description = "请求参数错误"),
@ApiResponse(responseCode = "500", description = "服务器内部错误")
})
@PutMapping("/batch-status")
public Result<String> batchUpdateStatus(
@Parameter(description = "公司ID列表", required = true)
@RequestParam @NotNull List<Integer> ids,
@Parameter(description = "新状态", required = true)
@RequestParam @NotNull String status
) {
try {
int updatedCount = companyService.batchUpdateStatus(ids, status);
log.info("批量更新公司状态成功,更新数量: {}", updatedCount);
return Result.success(String.format("成功更新 %d 个公司状态", updatedCount));
} catch (Exception e) {
log.error("批量更新公司状态失败", e);
return Result.error("批量更新失败");
}
}
@Operation(
summary = "删除公司",
description = "根据ID删除公司记录(软删除)"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "删除成功"),
@ApiResponse(responseCode = "404", description = "公司不存在"),
@ApiResponse(responseCode = "409", description = "公司有关联用户,无法删除"),
@ApiResponse(responseCode = "500", description = "服务器内部错误")
})
@DeleteMapping("/{id}")
public Result<String> deleteCompany(
@Parameter(description = "公司ID", required = true, example = "1")
@PathVariable @NotNull Integer id
) {
try {
if (!companyService.existsById(id)) {
return Result.error(ResultCode.COMPANY_NOT_FOUND);
}
// 检查是否有关联用户
if (companyService.hasAssociatedUsers(id)) {
return Result.error(ResultCode.CONFLICT, "公司有关联用户,无法删除");
}
companyService.deleteCompany(id);
log.info("成功删除公司, id: {}", id);
return Result.success("删除成功");
} catch (Exception e) {
log.error("删除公司失败, id: {}", id, e);
return Result.error("删除失败");
}
}
@Operation(
summary = "搜索公司",
description = "根据关键词搜索公司名称或子域名"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "搜索成功"),
@ApiResponse(responseCode = "400", description = "请求参数错误"),
@ApiResponse(responseCode = "500", description = "服务器内部错误")
})
@GetMapping("/search")
public Result<List<Company>> searchCompanies(
@Parameter(description = "搜索关键词", required = true, example = "AIGEO")
@RequestParam @NotNull String keyword,
@Parameter(description = "最大返回数量", example = "20")
@RequestParam(defaultValue = "20") @Min(1) @Max(100) int limit
) {
try {
List<Company> companies = companyService.searchByKeyword(keyword, limit);
return Result.success("搜索成功", companies);
} catch (Exception e) {
log.error("搜索公司失败, keyword: {}", keyword, e);
return Result.error("搜索失败");
}
}
}