pg_business_bonus_service.go 2.6 KB
package domainService

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

type BusinessBonusService struct {
	transactionContext *transaction.TransactionContext
}

func NewBusinessBonusService(tcx *transaction.TransactionContext) *BusinessBonusService {
	return &BusinessBonusService{
		transactionContext: tcx,
	}
}

func (srv BusinessBonusService) EnableOrDisable(parntnerId int64) error {
	var (
		bonusDao, _           = dao.NewBusinessBonusDao(srv.transactionContext)
		bonusReponsitory, _   = repository.NewBusinessBonusRepository(srv.transactionContext)
		partnerReponsitory, _ = repository.NewPartnerInfoRepository(srv.transactionContext)

		err                        error
		partnerHasInBonus          bool
		partnerData                *domain.PartnerInfo
		partnerHasBusinessCategory bool
	)
	partnerHasInBonus, err = bonusDao.ExistBusinessBonus(parntnerId)
	if err != nil {
		return err
	}
	partnerData, err = partnerReponsitory.FindOne(domain.PartnerFindOneQuery{UserId: parntnerId})
	if err != nil {
		return err
	}
	for _, v := range partnerData.PartnerCategoryInfos {
		if v.Id == 2 {
			partnerHasBusinessCategory = true
			break
		}
	}
	if partnerHasInBonus && partnerHasBusinessCategory {
		//已存在业务分红数据况 && 是业务合伙人类型
		var bonusData *domain.BusinessBonus
		bonusData, err = bonusReponsitory.FindOne(domain.BusinessBonusFindOneQuery{
			PartnerId: parntnerId,
		})
		if bonusData.IsDisable == domain.BUSINESS_BONUS_ENABLE {
			return nil
		}
		bonusData.IsDisable = domain.BUSINESS_BONUS_ENABLE
		err = bonusReponsitory.Edit(bonusData)
		return err
	}
	if partnerHasInBonus && !partnerHasBusinessCategory {
		//已存在业务分红数据况 && 不是业务合伙人类型
		var bonusData *domain.BusinessBonus
		bonusData, err = bonusReponsitory.FindOne(domain.BusinessBonusFindOneQuery{
			PartnerId: parntnerId,
		})
		if bonusData.IsDisable == domain.BUSINESS_BONUS_DISABLE {
			return nil
		}
		bonusData.IsDisable = domain.BUSINESS_BONUS_DISABLE
		err = bonusReponsitory.Edit(bonusData)
		return err
	}

	if !partnerHasInBonus && partnerHasBusinessCategory {
		//不存在业务分红数据况 && 是业务合伙人类型
		newBonus := &domain.BusinessBonus{
			PartnerInfoId: partnerData.Partner.Id,
			IsDisable:     domain.BUSINESS_BONUS_ENABLE,
			CompanyId:     partnerData.CompanyId,
			Bonus:         0,
		}
		err = bonusReponsitory.Add(newBonus)
		return err
	}

	return nil
}