OpsController: GET /api/ops/service-requests (with optional status filter),
POST /api/ops/service-requests/{id}/approve, POST .../reject.
OpsService: listServiceRequests() delegates to existing repository queries.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
105 行
4.4 KiB
Java
105 行
4.4 KiB
Java
package com.xuqm.tenant.service;
|
|
|
|
import com.xuqm.common.security.JwtUtil;
|
|
import com.xuqm.tenant.entity.OpsAdminEntity;
|
|
import com.xuqm.tenant.entity.ServiceActivationRequestEntity;
|
|
import com.xuqm.tenant.entity.TenantEntity;
|
|
import com.xuqm.tenant.repository.AppRepository;
|
|
import com.xuqm.tenant.repository.OpsAdminRepository;
|
|
import com.xuqm.tenant.repository.ServiceActivationRequestRepository;
|
|
import com.xuqm.tenant.repository.TenantRepository;
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.PageRequest;
|
|
import org.springframework.data.domain.Sort;
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.time.LocalDate;
|
|
import java.time.LocalDateTime;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
|
|
@Service
|
|
public class OpsService {
|
|
|
|
private final TenantRepository tenantRepository;
|
|
private final AppRepository appRepository;
|
|
private final OpsAdminRepository opsAdminRepository;
|
|
private final ServiceActivationRequestRepository requestRepository;
|
|
private final PasswordEncoder passwordEncoder;
|
|
private final JwtUtil jwtUtil;
|
|
|
|
public OpsService(TenantRepository tenantRepository, AppRepository appRepository,
|
|
OpsAdminRepository opsAdminRepository,
|
|
ServiceActivationRequestRepository requestRepository,
|
|
PasswordEncoder passwordEncoder, JwtUtil jwtUtil) {
|
|
this.tenantRepository = tenantRepository;
|
|
this.appRepository = appRepository;
|
|
this.opsAdminRepository = opsAdminRepository;
|
|
this.requestRepository = requestRepository;
|
|
this.passwordEncoder = passwordEncoder;
|
|
this.jwtUtil = jwtUtil;
|
|
}
|
|
|
|
public String login(String username, String password) {
|
|
OpsAdminEntity admin = opsAdminRepository.findByUsername(username)
|
|
.orElseThrow(() -> new IllegalArgumentException("用户名或密码错误"));
|
|
if (!passwordEncoder.matches(password, admin.getPassword())) {
|
|
throw new IllegalArgumentException("用户名或密码错误");
|
|
}
|
|
return jwtUtil.generate(admin.getId(), Map.of("username", username, "role", "OPS"));
|
|
}
|
|
|
|
public Page<TenantEntity> listTenants(String keyword, int page, int size) {
|
|
return tenantRepository.searchTenants(
|
|
keyword,
|
|
PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt"))
|
|
);
|
|
}
|
|
|
|
public void toggleStatus(String tenantId) {
|
|
TenantEntity tenant = tenantRepository.findById(tenantId)
|
|
.orElseThrow(() -> new IllegalArgumentException("租户不存在"));
|
|
if (tenant.getStatus() == TenantEntity.Status.ACTIVE) {
|
|
tenant.setStatus(TenantEntity.Status.DISABLED);
|
|
} else {
|
|
tenant.setStatus(TenantEntity.Status.ACTIVE);
|
|
}
|
|
tenantRepository.save(tenant);
|
|
}
|
|
|
|
public Map<String, Object> statistics() {
|
|
long totalTenants = tenantRepository.count();
|
|
LocalDateTime todayStart = LocalDate.now().atStartOfDay();
|
|
LocalDateTime todayEnd = todayStart.plusDays(1);
|
|
long todayNew = tenantRepository.countByCreatedAtBetween(todayStart, todayEnd);
|
|
long activeApps = appRepository.count();
|
|
return Map.of(
|
|
"totalTenants", totalTenants,
|
|
"todayNew", todayNew,
|
|
"activeApps", activeApps,
|
|
"onlineUsers", 0
|
|
);
|
|
}
|
|
|
|
public Page<ServiceActivationRequestEntity> listServiceRequests(String statusStr, int page, int size) {
|
|
var pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt"));
|
|
if (statusStr != null && !statusStr.isEmpty()) {
|
|
ServiceActivationRequestEntity.Status status =
|
|
ServiceActivationRequestEntity.Status.valueOf(statusStr.toUpperCase());
|
|
return requestRepository.findByStatusOrderByCreatedAtDesc(status, pageable);
|
|
}
|
|
return requestRepository.findAllByOrderByCreatedAtDesc(pageable);
|
|
}
|
|
|
|
public void initDefaultAdmin(String username, String rawPassword) {
|
|
if (opsAdminRepository.findByUsername(username).isPresent()) return;
|
|
OpsAdminEntity admin = new OpsAdminEntity();
|
|
admin.setId(UUID.randomUUID().toString());
|
|
admin.setUsername(username);
|
|
admin.setPassword(passwordEncoder.encode(rawPassword));
|
|
admin.setCreatedAt(LocalDateTime.now());
|
|
opsAdminRepository.save(admin);
|
|
}
|
|
}
|