pg_sys_message_produce_repository.go 3.3 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 SysMessageProduceRepository struct {
	transactionContext *transaction.TransactionContext
}

func (repository *SysMessageProduceRepository) Save(dm *domain.SysMessageProduce) (*domain.SysMessageProduce, error) {
	var (
		err error
		m   = &models.SysMessageProduce{}
		tx  = repository.transactionContext.PgTx
	)
	if err = common.GobModelTransform(m, dm); err != nil {
		return nil, err
	}

	if _, err = tx.Model(m).Returning("id").Insert(m); err != nil {
		return nil, err
	}
	return dm, nil
}

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

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

func (repository *SysMessageProduceRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.SysMessageProduce, error) {
	tx := repository.transactionContext.PgDd
	var SysMessageProduceModels []*models.SysMessageProduce
	SysMessageProduces := make([]*domain.SysMessageProduce, 0)
	query := NewQuery(tx.Model(&SysMessageProduceModels), queryOptions).
		SetWhere("status = ?", "status").
		SetOrder("update_time", "sortByUpdateTime")
	var err error
	if query.AffectRow, err = query.SelectAndCount(); err != nil {
		return 0, SysMessageProduces, err
	}
	for _, SysMessageProduceModel := range SysMessageProduceModels {
		if SysMessageProduce, err := repository.transformPgModelToDomainModel(SysMessageProduceModel); err != nil {
			return 0, SysMessageProduces, err
		} else {
			SysMessageProduces = append(SysMessageProduces, SysMessageProduce)
		}
	}
	return int64(query.AffectRow), SysMessageProduces, nil
}

func (repository *SysMessageProduceRepository) transformPgModelToDomainModel(SysMessageProduceModel *models.SysMessageProduce) (*domain.SysMessageProduce, error) {
	m := &domain.SysMessageProduce{}
	err := common.GobModelTransform(m, SysMessageProduceModel)
	return m, err
}

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