审查视图

pkg/application/mobile/cooperation/service/statistics_person.go 7.3 KB
yangfu authored
1 2 3
package service

import (
tangxuhui authored
4 5
	"time"
yangfu authored
6
	"github.com/linmadan/egglib-go/core/application"
yangfu authored
7
	"github.com/linmadan/egglib-go/utils/json"
yangfu authored
8
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/mobile/cooperation/command"
yangfu authored
9
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
yangfu authored
10
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_cooperation"
yangfu authored
11
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
yangfu authored
12 13
)
14
// 个人端统计  【0%】
yangfu authored
15 16 17
type PersonStatisticsService struct {
}
yangfu authored
18
// IndexStatistics  个人端 - 首页统计 (入口页面统计数据)
yangfu authored
19 20 21
func (srv PersonStatisticsService) IndexStatistics(cmd *command.IndexStatisticsCommand) (interface{}, error) {
	gateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		cmd.Operator)
yangfu authored
22 23

	// 1.项目概览统计
24 25
	contractStatisticsResult, err := gateway.CooperationStatistics(allied_creation_cooperation.PersonCooperationContractStatistics, map[string]interface{}{
		"userBaseId": cmd.Operator.UserBaseId,
yangfu authored
26 27 28 29
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
30 31 32 33 34 35
	type contractStatistics struct {
		ContractSum        int `json:"contractSum"`
		ContractStoppedSum int `json:"contractStoppedSum"`
	}
	var cs = &contractStatistics{}
	if err := json.UnmarshalFromString(json.MarshalToString(contractStatisticsResult), cs); err != nil {
yangfu authored
36 37
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
38
yangfu authored
39 40 41
	gatewayUser := allied_creation_user.NewHttplibAlliedCreationUser(
		cmd.Operator)
	users, err := gatewayUser.UserSearch(allied_creation_user.ReqUserSearch{
yangfu authored
42 43 44
		Limit:  1,
		Offset: 0,
		//UserType:   domain.UserTypeCooperation,
yangfu authored
45 46 47 48 49 50
		UserBaseId: cmd.Operator.UserBaseId,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	var overview = map[string]interface{}{
51 52 53
		"contractSum":        cs.ContractSum,        //总合约数
		"contractStoppedSum": cs.ContractStoppedSum, //停止的合约数
		"companySum":         users.Count,           //共创企业数
yangfu authored
54
	}
yangfu authored
55 56 57
	year, month, _ := time.Now().Date()
	beginTime := time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
	endTime := beginTime.AddDate(0, 1, 0)
yangfu authored
58
	// 2.本月分红统计 - 个人
yangfu authored
59
	unPaidResult, err := gateway.CooperationStatistics(allied_creation_cooperation.CreditAccountStatistics, map[string]interface{}{
yangfu authored
60
		"userBaseId": cmd.Operator.UserBaseId,
yangfu authored
61 62
		"beginTime":  beginTime,
		"endTime":    endTime,
yangfu authored
63 64 65 66 67
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	type AnnualDividend struct {
yangfu authored
68 69 70 71 72
		Total      float64 `json:"total"`
		Accounting float64 `json:"accounting"`
		Accounted  float64 `json:"accounted"`
		Paid       float64 `json:"paid"`
		Unpaid     float64 `json:"unpaid"`
yangfu authored
73
	}
yangfu authored
74 75 76 77 78 79 80 81 82 83 84 85
	var annualUnPaidDividend = &AnnualDividend{}
	if err := json.UnmarshalFromString(json.MarshalToString(unPaidResult), annualUnPaidDividend); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	// 2.本月分红统计 - 个人
	paymentResult, err := gateway.CooperationStatistics(allied_creation_cooperation.CreditAccountStatistics, map[string]interface{}{
		"userBaseId":       cmd.Operator.UserBaseId,
		"paymentBeginTime": beginTime,
		"paymentEndTime":   endTime,
	})
	var annualPaymentDividend = &AnnualDividend{}
	if err := json.UnmarshalFromString(json.MarshalToString(paymentResult), annualPaymentDividend); err != nil {
yangfu authored
86 87 88
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	dividendStatistics := map[string]interface{}{
yangfu authored
89 90 91
		"dividendAmount": annualPaymentDividend.Paid + annualUnPaidDividend.Unpaid, // 分红金额 annualDividend.Total
		"paidAmount":     annualPaymentDividend.Paid,                               // 已支付
		"unPaidAmount":   annualUnPaidDividend.Unpaid,                              // 未支付
yangfu authored
92 93
	}
yangfu authored
94
	return map[string]interface{}{
yangfu authored
95 96
		"overview":           overview,
		"dividendStatistics": dividendStatistics,
yangfu authored
97 98 99 100
	}, nil
}

// CompanyStatistics  共创用户-共创企业统计
yangfu authored
101 102 103 104 105 106 107 108
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,
yangfu authored
109
		//UserType:   domain.UserTypeCooperation,
yangfu authored
110
	})
yangfu authored
111 112 113
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
yangfu authored
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	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())
	}
yangfu authored
129
yangfu authored
130 131 132 133 134 135 136 137 138 139 140 141
	type cooperationCompanyStatisticsResponse struct {
		// 当天统计的企业id
		OrgId int64 `json:"orgId"`
		// 共创项目数
		CooperationProjectCount int64 `json:"cooperationProjectCount"`
		// 共创合约数
		CooperationContractCount int64 `json:"cooperationContractCount"`
		// 分红占比
		DividendsRatio float64 `json:"dividendsRatio"`
		// 分红支出
		DividendsIncome float64 `json:"dividendsIncome"`
		// 企业信息
yangfu authored
142
		Company domain.Company `json:"company"`
yangfu authored
143 144 145 146 147 148 149 150
	}
	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, "数据不匹配")
	}
yangfu authored
151
yangfu authored
152
	var values = make([]interface{}, 0)
yangfu authored
153 154
	for i := range users.Users {
		user := users.Users[i]
yangfu authored
155
		cooperationCompanyStatisticsResponses[i].Company = domain.Company{
yangfu authored
156 157
			CompanyID:   user.Org.OrgId,
			CompanyName: user.Org.OrgName,
yangfu authored
158
			Logo:        user.Company.Logo,
yangfu authored
159
		}
yangfu authored
160
		values = append(values, cooperationCompanyStatisticsResponses[i])
yangfu authored
161
	}
yangfu authored
162 163 164
	return map[string]interface{}{
		"list": values,
	}, nil
yangfu authored
165 166
}
yangfu authored
167 168
// CooperationProjectRecommend  TODO:其他公司按公开的项目查 猜你喜欢(共创项目)
func (srv PersonStatisticsService) CooperationProjectRecommend(projectQuery *command.ListCooperationProjectQuery) (int64, interface{}, error) {
yangfu authored
169 170 171
	if projectQuery.Operator.UserBaseId > 0 {

	}
yangfu authored
172 173 174
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		projectQuery.Operator)
	result, err := creationCooperationGateway.CooperationProjectsSearch(allied_creation_cooperation.ReqCooperationProjectSearch{
yangfu authored
175
		PageNumber:                       projectQuery.PageNumber + 1,
yangfu authored
176 177
		PageSize:                         projectQuery.PageSize,
		CooperationProjectUndertakerType: 3,
yangfu authored
178
		Status:                           1,
yangfu authored
179 180 181 182 183
	})
	if err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return int64(result.Total), result.List, nil
yangfu authored
184
}