157 lines
4.5 KiB
Go
157 lines
4.5 KiB
Go
package wheel
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"chatapp3-golang/internal/model"
|
|
)
|
|
|
|
// PageRecords returns admin draw records.
|
|
func (s *Service) PageRecords(
|
|
ctx context.Context,
|
|
sysOrigin string,
|
|
category string,
|
|
userID int64,
|
|
status string,
|
|
cursor int,
|
|
limit int,
|
|
) (*RecordPageResponse, error) {
|
|
sysOrigin = normalizeSysOrigin(sysOrigin)
|
|
if sysOrigin == "" {
|
|
return nil, NewAppError(400, "bad_request", "sysOrigin is required")
|
|
}
|
|
cursor, limit = normalizeRecordPage(cursor, limit)
|
|
|
|
query := s.db.WithContext(ctx).Model(&model.WheelDrawRecord{}).
|
|
Where("sys_origin = ?", sysOrigin)
|
|
if strings.TrimSpace(category) != "" {
|
|
query = query.Where("category = ?", normalizeCategory(category))
|
|
}
|
|
if userID > 0 {
|
|
query = query.Where("user_id = ?", userID)
|
|
}
|
|
if status != "" {
|
|
query = query.Where("status = ?", status)
|
|
}
|
|
|
|
var total int64
|
|
if err := query.Count(&total).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var rows []model.WheelDrawRecord
|
|
if err := query.Order("create_time desc, id desc").
|
|
Offset((cursor - 1) * limit).
|
|
Limit(limit).
|
|
Find(&rows).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
records := make([]DrawRecordPayload, 0, len(rows))
|
|
for _, row := range rows {
|
|
records = append(records, drawRecordPayload(row))
|
|
}
|
|
return &RecordPageResponse{
|
|
Records: records,
|
|
Total: total,
|
|
Current: cursor,
|
|
Size: limit,
|
|
}, nil
|
|
}
|
|
|
|
// PageUserHistory returns app draw history for the authenticated user.
|
|
func (s *Service) PageUserHistory(ctx context.Context, user AuthUser, cursor int, limit int) (*RecordPageResponse, error) {
|
|
return s.PageRecords(ctx, user.SysOrigin, "", user.UserID, drawStatusSuccess, cursor, limit)
|
|
}
|
|
|
|
// Hints returns recent successful rewards for ticker displays.
|
|
func (s *Service) Hints(ctx context.Context, user AuthUser, category string, limit int) ([]DrawRewardPayload, error) {
|
|
if limit <= 0 {
|
|
limit = 20
|
|
}
|
|
if limit > 50 {
|
|
limit = 50
|
|
}
|
|
query := s.db.WithContext(ctx).
|
|
Where("sys_origin = ? AND status = ?", normalizeSysOrigin(user.SysOrigin), drawStatusSuccess).
|
|
Order("create_time desc, id desc").
|
|
Limit(limit)
|
|
if strings.TrimSpace(category) != "" {
|
|
query = query.Where("category = ?", normalizeCategory(category))
|
|
}
|
|
var rows []model.WheelDrawRecord
|
|
if err := query.Find(&rows).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
rewards := make([]DrawRewardPayload, 0, len(rows))
|
|
for _, row := range rows {
|
|
name := row.ResourceName
|
|
if name == "" && row.RewardType == rewardTypeGold {
|
|
name = "金币"
|
|
}
|
|
rewards = append(rewards, DrawRewardPayload{
|
|
RewardType: row.RewardType,
|
|
ResourceID: resourceIDValue(row.ResourceID),
|
|
ResourceType: row.ResourceType,
|
|
ResourceName: row.ResourceName,
|
|
ResourceURL: row.ResourceURL,
|
|
CoverURL: row.CoverURL,
|
|
DurationDays: row.DurationDays,
|
|
DisplayGoldAmount: row.DisplayGoldAmount,
|
|
GoldAmount: row.GoldAmount,
|
|
Probability: row.Probability,
|
|
Count: 1,
|
|
Name: name,
|
|
Value: displayValueForRecord(row),
|
|
})
|
|
}
|
|
return rewards, nil
|
|
}
|
|
|
|
// WalletBalances returns current gold balance in the shape needed by the H5 compatibility API.
|
|
func (s *Service) WalletBalances(ctx context.Context, user AuthUser) (int64, error) {
|
|
if s.java == nil {
|
|
return 0, nil
|
|
}
|
|
balances, err := s.java.MapGoldBalance(ctx, []int64{user.UserID})
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return balances[user.UserID], nil
|
|
}
|
|
|
|
func drawRecordPayload(row model.WheelDrawRecord) DrawRecordPayload {
|
|
return DrawRecordPayload{
|
|
ID: row.ID,
|
|
DrawNo: row.DrawNo,
|
|
EventID: row.EventID,
|
|
UserID: row.UserID,
|
|
SysOrigin: row.SysOrigin,
|
|
Category: row.Category,
|
|
DrawTimes: row.DrawTimes,
|
|
PaidGold: row.PaidGold,
|
|
RewardType: row.RewardType,
|
|
ResourceID: resourceIDValue(row.ResourceID),
|
|
ResourceType: row.ResourceType,
|
|
ResourceName: row.ResourceName,
|
|
ResourceURL: row.ResourceURL,
|
|
CoverURL: row.CoverURL,
|
|
DurationDays: row.DurationDays,
|
|
DisplayGoldAmount: row.DisplayGoldAmount,
|
|
GoldAmount: row.GoldAmount,
|
|
Probability: row.Probability,
|
|
Status: row.Status,
|
|
ErrorMessage: row.ErrorMessage,
|
|
CreateTime: formatDateTime(row.CreateTime),
|
|
UpdateTime: formatDateTime(row.UpdateTime),
|
|
}
|
|
}
|
|
|
|
func displayValueForRecord(row model.WheelDrawRecord) int64 {
|
|
if normalizeRewardType(row.RewardType) == rewardTypeGold {
|
|
return row.GoldAmount
|
|
}
|
|
return row.DisplayGoldAmount
|
|
}
|