statistics_person.go 6.5 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/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_cooperation"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
	"time"
)

// 个人端统计  【0%】
type PersonStatisticsService struct {
}

// IndexStatistics  个人端 - 首页统计 (入口页面统计数据)
func (srv PersonStatisticsService) IndexStatistics(cmd *command.IndexStatisticsCommand) (interface{}, error) {
	gateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		cmd.Operator)

	// 1.项目概览统计
	contractStatisticsResult, err := gateway.CooperationStatistics(allied_creation_cooperation.PersonCooperationContractStatistics, map[string]interface{}{
		"userBaseId": cmd.Operator.UserBaseId,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	type contractStatistics struct {
		ContractSum        int `json:"contractSum"`
		ContractStoppedSum int `json:"contractStoppedSum"`
	}
	var cs = &contractStatistics{}
	if err := json.UnmarshalFromString(json.MarshalToString(contractStatisticsResult), cs); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}

	gatewayUser := allied_creation_user.NewHttplibAlliedCreationUser(
		cmd.Operator)
	users, err := gatewayUser.UserSearch(allied_creation_user.ReqUserSearch{
		Limit:  1,
		Offset: 0,
		//UserType:   domain.UserTypeCooperation,
		UserBaseId: cmd.Operator.UserBaseId,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	var overview = map[string]interface{}{
		"contractSum":        cs.ContractSum,        //总合约数
		"contractStoppedSum": cs.ContractStoppedSum, //停止的合约数
		"companySum":         users.Count,           //共创企业数
	}
	year, month, _ := time.Now().Date()
	beginTime := time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
	endTime := beginTime.AddDate(0, 1, 0)
	// 2.本月分红统计 - 个人
	dividendStatisticsResult, err := gateway.CooperationStatistics(allied_creation_cooperation.CreditAccountStatistics, map[string]interface{}{
		"userBaseId": cmd.Operator.UserBaseId,
		"beginTime":  beginTime,
		"endTime":    endTime,
	})
	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())
	}
	dividendStatistics := map[string]interface{}{
		"dividendAmount": annualDividend.Total,  // 分红金额
		"paidAmount":     annualDividend.Paid,   // 已支付
		"unPaidAmount":   annualDividend.Unpaid, // 未支付
	}

	return map[string]interface{}{
		"overview":           overview,
		"dividendStatistics": dividendStatistics,
	}, nil
}

// CompanyStatistics  共创用户-共创企业统计
func (srv PersonStatisticsService) CompanyStatistics(cmd *command.CooperationPersonStatisticsCommand) (interface{}, error) {

	gatewayUser := allied_creation_user.NewHttplibAlliedCreationUser(
		cmd.Operator)
	users, err := gatewayUser.UserSearch(allied_creation_user.ReqUserSearch{
		Limit:      100,
		Offset:     0,
		UserBaseId: cmd.Operator.UserBaseId,
		//UserType:   domain.UserTypeCooperation,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	var companyList []int
	for i := range users.Users {
		user := users.Users[i]
		companyList = append(companyList, user.Org.OrgId)
	}

	gateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		cmd.Operator)
	result, err := gateway.CooperationStatistics(allied_creation_cooperation.CooperationCompanyStatistics, map[string]interface{}{
		"userBaseId":  cmd.Operator.UserBaseId,
		"companyList": companyList,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}

	type cooperationCompanyStatisticsResponse struct {
		// 当天统计的企业id
		OrgId int64 `json:"orgId"`
		// 共创项目数
		CooperationProjectCount int64 `json:"cooperationProjectCount"`
		// 共创合约数
		CooperationContractCount int64 `json:"cooperationContractCount"`
		// 分红占比
		DividendsRatio float64 `json:"dividendsRatio"`
		// 分红支出
		DividendsIncome float64 `json:"dividendsIncome"`
		// 企业信息
		Company domain.CompanyData `json:"company"`
	}
	var cooperationCompanyStatisticsResponses []cooperationCompanyStatisticsResponse
	if err := json.UnmarshalFromString(json.MarshalToString(result), &cooperationCompanyStatisticsResponses); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if len(cooperationCompanyStatisticsResponses) != len(users.Users) {
		return nil, application.ThrowError(application.BUSINESS_ERROR, "数据不匹配")
	}

	var values = make([]interface{}, 0)
	for i := range users.Users {
		user := users.Users[i]
		cooperationCompanyStatisticsResponses[i].Company = domain.CompanyData{
			CompanyID:   user.Org.OrgId,
			CompanyName: user.Org.OrgName,
			CompanyLogo: user.Company.Log,
		}
		values = append(values, cooperationCompanyStatisticsResponses[i])
	}
	return map[string]interface{}{
		"list": values,
	}, nil
}

// CooperationProjectRecommend  TODO:其他公司按公开的项目查 猜你喜欢(共创项目)
func (srv PersonStatisticsService) CooperationProjectRecommend(projectQuery *command.ListCooperationProjectQuery) (int64, interface{}, error) {
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		projectQuery.Operator)
	result, err := creationCooperationGateway.CooperationProjectsSearch(allied_creation_cooperation.ReqCooperationProjectSearch{
		PageNumber: projectQuery.PageNumber,
		PageSize:   projectQuery.PageSize,
	})
	if err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return int64(result.Total), result.List, nil
}