pg_cooperation_contract_undertaker_repository.go 9.5 KB
package repository

import (
	"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/allied-creation-cooperation/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/transform"
)

type CooperationContractUndertakerRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

func (repository *CooperationContractUndertakerRepository) nextIdentify() (int64, error) {
	IdWorker, err := snowflake.NewIdWorker(1)
	if err != nil {
		return 0, err
	}
	id, err := IdWorker.NextId()
	return id, err
}
func (repository *CooperationContractUndertakerRepository) Save(cooperationContractUndertaker *domain.CooperationContractUndertaker) (*domain.CooperationContractUndertaker, error) {
	sqlBuildFields := []string{
		"cooperation_contract_undertaker_id",
		"cooperation_contract_number",
		"user_id",
		"user_base_id",
		"org",
		"orgs",
		"department",
		"role",
		"user_info",
		"user_type",
		"referrer",
		"salesman",
		"status",
		"company",
		"contract_attachment",
		"created_at",
		"updated_at",
		"deleted_at",
	}
	insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
	insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
	returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
	updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "cooperationContractUndertaker_id")
	updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
	tx := repository.transactionContext.PgTx
	if cooperationContractUndertaker.Identify() == nil {
		cooperationContractUndertakerId, err := repository.nextIdentify()
		if err != nil {
			return cooperationContractUndertaker, err
		} else {
			cooperationContractUndertaker.CooperationContractUndertakerId = cooperationContractUndertakerId
		}
		if _, err := tx.QueryOne(
			pg.Scan(
				&cooperationContractUndertaker.CooperationContractUndertakerId,
				&cooperationContractUndertaker.CooperationContractNumber,
				&cooperationContractUndertaker.Undertaker.UserId,
				&cooperationContractUndertaker.Undertaker.UserBaseId,
				&cooperationContractUndertaker.Undertaker.Org,
				&cooperationContractUndertaker.Undertaker.Orgs,
				&cooperationContractUndertaker.Undertaker.Department,
				&cooperationContractUndertaker.Undertaker.Role,
				&cooperationContractUndertaker.Undertaker.UserInfo,
				&cooperationContractUndertaker.Undertaker.UserType,
				&cooperationContractUndertaker.Undertaker.Referrer,
				&cooperationContractUndertaker.Undertaker.Salesman,
				&cooperationContractUndertaker.Undertaker.Status,
				&cooperationContractUndertaker.Undertaker.Company,
				&cooperationContractUndertaker.Undertaker.ContractAttachment,
				&cooperationContractUndertaker.CreatedAt,
				&cooperationContractUndertaker.UpdatedAt,
				&cooperationContractUndertaker.DeletedAt,
			),
			fmt.Sprintf("INSERT INTO cooperation_contract_undertakers (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
			cooperationContractUndertaker.CooperationContractUndertakerId,
			cooperationContractUndertaker.CooperationContractNumber,
			&cooperationContractUndertaker.Undertaker.UserId,
			&cooperationContractUndertaker.Undertaker.UserBaseId,
			&cooperationContractUndertaker.Undertaker.Org,
			&cooperationContractUndertaker.Undertaker.Orgs,
			&cooperationContractUndertaker.Undertaker.Department,
			&cooperationContractUndertaker.Undertaker.Role,
			&cooperationContractUndertaker.Undertaker.UserInfo,
			&cooperationContractUndertaker.Undertaker.UserType,
			&cooperationContractUndertaker.Undertaker.Referrer,
			&cooperationContractUndertaker.Undertaker.Salesman,
			&cooperationContractUndertaker.Undertaker.Status,
			&cooperationContractUndertaker.Undertaker.Company,
			&cooperationContractUndertaker.Undertaker.ContractAttachment,
			cooperationContractUndertaker.CreatedAt,
			cooperationContractUndertaker.UpdatedAt,
			cooperationContractUndertaker.DeletedAt,
		); err != nil {
			return cooperationContractUndertaker, err
		}
	} else {
		if _, err := tx.QueryOne(
			pg.Scan(
				&cooperationContractUndertaker.CooperationContractUndertakerId,
				&cooperationContractUndertaker.CooperationContractNumber,
				&cooperationContractUndertaker.Undertaker.UserId,
				&cooperationContractUndertaker.Undertaker.UserBaseId,
				&cooperationContractUndertaker.Undertaker.Org,
				&cooperationContractUndertaker.Undertaker.Orgs,
				&cooperationContractUndertaker.Undertaker.Department,
				&cooperationContractUndertaker.Undertaker.Role,
				&cooperationContractUndertaker.Undertaker.UserInfo,
				&cooperationContractUndertaker.Undertaker.UserType,
				&cooperationContractUndertaker.Undertaker.Referrer,
				&cooperationContractUndertaker.Undertaker.Salesman,
				&cooperationContractUndertaker.Undertaker.Status,
				&cooperationContractUndertaker.Undertaker.Company,
				&cooperationContractUndertaker.Undertaker.ContractAttachment,
				&cooperationContractUndertaker.CreatedAt,
				&cooperationContractUndertaker.UpdatedAt,
				&cooperationContractUndertaker.DeletedAt,
			),
			fmt.Sprintf("UPDATE cooperation_contract_undertakers SET %s WHERE cooperation_contract_undertaker_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
			cooperationContractUndertaker.CooperationContractUndertakerId,
			cooperationContractUndertaker.CooperationContractNumber,
			cooperationContractUndertaker.Undertaker.UserId,
			cooperationContractUndertaker.Undertaker.UserBaseId,
			cooperationContractUndertaker.Undertaker.Org,
			cooperationContractUndertaker.Undertaker.Orgs,
			cooperationContractUndertaker.Undertaker.Department,
			cooperationContractUndertaker.Undertaker.Role,
			cooperationContractUndertaker.Undertaker.UserInfo,
			cooperationContractUndertaker.Undertaker.UserType,
			cooperationContractUndertaker.Undertaker.Referrer,
			cooperationContractUndertaker.Undertaker.Salesman,
			cooperationContractUndertaker.Undertaker.Status,
			cooperationContractUndertaker.Undertaker.Company,
			cooperationContractUndertaker.Undertaker.ContractAttachment,
			cooperationContractUndertaker.CreatedAt,
			cooperationContractUndertaker.UpdatedAt,
			cooperationContractUndertaker.DeletedAt,
			cooperationContractUndertaker.Identify(),
		); err != nil {
			return cooperationContractUndertaker, err
		}
	}
	return cooperationContractUndertaker, nil
}
func (repository *CooperationContractUndertakerRepository) Remove(cooperationContractUndertaker *domain.CooperationContractUndertaker) (*domain.CooperationContractUndertaker, error) {
	tx := repository.transactionContext.PgTx
	cooperationContractUndertakerModel := new(models.CooperationContractUndertaker)
	cooperationContractUndertakerModel.CooperationContractUndertakerId = cooperationContractUndertaker.Identify().(int64)
	if _, err := tx.Model(cooperationContractUndertakerModel).WherePK().Delete(); err != nil {
		return cooperationContractUndertaker, err
	}
	return cooperationContractUndertaker, nil
}
func (repository *CooperationContractUndertakerRepository) FindOne(queryOptions map[string]interface{}) (*domain.CooperationContractUndertaker, error) {
	tx := repository.transactionContext.PgTx
	cooperationContractUndertakerModel := new(models.CooperationContractUndertaker)
	query := sqlbuilder.BuildQuery(tx.Model(cooperationContractUndertakerModel), queryOptions)
	query.SetWhereByQueryOption("cooperation_contract_undertaker.cooperation_contract_undertaker_id = ?", "cooperationContractUndertakerId")
	if err := query.First(); err != nil {
		if err.Error() == "pg: no rows in result set" {
			return nil, fmt.Errorf("没有此资源")
		} else {
			return nil, err
		}
	}
	if cooperationContractUndertakerModel.CooperationContractUndertakerId == 0 {
		return nil, nil
	} else {
		return transform.TransformToCooperationContractUndertakerDomainModelFromPgModels(cooperationContractUndertakerModel)
	}
}
func (repository *CooperationContractUndertakerRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.CooperationContractUndertaker, error) {
	tx := repository.transactionContext.PgTx
	var cooperationContractUndertakerModels []*models.CooperationContractUndertaker
	cooperationContractUndertakers := make([]*domain.CooperationContractUndertaker, 0)
	query := sqlbuilder.BuildQuery(tx.Model(&cooperationContractUndertakerModels), queryOptions)
	query.SetOffsetAndLimit(20)
	query.SetOrderDirect("cooperation_contract_undertaker_id", "DESC")
	if count, err := query.SelectAndCount(); err != nil {
		return 0, cooperationContractUndertakers, err
	} else {
		for _, cooperationContractUndertakerModel := range cooperationContractUndertakerModels {
			if cooperationContractUndertaker, err := transform.TransformToCooperationContractUndertakerDomainModelFromPgModels(cooperationContractUndertakerModel); err != nil {
				return 0, cooperationContractUndertakers, err
			} else {
				cooperationContractUndertakers = append(cooperationContractUndertakers, cooperationContractUndertaker)
			}
		}
		return int64(count), cooperationContractUndertakers, nil
	}
}
func NewCooperationContractUndertakerRepository(transactionContext *pgTransaction.TransactionContext) (*CooperationContractUndertakerRepository, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &CooperationContractUndertakerRepository{
			transactionContext: transactionContext,
		}, nil
	}
}