89 行
3.3 KiB
Java
89 行
3.3 KiB
Java
|
|
package com.xuqm.license.controller;
|
||
|
|
|
||
|
|
import com.xuqm.common.model.ApiResponse;
|
||
|
|
import com.xuqm.license.entity.CompanyEntity;
|
||
|
|
import com.xuqm.license.entity.DeviceEntity;
|
||
|
|
import com.xuqm.license.service.CompanyService;
|
||
|
|
import com.xuqm.license.service.DeviceService;
|
||
|
|
import jakarta.validation.constraints.NotBlank;
|
||
|
|
import jakarta.validation.constraints.NotNull;
|
||
|
|
import org.springframework.http.ResponseEntity;
|
||
|
|
import org.springframework.web.bind.annotation.*;
|
||
|
|
|
||
|
|
import java.time.LocalDateTime;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Map;
|
||
|
|
|
||
|
|
@RestController
|
||
|
|
@RequestMapping("/api/license/admin")
|
||
|
|
public class LicenseAdminController {
|
||
|
|
|
||
|
|
private final CompanyService companyService;
|
||
|
|
private final DeviceService deviceService;
|
||
|
|
|
||
|
|
public LicenseAdminController(CompanyService companyService, DeviceService deviceService) {
|
||
|
|
this.companyService = companyService;
|
||
|
|
this.deviceService = deviceService;
|
||
|
|
}
|
||
|
|
|
||
|
|
@GetMapping("/companies")
|
||
|
|
public ResponseEntity<ApiResponse<List<CompanyEntity>>> listCompanies() {
|
||
|
|
return ResponseEntity.ok(ApiResponse.success(companyService.listAll()));
|
||
|
|
}
|
||
|
|
|
||
|
|
@PostMapping("/companies")
|
||
|
|
public ResponseEntity<ApiResponse<CompanyEntity>> createCompany(@RequestBody CreateCompanyRequest req) {
|
||
|
|
CompanyEntity company = companyService.create(req.name(), req.maxDevices(), req.expiresAt(), req.remark());
|
||
|
|
return ResponseEntity.ok(ApiResponse.success(company));
|
||
|
|
}
|
||
|
|
|
||
|
|
@GetMapping("/companies/{id}")
|
||
|
|
public ResponseEntity<ApiResponse<Map<String, Object>>> getCompany(@PathVariable String id) {
|
||
|
|
CompanyEntity company = companyService.getById(id);
|
||
|
|
List<DeviceEntity> devices = deviceService.listByCompany(id);
|
||
|
|
Map<String, Object> data = new java.util.LinkedHashMap<>();
|
||
|
|
data.put("company", company);
|
||
|
|
data.put("devices", devices);
|
||
|
|
return ResponseEntity.ok(ApiResponse.success(data));
|
||
|
|
}
|
||
|
|
|
||
|
|
@PutMapping("/companies/{id}")
|
||
|
|
public ResponseEntity<ApiResponse<CompanyEntity>> updateCompany(@PathVariable String id, @RequestBody UpdateCompanyRequest req) {
|
||
|
|
CompanyEntity company = companyService.update(id, req.name(), req.maxDevices(), req.expiresAt(), req.isActive(), req.remark());
|
||
|
|
return ResponseEntity.ok(ApiResponse.success(company));
|
||
|
|
}
|
||
|
|
|
||
|
|
@DeleteMapping("/companies/{id}")
|
||
|
|
public ResponseEntity<ApiResponse<Void>> deleteCompany(@PathVariable String id) {
|
||
|
|
companyService.delete(id);
|
||
|
|
return ResponseEntity.ok(ApiResponse.ok());
|
||
|
|
}
|
||
|
|
|
||
|
|
@DeleteMapping("/devices/{id}")
|
||
|
|
public ResponseEntity<ApiResponse<Void>> revokeDevice(@PathVariable String id) {
|
||
|
|
deviceService.revoke(id);
|
||
|
|
return ResponseEntity.ok(ApiResponse.ok());
|
||
|
|
}
|
||
|
|
|
||
|
|
@PutMapping("/devices/{id}/reactivate")
|
||
|
|
public ResponseEntity<ApiResponse<Void>> reactivateDevice(@PathVariable String id) {
|
||
|
|
deviceService.reactivate(id);
|
||
|
|
return ResponseEntity.ok(ApiResponse.ok());
|
||
|
|
}
|
||
|
|
|
||
|
|
public record CreateCompanyRequest(
|
||
|
|
@NotBlank String name,
|
||
|
|
@NotNull Integer maxDevices,
|
||
|
|
LocalDateTime expiresAt,
|
||
|
|
String remark
|
||
|
|
) {}
|
||
|
|
|
||
|
|
public record UpdateCompanyRequest(
|
||
|
|
String name,
|
||
|
|
Integer maxDevices,
|
||
|
|
LocalDateTime expiresAt,
|
||
|
|
Boolean isActive,
|
||
|
|
String remark
|
||
|
|
) {}
|
||
|
|
}
|