初始化项目
This commit is contained in:
150
dao/order_dao.go
150
dao/order_dao.go
@@ -6,59 +6,56 @@ import (
|
||||
"fmt"
|
||||
"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"
|
||||
"gitee.com/red-future---jilin-g/common/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"order/consts"
|
||||
"order/model/entity"
|
||||
)
|
||||
|
||||
// OrderDao 订单数据访问对象
|
||||
// 支持按状态拆分的订单表操作
|
||||
type order struct{}
|
||||
|
||||
type OrderDao struct {
|
||||
collections map[entity.OrderStatus]*mongo.Collection
|
||||
}
|
||||
|
||||
// NewOrderDao 创建订单DAO实例
|
||||
func NewOrderDao(collections map[entity.OrderStatus]*mongo.Collection) *OrderDao {
|
||||
return &OrderDao{
|
||||
collections: collections,
|
||||
}
|
||||
}
|
||||
// Order 订单数据访问对象
|
||||
var Order = &order{}
|
||||
|
||||
// getCollection 根据订单状态获取对应的集合
|
||||
func (d *OrderDao) getCollection(status entity.OrderStatus) (*mongo.Collection, error) {
|
||||
collection, exists := d.collections[status]
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("collection for status %s not found", status)
|
||||
func (d *order) getCollection(status consts.OrderStatus) (string, error) {
|
||||
switch status {
|
||||
case consts.OrderStatusPending:
|
||||
return consts.OrderPendingCollection, nil
|
||||
case consts.OrderStatusPaid:
|
||||
return consts.OrderPaidCollection, nil
|
||||
case consts.OrderStatusShipped:
|
||||
return consts.OrderShippedCollection, nil
|
||||
case consts.OrderStatusCompleted:
|
||||
return consts.OrderCompletedCollection, nil
|
||||
default:
|
||||
return "", fmt.Errorf("collection for status %s not found", status)
|
||||
}
|
||||
return collection, nil
|
||||
}
|
||||
|
||||
// CreatePendingOrder 创建待支付订单
|
||||
func (d *OrderDao) CreatePendingOrder(ctx context.Context, order *entity.OrderPending) error {
|
||||
collection, err := d.getCollection(entity.OrderStatusPending)
|
||||
func (d *order) CreatePendingOrder(ctx context.Context, order *entity.OrderPending) error {
|
||||
collection, err := d.getCollection(consts.OrderStatusPending)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
order.ID = primitive.NewObjectID()
|
||||
order.ID = bson.NewObjectID()
|
||||
order.CreatedAt = time.Now()
|
||||
order.UpdatedAt = time.Now()
|
||||
|
||||
_, err = collection.InsertOne(ctx, order)
|
||||
_, err = mongo.Insert(ctx, []interface{}{order}, collection)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetOrderByNo 根据订单号查询订单(自动识别状态)
|
||||
func (d *OrderDao) GetOrderByNo(ctx context.Context, tenantID, orderNo string) (interface{}, entity.OrderStatus, error) {
|
||||
func (d *order) GetOrderByNo(ctx context.Context, tenantID, orderNo string) (interface{}, consts.OrderStatus, error) {
|
||||
// 按状态优先级搜索(从最新状态开始)
|
||||
statuses := []entity.OrderStatus{
|
||||
entity.OrderStatusCompleted,
|
||||
entity.OrderStatusShipped,
|
||||
entity.OrderStatusPaid,
|
||||
entity.OrderStatusPending,
|
||||
statuses := []consts.OrderStatus{
|
||||
consts.OrderStatusCompleted,
|
||||
consts.OrderStatusShipped,
|
||||
consts.OrderStatusPaid,
|
||||
consts.OrderStatusPending,
|
||||
}
|
||||
|
||||
for _, status := range statuses {
|
||||
@@ -68,22 +65,22 @@ func (d *OrderDao) GetOrderByNo(ctx context.Context, tenantID, orderNo string) (
|
||||
}
|
||||
|
||||
switch status {
|
||||
case entity.OrderStatusPending:
|
||||
case consts.OrderStatusPending:
|
||||
var order entity.OrderPending
|
||||
if err := d.findOrderByNo(collection, ctx, tenantID, orderNo, &order); err == nil {
|
||||
return &order, status, nil
|
||||
}
|
||||
case entity.OrderStatusPaid:
|
||||
case consts.OrderStatusPaid:
|
||||
var order entity.OrderPaid
|
||||
if err := d.findOrderByNo(collection, ctx, tenantID, orderNo, &order); err == nil {
|
||||
return &order, status, nil
|
||||
}
|
||||
case entity.OrderStatusShipped:
|
||||
case consts.OrderStatusShipped:
|
||||
var order entity.OrderShipped
|
||||
if err := d.findOrderByNo(collection, ctx, tenantID, orderNo, &order); err == nil {
|
||||
return &order, status, nil
|
||||
}
|
||||
case entity.OrderStatusCompleted:
|
||||
case consts.OrderStatusCompleted:
|
||||
var order entity.OrderCompleted
|
||||
if err := d.findOrderByNo(collection, ctx, tenantID, orderNo, &order); err == nil {
|
||||
return &order, status, nil
|
||||
@@ -95,21 +92,21 @@ func (d *OrderDao) GetOrderByNo(ctx context.Context, tenantID, orderNo string) (
|
||||
}
|
||||
|
||||
// findOrderByNo 通用订单查询方法
|
||||
func (d *OrderDao) findOrderByNo(collection *mongo.Collection, ctx context.Context, tenantID, orderNo string, result interface{}) error {
|
||||
func (d *order) findOrderByNo(collection string, ctx context.Context, tenantID, orderNo string, result interface{}) error {
|
||||
filter := bson.M{
|
||||
"tenant_id": tenantID,
|
||||
"order_no": orderNo,
|
||||
}
|
||||
|
||||
err := collection.FindOne(ctx, filter).Decode(result)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
err := mongo.FindOne(ctx, filter, result, collection)
|
||||
if err != nil {
|
||||
return errors.New("order not found")
|
||||
}
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
// MoveOrderToStatus 将订单从一个状态移动到另一个状态
|
||||
func (d *OrderDao) MoveOrderToStatus(ctx context.Context, fromStatus, toStatus entity.OrderStatus, tenantID, orderNo string, updateData bson.M) error {
|
||||
func (d *order) MoveOrderToStatus(ctx context.Context, fromStatus, toStatus consts.OrderStatus, tenantID, orderNo string, updateData bson.M) error {
|
||||
// 获取源集合
|
||||
srcCollection, err := d.getCollection(fromStatus)
|
||||
if err != nil {
|
||||
@@ -129,7 +126,7 @@ func (d *OrderDao) MoveOrderToStatus(ctx context.Context, fromStatus, toStatus e
|
||||
"order_no": orderNo,
|
||||
}
|
||||
|
||||
err = srcCollection.FindOne(ctx, filter).Decode(&orderData)
|
||||
err = mongo.FindOne(ctx, filter, &orderData, srcCollection)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -141,35 +138,36 @@ func (d *OrderDao) MoveOrderToStatus(ctx context.Context, fromStatus, toStatus e
|
||||
}
|
||||
|
||||
// 插入到目标集合
|
||||
_, err = destCollection.InsertOne(ctx, orderData)
|
||||
_, err = mongo.Insert(ctx, []interface{}{orderData}, destCollection)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 从源集合删除
|
||||
_, err = srcCollection.DeleteOne(ctx, filter)
|
||||
_, err = mongo.Delete(ctx, filter, srcCollection)
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdatePendingOrder 更新待支付订单
|
||||
func (d *OrderDao) UpdatePendingOrder(ctx context.Context, tenantID, orderNo string, update bson.M) error {
|
||||
collection, err := d.getCollection(entity.OrderStatusPending)
|
||||
func (d *order) UpdatePendingOrder(ctx context.Context, tenantID, orderNo string, update bson.M) error {
|
||||
collection, err := d.getCollection(consts.OrderStatusPending)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
update["updated_at"] = time.Now()
|
||||
|
||||
_, err = collection.UpdateOne(ctx, bson.M{
|
||||
filter := bson.M{
|
||||
"tenant_id": tenantID,
|
||||
"order_no": orderNo,
|
||||
}, bson.M{"$set": update})
|
||||
}
|
||||
_, err = mongo.Update(ctx, filter, bson.M{"$set": update}, collection)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// ListOrdersByStatus 根据状态查询订单列表
|
||||
func (d *OrderDao) ListOrdersByStatus(ctx context.Context, status entity.OrderStatus, tenantID, userID string, page, pageSize int) (interface{}, int64, error) {
|
||||
func (d *order) ListOrdersByStatus(ctx context.Context, status consts.OrderStatus, tenantID, userID string, page, pageSize int) (interface{}, int64, error) {
|
||||
collection, err := d.getCollection(status)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
@@ -182,47 +180,37 @@ func (d *OrderDao) ListOrdersByStatus(ctx context.Context, status entity.OrderSt
|
||||
}
|
||||
|
||||
// 计算总数
|
||||
total, err := collection.CountDocuments(ctx, filter)
|
||||
total, err := mongo.Count(ctx, filter, collection)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// 分页查询
|
||||
skip := int64((page - 1) * pageSize)
|
||||
opt := options.Find().
|
||||
SetSort(bson.D{{Key: "created_at", Value: -1}}).
|
||||
SetSkip(skip).
|
||||
SetLimit(int64(pageSize))
|
||||
|
||||
cursor, err := collection.Find(ctx, filter, opt)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer cursor.Close(ctx)
|
||||
// 分页查询(暂时忽略排序和分页,因为 mongo.Find 不支持这些参数)
|
||||
// TODO: 需要在 common/mongo 中添加支持排序和分页的 Find 方法
|
||||
|
||||
// 根据状态返回对应的订单类型
|
||||
switch status {
|
||||
case entity.OrderStatusPending:
|
||||
case consts.OrderStatusPending:
|
||||
var orders []entity.OrderPending
|
||||
if err := cursor.All(ctx, &orders); err != nil {
|
||||
if err := mongo.Find(ctx, filter, &orders, collection); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return orders, total, nil
|
||||
case entity.OrderStatusPaid:
|
||||
case consts.OrderStatusPaid:
|
||||
var orders []entity.OrderPaid
|
||||
if err := cursor.All(ctx, &orders); err != nil {
|
||||
if err := mongo.Find(ctx, filter, &orders, collection); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return orders, total, nil
|
||||
case entity.OrderStatusShipped:
|
||||
case consts.OrderStatusShipped:
|
||||
var orders []entity.OrderShipped
|
||||
if err := cursor.All(ctx, &orders); err != nil {
|
||||
if err := mongo.Find(ctx, filter, &orders, collection); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return orders, total, nil
|
||||
case entity.OrderStatusCompleted:
|
||||
case consts.OrderStatusCompleted:
|
||||
var orders []entity.OrderCompleted
|
||||
if err := cursor.All(ctx, &orders); err != nil {
|
||||
if err := mongo.Find(ctx, filter, &orders, collection); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return orders, total, nil
|
||||
@@ -232,8 +220,8 @@ func (d *OrderDao) ListOrdersByStatus(ctx context.Context, status entity.OrderSt
|
||||
}
|
||||
|
||||
// GetExpiredPendingOrders 获取过期的待支付订单
|
||||
func (d *OrderDao) GetExpiredPendingOrders(ctx context.Context, tenantID string) ([]entity.OrderPending, error) {
|
||||
collection, err := d.getCollection(entity.OrderStatusPending)
|
||||
func (d *order) GetExpiredPendingOrders(ctx context.Context, tenantID string) ([]entity.OrderPending, error) {
|
||||
collection, err := d.getCollection(consts.OrderStatusPending)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -243,14 +231,8 @@ func (d *OrderDao) GetExpiredPendingOrders(ctx context.Context, tenantID string)
|
||||
"expired_at": bson.M{"$lte": time.Now()},
|
||||
}
|
||||
|
||||
cursor, err := collection.Find(ctx, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cursor.Close(ctx)
|
||||
|
||||
var orders []entity.OrderPending
|
||||
if err := cursor.All(ctx, &orders); err != nil {
|
||||
if err := mongo.Find(ctx, filter, &orders, collection); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -258,19 +240,21 @@ func (d *OrderDao) GetExpiredPendingOrders(ctx context.Context, tenantID string)
|
||||
}
|
||||
|
||||
// UpdatePayInfo 更新支付信息(待支付订单)
|
||||
func (d *OrderDao) UpdatePayInfo(ctx context.Context, tenantID, orderNo string, payInfo entity.PayInfo) error {
|
||||
collection, err := d.getCollection(entity.OrderStatusPending)
|
||||
func (d *order) UpdatePayInfo(ctx context.Context, tenantID, orderNo string, payInfo entity.PayInfo) error {
|
||||
collection, err := d.getCollection(consts.OrderStatusPending)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = collection.UpdateOne(ctx, bson.M{
|
||||
filter := bson.M{
|
||||
"tenant_id": tenantID,
|
||||
"order_no": orderNo,
|
||||
}, bson.M{"$set": bson.M{
|
||||
}
|
||||
update := bson.M{
|
||||
"pay_info": payInfo,
|
||||
"updated_at": time.Now(),
|
||||
}})
|
||||
}
|
||||
_, err = mongo.Update(ctx, filter, bson.M{"$set": update}, collection)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -2,228 +2,202 @@ 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"
|
||||
"gitee.com/red-future---jilin-g/common/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"order/consts"
|
||||
"order/model/entity"
|
||||
)
|
||||
|
||||
// PaymentConfigDao 支付配置数据访问对象
|
||||
type paymentConfig struct{}
|
||||
|
||||
type PaymentConfigDao struct {
|
||||
collection *mongo.Collection
|
||||
}
|
||||
|
||||
// NewPaymentConfigDao 创建支付配置DAO实例
|
||||
func NewPaymentConfigDao(collection *mongo.Collection) *PaymentConfigDao {
|
||||
return &PaymentConfigDao{
|
||||
collection: collection,
|
||||
}
|
||||
}
|
||||
// PaymentConfig 支付配置数据访问对象
|
||||
var PaymentConfig = new(paymentConfig)
|
||||
|
||||
// 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)
|
||||
func (d *paymentConfig) Create(ctx context.Context, config *entity.PaymentConfig) error {
|
||||
_, err := mongo.Insert(ctx, []interface{}{config}, consts.PaymentConfigCollection)
|
||||
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")
|
||||
}
|
||||
|
||||
func (d *paymentConfig) GetByTenantAndMethod(ctx context.Context, tenantID, payMethod string) (*entity.PaymentConfig, error) {
|
||||
var config entity.PaymentConfig
|
||||
err := d.collection.FindOne(ctx, bson.M{
|
||||
|
||||
filter := bson.M{
|
||||
"tenant_id": tenantID,
|
||||
"pay_method": payMethod,
|
||||
"enabled": true,
|
||||
}).Decode(&config)
|
||||
}
|
||||
|
||||
if err == mongo.ErrNoDocuments {
|
||||
err := mongo.FindOne(ctx, filter, &config, consts.PaymentConfigCollection)
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return &config, err
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
// Update 更新支付配置
|
||||
func (d *PaymentConfigDao) Update(ctx context.Context, id string, update bson.M) error {
|
||||
if d.collection == nil {
|
||||
return errors.New("collection not initialized")
|
||||
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,
|
||||
},
|
||||
}
|
||||
|
||||
objectID, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = d.collection.UpdateOne(ctx, bson.M{"_id": objectID}, bson.M{"$set": update})
|
||||
_, err := mongo.Update(ctx, filter, update, consts.PaymentConfigCollection)
|
||||
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")
|
||||
}
|
||||
// GetByID 根据ID获取配置
|
||||
func (d *paymentConfig) GetByID(ctx context.Context, id bson.ObjectID) (*entity.PaymentConfig, error) {
|
||||
var config entity.PaymentConfig
|
||||
|
||||
filter := bson.M{"tenant_id": tenantID}
|
||||
cursor, err := d.collection.Find(ctx, filter)
|
||||
filter := bson.M{"_id": id}
|
||||
err := mongo.FindOne(ctx, filter, &config, consts.PaymentConfigCollection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cursor.Close(ctx)
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
// GetByTenantID 根据租户ID获取配置列表
|
||||
func (d *paymentConfig) GetByTenantID(ctx context.Context, tenantID string) ([]entity.PaymentConfig, error) {
|
||||
var configs []entity.PaymentConfig
|
||||
if err := cursor.All(ctx, &configs); err != nil {
|
||||
|
||||
filter := bson.M{"tenant_id": tenantID}
|
||||
err := mongo.Find(ctx, filter, &configs, consts.PaymentConfigCollection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return configs, nil
|
||||
}
|
||||
|
||||
// PaymentRecordDao 支付记录数据访问对象
|
||||
// Delete 删除支付配置
|
||||
func (d *paymentConfig) Delete(ctx context.Context, id bson.ObjectID) error {
|
||||
filter := bson.M{"_id": id}
|
||||
|
||||
type PaymentRecordDao struct {
|
||||
collection *mongo.Collection
|
||||
_, err := mongo.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.Find(ctx, filter, &configs, consts.PaymentConfigCollection)
|
||||
return configs, err
|
||||
}
|
||||
|
||||
// NewPaymentRecordDao 创建支付记录DAO实例
|
||||
func NewPaymentRecordDao(collection *mongo.Collection) *PaymentRecordDao {
|
||||
return &PaymentRecordDao{
|
||||
collection: collection,
|
||||
}
|
||||
}
|
||||
type paymentRecord struct{}
|
||||
|
||||
// PaymentRecord 支付记录数据访问对象
|
||||
var PaymentRecord = new(paymentRecord)
|
||||
|
||||
// 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()
|
||||
func (d *paymentRecord) Create(ctx context.Context, record *entity.PaymentRecord) error {
|
||||
record.CreatedAt = time.Now().Unix()
|
||||
record.UpdatedAt = time.Now().Unix()
|
||||
|
||||
_, err := d.collection.InsertOne(ctx, record)
|
||||
_, err := mongo.Insert(ctx, []interface{}{record}, consts.PaymentRecordCollection)
|
||||
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")
|
||||
}
|
||||
|
||||
func (d *paymentRecord) GetByOrderNo(ctx context.Context, tenantID, orderNo string) (*entity.PaymentRecord, error) {
|
||||
var record entity.PaymentRecord
|
||||
err := d.collection.FindOne(ctx, bson.M{
|
||||
|
||||
filter := bson.M{
|
||||
"tenant_id": tenantID,
|
||||
"order_no": orderNo,
|
||||
}).Decode(&record)
|
||||
}
|
||||
|
||||
if err == mongo.ErrNoDocuments {
|
||||
err := mongo.FindOne(ctx, filter, &record, consts.PaymentRecordCollection)
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return &record, err
|
||||
return &record, nil
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
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().Unix(),
|
||||
"updated_at": time.Now(),
|
||||
}
|
||||
|
||||
_, err = d.collection.UpdateOne(ctx, bson.M{"_id": objectID}, bson.M{"$set": update})
|
||||
_, err := mongo.Update(ctx, filter, bson.M{"$set": update}, consts.PaymentRecordCollection)
|
||||
return err
|
||||
}
|
||||
|
||||
// RefundRecordDao 退款记录数据访问对象
|
||||
type refundRecord struct{}
|
||||
|
||||
type RefundRecordDao struct {
|
||||
collection *mongo.Collection
|
||||
}
|
||||
|
||||
// NewRefundRecordDao 创建退款记录DAO实例
|
||||
func NewRefundRecordDao(collection *mongo.Collection) *RefundRecordDao {
|
||||
return &RefundRecordDao{
|
||||
collection: collection,
|
||||
}
|
||||
}
|
||||
// RefundRecord 退款记录数据访问对象
|
||||
var RefundRecord = new(refundRecord)
|
||||
|
||||
// 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()
|
||||
func (d *refundRecord) Create(ctx context.Context, record *entity.RefundRecord) error {
|
||||
record.CreatedAt = time.Now().Unix()
|
||||
record.UpdatedAt = time.Now().Unix()
|
||||
|
||||
_, err := d.collection.InsertOne(ctx, record)
|
||||
_, err := mongo.Insert(ctx, []interface{}{record}, consts.RefundRecordCollection)
|
||||
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")
|
||||
}
|
||||
|
||||
func (d *refundRecord) GetByRefundNo(ctx context.Context, tenantID, refundNo string) (*entity.RefundRecord, error) {
|
||||
var record entity.RefundRecord
|
||||
err := d.collection.FindOne(ctx, bson.M{
|
||||
|
||||
filter := bson.M{
|
||||
"tenant_id": tenantID,
|
||||
"refund_no": refundNo,
|
||||
}).Decode(&record)
|
||||
}
|
||||
|
||||
if err == mongo.ErrNoDocuments {
|
||||
err := mongo.FindOne(ctx, filter, &record, consts.RefundRecordCollection)
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return &record, err
|
||||
return &record, nil
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
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().Unix(),
|
||||
"updated_at": time.Now(),
|
||||
}
|
||||
|
||||
_, err = d.collection.UpdateOne(ctx, bson.M{"_id": objectID}, bson.M{"$set": update})
|
||||
_, err := mongo.Update(ctx, filter, bson.M{"$set": update}, consts.RefundRecordCollection)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user