mongo.go重构

This commit is contained in:
2025-12-30 11:03:11 +08:00
parent c150f38a87
commit 14a9af542d
10 changed files with 90 additions and 143 deletions

View File

@@ -4,28 +4,22 @@ import (
"context"
"time"
"gitee.com/red-future---jilin-g/common/mongo"
"go.mongodb.org/mongo-driver/v2/bson"
"order/consts"
"order/model/entity"
"gitee.com/red-future---jilin-g/common/mongo"
"go.mongodb.org/mongo-driver/v2/bson"
)
type paymentConfig struct {
NoCache bool
}
// PaymentConfig 支付配置数据访问对象
var PaymentConfig = &paymentConfig{
NoCache: false,
}
func (d *paymentConfig) SetNoCache() {
PaymentConfig.NoCache = true
}
var PaymentConfig = &paymentConfig{}
// Create 创建支付配置
func (d *paymentConfig) Create(ctx context.Context, config *entity.PaymentConfig) error {
_, err := mongo.Insert(ctx, []interface{}{config}, consts.PaymentConfigCollection)
_, err := mongo.DB().Insert(ctx, []interface{}{config}, consts.PaymentConfigCollection)
return err
}
@@ -39,7 +33,7 @@ func (d *paymentConfig) GetByTenantAndMethod(ctx context.Context, tenantID, payM
"enabled": true,
}
err := mongo.FindOne(ctx, d.NoCache, filter, &config, consts.PaymentConfigCollection)
err := mongo.DB().FindOne(ctx, filter, &config, consts.PaymentConfigCollection)
if err != nil {
return nil, nil
}
@@ -75,7 +69,7 @@ func (d *paymentConfig) Update(ctx context.Context, config *entity.PaymentConfig
},
}
_, err := mongo.Update(ctx, filter, update, consts.PaymentConfigCollection)
_, err := mongo.DB().Update(ctx, filter, update, consts.PaymentConfigCollection)
return err
}
@@ -84,7 +78,7 @@ func (d *paymentConfig) GetByID(ctx context.Context, id bson.ObjectID) (*entity.
var config entity.PaymentConfig
filter := bson.M{"_id": id}
err := mongo.FindOne(ctx, d.NoCache, filter, &config, consts.PaymentConfigCollection)
err := mongo.DB().FindOne(ctx, filter, &config, consts.PaymentConfigCollection)
if err != nil {
return nil, err
}
@@ -97,7 +91,7 @@ func (d *paymentConfig) GetByTenantID(ctx context.Context, tenantID string) ([]e
var configs []entity.PaymentConfig
filter := bson.M{"tenant_id": tenantID}
err := mongo.Find(ctx, d.NoCache, filter, &configs, consts.PaymentConfigCollection)
err := mongo.DB().Find(ctx, filter, &configs, consts.PaymentConfigCollection)
if err != nil {
return nil, err
}
@@ -109,7 +103,7 @@ func (d *paymentConfig) GetByTenantID(ctx context.Context, tenantID string) ([]e
func (d *paymentConfig) Delete(ctx context.Context, id bson.ObjectID) error {
filter := bson.M{"_id": id}
_, err := mongo.Delete(ctx, filter, consts.PaymentConfigCollection)
_, err := mongo.DB().Delete(ctx, filter, consts.PaymentConfigCollection)
return err
}
func (d *paymentConfig) ListByTenant(ctx context.Context, tenantID string) ([]entity.PaymentConfig, error) {
@@ -117,29 +111,22 @@ func (d *paymentConfig) ListByTenant(ctx context.Context, tenantID string) ([]en
filter := bson.M{"tenant_id": tenantID}
err := mongo.Find(ctx, d.NoCache, filter, &configs, consts.PaymentConfigCollection)
err := mongo.DB().Find(ctx, filter, &configs, consts.PaymentConfigCollection)
return configs, err
}
type paymentRecord struct {
NoCache bool
}
// PaymentRecord 支付记录数据访问对象
var PaymentRecord = &paymentRecord{
NoCache: false,
}
func (d *paymentRecord) SetNoCache() {
PaymentRecord.NoCache = true
}
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.Insert(ctx, []interface{}{record}, consts.PaymentRecordCollection)
_, err := mongo.DB().Insert(ctx, []interface{}{record}, consts.PaymentRecordCollection)
return err
}
@@ -152,7 +139,7 @@ func (d *paymentRecord) GetByOrderNo(ctx context.Context, tenantID, orderNo stri
"order_no": orderNo,
}
err := mongo.FindOne(ctx, d.NoCache, filter, &record, consts.PaymentRecordCollection)
err := mongo.DB().FindOne(ctx, filter, &record, consts.PaymentRecordCollection)
if err != nil {
return nil, nil
}
@@ -170,29 +157,22 @@ func (d *paymentRecord) UpdateStatus(ctx context.Context, id string, status, tra
"updated_at": time.Now(),
}
_, err := mongo.Update(ctx, filter, bson.M{"$set": update}, consts.PaymentRecordCollection)
_, err := mongo.DB().Update(ctx, filter, bson.M{"$set": update}, consts.PaymentRecordCollection)
return err
}
type refundRecord struct {
NoCache bool
}
// RefundRecord 退款记录数据访问对象
var RefundRecord = &refundRecord{
NoCache: false,
}
func (d *refundRecord) SetNoCache() {
RefundRecord.NoCache = true
}
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.Insert(ctx, []interface{}{record}, consts.RefundRecordCollection)
_, err := mongo.DB().Insert(ctx, []interface{}{record}, consts.RefundRecordCollection)
return err
}
@@ -205,7 +185,7 @@ func (d *refundRecord) GetByRefundNo(ctx context.Context, tenantID, refundNo str
"refund_no": refundNo,
}
err := mongo.FindOne(ctx, d.NoCache, filter, &record, consts.RefundRecordCollection)
err := mongo.DB().FindOne(ctx, filter, &record, consts.RefundRecordCollection)
if err != nil {
return nil, nil
}
@@ -222,6 +202,6 @@ func (d *refundRecord) UpdateRefundStatus(ctx context.Context, id, status, refun
"updated_at": time.Now(),
}
_, err := mongo.Update(ctx, filter, bson.M{"$set": update}, consts.RefundRecordCollection)
_, err := mongo.DB().Update(ctx, filter, bson.M{"$set": update}, consts.RefundRecordCollection)
return err
}