pg_query_set_repository.go 6.1 KB
package repository

import (
	"errors"
	"fmt"
	"github.com/go-pg/pg/v10"

	"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"github.com/linmadan/egglib-go/utils/snowflake"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/pg/transform"
)

type QuerySetRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

func (repository *QuerySetRepository) nextIdentify() (int64, error) {
	IdWorker, err := snowflake.NewIdWorker(1)
	if err != nil {
		return 0, err
	}
	id, err := IdWorker.NextId()
	return id, err
}
func (repository *QuerySetRepository) Save(querySet *domain.QuerySet) (*domain.QuerySet, error) {
	sqlBuildFields := []string{
		"query_set_id",
		"type",
		"flag",
		"name",
		"pin_name",
		"parent_id",
		"status",
		"query_set_info",
		"query_components",
		"sort",
		"created_at",
		"updated_at",
		"context",
	}
	insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "query_set_id", "deleted_at"))
	insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "query_set_id", "deleted_at"))
	returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
	updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "query_set_id")
	updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
	tx := repository.transactionContext.PgTx
	if querySet.Identify() == nil {
		if _, err := tx.QueryOne(
			pg.Scan(
				&querySet.QuerySetId,
				&querySet.Type,
				&querySet.Flag,
				&querySet.Name,
				&querySet.PinName,
				&querySet.ParentId,
				&querySet.Status,
				&querySet.QuerySetInfo,
				&querySet.QueryComponents,
				&querySet.Sort,
				&querySet.CreatedAt,
				&querySet.UpdatedAt,
				&querySet.Context,
			),
			fmt.Sprintf("INSERT INTO metadata.query_sets (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
			querySet.Type,
			querySet.Flag,
			querySet.Name,
			querySet.PinName,
			querySet.ParentId,
			querySet.Status,
			querySet.QuerySetInfo,
			querySet.QueryComponents,
			querySet.Sort,
			querySet.CreatedAt,
			querySet.UpdatedAt,
			querySet.Context,
		); err != nil {
			return querySet, err
		}
	} else {
		if _, err := tx.QueryOne(
			pg.Scan(
				&querySet.QuerySetId,
				&querySet.Type,
				&querySet.Flag,
				&querySet.Name,
				&querySet.PinName,
				&querySet.ParentId,
				&querySet.Status,
				&querySet.QuerySetInfo,
				&querySet.QueryComponents,
				&querySet.Sort,
				&querySet.CreatedAt,
				&querySet.UpdatedAt,
				&querySet.Context,
			),
			fmt.Sprintf("UPDATE metadata.query_sets SET %s WHERE query_set_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
			querySet.Type,
			querySet.Flag,
			querySet.Name,
			querySet.PinName,
			querySet.ParentId,
			querySet.Status,
			querySet.QuerySetInfo,
			querySet.QueryComponents,
			querySet.Sort,
			querySet.CreatedAt,
			querySet.UpdatedAt,
			querySet.Context,
			querySet.Identify(),
		); err != nil {
			return querySet, err
		}
	}
	return querySet, nil
}
func (repository *QuerySetRepository) Remove(querySet *domain.QuerySet) (*domain.QuerySet, error) {
	tx := repository.transactionContext.PgTx
	querySetModel := new(models.QuerySet)
	querySetModel.QuerySetId = querySet.Identify().(int)
	if _, err := tx.Model(querySetModel).WherePK().Delete(); err != nil {
		return querySet, err
	}
	return querySet, nil
}
func (repository *QuerySetRepository) FindOne(queryOptions map[string]interface{}) (*domain.QuerySet, error) {
	tx := repository.transactionContext.PgTx
	querySetModel := new(models.QuerySet)
	query := sqlbuilder.BuildQuery(tx.Model(querySetModel), queryOptions)
	WhereContext(query, queryOptions)
	query.SetWhereByQueryOption("query_set_id = ?", "querySetId")
	query.SetWhereByQueryOption("type = ?", "type")
	query.SetWhereByQueryOption("flag = ?", "flag")
	query.SetWhereByQueryOption("parent_id = ?", "parentId")
	query.SetWhereByQueryOption("name = ?", "name")
	if err := query.First(); err != nil {
		if errors.Is(err, pg.ErrNoRows) {
			return nil, domain.ErrorNotFound
		} else {
			return nil, err
		}
	}
	if querySetModel.QuerySetId == 0 {
		return nil, nil
	} else {
		return transform.TransformToQuerySetDomainModelFromPgModels(querySetModel)
	}
}
func (repository *QuerySetRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.QuerySet, error) {
	tx := repository.transactionContext.PgTx
	var querySetModels []*models.QuerySet
	querySets := make([]*domain.QuerySet, 0)
	query := sqlbuilder.BuildQuery(tx.Model(&querySetModels), queryOptions)
	WhereContext(query, queryOptions)
	query.SetWhereByQueryOption("type = ?", "type")
	query.SetWhereByQueryOption("flag = ?", "flag")
	query.SetWhereByQueryOption(fmt.Sprintf("name like '%%%v%%'", queryOptions["matchName"]), "matchName")
	if v, ok := queryOptions["inParentIds"]; ok && len(v.([]int)) > 0 {
		query.Where("parent_id in (?)", pg.In(v))
	}
	if v, ok := queryOptions["sortByName"]; ok && len(v.(string)) > 0 {
		query.SetOrderDirect("pin_name", v.(string))
	} else if v, ok := queryOptions["sortByTime"]; ok && len(v.(string)) > 0 {
		query.SetOrderDirect("created_at", v.(string))
	} else {
		query.SetOrderDirect("parent_id", "ASC")
		query.SetOrderDirect("sort", "ASC")
	}
	if count, err := query.SelectAndCount(); err != nil {
		return 0, querySets, err
	} else {
		for _, querySetModel := range querySetModels {
			if querySet, err := transform.TransformToQuerySetDomainModelFromPgModels(querySetModel); err != nil {
				return 0, querySets, err
			} else {
				querySets = append(querySets, querySet)
			}
		}
		return int64(count), querySets, nil
	}
}
func NewQuerySetRepository(transactionContext *pgTransaction.TransactionContext) (*QuerySetRepository, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &QuerySetRepository{
			transactionContext: transactionContext,
		}, nil
	}
}