XuqmGroup-Server/tenant-service/src/main/java/com/xuqm/tenant/controller/OpsController.java
2026-04-21 22:07:29 +08:00

63 行
2.5 KiB
Java

package com.xuqm.tenant.controller;
import com.xuqm.common.model.ApiResponse;
import com.xuqm.tenant.entity.TenantEntity;
import com.xuqm.tenant.service.OpsService;
import jakarta.validation.constraints.NotBlank;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
@RequestMapping
public class OpsController {
private final OpsService opsService;
public OpsController(OpsService opsService) {
this.opsService = opsService;
}
@PostMapping("/api/auth/ops/login")
public ResponseEntity<ApiResponse<Map<String, String>>> opsLogin(@RequestBody Map<String, String> body) {
String token = opsService.login(body.get("username"), body.get("password"));
return ResponseEntity.ok(ApiResponse.success(Map.of("token", token)));
}
@GetMapping("/api/ops/tenants")
@PreAuthorize("hasAuthority('ROLE_OPS')")
public ResponseEntity<ApiResponse<Map<String, Object>>> listTenants(
@RequestParam(defaultValue = "") String keyword,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size) {
Page<TenantEntity> result = opsService.listTenants(keyword, page, size);
return ResponseEntity.ok(ApiResponse.success(Map.of(
"content", result.getContent(),
"total", result.getTotalElements(),
"totalPages", result.getTotalPages()
)));
}
@PostMapping("/api/ops/tenants/{id}/toggle-status")
@PreAuthorize("hasAuthority('ROLE_OPS')")
public ResponseEntity<ApiResponse<Void>> toggleStatus(@PathVariable String id) {
opsService.toggleStatus(id);
return ResponseEntity.ok(ApiResponse.ok());
}
@GetMapping("/api/ops/statistics")
@PreAuthorize("hasAuthority('ROLE_OPS')")
public ResponseEntity<ApiResponse<Map<String, Object>>> statistics() {
return ResponseEntity.ok(ApiResponse.success(opsService.statistics()));
}
}