pg_business_bonus_repository.go 4.3 KB
package repository

import (
	"errors"
	"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 BusinessBonusRepository struct {
	transactionContext *transaction.TransactionContext
}

func (repository *BusinessBonusRepository) transformPgModelToDomainModel(m *models.BusinessBonus) (domain.BusinessBonus, error) {
	dm := domain.BusinessBonus{
		Id:            m.Id,
		CompanyId:     m.CompanyId,
		PartnerInfoId: m.PartnerInfoId,
		Bonus:         m.Bonus,
		BonusNot:      m.BonusNot,
		BonusHas:      m.BonusHas,
		BonusExpense:  m.BonusExpense,
		IsDisable:     m.IsDisable,
		BonusStatus:   m.BonusStatus,
		CreateAt:      m.CreateAt,
		UpdateAt:      m.UpdateAt,
	}
	switch m.BonusStatus {
	case domain.BUSINESS_BONUS_WAIT_PAY:
		dm.BusinessBonusPayStatus = domain.BusinessBonusWaitPay{}
	case domain.BUSINESS_BONUS_HAS_PAY:
		dm.BusinessBonusPayStatus = domain.BusinessBonusHasPay{}
	default:
		dm.BusinessBonusPayStatus = domain.BusinessBonusNullPay{}
	}
	return dm, nil
}

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

func (repository *BusinessBonusRepository) Add(dm *domain.BusinessBonus) error {
	tx := repository.transactionContext.PgTx
	bonusModel := models.BusinessBonus{
		Id:            dm.Id,
		CompanyId:     dm.CompanyId,
		PartnerInfoId: dm.PartnerInfoId,
		Bonus:         dm.Bonus,
		BonusNot:      dm.Bonus,
		BonusHas:      dm.BonusHas,
		BonusExpense:  dm.BonusExpense,
		IsDisable:     dm.IsDisable,
		BonusStatus:   dm.BonusStatus,
	}
	_, err := tx.Model(&bonusModel).
		Insert()
	dm.CreateAt = bonusModel.CreateAt
	dm.UpdateAt = bonusModel.UpdateAt
	dm.Id = bonusModel.Id
	return err
}

func (repository *BusinessBonusRepository) Edit(dm *domain.BusinessBonus) error {
	tx := repository.transactionContext.PgTx
	bonusModel := models.BusinessBonus{
		Id:            dm.Id,
		CompanyId:     dm.CompanyId,
		PartnerInfoId: dm.PartnerInfoId,
		Bonus:         dm.Bonus,
		BonusNot:      dm.BonusNot,
		BonusExpense:  dm.BonusExpense,
		BonusHas:      dm.BonusHas,
		IsDisable:     dm.IsDisable,
		BonusStatus:   dm.BonusStatus,
		CreateAt:      dm.CreateAt,
		UpdateAt:      dm.UpdateAt,
	}
	_, err := tx.Model(&bonusModel).WherePK().Update()
	return err

}

func (repository *BusinessBonusRepository) FindOne(queryOptions domain.BusinessBonusFindOneQuery) (*domain.BusinessBonus, error) {
	tx := repository.transactionContext.PgTx
	BusinessBonusModel := new(models.BusinessBonus)
	var hasCondition bool
	query := tx.Model(BusinessBonusModel).Where("delete_at ISNULL")
	if queryOptions.Id > 0 {
		hasCondition = true
		query = query.Where("id=?", queryOptions.Id)
	}
	if queryOptions.PartnerId > 0 {
		hasCondition = true
		query = query.Where("partner_info_id=?", queryOptions.PartnerId)
	}
	if queryOptions.CompanyId > 0 {
		hasCondition = true
		query = query.Where("company_id=?", queryOptions.CompanyId)
	}
	if !hasCondition {
		return nil, errors.New("没有查询条件")
	}
	if err := query.First(); err != nil {
		return nil, err
	}
	data, err := repository.transformPgModelToDomainModel(BusinessBonusModel)
	return &data, err
}

func (repository *BusinessBonusRepository) Find(queryOptions domain.BusinessBonusFindQuery) (int, []domain.BusinessBonus, error) {
	tx := repository.transactionContext.PgTx
	var (
		err                 error
		cnt                 int
		businessBonuss      []domain.BusinessBonus
		businessBonusModels []models.BusinessBonus
	)
	query := tx.Model(&businessBonusModels)
	if queryOptions.Offset > -1 {
		query = query.Offset(queryOptions.Offset)
	}
	if queryOptions.Limit > 0 {
		query = query.Limit(queryOptions.Limit)
	} else {
		query = query.Limit(1000)
	}
	query = query.Order("business_bonus.id DESC")
	if cnt, err = query.SelectAndCount(); err != nil {
		return 0, businessBonuss, err
	}

	for i := range businessBonusModels {
		businessBonus, err := repository.transformPgModelToDomainModel(&businessBonusModels[i])
		if err != nil {
			return 0, businessBonuss, err
		}
		businessBonuss = append(businessBonuss, businessBonus)
	}
	return cnt, businessBonuss, nil
}