pg_partner_sub_account_repository.go 3.4 KB
package repository

import (
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
	. "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
)

type PartnerSubAccountRepository struct {
	transactionContext *transaction.TransactionContext
}

func (repository *PartnerSubAccountRepository) Save(dm *domain.PartnerSubAccount) (*domain.PartnerSubAccount, error) {
	var (
		err error
		m   = &models.PartnerSubAccount{}
		tx  = repository.transactionContext.PgTx
	)
	if err = GobModelTransform(m, dm); err != nil {
		return nil, err
	}
	if dm.Identify() == nil {
		if dm.Id, err = NewSnowflakeId(); err != nil {
			return dm, err
		}
		m.Id = dm.Id
		if err = tx.Insert(m); err != nil {
			return nil, err
		}
		return dm, nil
	}
	if err = tx.Update(m); err != nil {
		return nil, err
	}
	return dm, nil
}

func (repository *PartnerSubAccountRepository) Remove(PartnerSubAccount *domain.PartnerSubAccount) (*domain.PartnerSubAccount, error) {
	var (
		tx                     = repository.transactionContext.PgTx
		PartnerSubAccountModel = &models.PartnerSubAccount{Id: PartnerSubAccount.Identify().(int64)}
	)
	if _, err := tx.Model(PartnerSubAccountModel).Where("id = ?", PartnerSubAccount.Id).Delete(); err != nil {
		return PartnerSubAccount, err
	}
	return PartnerSubAccount, nil
}

func (repository *PartnerSubAccountRepository) FindOne(queryOptions map[string]interface{}) (*domain.PartnerSubAccount, error) {
	tx := repository.transactionContext.PgTx
	PartnerSubAccountModel := new(models.PartnerSubAccount)
	query := NewQuery(tx.Model(PartnerSubAccountModel), queryOptions)
	query.SetWhere("id = ?", "id")
	query.SetWhere("account = ?", "account")
	if err := query.First(); err != nil {
		return nil, query.HandleError(err, "没有此合伙人")
	}
	if PartnerSubAccountModel.Id == 0 {
		return nil, nil
	}
	return repository.transformPgModelToDomainModel(PartnerSubAccountModel)
}

func (repository *PartnerSubAccountRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.PartnerSubAccount, error) {
	tx := repository.transactionContext.PgTx
	var PartnerSubAccountModels []*models.PartnerSubAccount
	PartnerSubAccounts := make([]*domain.PartnerSubAccount, 0)
	query := NewQuery(tx.Model(&PartnerSubAccountModels), queryOptions)
	query.
		SetWhere("account = ?", "account").
		SetLimit()
	var err error
	if query.AffectRow, err = query.SelectAndCount(); err != nil {
		return 0, PartnerSubAccounts, err
	}
	for _, PartnerSubAccountModel := range PartnerSubAccountModels {
		if PartnerSubAccount, err := repository.transformPgModelToDomainModel(PartnerSubAccountModel); err != nil {
			return 0, PartnerSubAccounts, err
		} else {
			PartnerSubAccounts = append(PartnerSubAccounts, PartnerSubAccount)
		}
	}
	return int64(query.AffectRow), PartnerSubAccounts, nil
}

func (repository *PartnerSubAccountRepository) transformPgModelToDomainModel(PartnerSubAccountModel *models.PartnerSubAccount) (*domain.PartnerSubAccount, error) {
	m := &domain.PartnerSubAccount{}
	err := GobModelTransform(m, PartnerSubAccountModel)
	return m, err
}

func NewPartnerSubAccountRepository(transactionContext *transaction.TransactionContext) (*PartnerSubAccountRepository, error) {
	if transactionContext == nil {
		return nil, ERR_EMPTY_TC
	}
	return &PartnerSubAccountRepository{transactionContext: transactionContext}, nil
}