All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 8m18s
109 lines
2.7 KiB
Go
109 lines
2.7 KiB
Go
package device
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"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) logDeviceAudit(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.AssetTypeDevice
|
|
}
|
|
s.assetAuditService.LogOperation(ctx, assetAuditSvc.BuildLog(ctx, p))
|
|
}
|
|
|
|
func (s *Service) logDeviceOperation(
|
|
ctx context.Context,
|
|
operationType string,
|
|
operationDesc string,
|
|
resultStatus string,
|
|
device *model.Device,
|
|
beforeData map[string]any,
|
|
afterData map[string]any,
|
|
batchTotal int,
|
|
successCount int,
|
|
failCount int,
|
|
err error,
|
|
) {
|
|
if strings.TrimSpace(operationDesc) == "" {
|
|
operationDesc = operationType
|
|
}
|
|
if strings.TrimSpace(resultStatus) == "" {
|
|
if err != nil {
|
|
resultStatus = constants.AssetAuditResultFailed
|
|
} else {
|
|
resultStatus = constants.AssetAuditResultSuccess
|
|
}
|
|
}
|
|
|
|
params := assetAuditSvc.BuildLogParams{
|
|
OperationType: operationType,
|
|
OperationDesc: operationDesc,
|
|
ResultStatus: resultStatus,
|
|
BatchTotal: batchTotal,
|
|
SuccessCount: successCount,
|
|
FailCount: failCount,
|
|
}
|
|
snapshot := deviceSnapshot(device)
|
|
beforeContent := stripDeviceOperationMeta(beforeData)
|
|
afterContent := stripDeviceOperationMeta(afterData)
|
|
params.BeforeData, params.AfterData = assetAuditSvc.WrapOperationContent(beforeContent, afterContent, snapshot)
|
|
|
|
if device != nil {
|
|
params.AssetID = device.ID
|
|
params.AssetIdentifier = device.VirtualNo
|
|
}
|
|
if err != nil {
|
|
params.ErrorCode, params.ErrorMsg = assetAuditSvc.BuildErrorInfo(err)
|
|
}
|
|
|
|
s.logDeviceAudit(ctx, params)
|
|
}
|
|
|
|
func stripDeviceOperationMeta(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 deviceSnapshot(device *model.Device) map[string]any {
|
|
if device == nil {
|
|
return nil
|
|
}
|
|
return map[string]any{
|
|
"id": device.ID,
|
|
"virtual_no": device.VirtualNo,
|
|
"shop_id": device.ShopID,
|
|
"status": device.Status,
|
|
"series_id": device.SeriesID,
|
|
"enable_polling": device.EnablePolling,
|
|
"realname_policy": device.RealnamePolicy,
|
|
}
|
|
}
|