Files

69 lines
2.5 KiB
Go

package asset
import (
"context"
biz "kra/internal/biz/asset"
"kra/internal/service/dto"
"time"
)
type Service struct{ uc *biz.Usecase }
func NewService(uc *biz.Usecase) *Service { return &Service{uc: uc} }
func dr(a, b string) []time.Time {
if a == "" || b == "" {
return nil
}
x, e1 := time.ParseInLocation("2006-01-02", a, time.Local)
y, e2 := time.ParseInLocation("2006-01-02", b, time.Local)
if e1 != nil || e2 != nil {
return nil
}
return []time.Time{x, y.Add(24*time.Hour - time.Nanosecond)}
}
func (s *Service) List(ctx context.Context, in *dto.AssetUserListRequest) ([]*dto.AssetUserResponse, int64, error) {
p, z := in.Page, in.PageSize
if p < 1 {
p = 1
}
if z < 1 {
z = 10
}
items, total, e := s.uc.List(ctx, biz.Filter{Keyword: in.Keyword, UserID: in.UserID, Enabled: in.Enabled, CreatedAt: dr(in.StartedAt, in.EndedAt)}, biz.ListOptions{Offset: (p - 1) * z, Limit: z, OrderBy: in.OrderBy})
if e != nil {
return nil, 0, e
}
out := make([]*dto.AssetUserResponse, 0, len(items))
for _, v := range items {
out = append(out, user(v))
}
return out, total, nil
}
func (s *Service) Get(ctx context.Context, id uint) (*dto.AssetUserResponse, error) {
v, e := s.uc.Get(ctx, id)
if e != nil {
return nil, e
}
return user(v), nil
}
func (s *Service) SetEnabled(ctx context.Context, actor, id uint, enabled bool) error {
return s.uc.SetEnabled(ctx, actor, id, enabled)
}
func (s *Service) Adjust(ctx context.Context, actor, id uint, in *dto.AssetAdjustmentRequest) (*dto.AssetUserResponse, error) {
v, e := s.uc.Adjust(ctx, &biz.Adjustment{UserID: id, OperatorUserID: actor, Type: in.Type, Amount: in.Amount, Reason: in.Reason})
if e != nil {
return nil, e
}
return user(v), nil
}
func user(v *biz.User) *dto.AssetUserResponse {
out := &dto.AssetUserResponse{UserID: v.UserID, Username: v.Username, Nickname: v.Nickname, Avatar: v.Avatar, Enabled: v.Enabled, DiamondBalance: v.DiamondBalance, TotalRechargeCents: v.TotalRechargeCents, TotalSpentDiamonds: v.TotalSpentDiamonds, CreatedAt: v.CreatedAt, LastLoginAt: v.LastLoginAt}
for _, x := range v.RecentOrders {
out.RecentOrders = append(out.RecentOrders, dto.AssetOrderRecord{OrderNo: x.OrderNo, Diamonds: x.Diamonds, AmountCents: x.AmountCents, Status: x.Status, CreatedAt: x.CreatedAt})
}
for _, x := range v.RecentSends {
out.RecentSends = append(out.RecentSends, dto.AssetSendRecord{SerialNo: x.SerialNo, RoomName: x.RoomName, GiftName: x.GiftName, GiftIcon: x.GiftIcon, DiamondTotal: x.DiamondTotal, CreatedAt: x.CreatedAt})
}
return out
}