This commit is contained in:
94
internal/service/asset/manual_adjustment_audit.go
Normal file
94
internal/service/asset/manual_adjustment_audit.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package asset
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
infraAudit "github.com/break/junhong_cmp_fiber/internal/infrastructure/audit"
|
||||
"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/auditcontext"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/auditfailure"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func (s *Service) appendPackageAdjustmentAudit(
|
||||
ctx context.Context,
|
||||
tx *gorm.DB,
|
||||
actionCode, summary, result string,
|
||||
usage *model.PackageUsage,
|
||||
assetType string,
|
||||
assetID uint,
|
||||
assetIdentifier string,
|
||||
beforeData, afterData map[string]any,
|
||||
businessErr error,
|
||||
) error {
|
||||
if s.auditWriter == nil || usage == nil || usage.ID == 0 {
|
||||
return errors.New(errors.CodeInvalidStatus, "资产套餐统一审计接缝未配置或资源不完整")
|
||||
}
|
||||
usageID := strconv.FormatUint(uint64(usage.ID), 10)
|
||||
assetResourceID := strconv.FormatUint(uint64(assetID), 10)
|
||||
var resourceType string
|
||||
switch assetType {
|
||||
case constants.AssetTypeIotCard:
|
||||
resourceType = constants.AuditResourceIotCard
|
||||
case constants.AssetTypeDevice:
|
||||
resourceType = constants.AuditResourceDevice
|
||||
default:
|
||||
return errors.New(errors.CodeInvalidParam, "资产类型无效")
|
||||
}
|
||||
errorCode, errorSummary := assetAuditSvc.BuildErrorInfo(businessErr)
|
||||
return s.auditWriter.Append(ctx, tx, infraAudit.AppendInput{
|
||||
ActionCode: actionCode,
|
||||
Summary: summary,
|
||||
Result: result,
|
||||
ErrorCode: errorCode,
|
||||
ErrorSummary: errorSummary,
|
||||
ScopeType: constants.AuditScopePlatform,
|
||||
Resources: []infraAudit.ResourceInput{
|
||||
{
|
||||
Type: resourceType, ID: &assetResourceID, Key: assetIdentifier, DisplayName: assetIdentifier,
|
||||
Relation: constants.AuditResourceRelationReference, Role: constants.AuditResourceRolePackageUsageAsset,
|
||||
IdentitySnapshot: map[string]any{"id": assetID, "asset_type": assetType, "identifier": assetIdentifier},
|
||||
SubjectVisibility: constants.AuditSubjectResult, SubjectSummary: summary,
|
||||
},
|
||||
{
|
||||
Type: constants.AuditResourcePackageUsage, ID: &usageID,
|
||||
Key: "package_usage:" + usageID, DisplayName: "套餐权益#" + usageID,
|
||||
Relation: constants.AuditResourceRelationPrimary, Role: constants.AuditResourceRolePackageUsageTarget,
|
||||
IdentitySnapshot: map[string]any{
|
||||
"id": usage.ID, "order_id": usage.OrderID, "package_id": usage.PackageID,
|
||||
"iot_card_id": usage.IotCardID, "device_id": usage.DeviceID, "status": usage.Status,
|
||||
},
|
||||
BeforeData: beforeData, AfterData: afterData,
|
||||
SubjectVisibility: constants.AuditSubjectResult, SubjectSummary: summary,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Service) recordPackageAdjustmentFailure(
|
||||
ctx context.Context,
|
||||
actionCode, summary string,
|
||||
usage *model.PackageUsage,
|
||||
assetType string,
|
||||
assetID uint,
|
||||
assetIdentifier string,
|
||||
beforeData, afterData map[string]any,
|
||||
businessErr error,
|
||||
) {
|
||||
if s == nil || s.db == nil || usage == nil {
|
||||
return
|
||||
}
|
||||
err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
return s.appendPackageAdjustmentAudit(ctx, tx, actionCode, summary, constants.AuditResultFailed, usage, assetType, assetID, assetIdentifier, beforeData, afterData, businessErr)
|
||||
})
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
linkage := auditcontext.From(ctx)
|
||||
errorCode, _ := assetAuditSvc.BuildErrorInfo(businessErr)
|
||||
auditfailure.RecordSecondaryWriteFailure(actionCode, strconv.FormatUint(uint64(usage.ID), 10), linkage.RequestID, linkage.CorrelationID, errorCode, err)
|
||||
}
|
||||
Reference in New Issue
Block a user