statistics_company.go 6.3 KB
package service

import (
	"fmt"
	"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/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"
	"math/rand"
	"time"
)

// 企业端统计  【25%】
type CompanyStatisticsService struct {
}

// IndexStatistics  首页统计 (入口页面统计数据)
func (srv CompanyStatisticsService) IndexStatistics(cmd *command.IndexStatisticsCommand) (interface{}, error) {
	value := dto.IndexStatistics{}
	value.ProjectOverviewStatistics.ContractSum = 20
	value.ProjectOverviewStatistics.CooperationUserCount = 5
	value.ProjectOverviewStatistics.ProjectSum = 5

	gateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		cmd.Operator)
	companyDividendsStatistics, err := gateway.CooperationStatistics(allied_creation_cooperation.CompanyDividendsStatistics, map[string]interface{}{
		"companyId": cmd.Operator.CompanyId,
		"orgId":     cmd.Operator.OrgId,
		"action":    1, //当前月
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	cooperationModeStatistics, err := gateway.CooperationStatistics(allied_creation_cooperation.CooperationModeStatistics, map[string]interface{}{
		"companyId": cmd.Operator.CompanyId,
		"orgId":     cmd.Operator.OrgId,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	cooperationGoodsStatistics, err := gateway.CooperationStatistics(allied_creation_cooperation.CooperationGoodsStatistics, map[string]interface{}{
		"companyId": cmd.Operator.CompanyId,
		"orgId":     cmd.Operator.OrgId,
		"rankType":  1, //月榜
		"top":       5,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}

	// 模式列表
	models, err := gateway.CooperationModeList(allied_creation_cooperation.ReqCooperationModeList{
		PageSize:   20,
		PageNumber: 1,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	var cooperationModes []interface{}
	for i := range models.List {
		item := models.List[i]
		cooperationModes = append(cooperationModes, map[string]interface{}{
			"cooperationModeId":     item.CooperationModeId,
			"cooperationModeName":   item.CooperationModeName,
			"cooperationModeNumber": item.CooperationModeNumber,
		})
	}
	// 项目概览统计
	contracts, err := gateway.CooperationContractSearch(allied_creation_cooperation.ReqCooperationContractSearch{
		PageNumber: 1,
		PageSize:   1,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	projects, err := gateway.CooperationProjectsSearch(allied_creation_cooperation.ReqCooperationProjectSearch{
		PageNumber: 1,
		PageSize:   1,
	})
	if 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,
		CompanyId:      cmd.Operator.CompanyId,
		OrganizationId: cmd.Operator.OrgId,
		UserType:       domain.UserTypeCooperation,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	var projectOverviewStatistics = map[string]interface{}{
		"contractSum":          contracts.Grid.Total,
		"cooperationUserCount": users.Count,
		"projectSum":           projects.Total,
	}

	return map[string]interface{}{
		"projectOverviewStatistics":       projectOverviewStatistics,
		"currentMonthDividendsStatistics": companyDividendsStatistics,
		"cooperationModeStatistics":       cooperationModeStatistics,
		"cooperationGoodsStatistics":      cooperationGoodsStatistics,
		"cooperationModes":                cooperationModes,
	}, nil
}

// CooperationPersonStatistics  TODO:共创人员统计(共创人员明细)
func (srv CompanyStatisticsService) CooperationPersonStatistics(userMenusCommand *command.CooperationPersonStatisticsCommand) (interface{}, error) {
	return map[string]interface{}{}, nil
}

// GoodsStatistics  TODO:产品统计排行榜 年月榜
func (srv CompanyStatisticsService) GoodsStatistics(userMenusCommand *command.GoodsStatisticsCommand) (int64, interface{}, error) {
	type rankItem struct {
		GoodAmount float64 `json:"goodAmount"`
		GoodName   string  `json:"goodName"`
		GoodRatio  float64 `json:"goodRatio"`
		Rank       int     `json:"rank"`
	}
	var items []rankItem
	for i := 0; i < 5; i++ {
		item := rankItem{
			GoodAmount: 2000,
			GoodName:   fmt.Sprintf("商品%v", rand.Intn(100)),
			GoodRatio:  20,
			Rank:       i + 1,
		}
		items = append(items, item)
	}
	return 5, items, nil
}

// CooperationDividendsStatistics TODO:公司共创人员列表(分红支出统计)
func (srv CompanyStatisticsService) CooperationDividendsStatistics(userMenusCommand *command.CooperationDividendsStatisticsCommand) (int64, interface{}, error) {
	type cooperationDividendItem struct {
		CooperationTime      int64   `json:"cooperationTime"`
		DividendsOrderAmount float64 `json:"dividendsOrderAmount"`
		ActuallyPaidAmount   float64 `json:"actuallyPaidAmount"`
		UnPaidAmount         float64 `json:"unPaidAmount"`
		Participator         struct {
			UserType int `json:"userType"`
			UserInfo struct {
				// 用户姓名
				UserName string `json:"userName,omitempty"`
				// 手机号码
				UserPhone string `json:"userPhone,omitempty"`
				// 头像
				//Avatar string `json:"avatar,omitempty"`
				// 邮箱
				//Email string `json:"email,omitempty"`
			} `json:"userInfo"`
		} `json:"participator"` // 参与人
	}

	var results []cooperationDividendItem
	for i := 0; i < 5; i++ {
		item := cooperationDividendItem{
			CooperationTime:      time.Now().Unix() * 1000,
			DividendsOrderAmount: 6000,
			ActuallyPaidAmount:   5000,
			UnPaidAmount:         1000,
		}
		item.Participator.UserInfo.UserName = fmt.Sprintf("用户%v", rand.Intn(100))
		results = append(results, item)
	}
	return 5, results, nil
}