65 lines
2.2 KiB
Go
65 lines
2.2 KiB
Go
package service
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"strings"
|
||
|
||
"affiliate_dash/internal/model"
|
||
)
|
||
|
||
const rockSupportSearchRequired = "请输入包ID、上游编号、提取码、兑换码、工单号、商户单号或角色 ID 后再查询"
|
||
|
||
// ListSupportBoundTokens 平台客服只读查询绑定中的 Token。
|
||
// 固定只返回 status=bound;search 必填,避免客服页面在未输入条件时拉取全量数据。
|
||
func (s *RockService) ListSupportBoundTokens(ctx context.Context, page, pageSize int, search string) ([]model.RockToken, int64, error) {
|
||
search = strings.TrimSpace(search)
|
||
if search == "" {
|
||
return nil, 0, errors.New(rockSupportSearchRequired)
|
||
}
|
||
if page <= 0 {
|
||
page = 1
|
||
}
|
||
if pageSize <= 0 {
|
||
pageSize = 20
|
||
}
|
||
if pageSize > 100 {
|
||
pageSize = 100
|
||
}
|
||
|
||
db := s.db.WithContext(ctx).Model(&model.RockToken{}).Where("status = ?", model.RockStatusBound)
|
||
term := "%" + search + "%"
|
||
db = db.Where(
|
||
"upstream_token_id = ? OR upstream_token_id LIKE ? OR redeem_code LIKE ? OR assigned_order_no LIKE ? OR expected_uid LIKE ? OR actual_role_name LIKE ? OR actual_role_id LIKE ? OR remark LIKE ? OR CAST(package_id AS TEXT) LIKE ? OR token LIKE ? OR CAST(id AS TEXT) = ? OR assigned_order_no IN (SELECT order_no FROM fulfillment_orders WHERE client_order_no LIKE ?)",
|
||
search, term, term, term, term, term, term, term, term, term, search, term,
|
||
)
|
||
|
||
var total int64
|
||
if err := db.Count(&total).Error; err != nil {
|
||
return nil, 0, err
|
||
}
|
||
var tokens []model.RockToken
|
||
if err := db.Order("id DESC").
|
||
Offset((page - 1) * pageSize).
|
||
Limit(pageSize).
|
||
Find(&tokens).Error; err != nil {
|
||
return nil, 0, err
|
||
}
|
||
s.prepareRockTokenList(ctx, tokens)
|
||
return tokens, total, nil
|
||
}
|
||
|
||
// ListSupportTokenStatusLogs 平台客服只读查询状态历史;只允许查看绑定中的 Token。
|
||
func (s *RockService) ListSupportTokenStatusLogs(ctx context.Context, tokenID uint) ([]model.RockTokenStatusLog, error) {
|
||
var count int64
|
||
if err := s.db.WithContext(ctx).Model(&model.RockToken{}).
|
||
Where("id = ? AND status = ?", tokenID, model.RockStatusBound).
|
||
Count(&count).Error; err != nil {
|
||
return nil, err
|
||
}
|
||
if count == 0 {
|
||
return nil, errors.New("Token 不存在或无权查看")
|
||
}
|
||
return s.ListTokenStatusLogs(ctx, tokenID)
|
||
}
|