pg_business_bonus_repository.go 3.5 KB
package repository

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

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

func (repository *BusinessBonusRepository) FindOne(queryOptions map[string]interface{}) (*domain.BusinessBonus, error) {
	tx := repository.transactionContext.PgTx
	BusinessBonusModel := new(models.BusinessBonus)
	query := NewQuery(tx.Model(BusinessBonusModel), queryOptions)
	query.SetWhere("id = ?", "id")
	query.SetWhere("is_disable = ?", "isDisable")
	query.SetWhere("partner_info_id = ?", "partner_id")
	if err := query.First(); err != nil {
		return nil, fmt.Errorf("query row not found")
	}
	if BusinessBonusModel.Id == 0 {
		return nil, fmt.Errorf("query row not found")
	}
	return repository.transformPgModelToDomainModel(BusinessBonusModel)
}

func (repository *BusinessBonusRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.BusinessBonus, error) {
	tx := repository.transactionContext.PgTx
	var BusinessBonusModels []*models.BusinessBonus
	BusinessBonuss := make([]*domain.BusinessBonus, 0)
	query := NewQuery(tx.Model(&BusinessBonusModels), queryOptions).
		SetOrder("create_time", "sortByCreateTime").
		SetOrder("update_time", "sortByUpdateTime").
		SetWhere("is_disable = ?", "isDisable")
	if v, ok := queryOptions["inPartnerIds"]; ok {
		query.Where("partner_info_id in (?)", pg.In(v.([]int64)))
	}
	var err error
	if query.AffectRow, err = query.SelectAndCount(); err != nil {
		return 0, BusinessBonuss, err
	}
	for _, BusinessBonusModel := range BusinessBonusModels {
		if BusinessBonus, err := repository.transformPgModelToDomainModel(BusinessBonusModel); err != nil {
			return 0, BusinessBonuss, err
		} else {
			BusinessBonuss = append(BusinessBonuss, BusinessBonus)
		}
	}
	return int64(query.AffectRow), BusinessBonuss, nil
}

func (repository *BusinessBonusRepository) transformPgModelToDomainModel(BusinessBonusModel *models.BusinessBonus) (*domain.BusinessBonus, error) {
	m := &domain.BusinessBonus{}
	err := GobModelTransform(m, BusinessBonusModel)
	return m, err
}

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