pg_order_repository.go 3.1 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 OrderRepository struct {
	transactionContext *transaction.TransactionContext
}

func (repository *OrderRepository) Save(dm *domain.Order) (*domain.Order, error) {
	var (
		err error
		m   = &models.Order{}
		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 *OrderRepository) Remove(Order *domain.Order) (*domain.Order, error) {
	var (
		tx         = repository.transactionContext.PgTx
		OrderModel = &models.Order{Id: Order.Identify().(int64)}
	)
	if _, err := tx.Model(OrderModel).Where("id = ?", Order.Id).Delete(); err != nil {
		return Order, err
	}
	return Order, nil
}

func (repository *OrderRepository) FindOne(queryOptions map[string]interface{}) (*domain.Order, error) {
	tx := repository.transactionContext.PgTx
	OrderModel := new(models.Order)
	query := NewQuery(tx.Model(OrderModel), queryOptions)
	query.SetWhere(`"order".id = ?`, "id")
	query.SetWhere(`"order".order_code = ?`, "orderCode")
	if err := query.First(); err != nil {
		return nil, query.HandleError(err, "没有此订单")
	}
	if OrderModel.Id == 0 {
		return nil, nil
	}
	return repository.transformPgModelToDomainModel(OrderModel)
}

func (repository *OrderRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Order, error) {
	tx := repository.transactionContext.PgTx
	var OrderModels []*models.Order
	Orders := make([]*domain.Order, 0)
	query := NewQuery(tx.Model(&OrderModels), queryOptions)
	query.
		SetWhere(`"order".partner_id= ?`, "partnerId").
		SetWhere(`"order".order_type= ?`, "orderType").
		SetWhere(`"order".order_status= ?`, "orderStatus").
		SetWhere(`"order".create_at >= ?`, "beginTime").
		SetWhere(`"order".create_at < ?`, "endTime").
		SetLimit().
		SetOrder(`order.create_at`, "sortByCreateTime").
		SetOrder(`order.update_at`, "sortByUpdateTime")
	var err error
	if query.AffectRow, err = query.SelectAndCount(); err != nil {
		return 0, Orders, err
	}
	for _, OrderModel := range OrderModels {
		if Order, err := repository.transformPgModelToDomainModel(OrderModel); err != nil {
			return 0, Orders, err
		} else {
			Orders = append(Orders, Order)
		}
	}
	return int64(query.AffectRow), Orders, nil
}

func (repository *OrderRepository) transformPgModelToDomainModel(OrderModel *models.Order) (*domain.Order, error) {
	m := &domain.Order{}
	err := GobModelTransform(m, OrderModel)
	return m, err
}

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