pg_partner_info_repository.go 4.4 KB
package repository

import (
	"fmt"

	"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
}

var (
	_ domain.PartnerInfoRepository = (*PartnerInfoRepository)(nil)
)

func (repository *PartnerInfoRepository) transformPgModelToDomainModel(partnerInfoModel *models.PartnerInfo) (domain.PartnerInfo, error) {
	m := domain.PartnerInfo{
		Id:              partnerInfoModel.Id,
		PartnerName:     partnerInfoModel.PartnerName,
		Account:         partnerInfoModel.Account,
		Password:        partnerInfoModel.Password,
		Status:          partnerInfoModel.Status,
		PartnerCategory: partnerInfoModel.PartnerCategory,
		CreateAt:        partnerInfoModel.CreateAt,
		Salesman:        partnerInfoModel.Salesman,
		RegionInfo:      partnerInfoModel.RegionInfo,
		CooperateTime:   partnerInfoModel.CooperateTime,
	}
	return m, nil
}

func NewPartnerInfoRepository(transactionContext *transaction.TransactionContext) (*PartnerInfoRepository, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	}
	return &PartnerInfoRepository{transactionContext: transactionContext}, nil
}

func (repository *PartnerInfoRepository) Save(dm *domain.PartnerInfo) (*domain.PartnerInfo, error) {
	var (
		err error
		tx  = repository.transactionContext.PgTx
	)
	m := &models.PartnerInfo{
		Id:              dm.Id,
		PartnerName:     dm.PartnerName,
		Account:         dm.Account,
		Password:        dm.Password,
		Status:          dm.Status,
		PartnerCategory: dm.PartnerCategory,
		Salesman:        dm.Salesman,
		RegionInfo:      dm.RegionInfo,
		CooperateTime:   dm.CooperateTime,
	}
	if m.Id == 0 {
		err = tx.Insert(m)
		dm.Id = m.Id
		if err != nil {
			return nil, err
		}
	} else {
		err = tx.Update(m)
		if err != nil {
			return nil, err
		}
	}
	return dm, nil
}

func (repository *PartnerInfoRepository) FindOne(queryOptions domain.PartnerFindOneQuery) (*domain.PartnerInfo, error) {
	tx := repository.transactionContext.PgDd
	PartnerInfoModel := new(models.PartnerInfo)
	query := tx.Model(PartnerInfoModel)
	if len(queryOptions.AccountEqual) > 0 {
		query = query.Where("account=?", queryOptions.AccountEqual)
	}
	if queryOptions.UserId > 0 {
		query = query.Where("id=?", queryOptions.UserId)
	}
	if err := query.First(); err != nil {
		return nil, err
	}
	result, err := repository.transformPgModelToDomainModel(PartnerInfoModel)
	return &result, err
}

func (repository *PartnerInfoRepository) Find(queryOption domain.PartnerFindQuery) ([]domain.PartnerInfo, error) {
	db := repository.transactionContext.PgDd
	partnerModels := []models.PartnerInfo{}
	query := db.Model(&partnerModels)
	if len(queryOption.PartnerName) > 0 {
		query = query.Where("partner_name like ?", "%"+queryOption.PartnerName+"%")
	}
	if queryOption.RegionInfo != nil {
		query = query.Where("region_info::jsonb->>'regionName' like ?", "%"+queryOption.RegionInfo.RegionName+"%")
	}
	if len(queryOption.PartnerCategory) > 0 {
		query = query.WhereIn("partner_category in(?)", queryOption.PartnerCategory)
	}
	if queryOption.Offset > -1 {
		query = query.Offset(queryOption.Offset)
	}
	if queryOption.Limit > 0 {
		query = query.Limit(queryOption.Limit)
	} else {
		query = query.Limit(20)
	}
	var (
		err           error
		partnerReturn = make([]domain.PartnerInfo, 0)
	)
	err = query.Select()
	if err != nil {
		return partnerReturn, err
	}
	for i := range partnerModels {
		domainPartner, err := repository.transformPgModelToDomainModel(&partnerModels[i])
		if err != nil {
			return partnerReturn, err
		}
		partnerReturn = append(partnerReturn, domainPartner)
	}
	return partnerReturn, nil
}

func (repository PartnerInfoRepository) CountAll(queryOption domain.PartnerFindQuery) (int, error) {
	db := repository.transactionContext.PgDd
	partnerModels := models.PartnerInfo{}
	query := db.Model(&partnerModels)
	if len(queryOption.PartnerName) > 0 {
		query = query.Where("partner_name like ?", "%"+queryOption.PartnerName+"%")
	}
	if queryOption.RegionInfo != nil {
		query = query.Where("region_info::jsonb->>'regionName' like ?", "%"+queryOption.RegionInfo.RegionName+"%")
	}
	if len(queryOption.PartnerCategory) > 0 {
		query = query.WhereIn("partner_category in(?)", queryOption.PartnerCategory)
	}
	cnt, err := query.Count()
	return cnt, err
}