pg_order_payment_reponsitory.go 4.0 KB
package repository

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

type OrderPaymentRepository struct {
	transactionContext *transaction.TransactionContext
}

func (repository *OrderPaymentRepository) Save(dm *domain.OrderPayment) (*domain.OrderPayment, error) {
	var (
		err error
		tx  = repository.transactionContext.PgTx
	)
	m := &models.OrderPayment{
		Id:            dm.Id,
		OrderId:       dm.OrderId,
		PartnerId:     dm.PartnerId,
		PaymentAmount: dm.PaymentAmount,
		BonusAmount:   dm.BonusAmount,
		BonusStatus:   dm.BonusStatus,
		CreateAt:      dm.CreateAt,
		UpdateAt:      dm.UpdateAt,
	}
	if m.Id == 0 {
		err = tx.Insert(m)
		dm.Id = m.Id
		if err != nil {
			return nil, err
		}
	} else {
		err = tx.Update(m)
		if 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 domain.OrderPaymentFindOneQuery) (*domain.OrderPayment, error) {
	tx := repository.transactionContext.PgTx
	OrderPaymentModel := new(models.OrderPayment)
	query := tx.Model(OrderPaymentModel)

	if queryOptions.OrderId > 0 {
		query.Where("order_payment.order_id = ?", queryOptions.OrderId)
	}
	if queryOptions.PaymentId > 0 {
		query.Where("order_payment.id = ?", queryOptions.PaymentId)
	}
	if err := query.First(); err != nil {
		return nil, err
	}
	if OrderPaymentModel.Id == 0 {
		return nil, nil
	}
	return repository.transformPgModelToDomainModel(OrderPaymentModel)
}

func (repository *OrderPaymentRepository) Find(queryOptions domain.OrderPaymentQuery) ([]*domain.OrderPayment, error) {
	tx := repository.transactionContext.PgTx
	var OrderPaymentModels []*models.OrderPayment
	query := tx.Model(&OrderPaymentModels)
	query.Where("order_id=?", queryOptions.OrderId)
	query.Order("id ASC")
	var (
		err error
		rsp = make([]*domain.OrderPayment, 0)
	)
	err = query.Select()
	if err != nil {
		return rsp, err
	}
	for i := range OrderPaymentModels {
		dm, err := repository.transformPgModelToDomainModel(OrderPaymentModels[i])
		if err != nil {
			return rsp, err
		}
		rsp = append(rsp, dm)
	}
	return rsp, nil
}

func (repository OrderPaymentRepository) CountAll(queryOption domain.OrderPaymentQuery) (int, error) {
	db := repository.transactionContext.PgDd
	partnerModels := models.PartnerInfo{}
	query := db.Model(&partnerModels)
	//if len(queryOption.PartnerName) > 0 {
	//	query = query.Where("partner_name like ?", "%"+queryOption.PartnerName+"%")
	//}
	//if queryOption.RegionInfo != nil {
	//	query = query.Where("region_info::jsonb->>'regionName' like ?", "%"+queryOption.RegionInfo.RegionName+"%")
	//}
	//if len(queryOption.PartnerCategory) > 0 {
	//	query = query.WhereIn("partner_category in(?)", queryOption.PartnerCategory)
	//}
	cnt, err := query.Count()
	return cnt, err
}

func (repository *OrderPaymentRepository) transformPgModelToDomainModel(dm *models.OrderPayment) (*domain.OrderPayment, error) {
	m := &domain.OrderPayment{
		Id:            dm.Id,
		OrderId:       dm.OrderId,
		PartnerId:     dm.PartnerId,
		PaymentAmount: dm.PaymentAmount,
		BonusAmount:   dm.BonusAmount,
		BonusStatus:   dm.BonusStatus,
		CreateAt:      dm.CreateAt,
		UpdateAt:      dm.UpdateAt,
	}
	return m, nil
}

func NewOrderPaymentRepository(transactionContext *transaction.TransactionContext) (*OrderPaymentRepository, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	}
	return &OrderPaymentRepository{transactionContext: transactionContext}, nil
}