first commit
This commit is contained in:
117
app/main/api/internal/logic/agent/getteamstatisticslogic.go
Normal file
117
app/main/api/internal/logic/agent/getteamstatisticslogic.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"ycc-server/app/main/model"
|
||||
"ycc-server/common/ctxdata"
|
||||
"ycc-server/common/globalkey"
|
||||
"ycc-server/common/xerr"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetTeamStatisticsLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetTeamStatisticsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTeamStatisticsLogic {
|
||||
return &GetTeamStatisticsLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetTeamStatisticsLogic) GetTeamStatistics() (resp *types.TeamStatisticsResp, err error) {
|
||||
userID, err := ctxdata.GetUidFromCtx(l.ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取用户信息失败, %v", err)
|
||||
}
|
||||
|
||||
// 1. 获取代理信息
|
||||
agent, err := l.svcCtx.AgentModel.FindOneByUserId(l.ctx, userID)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("您不是代理"), "")
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询代理信息失败, %v", err)
|
||||
}
|
||||
|
||||
// 2. 确定团队首领ID
|
||||
teamLeaderId := agent.Id
|
||||
if agent.TeamLeaderId.Valid {
|
||||
teamLeaderId = agent.TeamLeaderId.Int64
|
||||
}
|
||||
|
||||
// 3. 查询团队所有成员(通过 team_leader_id)
|
||||
builder := l.svcCtx.AgentModel.SelectBuilder().
|
||||
Where(squirrel.Or{
|
||||
squirrel.Eq{"team_leader_id": teamLeaderId},
|
||||
squirrel.And{
|
||||
squirrel.Eq{"id": teamLeaderId},
|
||||
squirrel.Eq{"level": 3}, // 钻石代理自己也是团队首领
|
||||
},
|
||||
}).
|
||||
Where("del_state = ?", globalkey.DelStateNo)
|
||||
|
||||
teamMembers, err := l.svcCtx.AgentModel.FindAll(l.ctx, builder, "")
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询团队成员失败, %v", err)
|
||||
}
|
||||
|
||||
// 4. 统计
|
||||
totalCount := int64(len(teamMembers))
|
||||
level1Count := int64(0) // 普通
|
||||
level2Count := int64(0) // 黄金
|
||||
level3Count := int64(0) // 钻石
|
||||
|
||||
// 统计直接下级
|
||||
directCount := int64(0)
|
||||
builder = l.svcCtx.AgentRelationModel.SelectBuilder().
|
||||
Where("parent_id = ? AND relation_type = ? AND del_state = ?", agent.Id, 1, globalkey.DelStateNo)
|
||||
directRelations, err := l.svcCtx.AgentRelationModel.FindAll(l.ctx, builder, "")
|
||||
if err == nil {
|
||||
directCount = int64(len(directRelations))
|
||||
}
|
||||
|
||||
for _, member := range teamMembers {
|
||||
// 排除自己
|
||||
if member.Id == agent.Id {
|
||||
continue
|
||||
}
|
||||
|
||||
switch member.Level {
|
||||
case 1:
|
||||
level1Count++
|
||||
case 2:
|
||||
level2Count++
|
||||
case 3:
|
||||
level3Count++
|
||||
}
|
||||
}
|
||||
|
||||
// 间接下级 = 总人数 - 直接下级 - 自己
|
||||
indirectCount := totalCount - directCount - 1
|
||||
if indirectCount < 0 {
|
||||
indirectCount = 0
|
||||
}
|
||||
|
||||
return &types.TeamStatisticsResp{
|
||||
TotalCount: totalCount - 1, // 排除自己
|
||||
DirectCount: directCount,
|
||||
IndirectCount: indirectCount,
|
||||
ByLevel: types.TeamLevelStats{
|
||||
Normal: level1Count,
|
||||
Gold: level2Count,
|
||||
Diamond: level3Count,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user