topfans/backend/gateway/dto/asset_converter.go
2026-04-10 21:05:19 +08:00

262 lines
6.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package dto
import (
pbAsset "github.com/topfans/backend/pkg/proto/asset"
)
// ========== Proto 转 DTO ==========
// ConvertCreateMintOrderResponse 转换创建铸造订单响应
func ConvertCreateMintOrderResponse(pbResp *pbAsset.CreateMintOrderResponse) *CreateMintOrderResponseDTO {
if pbResp == nil {
return nil
}
dto := &CreateMintOrderResponseDTO{}
// 转换订单信息
if pbResp.Order != nil {
dto.Order = ConvertMintOrder(pbResp.Order)
}
// 转换资产信息
if pbResp.Asset != nil {
dto.Asset = ConvertAsset(pbResp.Asset)
}
return dto
}
// ConvertMintOrder 转换铸造订单
func ConvertMintOrder(pbOrder *pbAsset.MintOrder) MintOrderDTO {
dto := MintOrderDTO{
OrderID: pbOrder.OrderId,
UserID: pbOrder.UserId,
AssetID: pbOrder.AssetId,
StarID: pbOrder.StarId,
Status: pbOrder.Status,
CostCrystal: pbOrder.CostCrystal,
RetryCount: pbOrder.RetryCount,
CreatedAt: pbOrder.CreatedAt,
UpdatedAt: pbOrder.UpdatedAt,
}
// 可选字段
if pbOrder.ErrorMessage != "" {
dto.ErrorMessage = pbOrder.ErrorMessage
}
if pbOrder.MaterialUrl != "" {
dto.MaterialURL = pbOrder.MaterialUrl
}
if pbOrder.Name != "" {
dto.Name = pbOrder.Name
}
if pbOrder.Description != "" {
dto.Description = pbOrder.Description
}
if pbOrder.MaterialType != "" {
dto.MaterialType = pbOrder.MaterialType
}
if pbOrder.Event != "" {
dto.Event = pbOrder.Event
}
if pbOrder.MintedAt > 0 {
dto.MintedAt = pbOrder.MintedAt
}
return dto
}
// ConvertAsset 转换资产信息
// 注意:预签名 URL 需要在 Controller 层生成,因为需要访问 OSS 配置
func ConvertAsset(pbAsset *pbAsset.Asset) AssetDTO {
dto := AssetDTO{
AssetID: pbAsset.AssetId,
OwnerUID: pbAsset.OwnerUid,
OwnerNickname: pbAsset.OwnerNickname,
StarID: pbAsset.StarId,
Name: pbAsset.Name,
CoverURL: pbAsset.CoverUrl,
Visibility: pbAsset.Visibility,
Status: pbAsset.Status,
LikeCount: pbAsset.LikeCount,
CreatedAt: pbAsset.CreatedAt,
UpdatedAt: pbAsset.UpdatedAt,
IsLiked: pbAsset.IsLiked,
Info: pbAsset.Info,
}
// 可选字段
if pbAsset.MaterialUrl != "" {
dto.MaterialURL = pbAsset.MaterialUrl
}
if pbAsset.Description != "" {
dto.Description = pbAsset.Description
}
if pbAsset.Rarity > 0 {
dto.Rarity = pbAsset.Rarity
}
if len(pbAsset.Tags) > 0 {
dto.Tags = pbAsset.Tags
}
if pbAsset.TxHash != "" {
dto.TxHash = pbAsset.TxHash
}
if pbAsset.BlockNumber > 0 {
dto.BlockNumber = pbAsset.BlockNumber
}
if pbAsset.MintedAt > 0 {
dto.MintedAt = pbAsset.MintedAt
}
// 持有者信息(保留用于兼容性)
if pbAsset.Owner != nil {
dto.Owner = &OwnerInfoDTO{
UserID: pbAsset.Owner.UserId,
Nickname: pbAsset.Owner.Nickname,
Avatar: pbAsset.Owner.Avatar,
}
}
return dto
}
// ConvertAssetWithPresignedURL 转换资产信息并生成预签名 URL
func ConvertAssetWithPresignedURL(pbAsset *pbAsset.Asset, generatePresignedURL func(string) (string, error)) AssetDTO {
dto := ConvertAsset(pbAsset)
// 为 cover_url 生成预签名 URL
if dto.CoverURL != "" {
if signedURL, err := generatePresignedURL(dto.CoverURL); err == nil {
dto.CoverURLSigned = signedURL
}
}
// 为 material_url 生成预签名 URL
if dto.MaterialURL != "" {
if signedURL, err := generatePresignedURL(dto.MaterialURL); err == nil {
dto.MaterialURLSigned = signedURL
}
}
return dto
}
// ConvertGetMyAssetsResponse 转换获取我的藏品列表响应
func ConvertGetMyAssetsResponse(pbResp *pbAsset.GetMyAssetsResponse) *GetMyAssetsResponseDTO {
if pbResp == nil {
return nil
}
dto := &GetMyAssetsResponseDTO{
Total: pbResp.Total,
Page: pbResp.Page,
PageSize: pbResp.PageSize,
HasMore: pbResp.HasMore,
Items: make([]AssetListItemDTO, 0, len(pbResp.Items)),
}
// 转换列表项
for _, item := range pbResp.Items {
if item != nil {
dto.Items = append(dto.Items, ConvertAssetListItem(item))
}
}
return dto
}
// ConvertAssetListItem 转换资产列表项
func ConvertAssetListItem(pbItem *pbAsset.AssetListItem) AssetListItemDTO {
dto := AssetListItemDTO{
AssetID: pbItem.AssetId,
Name: pbItem.Name,
CoverURL: pbItem.CoverUrl,
Status: pbItem.Status,
CreatedAt: pbItem.CreatedAt,
LikeCount: pbItem.LikeCount,
}
// 可选字段
if pbItem.TxHash != "" {
dto.TxHash = pbItem.TxHash
}
if pbItem.MintedAt > 0 {
dto.MintedAt = pbItem.MintedAt
}
return dto
}
// ConvertGetAssetResponse 转换获取资产详情响应
func ConvertGetAssetResponse(pbResp *pbAsset.GetAssetResponse) *GetAssetResponseDTO {
if pbResp == nil {
return nil
}
dto := &GetAssetResponseDTO{}
// 转换资产信息包含is_liked字段
if pbResp.Asset != nil {
dto.Asset = ConvertAsset(pbResp.Asset)
}
return dto
}
// ConvertGetAssetStatusResponse 转换查询上链状态响应
func ConvertGetAssetStatusResponse(pbResp *pbAsset.GetAssetStatusResponse) *GetAssetStatusResponseDTO {
if pbResp == nil {
return nil
}
dto := &GetAssetStatusResponseDTO{
AssetID: pbResp.AssetId,
Status: pbResp.Status,
}
// 可选字段
if pbResp.TxHash != "" {
dto.TxHash = pbResp.TxHash
}
if pbResp.BlockNumber > 0 {
dto.BlockNumber = pbResp.BlockNumber
}
if pbResp.MintedAt > 0 {
dto.MintedAt = pbResp.MintedAt
}
if pbResp.ErrorMessage != "" {
dto.ErrorMessage = pbResp.ErrorMessage
}
return dto
}
// ConvertGetMintOrderResponse 转换查询铸造订单状态响应
func ConvertGetMintOrderResponse(pbResp *pbAsset.GetMintOrderResponse) *GetMintOrderResponseDTO {
if pbResp == nil {
return nil
}
dto := &GetMintOrderResponseDTO{}
// 转换订单信息
if pbResp.Order != nil {
dto.Order = ConvertMintOrder(pbResp.Order)
// 如果订单成功且有资产信息将资产信息添加到订单DTO中用于兼容文档格式
if pbResp.Asset != nil && pbResp.Order.Status == "SUCCESS" {
dto.Order.CoverURL = pbResp.Asset.CoverUrl
dto.Order.TxHash = pbResp.Asset.TxHash
}
}
// 转换资产信息(如果存在)
if pbResp.Asset != nil {
assetDTO := ConvertAsset(pbResp.Asset)
dto.Asset = &assetDTO
}
return dto
}