96 lines
3.0 KiB
Go
96 lines
3.0 KiB
Go
package shared
|
||
|
||
import (
|
||
"fmt"
|
||
"strings"
|
||
)
|
||
|
||
func UserIdentityKeywordSQL(userAlias string, userIDExpr string, keyword string, nowMs int64) (string, []any) {
|
||
keyword = strings.TrimSpace(keyword)
|
||
if keyword == "" {
|
||
return "1 = 1", nil
|
||
}
|
||
like := "%" + keyword + "%"
|
||
// 后台表格里的用户搜索统一覆盖长 user_id、当前展示号、默认短号、昵称和 active 靓号租约。
|
||
// 靓号不能只依赖 users.current_display_user_id;历史修复、本地导入或过期恢复可能让 users 快照和租约审计表短暂不一致。
|
||
return fmt.Sprintf(`(CAST(%[2]s AS CHAR) LIKE ?
|
||
OR %[1]s.current_display_user_id LIKE ?
|
||
OR %[1]s.default_display_user_id LIKE ?
|
||
OR %[1]s.username LIKE ?
|
||
OR %[3]s)`, userAlias, userIDExpr, ActivePrettyDisplayIDSQL(userAlias, "LIKE ?")), []any{
|
||
like,
|
||
like,
|
||
like,
|
||
like,
|
||
nowMs,
|
||
like,
|
||
}
|
||
}
|
||
|
||
func UserIdentityExactSQL(userAlias string, userIDExpr string, keyword string, nowMs int64) (string, []any) {
|
||
keyword = strings.TrimSpace(keyword)
|
||
if keyword == "" {
|
||
return "1 = 1", nil
|
||
}
|
||
like := "%" + keyword + "%"
|
||
// 单用户解析先做精确 user_id/短号/靓号命中,再允许昵称模糊兜底,避免运营输入靓号时被当成普通昵称查询。
|
||
return fmt.Sprintf(`(CAST(%[2]s AS CHAR) = ?
|
||
OR %[1]s.current_display_user_id = ?
|
||
OR %[1]s.default_display_user_id = ?
|
||
OR %[3]s
|
||
OR %[1]s.username LIKE ?)`, userAlias, userIDExpr, ActivePrettyDisplayIDSQL(userAlias, "= ?")), []any{
|
||
keyword,
|
||
keyword,
|
||
keyword,
|
||
nowMs,
|
||
keyword,
|
||
like,
|
||
}
|
||
}
|
||
|
||
func UserIdentityExactOrderSQL(userAlias string, userIDExpr string, keyword string, nowMs int64) (string, []any) {
|
||
keyword = strings.TrimSpace(keyword)
|
||
return fmt.Sprintf(`CASE
|
||
WHEN CAST(%[2]s AS CHAR) = ? THEN 0
|
||
WHEN %[1]s.current_display_user_id = ? THEN 1
|
||
WHEN %[1]s.default_display_user_id = ? THEN 2
|
||
WHEN %[3]s THEN 3
|
||
ELSE 4
|
||
END`, userAlias, userIDExpr, ActivePrettyDisplayIDSQL(userAlias, "= ?")), []any{
|
||
keyword,
|
||
keyword,
|
||
keyword,
|
||
nowMs,
|
||
keyword,
|
||
}
|
||
}
|
||
|
||
func UserDisplayIDSQL(userAlias string, displayUserID string, nowMs int64) (string, []any) {
|
||
displayUserID = strings.TrimSpace(displayUserID)
|
||
if displayUserID == "" {
|
||
return "1 = 1", nil
|
||
}
|
||
// 精确短号筛选同样覆盖默认短号、当前展示号和 active 靓号,避免换靓号后只能按其中一种号码查到记录。
|
||
return fmt.Sprintf(`(%[1]s.current_display_user_id = ?
|
||
OR %[1]s.default_display_user_id = ?
|
||
OR %[2]s)`, userAlias, ActivePrettyDisplayIDSQL(userAlias, "= ?")), []any{
|
||
displayUserID,
|
||
displayUserID,
|
||
nowMs,
|
||
displayUserID,
|
||
}
|
||
}
|
||
|
||
func ActivePrettyDisplayIDSQL(userAlias string, displayPredicate string) string {
|
||
return fmt.Sprintf(`EXISTS (
|
||
SELECT 1
|
||
FROM pretty_display_user_id_leases lease
|
||
WHERE lease.app_code = %[1]s.app_code
|
||
AND lease.user_id = %[1]s.user_id
|
||
AND lease.status = 'active'
|
||
AND (COALESCE(lease.expires_at_ms, 0) = 0 OR lease.expires_at_ms > ?)
|
||
AND lease.display_user_id %[2]s
|
||
LIMIT 1
|
||
)`, userAlias, displayPredicate)
|
||
}
|