diff --git a/internal/logic/admin/payment/updatePaymentMethodLogic.go b/internal/logic/admin/payment/updatePaymentMethodLogic.go index c3bf26e..87b4fd7 100644 --- a/internal/logic/admin/payment/updatePaymentMethodLogic.go +++ b/internal/logic/admin/payment/updatePaymentMethodLogic.go @@ -39,7 +39,7 @@ func (l *UpdatePaymentMethodLogic) UpdatePaymentMethod(req *types.UpdatePaymentM return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "find payment method error: %s", err.Error()) } config := parsePaymentPlatformConfig(l.ctx, payment.ParsePlatform(req.Platform), req.Config) - tool.DeepCopy(method, req) + tool.DeepCopy(method, req, tool.CopyWithIgnoreEmpty(false)) method.Config = config if err := l.svcCtx.PaymentModel.Update(l.ctx, method); err != nil { l.Errorw("update payment method error", logger.Field("id", req.Id), logger.Field("error", err.Error())) diff --git a/internal/logic/admin/server/updateNodeLogic.go b/internal/logic/admin/server/updateNodeLogic.go index e7ced74..582c206 100644 --- a/internal/logic/admin/server/updateNodeLogic.go +++ b/internal/logic/admin/server/updateNodeLogic.go @@ -37,7 +37,7 @@ func (l *UpdateNodeLogic) UpdateNode(req *types.UpdateNodeRequest) error { if err != nil { return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "find server error: %v", err) } - tool.DeepCopy(nodeInfo, req) + tool.DeepCopy(nodeInfo, req, tool.CopyWithIgnoreEmpty(false)) config, err := json.Marshal(req.Config) if err != nil { return err @@ -50,8 +50,14 @@ func (l *UpdateNodeLogic) UpdateNode(req *types.UpdateNodeRequest) error { return err } - if len(req.Tags) > 0 { + // 处理Tags字段 + switch { + case len(req.Tags) > 0: + // 有Tags,进行连接 nodeInfo.Tags = strings.Join(req.Tags, ",") + default: + // 空数组,清空Tags + nodeInfo.Tags = "" } nodeInfo.City = req.City diff --git a/pkg/tool/copy.go b/pkg/tool/copy.go index 0dc629c..7d707c1 100644 --- a/pkg/tool/copy.go +++ b/pkg/tool/copy.go @@ -13,17 +13,28 @@ import ( "github.com/perfect-panel/server/pkg/constant" ) -func DeepCopy[T, K interface{}](destStruct T, srcStruct K) T { +// CopyOption 定义复制选项的函数类型 +type CopyOption func(*copier.Option) + +// CopyWithIgnoreEmpty 设置是否忽略空值 +func CopyWithIgnoreEmpty(ignoreEmpty bool) CopyOption { + return func(o *copier.Option) { + o.IgnoreEmpty = ignoreEmpty + } +} + +func DeepCopy[T, K any](destStruct T, srcStruct K, opts ...CopyOption) T { var dst = destStruct var src = srcStruct - _ = copier.CopyWithOption(dst, src, copier.Option{ + + option := copier.Option{ DeepCopy: true, IgnoreEmpty: true, Converters: []copier.TypeConverter{ { SrcType: time.Time{}, DstType: constant.Int64, - Fn: func(src interface{}) (interface{}, error) { + Fn: func(src any) (any, error) { s, ok := src.(time.Time) if !ok { return nil, errors.New("src type not matching") @@ -32,13 +43,21 @@ func DeepCopy[T, K interface{}](destStruct T, srcStruct K) T { }, }, }, - }) + } + + for _, opt := range opts { + opt(&option) + } + + _ = copier.CopyWithOption(dst, src, option) return dst } -func ShallowCopy[T, K interface{}](destStruct T, srcStruct K) T { + +func ShallowCopy[T, K interface{}](destStruct T, srcStruct K, opts ...CopyOption) T { var dst = destStruct var src = srcStruct - _ = copier.CopyWithOption(dst, src, copier.Option{ + + option := copier.Option{ IgnoreEmpty: true, Converters: []copier.TypeConverter{ { @@ -46,7 +65,6 @@ func ShallowCopy[T, K interface{}](destStruct T, srcStruct K) T { DstType: constant.Int64, Fn: func(src interface{}) (interface{}, error) { s, ok := src.(time.Time) - if !ok { return nil, errors.New("src type not matching") } @@ -54,7 +72,13 @@ func ShallowCopy[T, K interface{}](destStruct T, srcStruct K) T { }, }, }, - }) + } + + for _, opt := range opts { + opt(&option) + } + + _ = copier.CopyWithOption(dst, src, option) return dst }