refactor(api): 优化CreatorsAPI的列表逻辑与数据处理

- 增强list方法,添加重复用户ID过滤,确保返回数据唯一性
- 改进用户名称显示逻辑,支持格式化输出
- 统一SQL查询逻辑,提升代码可读性与维护性
- 处理查询结果时,确保错误处理和数据有效性检查更加严谨
This commit is contained in:
zhouyonggao 2025-12-17 15:51:30 +08:00
parent 526ff0054c
commit 5c143616d5
4 changed files with 173 additions and 188 deletions

View File

@ -2,6 +2,7 @@ package api
import (
"database/sql"
"fmt"
"net/http"
"strconv"
"strings"
@ -28,7 +29,9 @@ func (a *CreatorsAPI) list(w http.ResponseWriter, r *http.Request) {
limitStr := r.URL.Query().Get("limit")
limit := 2000
if limitStr != "" {
if n, err := strconv.Atoi(limitStr); err == nil && n > 0 && n <= 10000 { limit = n }
if n, err := strconv.Atoi(limitStr); err == nil && n > 0 && n <= 10000 {
limit = n
}
}
sql1 := "SELECT DISTINCT user_id, COALESCE(user_name, '') AS name FROM activity WHERE user_id IS NOT NULL"
args := []interface{}{}
@ -41,14 +44,30 @@ func (a *CreatorsAPI) list(w http.ResponseWriter, r *http.Request) {
args = append(args, limit)
rows, err := a.marketing.Query(sql1, args...)
out := []map[string]interface{}{}
used := map[int64]struct{}{}
if err == nil {
defer rows.Close()
for rows.Next() {
var id sql.NullInt64
var name sql.NullString
if err := rows.Scan(&id, &name); err != nil { continue }
if !id.Valid { continue }
m := map[string]interface{}{"id": id.Int64, "name": name.String}
if err := rows.Scan(&id, &name); err != nil {
continue
}
if !id.Valid {
continue
}
if _, ok := used[id.Int64]; ok {
continue
}
used[id.Int64] = struct{}{}
n := strings.TrimSpace(name.String)
if n == "" {
n = strconv.FormatInt(id.Int64, 10)
}
display := fmt.Sprintf("%s%d", n, id.Int64)
m := map[string]interface{}{"id": id.Int64, "name": display}
out = append(out, m)
}
}
@ -66,14 +85,32 @@ func (a *CreatorsAPI) list(w http.ResponseWriter, r *http.Request) {
if errPlan == nil {
defer rowsPlan.Close()
tmp := []map[string]interface{}{}
usedPlan := map[int64]struct{}{}
for rowsPlan.Next() {
var id sql.NullInt64
var name sql.NullString
if err := rowsPlan.Scan(&id, &name); err != nil { continue }
if !id.Valid { continue }
tmp = append(tmp, map[string]interface{}{"id": id.Int64, "name": name.String})
if err := rowsPlan.Scan(&id, &name); err != nil {
continue
}
if !id.Valid {
continue
}
if _, ok := usedPlan[id.Int64]; ok {
continue
}
usedPlan[id.Int64] = struct{}{}
n := strings.TrimSpace(name.String)
if n == "" {
n = strconv.FormatInt(id.Int64, 10)
}
display := fmt.Sprintf("%s%d", n, id.Int64)
tmp = append(tmp, map[string]interface{}{"id": id.Int64, "name": display})
}
if len(tmp) > 0 {
out = tmp
}
if len(tmp) > 0 { out = tmp }
}
if len(out) == 0 {
sql2 := "SELECT DISTINCT creator, '' AS name FROM `order` WHERE creator IS NOT NULL"
@ -91,12 +128,28 @@ func (a *CreatorsAPI) list(w http.ResponseWriter, r *http.Request) {
}
defer rows2.Close()
out = out[:0]
usedOrder := map[int64]struct{}{}
for rows2.Next() {
var id sql.NullInt64
var name sql.NullString
if err := rows2.Scan(&id, &name); err != nil { continue }
if !id.Valid { continue }
m := map[string]interface{}{"id": id.Int64, "name": name.String}
if err := rows2.Scan(&id, &name); err != nil {
continue
}
if !id.Valid {
continue
}
if _, ok := usedOrder[id.Int64]; ok {
continue
}
usedOrder[id.Int64] = struct{}{}
n := strings.TrimSpace(name.String)
if n == "" {
n = strconv.FormatInt(id.Int64, 10)
}
display := fmt.Sprintf("%s%d", n, id.Int64)
m := map[string]interface{}{"id": id.Int64, "name": display}
out = append(out, m)
}
}

View File

@ -2,6 +2,7 @@ package api
import (
"database/sql"
"fmt"
"net/http"
"strconv"
"strings"
@ -45,13 +46,31 @@ func (a *YMTUsersAPI) list(w http.ResponseWriter, r *http.Request) {
return
}
defer rows.Close()
out := []map[string]interface{}{}
used := map[int64]struct{}{}
for rows.Next() {
var id sql.NullInt64
var name sql.NullString
if err := rows.Scan(&id, &name); err != nil { continue }
if !id.Valid { continue }
out = append(out, map[string]interface{}{"id": id.Int64, "name": name.String})
if err := rows.Scan(&id, &name); err != nil {
continue
}
if !id.Valid {
continue
}
if _, ok := used[id.Int64]; ok {
// 根据 ID 去重
continue
}
used[id.Int64] = struct{}{}
n := strings.TrimSpace(name.String)
if n == "" {
n = strconv.FormatInt(id.Int64, 10)
}
display := fmt.Sprintf("%s%d", n, id.Int64)
out = append(out, map[string]interface{}{"id": id.Int64, "name": display})
}
ok(w, r, out)
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,2 @@
/opt/homebrew/opt/go/libexec/src/crypto/x509/cert_pool.go:10:2: package encoding/pem is not in std (/opt/homebrew/opt/go/libexec/src/encoding/pem)
cmd/server/main.go:7:2: open /Users/zhouyonggao/Library/Caches/go-build/81/81aa4fdeaa93757df0aec25e30b31ebae853b2651233ed934d1a1b081249443a-d: operation not permitted