pg_order_good_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 OrderGoodRepository struct {
	transactionContext *transaction.TransactionContext
}

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

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

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

func (repository *OrderGoodRepository) transformPgModelToDomainModel(OrderGoodModel *models.OrderGood) (*domain.OrderGood, error) {
	m := &domain.OrderGood{}
	err := GobModelTransform(m, OrderGoodModel)
	return m, err
}

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