package dao import ( "context" "time" "order/consts" "order/model/entity" "gitea.com/red-future/common/db/mongo" "go.mongodb.org/mongo-driver/v2/bson" ) type paymentConfig struct { } // PaymentConfig 支付配置数据访问对象 var PaymentConfig = &paymentConfig{} // Create 创建支付配置 func (d *paymentConfig) Create(ctx context.Context, config *entity.PaymentConfig) error { _, err := mongo.DB().Insert(ctx, []interface{}{config}, consts.PaymentConfigCollection) return err } // GetByTenantAndMethod 根据租户和支付方式获取配置 func (d *paymentConfig) GetByTenantAndMethod(ctx context.Context, tenantID, payMethod string) (*entity.PaymentConfig, error) { var config entity.PaymentConfig filter := bson.M{ "tenant_id": tenantID, "pay_method": payMethod, "enabled": true, } err := mongo.DB().FindOne(ctx, filter, &config, consts.PaymentConfigCollection) if err != nil { return nil, nil } return &config, nil } // Update 更新支付配置 func (d *paymentConfig) Update(ctx context.Context, config *entity.PaymentConfig) error { filter := bson.M{"_id": config.Id} update := bson.M{ "$set": bson.M{ "tenant_id": config.TenantId, "pay_method": config.PayMethod, "config_name": config.ConfigName, "description": config.Description, "enabled": config.Enabled, "app_id": config.AppID, "mch_id": config.MchID, "api_key": config.APIKey, "notify_url": config.NotifyURL, "return_url": config.ReturnURL, "cert_path": config.CertPath, "key_path": config.KeyPath, "app_private_key": config.AppPrivateKey, "alipay_public_key": config.AlipayPublicKey, "sandbox": config.Sandbox, "gateway_url": config.GatewayURL, "support_native": config.SupportNative, "support_jsapi": config.SupportJSAPI, "support_app": config.SupportAPP, "support_h5": config.SupportH5, }, } _, err := mongo.DB().Update(ctx, filter, update, consts.PaymentConfigCollection) return err } // GetByID 根据ID获取配置 func (d *paymentConfig) GetByID(ctx context.Context, id bson.ObjectID) (*entity.PaymentConfig, error) { var config entity.PaymentConfig filter := bson.M{"_id": id} err := mongo.DB().FindOne(ctx, filter, &config, consts.PaymentConfigCollection) if err != nil { return nil, err } return &config, nil } // GetByTenantID 根据租户ID获取配置列表 func (d *paymentConfig) GetByTenantID(ctx context.Context, tenantID string) ([]entity.PaymentConfig, error) { var configs []entity.PaymentConfig filter := bson.M{"tenant_id": tenantID} err := mongo.DB().Find(ctx, filter, &configs, consts.PaymentConfigCollection) if err != nil { return nil, err } return configs, nil } // Delete 删除支付配置 func (d *paymentConfig) Delete(ctx context.Context, id bson.ObjectID) error { filter := bson.M{"_id": id} _, err := mongo.DB().Delete(ctx, filter, consts.PaymentConfigCollection) return err } func (d *paymentConfig) ListByTenant(ctx context.Context, tenantID string) ([]entity.PaymentConfig, error) { var configs []entity.PaymentConfig filter := bson.M{"tenant_id": tenantID} err := mongo.DB().Find(ctx, filter, &configs, consts.PaymentConfigCollection) return configs, err } type paymentRecord struct { } // PaymentRecord 支付记录数据访问对象 var PaymentRecord = &paymentRecord{} // Create 创建支付记录 func (d *paymentRecord) Create(ctx context.Context, record *entity.PaymentRecord) error { record.CreatedAt = time.Now() record.UpdatedAt = time.Now() _, err := mongo.DB().Insert(ctx, []interface{}{record}, consts.PaymentRecordCollection) return err } // GetByOrderNo 根据订单号获取支付记录 func (d *paymentRecord) GetByOrderNo(ctx context.Context, tenantID, orderNo string) (*entity.PaymentRecord, error) { var record entity.PaymentRecord filter := bson.M{ "tenant_id": tenantID, "order_no": orderNo, } err := mongo.DB().FindOne(ctx, filter, &record, consts.PaymentRecordCollection) if err != nil { return nil, nil } return &record, nil } // UpdateStatus 更新支付记录状态 func (d *paymentRecord) UpdateStatus(ctx context.Context, id string, status, transactionID, tradeNo string) error { filter := bson.M{"_id": id} update := bson.M{ "status": status, "transaction_id": transactionID, "trade_no": tradeNo, "updated_at": time.Now(), } _, err := mongo.DB().Update(ctx, filter, bson.M{"$set": update}, consts.PaymentRecordCollection) return err } type refundRecord struct { } // RefundRecord 退款记录数据访问对象 var RefundRecord = &refundRecord{} // Create 创建退款记录 func (d *refundRecord) Create(ctx context.Context, record *entity.RefundRecord) error { record.CreatedAt = time.Now() record.UpdatedAt = time.Now() _, err := mongo.DB().Insert(ctx, []interface{}{record}, consts.RefundRecordCollection) return err } // GetByRefundNo 根据退款单号获取退款记录 func (d *refundRecord) GetByRefundNo(ctx context.Context, tenantID, refundNo string) (*entity.RefundRecord, error) { var record entity.RefundRecord filter := bson.M{ "tenant_id": tenantID, "refund_no": refundNo, } err := mongo.DB().FindOne(ctx, filter, &record, consts.RefundRecordCollection) if err != nil { return nil, nil } return &record, nil } // UpdateRefundStatus 更新退款记录状态 func (d *refundRecord) UpdateRefundStatus(ctx context.Context, id, status, refundID string) error { filter := bson.M{"_id": id} update := bson.M{ "status": status, "refund_id": refundID, "updated_at": time.Now(), } _, err := mongo.DB().Update(ctx, filter, bson.M{"$set": update}, consts.RefundRecordCollection) return err }