2026-04-30 13:29:22 +08:00

119 lines
3.9 KiB
Go

package rechargeagency
import (
"context"
"net/http"
"strings"
"chatapp3-golang/internal/integration"
"chatapp3-golang/internal/model"
)
// PageSellers 返回当前用户可见的 H5 币商名单。
func (s *Service) PageSellers(ctx context.Context, user AuthUser, cursor int, limit int) (*SellerListResponse, error) {
if s.java == nil {
return nil, NewAppError(http.StatusServiceUnavailable, "java_gateway_unavailable", "java gateway is unavailable")
}
if strings.TrimSpace(user.Authorization) == "" {
return nil, NewAppError(http.StatusUnauthorized, "missing_authorization", "authorization is required")
}
sysOrigin := normalizeSysOrigin(user.SysOrigin)
if sysOrigin == "" {
return nil, NewAppError(http.StatusUnauthorized, "invalid_sys_origin", "authenticated sysOrigin is required")
}
cursor, limit = normalizePage(cursor, limit)
page, err := s.java.PageFreightSellers(ctx, user.Authorization, sysOrigin, cursor, limit)
if err != nil {
return nil, NewAppError(http.StatusBadGateway, "freight_seller_list_failed", err.Error())
}
records := make([]SellerView, 0, len(page.Records))
for _, record := range page.Records {
records = append(records, buildSellerView(record))
}
if s.db != nil {
whatsapps, err := s.loadSellerWhatsapps(ctx, sysOrigin, collectSellerUserIDs(records))
if err != nil {
return nil, err
}
for i := range records {
if whatsapp := strings.TrimSpace(whatsapps[records[i].UserID]); whatsapp != "" {
records[i].Whatsapp = whatsapp
}
}
}
return &SellerListResponse{
SysOrigin: sysOrigin,
Cursor: cursor,
Limit: limit,
Total: page.Total,
Records: records,
}, nil
}
func buildSellerView(record integration.FreightSellerRecord) SellerView {
profile := record.UserBaseInfo
return SellerView{
ID: int64(record.ID),
SysOrigin: strings.TrimSpace(record.SysOrigin),
UserID: int64(record.UserID),
Account: firstNonBlank(profile.Account, formatInt64(int64(record.UserID))),
UserAvatar: strings.TrimSpace(profile.UserAvatar),
UserNickname: firstNonBlank(profile.UserNickname, profile.Account, formatInt64(int64(record.UserID))),
CountryCode: strings.TrimSpace(profile.CountryCode),
CountryName: strings.TrimSpace(profile.CountryName),
RegionCode: strings.TrimSpace(profile.RegionCode),
Balance: normalizeDecimalString(string(record.Balance)),
ContactInfo: strings.TrimSpace(record.ContactInfo),
SupportedCountries: strings.TrimSpace(record.SupportedCountries),
NationalFlag: normalizeFlags(record.NationalFlag),
OwnNationalFlag: strings.TrimSpace(record.OwnNationalFlag),
BecomeDays: record.BecomeDays,
TransactionCount: record.TransactionCount,
IsFollow: record.IsFollow,
SuperDealer: record.SuperDealer,
}
}
func collectSellerUserIDs(records []SellerView) []int64 {
seen := make(map[int64]struct{}, len(records))
userIDs := make([]int64, 0, len(records))
for _, record := range records {
if record.UserID <= 0 {
continue
}
if _, ok := seen[record.UserID]; ok {
continue
}
seen[record.UserID] = struct{}{}
userIDs = append(userIDs, record.UserID)
}
return userIDs
}
func (s *Service) loadSellerWhatsapps(ctx context.Context, sysOrigin string, userIDs []int64) (map[int64]string, error) {
whatsapps := make(map[int64]string, len(userIDs))
if len(userIDs) == 0 {
return whatsapps, nil
}
var rows []model.RechargeAgencySellerInfo
err := s.db.WithContext(ctx).
Select("user_id", "whatsapp").
Where("sys_origin = ? AND user_id IN ?", sysOrigin, userIDs).
Find(&rows).Error
if err != nil {
return nil, NewAppError(http.StatusInternalServerError, "seller_whatsapp_query_failed", err.Error())
}
for _, row := range rows {
if whatsapp := strings.TrimSpace(row.Whatsapp); whatsapp != "" {
whatsapps[row.UserID] = whatsapp
}
}
return whatsapps, nil
}