pg_cooperation_contract_repository.go 8.0 KB
package repository

import (
	"fmt"

	"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 CooperationContractRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

func (repository *CooperationContractRepository) nextIdentify() (int64, error) {
	IdWorker, err := snowflake.NewIdWorker(1)
	if err != nil {
		return 0, err
	}
	id, err := IdWorker.NextId()
	return id, err
}
func (repository *CooperationContractRepository) Save(cooperationContract *domain.CooperationContract) (*domain.CooperationContract, error) {
	sqlBuildFields := []string{
		"cooperation_contract_id",
		"cooperation_contract_description",
		"cooperation_contract_name",
		"cooperation_contract_number",
		"cooperation_contract_referrer",
		"cooperation_contract_salesman",
		"cooperation_contract_undertaker_type",
		"cooperation_contract_sponsor",
		"cooperation_mode",
		"status",
		"org",
		"company",
		"operator",
		"operate_time",
		"created_at",
		"deleted_at",
		"updated_at",
	}
	insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
	insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
	returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
	updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "cooperationContract_id")
	updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
	tx := repository.transactionContext.PgTx
	if cooperationContract.Identify() == nil {
		cooperationContractId, err := repository.nextIdentify()
		if err != nil {
			return cooperationContract, err
		} else {
			cooperationContract.CooperationContractId = cooperationContractId
		}
		if _, err := tx.QueryOne(
			pg.Scan(
				&cooperationContract.CooperationContractId,
				&cooperationContract.CooperationContractDescription,
				&cooperationContract.CooperationContractName,
				&cooperationContract.CooperationContractNumber,
				&cooperationContract.CooperationContractReferrer,
				&cooperationContract.CooperationContractSalesman,
				pg.Array(&cooperationContract.CooperationContractUndertakerType),
				&cooperationContract.CooperationContractSponsor,
				&cooperationContract.CooperationMode,
				&cooperationContract.Status,
				&cooperationContract.Org,
				&cooperationContract.Company,
				&cooperationContract.Operator,
				&cooperationContract.OperateTime,
				&cooperationContract.CreatedAt,
				&cooperationContract.DeletedAt,
				&cooperationContract.UpdatedAt,
			),
			fmt.Sprintf("INSERT INTO cooperation_contracts (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
			cooperationContract.CooperationContractId,
			cooperationContract.CooperationContractDescription,
			cooperationContract.CooperationContractName,
			cooperationContract.CooperationContractNumber,
			cooperationContract.CooperationContractReferrer,
			cooperationContract.CooperationContractSalesman,
			pg.Array(cooperationContract.CooperationContractUndertakerType),
			cooperationContract.CooperationContractSponsor,
			cooperationContract.CooperationMode,
			cooperationContract.Status,
			cooperationContract.Org,
			cooperationContract.Company,
			cooperationContract.Operator,
			cooperationContract.OperateTime,
			cooperationContract.CreatedAt,
			cooperationContract.DeletedAt,
			cooperationContract.UpdatedAt,
		); err != nil {
			return cooperationContract, err
		}
	} else {
		if _, err := tx.QueryOne(
			pg.Scan(
				&cooperationContract.CooperationContractId,
				&cooperationContract.CooperationContractDescription,
				&cooperationContract.CooperationContractName,
				&cooperationContract.CooperationContractNumber,
				&cooperationContract.CooperationContractReferrer,
				&cooperationContract.CooperationContractSalesman,
				pg.Array(&cooperationContract.CooperationContractUndertakerType),
				&cooperationContract.CooperationContractSponsor,
				&cooperationContract.CooperationMode,
				&cooperationContract.Status,
				&cooperationContract.Org,
				&cooperationContract.Company,
				&cooperationContract.Operator,
				&cooperationContract.OperateTime,
				&cooperationContract.CreatedAt,
				&cooperationContract.DeletedAt,
				&cooperationContract.UpdatedAt,
			),
			fmt.Sprintf("UPDATE cooperation_contracts SET %s WHERE cooperation_contract_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
			cooperationContract.CooperationContractId,
			cooperationContract.CooperationContractDescription,
			cooperationContract.CooperationContractName,
			cooperationContract.CooperationContractNumber,
			cooperationContract.CooperationContractReferrer,
			cooperationContract.CooperationContractSalesman,
			pg.Array(cooperationContract.CooperationContractUndertakerType),
			cooperationContract.CooperationContractSponsor,
			cooperationContract.CooperationMode,
			cooperationContract.Status,
			cooperationContract.Org,
			cooperationContract.Company,
			cooperationContract.Operator,
			cooperationContract.OperateTime,
			cooperationContract.CreatedAt,
			cooperationContract.DeletedAt,
			cooperationContract.UpdatedAt,
			cooperationContract.Identify(),
		); err != nil {
			return cooperationContract, err
		}
	}
	return cooperationContract, nil
}
func (repository *CooperationContractRepository) Remove(cooperationContract *domain.CooperationContract) (*domain.CooperationContract, error) {
	tx := repository.transactionContext.PgTx
	cooperationContractModel := new(models.CooperationContract)
	cooperationContractModel.CooperationContractId = cooperationContract.Identify().(int64)
	if _, err := tx.Model(cooperationContractModel).WherePK().Delete(); err != nil {
		return cooperationContract, err
	}
	return cooperationContract, nil
}
func (repository *CooperationContractRepository) FindOne(queryOptions map[string]interface{}) (*domain.CooperationContract, error) {
	tx := repository.transactionContext.PgTx
	cooperationContractModel := new(models.CooperationContract)
	query := sqlbuilder.BuildQuery(tx.Model(cooperationContractModel), queryOptions)
	query.SetWhereByQueryOption("cooperation_contract.cooperation_contract_id = ?", "cooperationContractId")
	if err := query.First(); err != nil {
		if err.Error() == "pg: no rows in result set" {
			return nil, fmt.Errorf("没有此资源")
		} else {
			return nil, err
		}
	}
	if cooperationContractModel.CooperationContractId == 0 {
		return nil, nil
	} else {
		return transform.TransformToCooperationContractDomainModelFromPgModels(cooperationContractModel)
	}
}
func (repository *CooperationContractRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.CooperationContract, error) {
	tx := repository.transactionContext.PgTx
	var cooperationContractModels []*models.CooperationContract
	cooperationContracts := make([]*domain.CooperationContract, 0)
	query := sqlbuilder.BuildQuery(tx.Model(&cooperationContractModels), queryOptions)
	query.SetOffsetAndLimit(20)
	query.SetOrderDirect("cooperation_contract_id", "DESC")
	if count, err := query.SelectAndCount(); err != nil {
		return 0, cooperationContracts, err
	} else {
		for _, cooperationContractModel := range cooperationContractModels {
			if cooperationContract, err := transform.TransformToCooperationContractDomainModelFromPgModels(cooperationContractModel); err != nil {
				return 0, cooperationContracts, err
			} else {
				cooperationContracts = append(cooperationContracts, cooperationContract)
			}
		}
		return int64(count), cooperationContracts, nil
	}
}
func NewCooperationContractRepository(transactionContext *pgTransaction.TransactionContext) (*CooperationContractRepository, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &CooperationContractRepository{
			transactionContext: transactionContext,
		}, nil
	}
}