statistics_company.go 8.1 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"
)

// 企业端统计
type CompanyStatisticsService struct {
}

// IndexStatistics  首页统计 (入口页面统计数据)
func (srv CompanyStatisticsService) IndexStatistics(cmd *command.IndexStatisticsCommand) (interface{}, error) {
	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())
	}

	// 模式列表
	var modeStatistics []CooperationModeStatistics
	var modeNumbers []string
	err = json.UnmarshalFromString(json.MarshalToString(cooperationModeStatistics), &modeStatistics)
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	for i := range modeStatistics {
		modeNumbers = append(modeNumbers, modeStatistics[i].CooperationModeNumber)
	}

	var cooperationModes = make([]interface{}, 0)
	if len(modeNumbers) > 0 {
		models, err := gateway.CooperationModesSearch(allied_creation_cooperation.ReqCooperationModesSearch{
			PageSize:   20,
			PageNumber: 1,
			//CompanyId:  cmd.Operator.CompanyId,
			//OrgId:      cmd.Operator.OrgId,
			CooperationModeNumbers: modeNumbers,
		})
		if err != nil {
			return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
		}
		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,
		CompanyId:  cmd.Operator.CompanyId,
		OrgId:      cmd.Operator.OrgId,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	projects, err := gateway.CooperationProjectsSearch(allied_creation_cooperation.ReqCooperationProjectSearch{
		PageNumber: 1,
		PageSize:   1,
		//CompanyId:  cmd.Operator.CompanyId,
		OrgId: cmd.Operator.OrgId,
	})
	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
}

// GoodsStatistics  产品统计排行榜 年月榜
func (srv CompanyStatisticsService) GoodsStatistics(cmd *command.GoodsStatisticsCommand) (int64, interface{}, error) {
	gateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		cmd.Operator)
	cooperationGoodsStatistics, err := gateway.CooperationStatistics(allied_creation_cooperation.CooperationGoodsStatistics, map[string]interface{}{
		"companyId": cmd.Operator.CompanyId,
		"orgId":     cmd.Operator.OrgId,
		"rankType":  cmd.RankingType, //当前月
		//"offset":cmd.PageNumber*cmd.PageSize,
		"top": 100,
	})
	if err != nil {
		return 0, nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}

	return 0, cooperationGoodsStatistics, nil
}

// CooperationDividendsStatistics 公司共创人员列表
func (srv CompanyStatisticsService) CooperationDividendsStatistics(cmd *command.CooperationDividendsStatisticsCommand) (int64, interface{}, error) {
	gateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		cmd.Operator)
	cooperationUsersStatistics, err := gateway.CooperationStatistics(allied_creation_cooperation.CompanyCooperationUsersStatistics, map[string]interface{}{
		"orgId":                    cmd.Operator.OrgId,
		"offset":                   cmd.PageNumber * cmd.PageSize,
		"limit":                    cmd.PageSize,
		"sortByActuallyPaidAmount": cmd.SortByActuallyPaidAmount,
	})
	if err != nil {
		return 0, nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return 0, cooperationUsersStatistics, nil
}

// CooperationPersonStatistics  共创人明细员统计(共创人员明细)
func (srv CompanyStatisticsService) CooperationPersonStatistics(cmd *command.CooperationPersonStatisticsCommand) (interface{}, error) {
	gatewayUser := allied_creation_user.NewHttplibAlliedCreationUser(
		cmd.Operator)
	user, err := gatewayUser.UserGet(allied_creation_user.ReqGetUser{
		UserId: cmd.UserId,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}

	gateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		cmd.Operator)
	cooperationUserModeStatistics, err := gateway.CooperationStatistics(allied_creation_cooperation.CooperationUserModeStatistics, map[string]interface{}{
		"userId": cmd.UserId,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}

	var modeStatistics = struct {
		CooperationTypes interface{} `json:"cooperationTypes"`
		DividendsDetails interface{} `json:"dividendsDetails"`
		OrderDetails     interface{} `json:"orderDetails"`
	}{}

	if err := json.UnmarshalFromString(json.MarshalToString(cooperationUserModeStatistics), &modeStatistics); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}

	var response = map[string]interface{}{
		"userName":         user.UserInfo.UserName,
		"cooperationTime":  user.CreatedAt.Unix() * 1000,
		"cooperationTypes": modeStatistics.CooperationTypes,
		"dividendsDetails": modeStatistics.DividendsDetails,
		"orderDetails":     modeStatistics.OrderDetails,
	}
	return map[string]interface{}{
		"cooperationPersonStatistics": response,
	}, nil
}

type CooperationModeStatistics struct {
	// 共创人数
	CooperationPeople float64 `json:"cooperationPeople"`
	// 分红预算
	DividendsEstimate string `json:"dividendsEstimate"`
	// 订单金额
	OrderAmount float64 `json:"orderAmount"`
	// 共创模式编号
	CooperationModeNumber string `json:"cooperationModeNumber"`
	// 结算金额
	SettlementAmount float64 `json:"settlementAmount"`
}