mirror of
https://github.com/hpd840321/craftlabs-authorization-sdk.git
synced 2026-06-09 10:00:30 +08:00
feat(m10): add audit event search and CSV export
This commit is contained in:
+54
-4
@@ -5,14 +5,20 @@ import cn.craftlabs.platform.api.web.dto.AuditEventResponse;
|
||||
import cn.craftlabs.platform.api.web.dto.PageResponse;
|
||||
import jakarta.validation.constraints.Max;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/audit-events")
|
||||
@Validated
|
||||
@@ -26,10 +32,54 @@ public class AuditController {
|
||||
|
||||
@GetMapping
|
||||
public PageResponse<AuditEventResponse> list(
|
||||
@RequestParam("entityType") @NotBlank String entityType,
|
||||
@RequestParam("entityId") @NotNull Long entityId,
|
||||
@RequestParam(required = false) String entityType,
|
||||
@RequestParam(required = false) Long entityId,
|
||||
@RequestParam(value = "page", defaultValue = "0") @Min(0) int page,
|
||||
@RequestParam(value = "size", defaultValue = "20") @Min(1) @Max(200) int size) {
|
||||
return auditService.page(entityType, entityId, page, size);
|
||||
}
|
||||
|
||||
@GetMapping("/export")
|
||||
public ResponseEntity<Resource> exportAuditEvents(
|
||||
@RequestParam(required = false) String entityType,
|
||||
@RequestParam(required = false) Long entityId,
|
||||
@RequestParam(required = false) String from,
|
||||
@RequestParam(required = false) String to) {
|
||||
|
||||
List<AuditEventResponse> events = auditService.searchAuditEvents(entityType, entityId, from, to);
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("时间,操作者,动作,实体类型,实体ID,摘要,详情\n");
|
||||
for (AuditEventResponse e : events) {
|
||||
sb.append(escapeCsv(e.getCreatedAt() != null ? e.getCreatedAt().toString() : "")).append(",");
|
||||
sb.append(escapeCsv(e.getActorUserId())).append(",");
|
||||
sb.append(escapeCsv(e.getAction())).append(",");
|
||||
sb.append(escapeCsv(e.getEntityType())).append(",");
|
||||
sb.append(e.getEntityId() != null ? String.valueOf(e.getEntityId()) : "").append(",");
|
||||
sb.append(escapeCsv(e.getFieldName())).append(",");
|
||||
sb.append(escapeCsv(e.getOldValue())).append("\n");
|
||||
}
|
||||
|
||||
byte[] bytes = sb.toString().getBytes(StandardCharsets.UTF_8);
|
||||
byte[] bom = {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF};
|
||||
byte[] withBom = new byte[bom.length + bytes.length];
|
||||
System.arraycopy(bom, 0, withBom, 0, bom.length);
|
||||
System.arraycopy(bytes, 0, withBom, bom.length, bytes.length);
|
||||
|
||||
ByteArrayResource resource = new ByteArrayResource(withBom);
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION,
|
||||
"attachment; filename=audit-events-" + LocalDate.now() + ".csv")
|
||||
.header(HttpHeaders.CONTENT_TYPE, "text/csv; charset=utf-8")
|
||||
.body(resource);
|
||||
}
|
||||
|
||||
private static String escapeCsv(String value) {
|
||||
if (value == null) return "";
|
||||
if (value.contains(",") || value.contains("\"") || value.contains("\n")) {
|
||||
return "\"" + value.replace("\"", "\"\"") + "\"";
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
+30
-5
@@ -50,11 +50,7 @@ public class AuditService {
|
||||
@Transactional(readOnly = true)
|
||||
public PageResponse<AuditEventResponse> page(
|
||||
String entityType, Long entityId, int page, int size) {
|
||||
LambdaQueryWrapper<PlatformAuditEvent> q =
|
||||
Wrappers.lambdaQuery(PlatformAuditEvent.class)
|
||||
.eq(PlatformAuditEvent::getEntityType, entityType.trim())
|
||||
.eq(PlatformAuditEvent::getEntityId, entityId)
|
||||
.orderByDesc(PlatformAuditEvent::getId);
|
||||
LambdaQueryWrapper<PlatformAuditEvent> q = buildQuery(entityType, entityId, null, null);
|
||||
Page<PlatformAuditEvent> mpPage = new Page<>(page + 1L, size);
|
||||
auditEventMapper.selectPage(mpPage, q);
|
||||
List<AuditEventResponse> content =
|
||||
@@ -62,6 +58,35 @@ public class AuditService {
|
||||
return new PageResponse<>(content, mpPage.getTotal(), page, size);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<AuditEventResponse> searchAuditEvents(
|
||||
String entityType, Long entityId, String from, String to) {
|
||||
LambdaQueryWrapper<PlatformAuditEvent> q = buildQuery(entityType, entityId, from, to);
|
||||
return auditEventMapper.selectList(q).stream()
|
||||
.map(this::toResponse)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private LambdaQueryWrapper<PlatformAuditEvent> buildQuery(
|
||||
String entityType, Long entityId, String from, String to) {
|
||||
LambdaQueryWrapper<PlatformAuditEvent> q =
|
||||
Wrappers.lambdaQuery(PlatformAuditEvent.class)
|
||||
.orderByDesc(PlatformAuditEvent::getId);
|
||||
if (entityType != null && !entityType.isBlank()) {
|
||||
q.eq(PlatformAuditEvent::getEntityType, entityType.trim());
|
||||
}
|
||||
if (entityId != null) {
|
||||
q.eq(PlatformAuditEvent::getEntityId, entityId);
|
||||
}
|
||||
if (from != null && !from.isBlank()) {
|
||||
q.ge(PlatformAuditEvent::getCreatedAt, OffsetDateTime.parse(from + "T00:00:00Z"));
|
||||
}
|
||||
if (to != null && !to.isBlank()) {
|
||||
q.le(PlatformAuditEvent::getCreatedAt, OffsetDateTime.parse(to + "T23:59:59Z"));
|
||||
}
|
||||
return q;
|
||||
}
|
||||
|
||||
private AuditEventResponse toResponse(PlatformAuditEvent e) {
|
||||
AuditEventResponse r = new AuditEventResponse();
|
||||
r.setId(e.getId());
|
||||
|
||||
Reference in New Issue
Block a user