This commit is contained in:
2026-05-13 14:43:10 +08:00
parent 2312f54e1e
commit 3399de0dc5
49 changed files with 1637 additions and 287 deletions

View File

@@ -4,6 +4,9 @@ import (
"context"
"fmt"
"qnc-server/common/globalkey"
"github.com/Masterminds/squirrel"
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlx"
@@ -18,6 +21,8 @@ type (
agentModel
// UpdateInTransaction 在事务中更新刚插入的记录,避免 UpdateWithVersion 中的 FindOne 缓存问题
UpdateInTransaction(ctx context.Context, session sqlx.Session, data *Agent) error
// CountSubordinatesByTeamLeaderIds 按团队首领 id 统计未删除的直属代理数量team_leader_id = 传入 id
CountSubordinatesByTeamLeaderIds(ctx context.Context, leaderIds []string) (map[string]int64, error)
}
customAgentModel struct {
@@ -63,3 +68,36 @@ func (m *customAgentModel) UpdateInTransaction(ctx context.Context, session sqlx
return nil
}
type teamLeaderSubCountRow struct {
TeamLeaderId string `db:"team_leader_id"`
Cnt int64 `db:"cnt"`
}
func (m *customAgentModel) CountSubordinatesByTeamLeaderIds(ctx context.Context, leaderIds []string) (map[string]int64, error) {
out := make(map[string]int64)
if len(leaderIds) == 0 {
return out, nil
}
// 不含「自己挂在自己名下」的记录,否则首领会在自己的下级列表里且下级数量虚高
qb := squirrel.Select("team_leader_id", "COUNT(*) AS cnt").
From(m.table).
Where(squirrel.Eq{"del_state": globalkey.DelStateNo}).
Where(squirrel.Eq{"team_leader_id": leaderIds}).
Where("`id` <> `team_leader_id`").
GroupBy("team_leader_id")
query, args, err := qb.ToSql()
if err != nil {
return nil, err
}
var rows []teamLeaderSubCountRow
if err := m.QueryRowsNoCacheCtx(ctx, &rows, query, args...); err != nil {
return nil, err
}
for _, r := range rows {
if r.TeamLeaderId != "" {
out[r.TeamLeaderId] = r.Cnt
}
}
return out, nil
}