328 lines
7.8 KiB
Go
328 lines
7.8 KiB
Go
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,
|
||
DisplayStatus: pbAsset.DisplayStatus,
|
||
}
|
||
|
||
// 可选字段
|
||
if pbAsset.MaterialUrl != "" {
|
||
dto.MaterialURL = pbAsset.MaterialUrl
|
||
}
|
||
if pbAsset.Description != "" {
|
||
dto.Description = pbAsset.Description
|
||
}
|
||
if pbAsset.Grade > 0 {
|
||
dto.Grade = pbAsset.Grade
|
||
}
|
||
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{}
|
||
|
||
if pbResp.Data != nil {
|
||
dto.Data = &AssetListDataDTO{
|
||
Total: pbResp.Data.Total,
|
||
Page: pbResp.Data.Page,
|
||
PageSize: pbResp.Data.PageSize,
|
||
HasMore: pbResp.Data.HasMore,
|
||
Groups: make([]AssetGroupDTO, 0, len(pbResp.Data.Groups)),
|
||
}
|
||
|
||
// 转换分组
|
||
for _, group := range pbResp.Data.Groups {
|
||
if group != nil {
|
||
dto.Data.Groups = append(dto.Data.Groups, ConvertAssetGroup(group))
|
||
}
|
||
}
|
||
}
|
||
|
||
return dto
|
||
}
|
||
|
||
// ConvertAssetGroup 转换资产分组
|
||
func ConvertAssetGroup(pbGroup *pbAsset.AssetGroup) AssetGroupDTO {
|
||
dto := AssetGroupDTO{
|
||
Type: pbGroup.Type,
|
||
Category: pbGroup.Category,
|
||
CategoryName: pbGroup.CategoryName,
|
||
TotalCount: pbGroup.TotalCount,
|
||
HasMore: pbGroup.HasMore,
|
||
Grades: make([]GradeSectionDTO, 0, len(pbGroup.Grades)),
|
||
Items: make([]AssetItemDTO, 0, len(pbGroup.Items)),
|
||
}
|
||
|
||
// 转换 grades
|
||
for _, grade := range pbGroup.Grades {
|
||
if grade != nil {
|
||
dto.Grades = append(dto.Grades, ConvertGradeSection(grade))
|
||
}
|
||
}
|
||
|
||
// 转换 items
|
||
for _, item := range pbGroup.Items {
|
||
if item != nil {
|
||
dto.Items = append(dto.Items, ConvertAssetItem(item))
|
||
}
|
||
}
|
||
|
||
return dto
|
||
}
|
||
|
||
// ConvertGradeSection 转换等级分组
|
||
func ConvertGradeSection(pbGrade *pbAsset.GradeSection) GradeSectionDTO {
|
||
dto := GradeSectionDTO{
|
||
Grade: pbGrade.Grade,
|
||
TotalCount: pbGrade.TotalCount,
|
||
HasMore: pbGrade.HasMore,
|
||
Items: make([]AssetItemDTO, 0, len(pbGrade.Items)),
|
||
}
|
||
|
||
for _, item := range pbGrade.Items {
|
||
if item != nil {
|
||
dto.Items = append(dto.Items, ConvertAssetItem(item))
|
||
}
|
||
}
|
||
|
||
return dto
|
||
}
|
||
|
||
// ConvertAssetItem 转换资产项
|
||
func ConvertAssetItem(pbItem *pbAsset.AssetItem) AssetItemDTO {
|
||
return AssetItemDTO{
|
||
AssetID: pbItem.AssetId,
|
||
Name: pbItem.Name,
|
||
CoverURLSigned: pbItem.CoverUrlSigned,
|
||
LikeCount: pbItem.LikeCount,
|
||
CreatedAt: pbItem.CreatedAt,
|
||
Category: pbItem.Category,
|
||
Grade: pbItem.Grade,
|
||
DisplayStatus: pbItem.DisplayStatus,
|
||
}
|
||
}
|
||
|
||
// 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
|
||
}
|