pg_order_payment_reponsitory.go 3.5 KB
package repository

import (
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
	. "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
)

type OrderPaymentRepository struct {
	transactionContext *transaction.TransactionContext
}

func (repository *OrderPaymentRepository) Save(dm *domain.OrderPayment) (*domain.OrderPayment, error) {
	var (
		err error
		m   = &models.OrderPayment{}
		tx  = repository.transactionContext.PgTx
	)
	if err = GobModelTransform(m, dm); err != nil {
		return nil, err
	}
	if dm.Identify() == nil {
		if dm.Id, err = NewSnowflakeId(); err != nil {
			return dm, err
		}
		m.Id = dm.Id
		if err = tx.Insert(m); err != nil {
			return nil, err
		}
		return dm, nil
	}
	if err = tx.Update(m); err != nil {
		return nil, err
	}
	return dm, nil
}

func (repository *OrderPaymentRepository) Remove(OrderPayment *domain.OrderPayment) (*domain.OrderPayment, error) {
	var (
		tx                = repository.transactionContext.PgTx
		OrderPaymentModel = &models.OrderPayment{Id: OrderPayment.Identify().(int64)}
	)
	if _, err := tx.Model(OrderPaymentModel).Where("id = ?", OrderPayment.Id).Delete(); err != nil {
		return OrderPayment, err
	}
	return OrderPayment, nil
}

func (repository *OrderPaymentRepository) FindOne(queryOptions map[string]interface{}) (*domain.OrderPayment, error) {
	tx := repository.transactionContext.PgTx
	OrderPaymentModel := new(models.OrderPayment)
	query := NewQuery(tx.Model(OrderPaymentModel), queryOptions)
	query.SetWhere("order_payment.id = ?", "id")
	query.SetWhere("order_payment.order_id = ?", "orderId")
	if err := query.First(); err != nil {
		return nil, query.HandleError(err, "没有此订单")
	}
	if OrderPaymentModel.Id == 0 {
		return nil, nil
	}
	return repository.transformPgModelToDomainModel(OrderPaymentModel)
}

func (repository *OrderPaymentRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.OrderPayment, error) {
	tx := repository.transactionContext.PgTx
	var OrderPaymentModels []*models.OrderPayment
	OrderPayments := make([]*domain.OrderPayment, 0)
	query := NewQuery(tx.Model(&OrderPaymentModels), queryOptions)
	query.
		SetWhere("order_payment.partner_id = ?", "partnerId").
		SetWhere("order_payment.order_id = ?", "orderId").
		SetWhere(`order_payment.create_at >= ?`, "beginTime").
		SetWhere(`order_payment.create_at < ?`, "endTime").
		SetLimit().
		SetOrder("order_payment.create_at", "sortByCreateTime").
		SetOrder("order_payment.update_at", "sortByUpdateTime")
	var err error
	if query.AffectRow, err = query.SelectAndCount(); err != nil {
		return 0, OrderPayments, err
	}
	for _, OrderPaymentModel := range OrderPaymentModels {
		if OrderPayment, err := repository.transformPgModelToDomainModel(OrderPaymentModel); err != nil {
			return 0, OrderPayments, err
		} else {
			OrderPayments = append(OrderPayments, OrderPayment)
		}
	}
	return int64(query.AffectRow), OrderPayments, nil
}

func (repository *OrderPaymentRepository) transformPgModelToDomainModel(OrderPaymentModel *models.OrderPayment) (*domain.OrderPayment, error) {
	m := &domain.OrderPayment{}
	err := GobModelTransform(m, OrderPaymentModel)
	return m, err
}

func NewOrderPaymentRepository(transactionContext *transaction.TransactionContext) (*OrderPaymentRepository, error) {
	if transactionContext == nil {
		return nil, ERR_EMPTY_TC
	}
	return &OrderPaymentRepository{transactionContext: transactionContext}, nil
}