初始化项目
This commit is contained in:
229
dao/payment_dao.go
Normal file
229
dao/payment_dao.go
Normal file
@@ -0,0 +1,229 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"order/model/entity"
|
||||
)
|
||||
|
||||
// PaymentConfigDao 支付配置数据访问对象
|
||||
|
||||
type PaymentConfigDao struct {
|
||||
collection *mongo.Collection
|
||||
}
|
||||
|
||||
// NewPaymentConfigDao 创建支付配置DAO实例
|
||||
func NewPaymentConfigDao(collection *mongo.Collection) *PaymentConfigDao {
|
||||
return &PaymentConfigDao{
|
||||
collection: collection,
|
||||
}
|
||||
}
|
||||
|
||||
// Create 创建支付配置
|
||||
func (d *PaymentConfigDao) Create(ctx context.Context, config *entity.PaymentConfig) error {
|
||||
if d.collection == nil {
|
||||
return errors.New("collection not initialized")
|
||||
}
|
||||
|
||||
config.ID = primitive.NewObjectID()
|
||||
|
||||
_, err := d.collection.InsertOne(ctx, config)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetByTenantAndMethod 根据租户和支付方式获取配置
|
||||
func (d *PaymentConfigDao) GetByTenantAndMethod(ctx context.Context, tenantID, payMethod string) (*entity.PaymentConfig, error) {
|
||||
if d.collection == nil {
|
||||
return nil, errors.New("collection not initialized")
|
||||
}
|
||||
|
||||
var config entity.PaymentConfig
|
||||
err := d.collection.FindOne(ctx, bson.M{
|
||||
"tenant_id": tenantID,
|
||||
"pay_method": payMethod,
|
||||
"enabled": true,
|
||||
}).Decode(&config)
|
||||
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return &config, err
|
||||
}
|
||||
|
||||
// Update 更新支付配置
|
||||
func (d *PaymentConfigDao) Update(ctx context.Context, id string, update bson.M) error {
|
||||
if d.collection == nil {
|
||||
return errors.New("collection not initialized")
|
||||
}
|
||||
|
||||
objectID, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = d.collection.UpdateOne(ctx, bson.M{"_id": objectID}, bson.M{"$set": update})
|
||||
return err
|
||||
}
|
||||
|
||||
// ListByTenant 查询租户的支付配置列表
|
||||
func (d *PaymentConfigDao) ListByTenant(ctx context.Context, tenantID string) ([]entity.PaymentConfig, error) {
|
||||
if d.collection == nil {
|
||||
return nil, errors.New("collection not initialized")
|
||||
}
|
||||
|
||||
filter := bson.M{"tenant_id": tenantID}
|
||||
cursor, err := d.collection.Find(ctx, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cursor.Close(ctx)
|
||||
|
||||
var configs []entity.PaymentConfig
|
||||
if err := cursor.All(ctx, &configs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return configs, nil
|
||||
}
|
||||
|
||||
// PaymentRecordDao 支付记录数据访问对象
|
||||
|
||||
type PaymentRecordDao struct {
|
||||
collection *mongo.Collection
|
||||
}
|
||||
|
||||
// NewPaymentRecordDao 创建支付记录DAO实例
|
||||
func NewPaymentRecordDao(collection *mongo.Collection) *PaymentRecordDao {
|
||||
return &PaymentRecordDao{
|
||||
collection: collection,
|
||||
}
|
||||
}
|
||||
|
||||
// Create 创建支付记录
|
||||
func (d *PaymentRecordDao) Create(ctx context.Context, record *entity.PaymentRecord) error {
|
||||
if d.collection == nil {
|
||||
return errors.New("collection not initialized")
|
||||
}
|
||||
|
||||
record.ID = primitive.NewObjectID()
|
||||
record.CreatedAt = time.Now().Unix()
|
||||
record.UpdatedAt = time.Now().Unix()
|
||||
|
||||
_, err := d.collection.InsertOne(ctx, record)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetByOrderNo 根据订单号获取支付记录
|
||||
func (d *PaymentRecordDao) GetByOrderNo(ctx context.Context, tenantID, orderNo string) (*entity.PaymentRecord, error) {
|
||||
if d.collection == nil {
|
||||
return nil, errors.New("collection not initialized")
|
||||
}
|
||||
|
||||
var record entity.PaymentRecord
|
||||
err := d.collection.FindOne(ctx, bson.M{
|
||||
"tenant_id": tenantID,
|
||||
"order_no": orderNo,
|
||||
}).Decode(&record)
|
||||
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return &record, err
|
||||
}
|
||||
|
||||
// UpdateStatus 更新支付记录状态
|
||||
func (d *PaymentRecordDao) UpdateStatus(ctx context.Context, id string, status, transactionID, tradeNo string) error {
|
||||
if d.collection == nil {
|
||||
return errors.New("collection not initialized")
|
||||
}
|
||||
|
||||
objectID, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
update := bson.M{
|
||||
"status": status,
|
||||
"transaction_id": transactionID,
|
||||
"trade_no": tradeNo,
|
||||
"updated_at": time.Now().Unix(),
|
||||
}
|
||||
|
||||
_, err = d.collection.UpdateOne(ctx, bson.M{"_id": objectID}, bson.M{"$set": update})
|
||||
return err
|
||||
}
|
||||
|
||||
// RefundRecordDao 退款记录数据访问对象
|
||||
|
||||
type RefundRecordDao struct {
|
||||
collection *mongo.Collection
|
||||
}
|
||||
|
||||
// NewRefundRecordDao 创建退款记录DAO实例
|
||||
func NewRefundRecordDao(collection *mongo.Collection) *RefundRecordDao {
|
||||
return &RefundRecordDao{
|
||||
collection: collection,
|
||||
}
|
||||
}
|
||||
|
||||
// Create 创建退款记录
|
||||
func (d *RefundRecordDao) Create(ctx context.Context, record *entity.RefundRecord) error {
|
||||
if d.collection == nil {
|
||||
return errors.New("collection not initialized")
|
||||
}
|
||||
|
||||
record.ID = primitive.NewObjectID()
|
||||
record.CreatedAt = time.Now().Unix()
|
||||
record.UpdatedAt = time.Now().Unix()
|
||||
|
||||
_, err := d.collection.InsertOne(ctx, record)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetByRefundNo 根据退款单号获取退款记录
|
||||
func (d *RefundRecordDao) GetByRefundNo(ctx context.Context, tenantID, refundNo string) (*entity.RefundRecord, error) {
|
||||
if d.collection == nil {
|
||||
return nil, errors.New("collection not initialized")
|
||||
}
|
||||
|
||||
var record entity.RefundRecord
|
||||
err := d.collection.FindOne(ctx, bson.M{
|
||||
"tenant_id": tenantID,
|
||||
"refund_no": refundNo,
|
||||
}).Decode(&record)
|
||||
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return &record, err
|
||||
}
|
||||
|
||||
// UpdateRefundStatus 更新退款记录状态
|
||||
func (d *RefundRecordDao) UpdateRefundStatus(ctx context.Context, id, status, refundID string) error {
|
||||
if d.collection == nil {
|
||||
return errors.New("collection not initialized")
|
||||
}
|
||||
|
||||
objectID, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
update := bson.M{
|
||||
"status": status,
|
||||
"refund_id": refundID,
|
||||
"updated_at": time.Now().Unix(),
|
||||
}
|
||||
|
||||
_, err = d.collection.UpdateOne(ctx, bson.M{"_id": objectID}, bson.M{"$set": update})
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user