cooperation_statistics.go 1.9 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/cooperationStatistics/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/domain_service"
)

// ContractDividendStatistics 合约分红统计
type CooperationStatisticsService struct {
}

// CooperationContractStatistics 共创合约统计
func (svr *CooperationStatisticsService) CooperationContractStatistics(contractStatisticsQuery *query.CooperationStatisticsQuery) (interface{}, error) {
	if err := contractStatisticsQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	var err error
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()

	statisticsService, err := factory.CreateCooperationStatisticsService(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	var res interface{}
	switch contractStatisticsQuery.Action {
	case domain_service.SearchContractDividends:
		res, err = statisticsService.SearchContractDividends(contractStatisticsQuery.QueryOptions)
	case domain_service.GetContractDividends:
		res, err = statisticsService.GetContractDividends(contractStatisticsQuery.QueryOptions)
	}
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return res, nil
}