pg_client_version_repository.go 4.4 KB
package repository

import (
	"fmt"
	"github.com/tiptok/gocomm/common"
	"github.com/tiptok/gocomm/pkg/cache"
	. "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"
)

var (
	cacheClientVersionIdKey = func(id int64) string {
		//return fmt.Sprintf("%v:cache:ClientVersion:id:%v", constant.POSTGRESQL_DB_NAME, id)
		// 不需要执行缓存时,key设置为空
		return ""
	}
)

type ClientVersionRepository struct {
	*cache.CachedRepository
	transactionContext *transaction.TransactionContext
}

func (repository *ClientVersionRepository) Save(dm *domain.ClientVersion) (*domain.ClientVersion, error) {
	var (
		err error
		m   = &models.ClientVersion{}
		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
		}
		dm.Id = m.Id
		return dm, nil
	}
	queryFunc := func() (interface{}, error) {
		return nil, tx.Update(m)
	}
	if _, err = repository.Query(queryFunc, cacheClientVersionIdKey(dm.Id)); err != nil {
		return nil, err
	}
	return dm, nil
}

func (repository *ClientVersionRepository) Remove(ClientVersion *domain.ClientVersion) (*domain.ClientVersion, error) {
	var (
		tx                 = repository.transactionContext.PgTx
		ClientVersionModel = &models.ClientVersion{Id: ClientVersion.Identify().(int64)}
	)
	queryFunc := func() (interface{}, error) {
		return tx.Model(ClientVersionModel).Where("id = ?", ClientVersion.Id).Delete()
	}
	if _, err := repository.Query(queryFunc, cacheClientVersionIdKey(ClientVersion.Id)); err != nil {
		return ClientVersion, err
	}
	return ClientVersion, nil
}

func (repository *ClientVersionRepository) FindOne(queryOptions map[string]interface{}) (*domain.ClientVersion, error) {
	tx := repository.transactionContext.PgDd
	ClientVersionModel := new(models.ClientVersion)
	queryFunc := func() (interface{}, error) {
		query := NewQuery(tx.Model(ClientVersionModel), queryOptions)
		query.SetWhere("id = ?", "id")
		if err := query.First(); err != nil {
			return nil, fmt.Errorf("query row not found")
		}
		return ClientVersionModel, nil
	}
	var options []cache.QueryOption
	if _, ok := queryOptions["id"]; !ok {
		options = append(options, cache.WithNoCacheFlag())
	} else {
		ClientVersionModel.Id = queryOptions["id"].(int64)
	}
	if err := repository.QueryCache(cacheClientVersionIdKey(ClientVersionModel.Id), ClientVersionModel, queryFunc, options...); err != nil {
		return nil, err
	}
	if ClientVersionModel.Id == 0 {
		return nil, fmt.Errorf("query row not found")
	}
	return repository.transformPgModelToDomainModel(ClientVersionModel)
}

func (repository *ClientVersionRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.ClientVersion, error) {
	tx := repository.transactionContext.PgDd
	var ClientVersionModels []*models.ClientVersion
	ClientVersions := make([]*domain.ClientVersion, 0)
	query := NewQuery(tx.Model(&ClientVersionModels), queryOptions).
		SetOrder("create_time", "sortByCreateTime").
		SetOrder("update_time", "sortByUpdateTime").
		SetLimit()

	if searchByText, ok := queryOptions["searchByText"]; ok && len(searchByText.(string)) > 0 {
		query.Where(fmt.Sprintf(`project_name like '%%%v%%'`, searchByText))
	}
	var err error
	if query.AffectRow, err = query.SelectAndCount(); err != nil {
		return 0, ClientVersions, err
	}
	for _, ClientVersionModel := range ClientVersionModels {
		if ClientVersion, err := repository.transformPgModelToDomainModel(ClientVersionModel); err != nil {
			return 0, ClientVersions, err
		} else {
			ClientVersions = append(ClientVersions, ClientVersion)
		}
	}
	return int64(query.AffectRow), ClientVersions, nil
}

func (repository *ClientVersionRepository) transformPgModelToDomainModel(ClientVersionModel *models.ClientVersion) (*domain.ClientVersion, error) {
	m := &domain.ClientVersion{}
	err := common.GobModelTransform(m, ClientVersionModel)
	return m, err
}

func NewClientVersionRepository(transactionContext *transaction.TransactionContext) (*ClientVersionRepository, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	}
	return &ClientVersionRepository{transactionContext: transactionContext, CachedRepository: cache.NewDefaultCachedRepository()}, nil
}