pg_order_base_repository.go 3.9 KB
package repository

import (
	"encoding/json"
	"github.com/go-pg/pg/v10"
	"github.com/go-pg/pg/v10/orm"
	"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 OrderBaseRepository struct {
	transactionContext *transaction.TransactionContext
}

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

func (repository *OrderBaseRepository) FindOne(queryOptions map[string]interface{}) (*domain.OrderBase, error) {
	tx := repository.transactionContext.PgTx
	OrderBaseModel := new(models.OrderBase)
	query := NewQuery(tx.Model(OrderBaseModel), queryOptions)
	query.ColumnExpr("order_base.*")
	query.Relation("OrderGood", func(q *orm.Query) (*orm.Query, error) {
		q.ColumnExpr("order_good.*")
		q.Order("id")
		return q, nil
	})
	query.SetWhere(`"order_base".id = ?`, "id")
	query.SetWhere(`"order_base".order_code = ?`, "order_code")

	if err := query.First(); err != nil {
		return nil, query.HandleError(err, "没有此订单")
	}
	if OrderBaseModel.Id == 0 {
		return nil, nil
	}
	return repository.transformPgModelToDomainModel(OrderBaseModel)
}

func (repository *OrderBaseRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.OrderBase, error) {
	tx := repository.transactionContext.PgTx
	var OrderBaseModels []*models.OrderBase
	OrderBases := make([]*domain.OrderBase, 0)
	query := NewQuery(tx.Model(&OrderBaseModels), queryOptions)
	query.
		SetWhere(`"order_base".partner_id= ?`, "partnerId").
		SetWhere(`"order_base".order_type= ?`, "orderType").
		SetWhere(`"order_base".create_time >= ?`, "beginTime").
		SetWhere(`"order_base".create_time < ?`, "endTime").
		//SetWhere(`"order_base".is_disable = ?`, "isDisable").
		SetLimit().
		SetOrder(`order_base.create_time`, "sortByCreateTime").
		SetOrder(`order_base.update_time`, "sortByUpdateTime")
	if v, ok := queryOptions["orderTypes"]; ok {
		query.Where(`"order_base".order_type in (?)`, pg.In(v))
	}
	if v, ok := queryOptions["partnerCategoryId"]; ok {
		catId, _ := (v.(json.Number)).Int64()
		query.Where(`"order_base".partner_category @>'{"id":?}'`, catId)
	}
	var err error
	if query.AffectRow, err = query.SelectAndCount(); err != nil {
		return 0, OrderBases, err
	}
	for _, OrderBaseModel := range OrderBaseModels {
		if OrderBase, err := repository.transformPgModelToDomainModel(OrderBaseModel); err != nil {
			return 0, OrderBases, err
		} else {
			OrderBases = append(OrderBases, OrderBase)
		}
	}
	return int64(query.AffectRow), OrderBases, nil
}

func (repository *OrderBaseRepository) transformPgModelToDomainModel(OrderBaseModel *models.OrderBase) (*domain.OrderBase, error) {
	m := &domain.OrderBase{}
	err := GobModelTransform(m, OrderBaseModel)
	return m, err
}

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