pg_partner_info_repository_go.bak 3.3 KB
package repository

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

type PartnerInfoRepository struct {
	transactionContext *transaction.TransactionContext
}

func (repository *PartnerInfoRepository) Save(dm *domain.PartnerInfo) (*domain.PartnerInfo, error) {
	var (
		err error
		m   = &models.PartnerInfo{}
		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 *PartnerInfoRepository) Remove(PartnerInfo *domain.PartnerInfo) (*domain.PartnerInfo, error) {
	var (
		tx               = repository.transactionContext.PgTx
		PartnerInfoModel = &models.PartnerInfo{Id: PartnerInfo.Identify().(int64)}
	)
	if _, err := tx.Model(PartnerInfoModel).Where("id = ?", PartnerInfo.Id).Delete(); err != nil {
		return PartnerInfo, err
	}
	return PartnerInfo, nil
}

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

func (repository *PartnerInfoRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.PartnerInfo, error) {
	tx := repository.transactionContext.PgTx
	var PartnerInfoModels []*models.PartnerInfo
	PartnerInfos := make([]*domain.PartnerInfo, 0)
	query := NewQuery(tx.Model(&PartnerInfoModels), queryOptions)
	query.
		SetWhere("partner_info.account = ?", "account").
		SetWhere(`partner_info.status = ?`, "status").
		SetWhere(`partner_info.partner_category = ?`, "partnerCategory").
		SetLimit().
		SetOrder("partner_info.create_at", "sortByCreateTime").
		SetOrder("partner_info.update_at", "sortByUpdateTime")
	var err error
	if query.AffectRow, err = query.SelectAndCount(); err != nil {
		return 0, PartnerInfos, err
	}
	for _, PartnerInfoModel := range PartnerInfoModels {
		if PartnerInfo, err := repository.transformPgModelToDomainModel(PartnerInfoModel); err != nil {
			return 0, PartnerInfos, err
		} else {
			PartnerInfos = append(PartnerInfos, PartnerInfo)
		}
	}
	return int64(query.AffectRow), PartnerInfos, nil
}

func (repository *PartnerInfoRepository) transformPgModelToDomainModel(PartnerInfoModel *models.PartnerInfo) (*domain.PartnerInfo, error) {
	m := &domain.PartnerInfo{}
	err := GobModelTransform(m, PartnerInfoModel)
	return m, err
}

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