2023-08-07 03:44:54 +00:00
|
|
|
|
package consumer
|
|
|
|
|
|
2023-08-07 05:20:18 +00:00
|
|
|
|
import (
|
2023-08-10 04:30:53 +00:00
|
|
|
|
"bytes"
|
2023-08-08 04:22:15 +00:00
|
|
|
|
"context"
|
2023-08-07 11:13:16 +00:00
|
|
|
|
"encoding/json"
|
2023-08-08 04:22:15 +00:00
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2023-08-08 10:35:29 +00:00
|
|
|
|
"fusenapi/constants"
|
|
|
|
|
"fusenapi/initalize"
|
2023-08-10 04:30:53 +00:00
|
|
|
|
"fusenapi/model/gmodel"
|
2023-08-09 11:34:11 +00:00
|
|
|
|
"fusenapi/server/render/internal/svc"
|
|
|
|
|
"fusenapi/utils/curl"
|
|
|
|
|
"fusenapi/utils/file"
|
|
|
|
|
"fusenapi/utils/hash"
|
2023-08-07 11:13:16 +00:00
|
|
|
|
"fusenapi/utils/websocket_data"
|
2023-08-07 05:20:18 +00:00
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
2023-08-08 04:22:15 +00:00
|
|
|
|
"gorm.io/gorm"
|
2023-08-09 11:34:11 +00:00
|
|
|
|
"io/ioutil"
|
2023-08-08 10:35:29 +00:00
|
|
|
|
"strconv"
|
2023-08-07 05:20:18 +00:00
|
|
|
|
)
|
2023-08-07 03:44:54 +00:00
|
|
|
|
|
2023-08-08 04:22:15 +00:00
|
|
|
|
// 这里请求的py接口返回数据
|
|
|
|
|
type pythonApiRsp struct {
|
2023-08-09 11:34:11 +00:00
|
|
|
|
Id string `json:"id"` //物料模板的id
|
|
|
|
|
LogoUrl string `json:"logo_url"` //logo地址
|
|
|
|
|
result string `json:"result"` //图片base64
|
2023-08-08 04:22:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 03:44:54 +00:00
|
|
|
|
// 消费渲染需要组装的数据
|
|
|
|
|
type MqConsumerRenderAssemble struct {
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-08 04:22:15 +00:00
|
|
|
|
func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error {
|
2023-08-07 05:20:18 +00:00
|
|
|
|
logx.Info("收到需要组装的消息:", string(data))
|
2023-08-07 11:13:16 +00:00
|
|
|
|
var parseInfo websocket_data.AssembleRenderData
|
|
|
|
|
if err := json.Unmarshal(data, &parseInfo); err != nil {
|
|
|
|
|
logx.Error("MqConsumerRenderAssemble数据格式错误:", err)
|
|
|
|
|
return nil //不返回错误就删除消息
|
|
|
|
|
}
|
2023-08-09 11:34:11 +00:00
|
|
|
|
val := ctx.Value("svcctx")
|
2023-08-08 04:22:15 +00:00
|
|
|
|
if val == nil {
|
2023-08-09 11:34:11 +00:00
|
|
|
|
return errors.New("svcctx is nil")
|
2023-08-08 04:22:15 +00:00
|
|
|
|
}
|
2023-08-09 11:34:11 +00:00
|
|
|
|
svcCtx, ok := val.(*svc.ServiceContext)
|
2023-08-08 04:22:15 +00:00
|
|
|
|
if !ok {
|
2023-08-09 11:34:11 +00:00
|
|
|
|
return errors.New("svcctx is nil!!")
|
2023-08-08 04:22:15 +00:00
|
|
|
|
}
|
2023-08-09 07:29:27 +00:00
|
|
|
|
rabbitmq := initalize.RabbitMqHandle{}
|
2023-08-09 09:11:45 +00:00
|
|
|
|
//获取模板(产品第一个sku的模板)
|
2023-08-10 04:30:53 +00:00
|
|
|
|
productTemplate, err := svcCtx.AllModels.FsProductTemplateV2.FindOneByProductIdTagIdWithSizeTable(ctx, parseInfo.RenderData.ProductId, fmt.Sprintf("%d", parseInfo.RenderData.TemplateTagId))
|
2023-08-08 04:22:15 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
logx.Error("template info is not found")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
logx.Error("failed to get template info:", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
2023-08-10 03:39:57 +00:00
|
|
|
|
combineImage := "" //刀版图
|
2023-08-09 11:34:11 +00:00
|
|
|
|
combineHash := hash.JsonHashKey(parseInfo) //区别于云渲染的taskid,这个用获取刀版图缓存
|
|
|
|
|
//获取该hash值下有没有对应的资源
|
|
|
|
|
resource, err := svcCtx.AllModels.FsResource.FindOneById(ctx, combineHash)
|
|
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
logx.Error("failed to get resource :", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
//如果不存在,则请求生成刀版图
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
2023-08-10 04:30:53 +00:00
|
|
|
|
combineImage, err = getCombineImage(ctx, svcCtx, parseInfo, productTemplate, combineHash)
|
2023-08-09 11:34:11 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
combineImage = *resource.ResourceUrl
|
|
|
|
|
}
|
2023-08-08 04:22:15 +00:00
|
|
|
|
//获取渲染设置信息
|
2023-08-10 04:30:53 +00:00
|
|
|
|
element, err := svcCtx.AllModels.FsProductTemplateElement.FindOneByModelId(ctx, *productTemplate.ModelId)
|
2023-08-08 10:35:29 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
2023-08-10 04:30:53 +00:00
|
|
|
|
logx.Error("element info is not found,model_id = ?", *productTemplate.ModelId)
|
2023-08-08 10:35:29 +00:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
logx.Error("failed to get element list,", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
//组装数据
|
|
|
|
|
refletion := -1
|
|
|
|
|
if element.Refletion != nil && *element.Refletion != "" {
|
|
|
|
|
refletion, err = strconv.Atoi(*element.Refletion)
|
|
|
|
|
}
|
|
|
|
|
//组装data数据
|
|
|
|
|
var mode map[string]interface{}
|
|
|
|
|
if element.Mode != nil && *element.Mode != "" {
|
|
|
|
|
if err = json.Unmarshal([]byte(*element.Mode), &mode); err != nil {
|
|
|
|
|
logx.Error("faile to parse element mode json:", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-09 02:15:22 +00:00
|
|
|
|
tempData := make([]map[string]interface{}, 0, 3)
|
2023-08-08 10:35:29 +00:00
|
|
|
|
if element.Base != nil && *element.Base != "" {
|
|
|
|
|
tempData = append(tempData, map[string]interface{}{
|
|
|
|
|
"name": "model",
|
2023-08-09 11:34:11 +00:00
|
|
|
|
"data": "0," + combineImage + "," + *element.Base,
|
2023-08-08 10:35:29 +00:00
|
|
|
|
"type": "other",
|
|
|
|
|
"layer": "0",
|
|
|
|
|
"is_update": 1,
|
|
|
|
|
"mode": mode["model"],
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
if element.Shadow != nil && *element.Shadow != "" {
|
|
|
|
|
tempData = append(tempData, map[string]interface{}{
|
|
|
|
|
"name": "shadow",
|
|
|
|
|
"data": *element.Shadow,
|
|
|
|
|
"type": "other",
|
|
|
|
|
"layer": "0",
|
|
|
|
|
"is_update": 0,
|
|
|
|
|
"mode": mode["shadow"],
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
if element.ModelP != nil && *element.ModelP != "" {
|
|
|
|
|
tempData = append(tempData, map[string]interface{}{
|
|
|
|
|
"name": "model_P",
|
|
|
|
|
"data": "0," + *element.ModelP,
|
|
|
|
|
"type": "other",
|
|
|
|
|
"layer": "0",
|
|
|
|
|
"is_update": 0,
|
|
|
|
|
"mode": mode["model_P"],
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
result := []interface{}{
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"light": *element.Light,
|
|
|
|
|
"refletion": refletion,
|
|
|
|
|
"scale": *element.Scale,
|
2023-08-10 04:30:53 +00:00
|
|
|
|
"sku_id": parseInfo.RenderData.ProductId,
|
2023-08-08 10:35:29 +00:00
|
|
|
|
"tid": *element.Title,
|
|
|
|
|
"rotation": *element.Rotation,
|
|
|
|
|
"filePath": "", //todo 文件路径,针对千人千面
|
|
|
|
|
"data": tempData,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
sendData := map[string]interface{}{
|
2023-08-09 06:12:58 +00:00
|
|
|
|
"id": parseInfo.TaskId,
|
2023-08-08 10:35:29 +00:00
|
|
|
|
"order_id": 0,
|
|
|
|
|
"user_id": parseInfo.RenderData.UserId,
|
2023-08-09 06:12:58 +00:00
|
|
|
|
"guest_id": parseInfo.RenderData.GuestId,
|
2023-08-08 10:35:29 +00:00
|
|
|
|
"sku_ids": []int64{parseInfo.RenderData.ProductId},
|
|
|
|
|
"tids": []string{*element.Title},
|
|
|
|
|
"data": result,
|
|
|
|
|
"is_thousand_face": 0,
|
|
|
|
|
"folder": "", //todo 千人千面需要使用
|
|
|
|
|
}
|
|
|
|
|
b, _ := json.Marshal(sendData)
|
|
|
|
|
if err = rabbitmq.SendMsg(constants.RABBIT_MQ_TO_UNITY, b); err != nil {
|
|
|
|
|
logx.Error("发送渲染组装数据到rabbitmq失败:", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
2023-08-09 08:58:08 +00:00
|
|
|
|
logx.Info("发送渲染组装数据到unity成功")
|
2023-08-07 03:44:54 +00:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-08-09 11:34:11 +00:00
|
|
|
|
|
|
|
|
|
// 获取刀版图
|
2023-08-10 04:30:53 +00:00
|
|
|
|
func getCombineImage(ctx context.Context, svcCtx *svc.ServiceContext, parseInfo websocket_data.AssembleRenderData, productTemplate *gmodel.FsProductTemplateV2, combineHash string) (image string, err error) {
|
|
|
|
|
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)
|
2023-08-09 11:34:11 +00:00
|
|
|
|
url := "http://192.168.1.7:45678/LogoCombine"
|
|
|
|
|
header := make(map[string]string)
|
|
|
|
|
header["content-type"] = "application/json"
|
2023-08-10 04:30:53 +00:00
|
|
|
|
httpRsp, err := curl.ApiCall(url, "POST", header, bytes.NewReader(postData), 20)
|
2023-08-09 11:34:11 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error("failed to combine logo:", err)
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
defer httpRsp.Body.Close()
|
|
|
|
|
bytes, err := ioutil.ReadAll(httpRsp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error("failed to read python api rsp body:", err)
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
var pythonApiInfo pythonApiRsp
|
|
|
|
|
if err = json.Unmarshal(bytes, &pythonApiInfo); err != nil {
|
|
|
|
|
logx.Error("failed to parse python api rsp:", err)
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
//上传刀版图
|
|
|
|
|
var upload = file.Upload{
|
|
|
|
|
Ctx: ctx,
|
|
|
|
|
MysqlConn: svcCtx.MysqlConn,
|
|
|
|
|
AwsSession: svcCtx.AwsSession,
|
|
|
|
|
}
|
|
|
|
|
uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{
|
|
|
|
|
FileHash: combineHash,
|
|
|
|
|
FileData: pythonApiInfo.result,
|
|
|
|
|
UploadBucket: 1,
|
|
|
|
|
ApiType: 2,
|
|
|
|
|
UserId: parseInfo.RenderData.UserId,
|
|
|
|
|
GuestId: parseInfo.RenderData.GuestId,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error("上传刀版图到s3失败:", err)
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return uploadRes.ResourceUrl, nil
|
|
|
|
|
}
|