service.go 5.7 KB
package service

import (
	"fmt"

	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/businessBonus/command"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/businessBonus/query"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/dao"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)

type BusinessBonusService struct {
}

func NewBusinessBonusService(option map[string]interface{}) *BusinessBonusService {
	newService := new(BusinessBonusService)
	return newService
}

func (srv BusinessBonusService) ListBusinessBonus(queryOption query.ListBusinessBonusQuery) (int, interface{}, error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
		err                   error
	)
	if err = transactionContext.StartTransaction(); err != nil {
		return 0, nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	var (
		bonusDaoao *dao.BusinessBonusDao
	)
	if bonusDaoao, err = factory.CreateBusinessBonusDao(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return 0, nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	result, err := bonusDaoao.SearchBusinessBonus(queryOption.PartnerId, queryOption.PartnerNameMatch,
		queryOption.CompanyId, queryOption.Limit, queryOption.Offset)
	if err != nil {
		return 0, nil, err
	}
	err = transactionContext.CommitTransaction()

	returnData := []map[string]interface{}{}
	for _, v := range result {
		m := map[string]interface{}{
			"id":                   v.Id,
			"uncollectedDividends": v.BonusNot,
			"receiveDividends":     v.BonusHas,
			"dividendsReceivable":  v.Bonus,
			"stateOfPayment":       v.BonusStatus,
			"stateOfPaymentName":   domain.DescribeBusinessBonusStatus(v.BonusStatus),
			"partner":              v.PartnerName,
			"updateTime":           v.UpdateAt.Format("2006-01-02 15:04:05"),
		}
		returnData = append(returnData, m)
	}
	return 0, returnData, nil
}

func (srv BusinessBonusService) UpdateBusinessBonus(cmd command.UpdateBusinessBonusCommand) error {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
		err                   error
	)
	if err = transactionContext.StartTransaction(); err != nil {
		return err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var (
		bonusRespository domain.BusinessBonusRepository
		bonusData        *domain.BusinessBonus
	)
	if bonusRespository, err = factory.CreateBusinessBonusRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	bonusData, err = bonusRespository.FindOne(domain.BusinessBonusFindOneQuery{
		Id: cmd.Id, CompanyId: cmd.CompanyId,
	})
	if err != nil {
		e := fmt.Sprintf("获取业务分红(id=%d;company_id=%d)数据失败:%s", cmd.Id, cmd.CompanyId, err)
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
	}
	bonusData.Bonus = cmd.Bonus
	switch cmd.BonusStatus {
	case domain.BUSINESS_BONUS_HAS_PAY:
		bonusData.BusinessBonusPayStatus.PayPartnerBonus(bonusData)
	case domain.BUSINESS_BONUS_WAIT_PAY:
		bonusData.BusinessBonusPayStatus.WartPayPartnerBonus(bonusData)
	default:
		return lib.ThrowError(lib.BUSINESS_ERROR, "支付状态错误")
	}
	err = bonusRespository.Edit(bonusData)
	if err != nil {
		e := fmt.Sprintf("更新业务分行(id=%d)数据失败:%s", bonusData.Id, err)
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
	}
	err = transactionContext.CommitTransaction()
	return nil
}

func (srv BusinessBonusService) GetBusinessBonus(queryOption query.GetBusinessBonusQuery) (map[string]interface{}, error) {

	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
		err                   error
	)
	if err = transactionContext.StartTransaction(); err != nil {
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var (
		bonusRespository      domain.BusinessBonusRepository
		partnerInfoRepository domain.PartnerInfoRepository
		bonusData             *domain.BusinessBonus
		partnerData           *domain.PartnerInfo
	)
	if bonusRespository, err = factory.CreateBusinessBonusRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	bonusData, err = bonusRespository.FindOne(domain.BusinessBonusFindOneQuery{
		Id: queryOption.Id, CompanyId: queryOption.CompanyId,
	})
	if err != nil {
		e := fmt.Sprintf("获取业务分红(id=%d;company_id=%d)数据失败:%s", queryOption.Id, queryOption.CompanyId, err)
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
	}
	if partnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	partnerData, err = partnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: bonusData.PartnerInfoId})
	if err != nil {
		e := fmt.Sprintf("获取合伙人(id=%d)数据失败:%s", bonusData.PartnerInfoId, err)
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
	}
	err = transactionContext.CommitTransaction()
	returnData := map[string]interface{}{
		"partner":              partnerData.Partner.PartnerName,
		"dividendsReceivable":  bonusData.Bonus,
		"receiveDividends":     bonusData.BonusHas,
		"uncollectedDividends": bonusData.BonusNot,
		"updateTime":           bonusData.UpdateAt.Format("2006-01-02 15:04:05"),
		"stateOfPayment":       bonusData.BonusStatus,
		"stateOfPaymentName":   domain.DescribeBusinessBonusStatus(bonusData.BonusStatus),
		"id":                   bonusData.Id,
	}
	return returnData, nil
}