credit_accounts_company.go 5.9 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"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"
)

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

// CreditAccountSearch  企业的账期结算列表
func (srv CompanyCreditAccountService) CreditAccountSearch(cmd *command.CreditAccountSearchCommand) (int64, interface{}, error) {
	gateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		cmd.Operator)
	resultMenu, err := gateway.CreditAccountsSearch(allied_creation_cooperation.ReqCreditAccountsSearch{
		PageNumber:    cmd.PageNumber,
		PageSize:      cmd.PageSize,
		PaymentStatus: cmd.PaymentStatus,
		OrgId:         cmd.Operator.OrgId,
	})
	if err != nil {
		return 0, nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	var items []*dto.CreditAccountItem
	for i := 0; i < len(resultMenu.Grid.List); i++ {
		items = append(items, dto.ToCreditAccountItem(&resultMenu.Grid.List[i]))
	}
	return int64(len(items)), items, nil
}

// CreditAccountGet  企业的账期结算明细
func (srv CompanyCreditAccountService) CreditAccountGet(cmd *command.CreditAccountGetCommand) (interface{}, error) {
	gateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		cmd.Operator)
	credit, err := gateway.CreditAccountGet(allied_creation_cooperation.ReqCreditAccountGet{
		CreditAccountId: cmd.CreditAccountId,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	ret := map[string]interface{}{
		"creditAccount":     dto.ToCreditAccountItem(&credit.CreditAccount),
		"dividendsEstimate": credit.CreditAccount.AccountDetail,
	}
	return ret, nil
}

// CreditAccountDelete  企业的账期结算删除
func (srv CompanyCreditAccountService) CreditAccountDelete(cmd *command.CreditAccountGetCommand) (interface{}, error) {
	gateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		cmd.Operator)
	resultMenu, err := gateway.CreditAccountRemove(allied_creation_cooperation.ReqCreditAccountRemove{
		CreditAccountId: cmd.CreditAccountId,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return resultMenu, nil
}

// CreditAccountPay  企业的账期结算支付
func (srv CompanyCreditAccountService) CreditAccountPay(cmd *command.CreditAccountPayCommand) (interface{}, error) {
	gateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		cmd.Operator)
	resultMenu, err := gateway.CreditAccountsPay(allied_creation_cooperation.ReqCreditAccountsPay{
		CreditAccountId:    cmd.CreditAccountId,
		ActuallyPaidAmount: cmd.ActuallyPaidAmount,
		Remarks:            cmd.Remarks,
		Attachment:         cmd.Attachment,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return resultMenu, nil
}

// CreditAccountPaySearch  企业分红结算支付记录
func (srv CompanyCreditAccountService) CreditAccountPaySearch(cmd *command.CreditAccountPaySearchCommand) (interface{}, error) {
	gateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		cmd.Operator)
	resultMenu, err := gateway.CreditAccountsSearch(allied_creation_cooperation.ReqCreditAccountsSearch{
		PageNumber:    cmd.PageNumber,
		PageSize:      cmd.PageSize,
		PaymentStatus: 2,
		//TODO:时间段过滤
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	var items []*dto.CreditAccountItem
	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,
			//TODO:sum 时间段支付金额
			"sum": 6000,
		},
	}, nil
}

// PaymentHistoryStatistics  支付历史统计
func (srv CompanyCreditAccountService) PaymentHistoryStatistics(cmd *command.CreditAccountPaySearchCommand) (interface{}, error) {
	gateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		cmd.Operator)
	queryOptions := map[string]interface{}{
		"orgId":                    cmd.Operator.OrgId,
		"offset":                   cmd.PageNumber * cmd.PageSize,
		"limit":                    cmd.PageSize,
		"sortByActuallyPaidAmount": 2,
	}
	if cmd.BeginTime > 0 {
		queryOptions["beginTime"] = time.Unix(cmd.BeginTime/1000, 0)
	}
	if cmd.EndTime > 0 {
		queryOptions["endTime"] = time.Unix(cmd.EndTime/1000, 0)
	}
	cooperationUsersStatistics, err := gateway.CooperationStatistics(allied_creation_cooperation.CompanyPaymentHistoryStatistics, 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 CompanyCreditAccountService) PaymentHistoryHistogramStatistics(cmd *command.CreditAccountPaySearchCommand) (interface{}, error) {
	gateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		cmd.Operator)
	paymentHistoryHistogramStatistics, err := gateway.CooperationStatistics(allied_creation_cooperation.PaymentHistoryHistogramStatistics, map[string]interface{}{
		"orgId": cmd.Operator.OrgId,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}

	return paymentHistoryHistogramStatistics, nil
}