credit_accounts_person.go 4.7 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"github.com/linmadan/egglib-go/utils/json"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/mobile/cooperation/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/mobile/cooperation/dto"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_cooperation"
	"time"
)

// PersonCreditAccountService 企业端账期结算
type PersonCreditAccountService struct {
}

// CreditAccountPaySearch  企业分红结算支付记录
func (srv PersonCreditAccountService) CreditAccountPaySearch(cmd *command.CreditAccountPaySearchCommand) (interface{}, error) {
	gateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		cmd.Operator)
	var beginTime, endTime time.Time
	if cmd.BeginTime > 0 {
		beginTime = time.Unix(cmd.BeginTime/1000, 0)
	}
	if cmd.EndTime > 0 {
		endTime = time.Unix(cmd.EndTime/1000, 0)
	}
	resultMenu, err := gateway.CreditAccountsSearch(allied_creation_cooperation.ReqCreditAccountsSearch{
		PageNumber:       cmd.PageNumber + 1, //手机序号从0开始的
		PageSize:         cmd.PageSize,
		PaymentStatus:    2,
		UserBaseId:       cmd.Operator.UserBaseId,
		PaymentBeginTime: beginTime,
		PaymentEndTime:   endTime,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	queryOptions := map[string]interface{}{
		"userBaseId": cmd.Operator.UserBaseId,
	}
	if cmd.BeginTime > 0 {
		queryOptions["paymentBeginTime"] = beginTime
	}
	if cmd.EndTime > 0 {
		queryOptions["paymentEndTime"] = endTime
	}
	// 2.分红统计
	dividendStatisticsResult, err := gateway.CooperationStatistics(allied_creation_cooperation.CreditAccountStatistics, queryOptions)
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	type AnnualDividend struct {
		Total      float64 `json:"total"`
		Accounting float64 `json:"accounting"`
		Accounted  float64 `json:"accounted"`
		Paid       float64 `json:"paid"`
		Unpaid     float64 `json:"unpaid"`
	}
	var annualDividend = &AnnualDividend{}
	if err := json.UnmarshalFromString(json.MarshalToString(dividendStatisticsResult), annualDividend); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}

	var items = make([]*dto.CreditAccountItem, 0)
	for i := 0; i < len(resultMenu.Grid.List); i++ {
		items = append(items, dto.ToCreditAccountItem(&resultMenu.Grid.List[i]))
	}
	return map[string]interface{}{
		"grid": map[string]interface{}{
			"list": items,
			"sum":  annualDividend.Paid,
		},
	}, nil
}

// PaymentHistoryStatistics  支付历史统计
func (srv PersonCreditAccountService) PaymentHistoryStatistics(cmd *command.CreditAccountPaySearchCommand) (interface{}, error) {
	gateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		cmd.Operator)
	queryOptions := map[string]interface{}{
		//"orgId":                    cmd.Operator.OrgId,
		"userBaseId":               cmd.Operator.UserBaseId,
		"offset":                   cmd.PageNumber * cmd.PageSize,
		"limit":                    cmd.PageSize,
		"sortByActuallyPaidAmount": 2,
	}
	if cmd.BeginTime > 0 {
		queryOptions["paymentBeginTime"] = time.Unix(cmd.BeginTime/1000, 0)
	}
	if cmd.EndTime > 0 {
		queryOptions["paymentEndTime"] = time.Unix(cmd.EndTime/1000, 0)
	}
	cooperationUsersStatistics, err := gateway.CooperationStatistics(allied_creation_cooperation.PersonCompanyPaymentHistoryStatistics, queryOptions)
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}

	creditAccountStatistics, err := gateway.CreditAccountStatistics(allied_creation_cooperation.CreditAccountStatistics, queryOptions)
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}

	return map[string]interface{}{
		"rankItems":          cooperationUsersStatistics,
		"totalPaymentAmount": creditAccountStatistics.Paid,
	}, nil
}

// PaymentHistoryHistogramStatistics  支付历史统计-直方图
func (srv PersonCreditAccountService) PaymentHistoryHistogramStatistics(cmd *command.CreditAccountPaySearchCommand) (interface{}, error) {
	gateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		cmd.Operator)
	paymentHistoryHistogramStatistics, err := gateway.CooperationStatistics(allied_creation_cooperation.PaymentHistoryHistogramStatistics, map[string]interface{}{
		"userBaseId": cmd.Operator.UserBaseId,
		"beginTime":  time.Unix(cmd.BeginTime/1000, 0),
		"endTime":    time.Unix(cmd.EndTime/1000, 0),
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}

	return paymentHistoryHistogramStatistics, nil
}