88 lines
3.6 KiB
Go
88 lines
3.6 KiB
Go
package gift
|
|
|
|
import (
|
|
"context"
|
|
biz "kra/internal/biz/gift"
|
|
"kra/internal/service/dto"
|
|
"time"
|
|
)
|
|
|
|
type Service struct{ uc *biz.Usecase }
|
|
|
|
func NewService(uc *biz.Usecase) *Service { return &Service{uc: uc} }
|
|
func rangeOf(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 opts(v dto.LiveListRequest) biz.ListOptions {
|
|
p, s := v.Page, v.PageSize
|
|
if p < 1 {
|
|
p = 1
|
|
}
|
|
if s < 1 {
|
|
s = 10
|
|
}
|
|
return biz.ListOptions{Offset: (p - 1) * s, Limit: s, OrderBy: v.OrderBy}
|
|
}
|
|
func (s *Service) List(ctx context.Context, in *dto.GiftListRequest) ([]*dto.GiftResponse, int64, error) {
|
|
items, total, err := s.uc.List(ctx, biz.Filter{Name: in.Name, MinPrice: in.MinPrice, MaxPrice: in.MaxPrice, Enabled: in.Enabled}, opts(in.LiveListRequest))
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
out := make([]*dto.GiftResponse, 0, len(items))
|
|
for _, v := range items {
|
|
out = append(out, gift(v))
|
|
}
|
|
return out, total, nil
|
|
}
|
|
func (s *Service) Get(ctx context.Context, id uint) (*dto.GiftResponse, error) {
|
|
v, e := s.uc.Get(ctx, id)
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
return gift(v), nil
|
|
}
|
|
func (s *Service) Save(ctx context.Context, id uint, in *dto.GiftRequest) (*dto.GiftResponse, error) {
|
|
v, e := s.uc.Save(ctx, &biz.Gift{ID: id, Name: in.Name, Icon: in.Icon, Price: in.Price, Description: in.Description, Tiers: in.Tiers, Sort: in.Sort, Enabled: in.Enabled})
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
return gift(v), nil
|
|
}
|
|
func (s *Service) Delete(ctx context.Context, id uint) error { return s.uc.Delete(ctx, id) }
|
|
func (s *Service) SetEnabled(ctx context.Context, id uint, v bool) error {
|
|
return s.uc.SetEnabled(ctx, id, v)
|
|
}
|
|
func (s *Service) SaveSort(ctx context.Context, v map[uint]int) error { return s.uc.SaveSort(ctx, v) }
|
|
func (s *Service) ListSends(ctx context.Context, in *dto.GiftSendListRequest) ([]*dto.GiftSendResponse, int64, *dto.GiftSendSummary, error) {
|
|
items, total, sum, err := s.uc.ListSends(ctx, biz.SendFilter{RoomName: in.RoomName, AnchorName: in.AnchorName, UserKeyword: in.UserKeyword, GiftID: in.GiftID, CreatedAt: rangeOf(in.StartedAt, in.EndedAt)}, opts(in.LiveListRequest))
|
|
if err != nil {
|
|
return nil, 0, nil, err
|
|
}
|
|
out := make([]*dto.GiftSendResponse, 0, len(items))
|
|
for _, v := range items {
|
|
out = append(out, send(v))
|
|
}
|
|
return out, total, &dto.GiftSendSummary{Count: sum.Count, Diamonds: sum.Diamonds, AmountCents: sum.AmountCents}, nil
|
|
}
|
|
func (s *Service) Send(ctx context.Context, userID, roomID uint, in *dto.GiftSendRequest) (*dto.GiftSendResponse, error) {
|
|
v, e := s.uc.Send(ctx, userID, roomID, in.GiftID, in.Quantity)
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
return send(v), nil
|
|
}
|
|
func gift(v *biz.Gift) *dto.GiftResponse {
|
|
return &dto.GiftResponse{ID: v.ID, Name: v.Name, Icon: v.Icon, Price: v.Price, Description: v.Description, Tiers: v.Tiers, Sort: v.Sort, Enabled: v.Enabled, SendCount: v.SendCount, CreatedAt: v.CreatedAt, UpdatedAt: v.UpdatedAt}
|
|
}
|
|
func send(v *biz.Send) *dto.GiftSendResponse {
|
|
return &dto.GiftSendResponse{ID: v.ID, SerialNo: v.SerialNo, UserID: v.UserID, Username: v.Username, Nickname: v.Nickname, RoomID: v.RoomID, RoomName: v.RoomName, RoomDeleted: v.RoomDeleted, AnchorUserID: v.AnchorUserID, AnchorName: v.AnchorName, GiftID: v.GiftID, GiftName: v.GiftName, GiftIcon: v.GiftIcon, GiftPrice: v.GiftPrice, Quantity: v.Quantity, DiamondTotal: v.DiamondTotal, EquivalentCents: v.EquivalentCents, CreatedAt: v.CreatedAt}
|
|
}
|