pg_partner_info_repository.go 4.4 KB
package repository

import (
	"github.com/go-pg/pg/v10"
	"github.com/go-pg/pg/v10/orm"
	"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 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.Model(m).Returning("*").Insert(m); err != nil {
			return nil, err
		}
		return dm, nil
	}
	if _, err = tx.Model(m).Where("id=?", m.Id).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")
	query.SetWhere("partner_info.account = ?", "account")
	query.SetWhere("partner_info.status = ?", "status")
	query.SetWhere("partner_info.company_id = ?", "companyId")
	query.SetWhere("partner_info.company_id <> ?", "isNot")
	//if inParterIds,ok :=queryOptions["inParterIds"];ok{
	//	query.Relation("PartnerInfo", func(q *orm.Query) (*orm.Query, error) {
	//		return q.Where("id in (?)",pg.In(inParterIds.([]int64))),nil
	//	})
	//}
	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").
		SetWhere("partner_info.company_id <> ?", "isNot").
		SetLimit().
		SetOrder("partner_info.create_at", "sortByCreateTime").
		SetOrder("partner_info.update_at", "sortByUpdateTime")

	if inPartnerIds, ok := queryOptions["inPartnerIds"]; ok {
		query.Where("id in (?)", pg.In(inPartnerIds.([]int64)))
	}
	// 合伙人类型
	if inPartnerCategory, ok := queryOptions["inPartnerCategory"]; ok {
		query.Query = query.WhereGroup(func(q *orm.Query) (*orm.Query, error) {
			if arrayInPartnerCategory, ok := inPartnerCategory.([]int64); ok {
				for i := range arrayInPartnerCategory {
					q.WhereOr(`partner_info.partner_category_infos @>'[{"id":?}]')`, arrayInPartnerCategory[i])
				}
			}
			return q, nil
		})
	}
	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
}