feat: align ids to int64

This commit is contained in:
2026-01-08 09:57:04 +08:00
parent a1de16bc01
commit d98f41f1ac
39 changed files with 298 additions and 339 deletions

View File

@@ -3,7 +3,7 @@ package services
import (
"context"
"errors"
"fmt"
"strconv"
"time"
"quyun/v2/app/errorx"
@@ -14,7 +14,6 @@ import (
"quyun/v2/pkg/consts"
"github.com/google/uuid"
"github.com/spf13/cast"
"go.ipao.vip/gen/types"
"gorm.io/gorm"
)
@@ -125,9 +124,6 @@ func (s *creator) ListContents(
if filter.Visibility != nil && *filter.Visibility != "" {
q = q.Where(tbl.Visibility.Eq(consts.ContentVisibility(*filter.Visibility)))
}
if filter.Visibility != nil && *filter.Visibility != "" {
q = q.Where(tbl.Visibility.Eq(consts.ContentVisibility(*filter.Visibility)))
}
if filter.Genre != nil && *filter.Genre != "" {
val := *filter.Genre
if cn, ok := genreMap[val]; ok {
@@ -137,7 +133,6 @@ func (s *creator) ListContents(
}
}
if filter.Key != nil && *filter.Key != "" {
fmt.Printf("DEBUG: Filter Key: '%s'\n", *filter.Key)
q = q.Where(tbl.Key.Eq(*filter.Key))
}
if filter.Keyword != nil && *filter.Keyword != "" {
@@ -227,7 +222,7 @@ func (s *creator) ListContents(
}
data = append(data, creator_dto.CreatorContentItem{
ID: cast.ToString(item.ID),
ID: item.ID,
Title: item.Title,
Genre: item.Genre,
Key: item.Key,
@@ -286,7 +281,7 @@ func (s *creator) CreateContent(ctx context.Context, userID int64, form *creator
TenantID: tid,
UserID: uid,
ContentID: content.ID,
AssetID: cast.ToInt64(mid),
AssetID: mid,
Sort: int32(i),
Role: consts.ContentAssetRoleCover,
})
@@ -297,7 +292,7 @@ func (s *creator) CreateContent(ctx context.Context, userID int64, form *creator
TenantID: tid,
UserID: uid,
ContentID: content.ID,
AssetID: cast.ToInt64(mid),
AssetID: mid,
Sort: int32(i),
Role: consts.ContentAssetRoleMain,
})
@@ -327,19 +322,18 @@ func (s *creator) CreateContent(ctx context.Context, userID int64, form *creator
func (s *creator) UpdateContent(
ctx context.Context,
userID int64,
id string,
id int64,
form *creator_dto.ContentUpdateForm,
) error {
tid, err := s.getTenantID(ctx, userID)
if err != nil {
return err
}
cid := cast.ToInt64(id)
uid := userID
return models.Q.Transaction(func(tx *models.Query) error {
// 1. Check Ownership
c, err := tx.Content.WithContext(ctx).Where(tx.Content.ID.Eq(cid), tx.Content.TenantID.Eq(tid)).First()
c, err := tx.Content.WithContext(ctx).Where(tx.Content.ID.Eq(id), tx.Content.TenantID.Eq(tid)).First()
if err != nil {
return errorx.ErrRecordNotFound
}
@@ -366,7 +360,7 @@ func (s *creator) UpdateContent(
}
// Perform standard updates
_, err = tx.Content.WithContext(ctx).Where(tx.Content.ID.Eq(cid)).Updates(contentUpdates)
_, err = tx.Content.WithContext(ctx).Where(tx.Content.ID.Eq(id)).Updates(contentUpdates)
if err != nil {
return err
}
@@ -374,13 +368,13 @@ func (s *creator) UpdateContent(
// Handle IsPinned Logic
if finalStatus != consts.ContentStatusPublished {
// Force Unpin if not published
_, err = tx.Content.WithContext(ctx).Where(tx.Content.ID.Eq(cid)).UpdateSimple(tx.Content.IsPinned.Value(false))
_, err = tx.Content.WithContext(ctx).Where(tx.Content.ID.Eq(id)).UpdateSimple(tx.Content.IsPinned.Value(false))
if err != nil {
return err
}
} else if form.IsPinned != nil {
// Explicit Pin Update requested
_, err = tx.Content.WithContext(ctx).Where(tx.Content.ID.Eq(cid)).UpdateSimple(tx.Content.IsPinned.Value(*form.IsPinned))
_, err = tx.Content.WithContext(ctx).Where(tx.Content.ID.Eq(id)).UpdateSimple(tx.Content.IsPinned.Value(*form.IsPinned))
if err != nil {
return err
}
@@ -388,7 +382,7 @@ func (s *creator) UpdateContent(
// If setting to true, unpin others
if *form.IsPinned {
if _, err := tx.Content.WithContext(ctx).
Where(tx.Content.TenantID.Eq(tid), tx.Content.ID.Neq(cid)).
Where(tx.Content.TenantID.Eq(tid), tx.Content.ID.Neq(id)).
UpdateSimple(tx.Content.IsPinned.Value(false)); err != nil {
return err
}
@@ -398,17 +392,17 @@ func (s *creator) UpdateContent(
// 3. Update Price
// Check if price exists
if form.Price != nil {
count, _ := tx.ContentPrice.WithContext(ctx).Where(tx.ContentPrice.ContentID.Eq(cid)).Count()
count, _ := tx.ContentPrice.WithContext(ctx).Where(tx.ContentPrice.ContentID.Eq(id)).Count()
newPrice := int64(*form.Price * 100)
if count > 0 {
_, err = tx.ContentPrice.WithContext(ctx).
Where(tx.ContentPrice.ContentID.Eq(cid)).
Where(tx.ContentPrice.ContentID.Eq(id)).
UpdateSimple(tx.ContentPrice.PriceAmount.Value(newPrice))
} else {
err = tx.ContentPrice.WithContext(ctx).Create(&models.ContentPrice{
TenantID: tid,
UserID: c.UserID,
ContentID: cid,
ContentID: id,
PriceAmount: newPrice,
Currency: consts.CurrencyCNY,
})
@@ -419,7 +413,7 @@ func (s *creator) UpdateContent(
}
// 4. Update Assets (Full replacement strategy)
_, err = tx.ContentAsset.WithContext(ctx).Where(tx.ContentAsset.ContentID.Eq(cid)).Delete()
_, err = tx.ContentAsset.WithContext(ctx).Where(tx.ContentAsset.ContentID.Eq(id)).Delete()
if err != nil {
return err
}
@@ -430,8 +424,8 @@ func (s *creator) UpdateContent(
assets = append(assets, &models.ContentAsset{
TenantID: tid,
UserID: uid,
ContentID: cid,
AssetID: cast.ToInt64(mid),
ContentID: id,
AssetID: mid,
Sort: int32(i),
Role: consts.ContentAssetRoleCover,
})
@@ -441,8 +435,8 @@ func (s *creator) UpdateContent(
assets = append(assets, &models.ContentAsset{
TenantID: tid,
UserID: uid,
ContentID: cid,
AssetID: cast.ToInt64(mid),
ContentID: id,
AssetID: mid,
Sort: int32(i),
Role: consts.ContentAssetRoleMain,
})
@@ -457,21 +451,20 @@ func (s *creator) UpdateContent(
})
}
func (s *creator) DeleteContent(ctx context.Context, userID int64, id string) error {
cid := cast.ToInt64(id)
func (s *creator) DeleteContent(ctx context.Context, userID int64, id int64) error {
tid, err := s.getTenantID(ctx, userID)
if err != nil {
return err
}
// Check if purchased (ContentAccess exists)
count, _ := models.ContentAccessQuery.WithContext(ctx).Where(models.ContentAccessQuery.ContentID.Eq(cid)).Count()
count, _ := models.ContentAccessQuery.WithContext(ctx).Where(models.ContentAccessQuery.ContentID.Eq(id)).Count()
if count > 0 {
return errorx.ErrPreconditionFailed.WithMsg("该内容已被购买,无法删除")
}
_, err = models.ContentQuery.WithContext(ctx).
Where(models.ContentQuery.ID.Eq(cid), models.ContentQuery.TenantID.Eq(tid)).
Where(models.ContentQuery.ID.Eq(id), models.ContentQuery.TenantID.Eq(tid)).
Delete()
if err != nil {
return errorx.ErrDatabaseError.WithCause(err)
@@ -479,17 +472,16 @@ func (s *creator) DeleteContent(ctx context.Context, userID int64, id string) er
return nil
}
func (s *creator) GetContent(ctx context.Context, userID int64, id string) (*creator_dto.ContentEditDTO, error) {
func (s *creator) GetContent(ctx context.Context, userID int64, id int64) (*creator_dto.ContentEditDTO, error) {
tid, err := s.getTenantID(ctx, userID)
if err != nil {
return nil, err
}
cid := cast.ToInt64(id)
// Fetch Content with preloads
var c models.Content
err = models.ContentQuery.WithContext(ctx).
Where(models.ContentQuery.ID.Eq(cid), models.ContentQuery.TenantID.Eq(tid)).
Where(models.ContentQuery.ID.Eq(id), models.ContentQuery.TenantID.Eq(tid)).
UnderlyingDB().
Preload("ContentAssets", func(db *gorm.DB) *gorm.DB {
return db.Order("sort ASC")
@@ -505,13 +497,13 @@ func (s *creator) GetContent(ctx context.Context, userID int64, id string) (*cre
// Fetch Price
var price float64
cp, err := models.ContentPriceQuery.WithContext(ctx).Where(models.ContentPriceQuery.ContentID.Eq(cid)).First()
cp, err := models.ContentPriceQuery.WithContext(ctx).Where(models.ContentPriceQuery.ContentID.Eq(id)).First()
if err == nil {
price = float64(cp.PriceAmount) / 100.0
}
dto := &creator_dto.ContentEditDTO{
ID: cast.ToString(c.ID),
ID: c.ID,
Title: c.Title,
Genre: c.Genre,
Key: c.Key,
@@ -538,10 +530,10 @@ func (s *creator) GetContent(ctx context.Context, userID int64, id string) (*cre
sizeBytes := meta.Size
sizeMB := float64(sizeBytes) / 1024.0 / 1024.0
sizeStr := cast.ToString(float64(int(sizeMB*100))/100.0) + " MB"
sizeStr := strconv.FormatFloat(float64(int(sizeMB*100))/100.0, 'f', -1, 64) + " MB"
dto.Assets = append(dto.Assets, creator_dto.AssetDTO{
ID: cast.ToString(ca.AssetID),
ID: ca.AssetID,
Role: string(ca.Role),
Type: string(ca.Asset.Type),
URL: Common.GetAssetURL(ca.Asset.ObjectKey),
@@ -573,7 +565,7 @@ func (s *creator) ListOrders(
if filter.Keyword != nil && *filter.Keyword != "" {
k := *filter.Keyword
if id, err := cast.ToInt64E(k); err == nil {
if id, err := strconv.ParseInt(k, 10, 64); err == nil {
q = q.Where(tbl.ID.Eq(id))
} else {
uTbl, uQ := models.UserQuery.QueryContext(ctx)
@@ -629,7 +621,7 @@ func (s *creator) ListOrders(
}
data = append(data, creator_dto.Order{
ID: cast.ToString(o.ID),
ID: o.ID,
Status: string(o.Status),
Amount: float64(o.AmountPaid) / 100.0,
CreateTime: o.CreatedAt.Format(time.RFC3339),
@@ -642,17 +634,16 @@ func (s *creator) ListOrders(
return data, nil
}
func (s *creator) ProcessRefund(ctx context.Context, userID int64, id string, form *creator_dto.RefundForm) error {
func (s *creator) ProcessRefund(ctx context.Context, userID int64, id int64, form *creator_dto.RefundForm) error {
tid, err := s.getTenantID(ctx, userID)
if err != nil {
return err
}
oid := cast.ToInt64(id)
uid := userID // Creator ID
// Fetch Order
o, err := models.OrderQuery.WithContext(ctx).
Where(models.OrderQuery.ID.Eq(oid), models.OrderQuery.TenantID.Eq(tid)).
Where(models.OrderQuery.ID.Eq(id), models.OrderQuery.TenantID.Eq(tid)).
First()
if err != nil {
return errorx.ErrRecordNotFound
@@ -666,7 +657,7 @@ func (s *creator) ProcessRefund(ctx context.Context, userID int64, id string, fo
}
if form.Action == "reject" {
_, err := models.OrderQuery.WithContext(ctx).Where(models.OrderQuery.ID.Eq(oid)).Updates(&models.Order{
_, err := models.OrderQuery.WithContext(ctx).Where(models.OrderQuery.ID.Eq(id)).Updates(&models.Order{
Status: consts.OrderStatusPaid,
RefundReason: form.Reason, // Store reject reason? Or clear it?
})
@@ -696,7 +687,7 @@ func (s *creator) ProcessRefund(ctx context.Context, userID int64, id string, fo
}
// 3. Update Order Status
_, err = tx.Order.WithContext(ctx).Where(tx.Order.ID.Eq(oid)).Updates(&models.Order{
_, err = tx.Order.WithContext(ctx).Where(tx.Order.ID.Eq(id)).Updates(&models.Order{
Status: consts.OrderStatusRefunded,
RefundedAt: time.Now(),
RefundOperatorUserID: uid,
@@ -755,7 +746,7 @@ func (s *creator) GetSettings(ctx context.Context, userID int64) (*creator_dto.S
}
cfg := t.Config.Data()
return &creator_dto.Settings{
ID: cast.ToString(t.ID),
ID: t.ID,
Name: t.Name,
Bio: cfg.Bio,
Avatar: cfg.Avatar,
@@ -802,7 +793,7 @@ func (s *creator) ListPayoutAccounts(ctx context.Context, userID int64) ([]creat
var data []creator_dto.PayoutAccount
for _, v := range list {
data = append(data, creator_dto.PayoutAccount{
ID: cast.ToString(v.ID),
ID: v.ID,
Type: v.Type,
Name: v.Name,
Account: v.Account,
@@ -833,15 +824,14 @@ func (s *creator) AddPayoutAccount(ctx context.Context, userID int64, form *crea
return nil
}
func (s *creator) RemovePayoutAccount(ctx context.Context, userID int64, id string) error {
func (s *creator) RemovePayoutAccount(ctx context.Context, userID int64, id int64) error {
tid, err := s.getTenantID(ctx, userID)
if err != nil {
return err
}
pid := cast.ToInt64(id)
_, err = models.PayoutAccountQuery.WithContext(ctx).
Where(models.PayoutAccountQuery.ID.Eq(pid), models.PayoutAccountQuery.TenantID.Eq(tid)).
Where(models.PayoutAccountQuery.ID.Eq(id), models.PayoutAccountQuery.TenantID.Eq(tid)).
Delete()
if err != nil {
return errorx.ErrDatabaseError.WithCause(err)
@@ -872,7 +862,7 @@ func (s *creator) Withdraw(ctx context.Context, userID int64, form *creator_dto.
// Validate Payout Account
_, err = models.PayoutAccountQuery.WithContext(ctx).
Where(models.PayoutAccountQuery.ID.Eq(cast.ToInt64(form.AccountID)), models.PayoutAccountQuery.TenantID.Eq(tid)).
Where(models.PayoutAccountQuery.ID.Eq(form.AccountID), models.PayoutAccountQuery.TenantID.Eq(tid)).
First()
if err != nil {
return errorx.ErrRecordNotFound.WithMsg("收款账户不存在")