pg_access_repository.go 3.0 KB
package repository

import (
	"fmt"
	"github.com/go-pg/pg/v10"
	"github.com/tiptok/gocomm/common"
	. "github.com/tiptok/gocomm/pkg/orm/pgx"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/transaction"
)

type AccessRepository struct {
	transactionContext *transaction.TransactionContext
}

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

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

func (repository *AccessRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Access, error) {
	tx := repository.transactionContext.PgTx
	var AccessModels []*models.Access
	Accesss := make([]*domain.Access, 0)
	query := NewQuery(tx.Model(&AccessModels), queryOptions).
		SetOrder("create_time", "sortByCreateTime").
		SetOrder("update_time", "sortByUpdateTime")

	if inAccessIds, ok := queryOptions["inAccessIds"]; ok {
		query.Where("id in (?)", pg.In(inAccessIds))
	}
	var err error
	if query.AffectRow, err = query.SelectAndCount(); err != nil {
		return 0, Accesss, err
	}
	for _, AccessModel := range AccessModels {
		if Access, err := repository.transformPgModelToDomainModel(AccessModel); err != nil {
			return 0, Accesss, err
		} else {
			Accesss = append(Accesss, Access)
		}
	}
	return int64(query.AffectRow), Accesss, nil
}

func (repository *AccessRepository) transformPgModelToDomainModel(AccessModel *models.Access) (*domain.Access, error) {
	m := &domain.Access{}
	err := common.GobModelTransform(m, AccessModel)
	return m, err
}

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