All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 8m18s
95 lines
2.6 KiB
Go
95 lines
2.6 KiB
Go
package iot_card
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/break/junhong_cmp_fiber/internal/model"
|
|
assetAuditSvc "github.com/break/junhong_cmp_fiber/internal/service/asset_audit"
|
|
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
|
)
|
|
|
|
// AssetAuditService 资产审计服务接口。
|
|
type AssetAuditService interface {
|
|
LogOperation(ctx context.Context, log *model.AssetOperationLog)
|
|
}
|
|
|
|
func (s *Service) logCardAudit(ctx context.Context, p assetAuditSvc.BuildLogParams) {
|
|
if s == nil || s.assetAuditService == nil {
|
|
return
|
|
}
|
|
if p.Operator.Type == "" {
|
|
p.Operator = assetAuditSvc.OperatorFromContext(ctx)
|
|
}
|
|
if p.AssetType == "" {
|
|
p.AssetType = constants.AssetTypeIotCard
|
|
}
|
|
p.BeforeData, p.AfterData = normalizeCardAuditPayload(p.BeforeData, p.AfterData)
|
|
s.assetAuditService.LogOperation(ctx, assetAuditSvc.BuildLog(ctx, p))
|
|
}
|
|
|
|
func (s *StopResumeService) logCardAudit(ctx context.Context, p assetAuditSvc.BuildLogParams) {
|
|
if s == nil || s.assetAuditService == nil {
|
|
return
|
|
}
|
|
if p.Operator.Type == "" {
|
|
p.Operator = assetAuditSvc.OperatorFromContext(ctx)
|
|
}
|
|
if p.AssetType == "" {
|
|
p.AssetType = constants.AssetTypeIotCard
|
|
}
|
|
p.BeforeData, p.AfterData = normalizeCardAuditPayload(p.BeforeData, p.AfterData)
|
|
s.assetAuditService.LogOperation(ctx, assetAuditSvc.BuildLog(ctx, p))
|
|
}
|
|
|
|
func normalizeCardAuditPayload(beforeData, afterData map[string]any) (map[string]any, map[string]any) {
|
|
snapshot := map[string]any(nil)
|
|
if raw, ok := beforeData["card"]; ok {
|
|
if m, ok := raw.(map[string]any); ok {
|
|
snapshot = m
|
|
}
|
|
}
|
|
if snapshot == nil {
|
|
if raw, ok := afterData["card"]; ok {
|
|
if m, ok := raw.(map[string]any); ok {
|
|
snapshot = m
|
|
}
|
|
}
|
|
}
|
|
return assetAuditSvc.WrapOperationContent(stripCardOperationMeta(beforeData), stripCardOperationMeta(afterData), snapshot)
|
|
}
|
|
|
|
func stripCardOperationMeta(data map[string]any) map[string]any {
|
|
if len(data) == 0 {
|
|
return nil
|
|
}
|
|
out := make(map[string]any, len(data))
|
|
for k, v := range data {
|
|
if k == "device" || k == "card" || k == "cards" || k == "asset_snapshot" || k == "operation_content" {
|
|
continue
|
|
}
|
|
out[k] = v
|
|
}
|
|
if len(out) == 0 {
|
|
return nil
|
|
}
|
|
return out
|
|
}
|
|
|
|
func cardSnapshot(card *model.IotCard) map[string]any {
|
|
if card == nil {
|
|
return nil
|
|
}
|
|
return map[string]any{
|
|
"id": card.ID,
|
|
"iccid": card.ICCID,
|
|
"shop_id": card.ShopID,
|
|
"status": card.Status,
|
|
"series_id": card.SeriesID,
|
|
"enable_polling": card.EnablePolling,
|
|
"realname_policy": card.RealnamePolicy,
|
|
"real_name_status": card.RealNameStatus,
|
|
"network_status": card.NetworkStatus,
|
|
"stop_reason": card.StopReason,
|
|
}
|
|
}
|