pg_sys_message_consume_repository.go 3.4 KB
package repository

import (
	"fmt"

	"github.com/tiptok/gocomm/common"
	. "github.com/tiptok/gocomm/pkg/orm/pgx"
	"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"
)

type SysMessageConsumeRepository struct {
	transactionContext *transaction.TransactionContext
}

func (repository *SysMessageConsumeRepository) Save(dm *domain.SysMessageConsume) (*domain.SysMessageConsume, error) {
	var (
		err error
		m   = &models.SysMessageConsume{}
		tx  = repository.transactionContext.PgTx
	)
	if err = common.GobModelTransform(m, dm); err != nil {
		return nil, err
	}
	if dm.Identify() == nil {
		if _, err = tx.Model(m).Returning("*").Insert(m); err != nil {
			return nil, err
		}
		return dm, nil
	}
	if _, err = tx.Model(m).Update(m); err != nil {
		return nil, err
	}
	return dm, nil
}

func (repository *SysMessageConsumeRepository) Remove(SysMessageConsume *domain.SysMessageConsume) (*domain.SysMessageConsume, error) {
	var (
		tx                     = repository.transactionContext.PgTx
		SysMessageConsumeModel = &models.SysMessageConsume{Id: SysMessageConsume.Identify().(int64)}
	)
	if _, err := tx.Model(SysMessageConsumeModel).Where("id = ?", SysMessageConsume.Id).Delete(); err != nil {
		return SysMessageConsume, err
	}
	return SysMessageConsume, nil
}

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

func (repository *SysMessageConsumeRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.SysMessageConsume, error) {
	tx := repository.transactionContext.PgTx
	var SysMessageConsumeModels []*models.SysMessageConsume
	SysMessageConsumes := make([]*domain.SysMessageConsume, 0)
	query := NewQuery(tx.Model(&SysMessageConsumeModels), queryOptions).
		SetOrder("create_at", "sortByCreateTime")
	var err error
	if query.AffectRow, err = query.SelectAndCount(); err != nil {
		return 0, SysMessageConsumes, err
	}
	for _, SysMessageConsumeModel := range SysMessageConsumeModels {
		if SysMessageConsume, err := repository.transformPgModelToDomainModel(SysMessageConsumeModel); err != nil {
			return 0, SysMessageConsumes, err
		} else {
			SysMessageConsumes = append(SysMessageConsumes, SysMessageConsume)
		}
	}
	return int64(query.AffectRow), SysMessageConsumes, nil
}

func (repository *SysMessageConsumeRepository) transformPgModelToDomainModel(SysMessageConsumeModel *models.SysMessageConsume) (*domain.SysMessageConsume, error) {
	m := &domain.SysMessageConsume{}
	err := common.GobModelTransform(m, SysMessageConsumeModel)
	return m, err
}

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