pg_operator_log_repository.go 4.3 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/constant"
	"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 (
	cacheOperatorLogIdKey = func(id int64) string {
		return fmt.Sprintf("%v:cache:OperatorLog:id:%v", constant.POSTGRESQL_DB_NAME, id)
		// 不需要执行缓存时,key设置为空
		// return ""
	}
)

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

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

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

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

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

	query.SetWhere("resource_id=?", "resourceId")
	query.SetWhere("id<?", "lastId")
	query.SetOrder("id", "sortById").SetLimit()
	var err error
	if query.AffectRow, err = query.SelectAndCount(); err != nil {
		return 0, OperatorLogs, err
	}
	for _, OperatorLogModel := range OperatorLogModels {
		if OperatorLog, err := repository.transformPgModelToDomainModel(OperatorLogModel); err != nil {
			return 0, OperatorLogs, err
		} else {
			OperatorLogs = append(OperatorLogs, OperatorLog)
		}
	}
	return int64(query.AffectRow), OperatorLogs, nil
}

func (repository *OperatorLogRepository) transformPgModelToDomainModel(OperatorLogModel *models.OperatorLog) (*domain.OperatorLog, error) {
	m := &domain.OperatorLog{}
	err := common.GobModelTransform(m, OperatorLogModel)
	return m, err
}

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