From 29d3d48dbe9420a9c1599b11db01804b832982ee Mon Sep 17 00:00:00 2001 From: huang Date: Wed, 6 May 2026 16:59:10 +0800 Subject: [PATCH] =?UTF-8?q?=E9=BB=91=E9=AD=94=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/service/device/audit.go | 64 ++++++++++++++++++++++++++++++ internal/service/iot_card/audit.go | 20 ++++++++++ 2 files changed, 84 insertions(+) diff --git a/internal/service/device/audit.go b/internal/service/device/audit.go index 6af200e..29d3dd1 100644 --- a/internal/service/device/audit.go +++ b/internal/service/device/audit.go @@ -63,6 +63,8 @@ func (s *Service) logDeviceOperation( snapshot := deviceSnapshot(device) beforeContent := stripDeviceOperationMeta(beforeData) afterContent := stripDeviceOperationMeta(afterData) + beforeContent = ensureDeviceOperationContentMap(beforeContent, beforeData) + afterContent = ensureDeviceOperationContentMap(afterContent, afterData) enrichDeviceOperationContent(beforeContent, beforeData) enrichDeviceOperationContent(afterContent, afterData) params.BeforeData, params.AfterData = assetAuditSvc.WrapOperationContent(beforeContent, afterContent, snapshot) @@ -95,6 +97,27 @@ func stripDeviceOperationMeta(data map[string]any) map[string]any { return out } +func ensureDeviceOperationContentMap(content map[string]any, raw map[string]any) map[string]any { + if content != nil || len(raw) == 0 { + return content + } + if _, ok := raw["device"].(map[string]any); ok { + return make(map[string]any) + } + if _, ok := raw["card"].(map[string]any); ok { + return make(map[string]any) + } + switch raw["devices"].(type) { + case []map[string]any, []any: + return make(map[string]any) + } + switch raw["cards"].(type) { + case []map[string]any, []any: + return make(map[string]any) + } + return content +} + func enrichDeviceOperationContent(content map[string]any, raw map[string]any) { if len(raw) == 0 { return @@ -133,6 +156,33 @@ func enrichDeviceOperationContent(content map[string]any, raw map[string]any) { content["device_virtual_nos"] = virtualNos } } + + switch cardsRaw := raw["cards"].(type) { + case []map[string]any: + cardIDs, iccids := collectDeviceAuditCardReadableLists(cardsRaw) + if len(cardIDs) > 0 && content["card_ids"] == nil { + content["card_ids"] = cardIDs + } + if len(iccids) > 0 && content["iccids"] == nil { + content["iccids"] = iccids + } + case []any: + cards := make([]map[string]any, 0, len(cardsRaw)) + for _, item := range cardsRaw { + cardMap, ok := item.(map[string]any) + if !ok { + continue + } + cards = append(cards, cardMap) + } + cardIDs, iccids := collectDeviceAuditCardReadableLists(cards) + if len(cardIDs) > 0 && content["card_ids"] == nil { + content["card_ids"] = cardIDs + } + if len(iccids) > 0 && content["iccids"] == nil { + content["iccids"] = iccids + } + } } func mergeReadableDeviceFields(content map[string]any, device map[string]any) { @@ -191,6 +241,20 @@ func collectDeviceReadableLists(devices []map[string]any) ([]any, []string) { return deviceIDs, virtualNos } +func collectDeviceAuditCardReadableLists(cards []map[string]any) ([]any, []string) { + cardIDs := make([]any, 0, len(cards)) + iccids := make([]string, 0, len(cards)) + for _, card := range cards { + if id, ok := card["id"]; ok { + cardIDs = append(cardIDs, id) + } + if iccid := stringifyDeviceAuditValue(card["iccid"]); iccid != "" { + iccids = append(iccids, iccid) + } + } + return cardIDs, iccids +} + func stringifyDeviceAuditValue(v any) string { switch vv := v.(type) { case string: diff --git a/internal/service/iot_card/audit.go b/internal/service/iot_card/audit.go index 59e3d90..bf84d1e 100644 --- a/internal/service/iot_card/audit.go +++ b/internal/service/iot_card/audit.go @@ -58,6 +58,8 @@ func normalizeCardAuditPayload(beforeData, afterData map[string]any) (map[string } beforeContent := stripCardOperationMeta(beforeData) afterContent := stripCardOperationMeta(afterData) + beforeContent = ensureCardOperationContentMap(beforeContent, beforeData) + afterContent = ensureCardOperationContentMap(afterContent, afterData) enrichCardOperationContent(beforeContent, beforeData) enrichCardOperationContent(afterContent, afterData) return assetAuditSvc.WrapOperationContent(beforeContent, afterContent, snapshot) @@ -80,6 +82,24 @@ func stripCardOperationMeta(data map[string]any) map[string]any { return out } +func ensureCardOperationContentMap(content map[string]any, raw map[string]any) map[string]any { + if content != nil || len(raw) == 0 { + return content + } + if _, ok := raw["card"].(map[string]any); ok { + return make(map[string]any) + } + if _, ok := raw["device"].(map[string]any); ok { + return make(map[string]any) + } + switch raw["cards"].(type) { + case []map[string]any, []any: + return make(map[string]any) + default: + return content + } +} + func enrichCardOperationContent(content map[string]any, raw map[string]any) { if len(raw) == 0 { return