@@ -1,12 +1,14 @@
package consumer
package consumer
import (
import (
"bytes"
"context"
"context"
"encoding/json"
"encoding/json"
"errors"
"errors"
"fmt"
"fmt"
"fusenapi/constants"
"fusenapi/constants"
"fusenapi/initalize"
"fusenapi/initalize"
"fusenapi/model/gmodel"
"fusenapi/server/render/internal/svc"
"fusenapi/server/render/internal/svc"
"fusenapi/utils/curl"
"fusenapi/utils/curl"
"fusenapi/utils/file"
"fusenapi/utils/file"
@@ -16,7 +18,6 @@ import (
"gorm.io/gorm"
"gorm.io/gorm"
"io/ioutil"
"io/ioutil"
"strconv"
"strconv"
"strings"
)
)
// 这里请求的py接口返回数据
// 这里请求的py接口返回数据
@@ -47,7 +48,7 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error {
}
}
rabbitmq := initalize . RabbitMqHandle { }
rabbitmq := initalize . RabbitMqHandle { }
//获取模板( 产品第一个sku的模板)
//获取模板( 产品第一个sku的模板)
t emplateInfo , err := svcCtx . AllModels . FsProductTemplateV2 . FindOneByProductIdTagIdWithSizeTable ( ctx , parseInfo . RenderData . ProductId , fmt . Sprintf ( "%d" , parseInfo . RenderData . TemplateTagId ) )
productT emplate, err := svcCtx . AllModels . FsProductTemplateV2 . FindOneByProductIdTagIdWithSizeTable ( ctx , parseInfo . RenderData . ProductId , fmt . Sprintf ( "%d" , parseInfo . RenderData . TemplateTagId ) )
if err != nil {
if err != nil {
if errors . Is ( err , gorm . ErrRecordNotFound ) {
if errors . Is ( err , gorm . ErrRecordNotFound ) {
logx . Error ( "template info is not found" )
logx . Error ( "template info is not found" )
@@ -66,7 +67,7 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error {
}
}
//如果不存在,则请求生成刀版图
//如果不存在,则请求生成刀版图
if errors . Is ( err , gorm . ErrRecordNotFound ) {
if errors . Is ( err , gorm . ErrRecordNotFound ) {
combineImage , err = getCombineImage ( ctx , svcCtx , parseInfo , combineHash )
combineImage , err = getCombineImage ( ctx , svcCtx , parseInfo , productTemplate , combineHash )
if err != nil {
if err != nil {
return err
return err
}
}
@@ -74,10 +75,10 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error {
combineImage = * resource . ResourceUrl
combineImage = * resource . ResourceUrl
}
}
//获取渲染设置信息
//获取渲染设置信息
element , err := svcCtx . AllModels . FsProductTemplateElement . FindOneByModelId ( ctx , * t emplateInfo . ModelId )
element , err := svcCtx . AllModels . FsProductTemplateElement . FindOneByModelId ( ctx , * productT emplate. ModelId )
if err != nil {
if err != nil {
if errors . Is ( err , gorm . ErrRecordNotFound ) {
if errors . Is ( err , gorm . ErrRecordNotFound ) {
logx . Error ( "element info is not found,model_id = ?" , * t emplateInfo . ModelId )
logx . Error ( "element info is not found,model_id = ?" , * productT emplate. ModelId )
return nil
return nil
}
}
logx . Error ( "failed to get element list," , err )
logx . Error ( "failed to get element list," , err )
@@ -132,7 +133,7 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error {
"light" : * element . Light ,
"light" : * element . Light ,
"refletion" : refletion ,
"refletion" : refletion ,
"scale" : * element . Scale ,
"scale" : * element . Scale ,
"sku_id" : * templateInfo . ProductId ,
"sku_id" : parseInfo . RenderData . ProductId ,
"tid" : * element . Title ,
"tid" : * element . Title ,
"rotation" : * element . Rotation ,
"rotation" : * element . Rotation ,
"filePath" : "" , //todo 文件路径,针对千人千面
"filePath" : "" , //todo 文件路径,针对千人千面
@@ -160,14 +161,67 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error {
}
}
// 获取刀版图
// 获取刀版图
func getCombineImage ( ctx context . Context , svcCtx * svc . ServiceContext , parseInfo websocket_data . AssembleRenderData , combineHash string ) ( image string , err error ) {
func getCombineImage ( ctx context . Context , svcCtx * svc . ServiceContext , parseInfo websocket_data . AssembleRenderData , productTemplate * gmodel . FsProductTemplateV2 , combineHash string ) ( image string , err error ) {
// todo 获取sku对应用来合成刀版图的json数据
if productTemplate . TemplateInfo == nil || * productTemplate . TemplateInfo == "" {
logx . Error ( "product template info`template_info is empty" )
return "" , errors . New ( "product template info`template_info is empty" )
}
//反序列化替换其中一些参数
var combineInfo map [ string ] interface { }
if err = json . Unmarshal ( [ ] byte ( * productTemplate . TemplateInfo ) , & combineInfo ) ; err != nil {
logx . Error ( "failed to parse json:template_info:" , err )
return "" , err
}
//需要替换的参数
replaceData := map [ string ] interface { } {
"logo_url" : parseInfo . RenderData . Logo ,
"website" : "" ,
"slogan" : "" ,
"address" : "" ,
"phone" : "" ,
"colors" : [ ] string { } ,
"template_tagid" : [ ] string { } ,
"is_crop" : false ,
"shape" : "" ,
"ratio" : 0 ,
"line" : "" ,
"other" : "" ,
"other1" : "" ,
}
//获取用户素材信息
if parseInfo . RenderData . UserMaterialId > 0 {
userMaterial , err := svcCtx . AllModels . FsUserMaterial . FindOneById ( ctx , parseInfo . RenderData . UserMaterialId )
if err != nil {
if errors . Is ( err , gorm . ErrRecordNotFound ) {
logx . Error ( "user material not exists:" , parseInfo . RenderData . UserMaterialId )
return "" , errors . New ( "user material not exists" )
}
logx . Error ( "err failed to get user material info" )
}
if userMaterial . Metadata != nil && * userMaterial . Metadata != "" {
//解析元数据
var materialMetaData map [ string ] interface { }
if err = json . Unmarshal ( [ ] byte ( * userMaterial . Metadata ) , & materialMetaData ) ; err != nil {
logx . Error ( "failed to parse user material`matadata: " , err )
return "" , err
}
//赋值
replaceData [ "colors" ] = materialMetaData [ "colors" ]
replaceData [ "logo_url" ] = materialMetaData [ "logo_url" ]
replaceData [ "shape" ] = materialMetaData [ "shape" ]
replaceData [ "is_crop" ] = materialMetaData [ "is_crop" ]
replaceData [ "ratio" ] = materialMetaData [ "ratio" ]
replaceData [ "line" ] = materialMetaData [ "line" ]
replaceData [ "other" ] = materialMetaData [ "other" ]
replaceData [ "other1" ] = materialMetaData [ "other1" ]
}
}
combineInfo [ "param_data" ] = replaceData
postData , _ := json . Marshal ( combineInfo )
url := "http://192.168.1.7:45678/LogoCombine"
url := "http://192.168.1.7:45678/LogoCombine"
header := make ( map [ string ] string )
header := make ( map [ string ] string )
header [ "content-type" ] = "application/json"
header [ "content-type" ] = "application/json"
postData := "" // todo 请求数据要查出来
httpRsp , err := curl . ApiCall ( url , "POST" , header , bytes . NewReader ( postData ) , 20 )
httpRsp , err := curl . ApiCall ( url , "POST" , header , strings . NewReader ( postData ) , 20 )
if err != nil {
if err != nil {
logx . Error ( "failed to combine logo:" , err )
logx . Error ( "failed to combine logo:" , err )
return "" , err
return "" , err