This commit is contained in:
laodaming 2023-08-08 18:35:29 +08:00
parent 2f1ef81263
commit 5faeacd472
4 changed files with 128 additions and 100 deletions

View File

@ -8,4 +8,13 @@ const (
RABBIT_MQ_ASSEMBLE_RENDER_DATA RABBIT_MQ = "RABBIT_MQ_ASSEMBLE_RENDER_DATA"
//渲染结果数据队列
RABBIT_MQ_RENDER_RESULT_DATA RABBIT_MQ = "RABBIT_MQ_RENDER_RESULT_DATA"
//原来发送到unity渲染的队列
RABBIT_MQ_TO_UNITY RABBIT_MQ = "newTaskQueue"
)
// 队列列表
var MqQueueArr = []RABBIT_MQ{
RABBIT_MQ_ASSEMBLE_RENDER_DATA,
RABBIT_MQ_RENDER_RESULT_DATA,
RABBIT_MQ_TO_UNITY,
}

View File

@ -21,12 +21,6 @@ type queueItem struct {
queue amqp.Queue
}
// 队列列表
var mqQueueArr = []constants.RABBIT_MQ{
constants.RABBIT_MQ_ASSEMBLE_RENDER_DATA,
constants.RABBIT_MQ_RENDER_RESULT_DATA,
}
// 存储连接
var mapMq = make(map[constants.RABBIT_MQ]queueItem)
@ -44,7 +38,7 @@ func InitRabbitMq(url string, config *tls.Config) *RabbitMqHandle {
log.Fatalf("Failed to open a channel: %v", err)
}
//声明队列
for _, queueName := range mqQueueArr {
for _, queueName := range constants.MqQueueArr {
q, err := ch.QueueDeclare(
string(queueName), // 队列名
true, // 是否持久化

View File

@ -1,3 +1,13 @@
package gmodel
import "context"
// TODO: 使用model的属性做你想做的
func (e *FsProductTemplateElementModel) FindOneByModelId(ctx context.Context, modelId int64) (resp *FsProductTemplateElement, err error) {
err = e.db.WithContext(ctx).Model(&FsProductTemplateElement{}).
//以前的神仙员工把表model_id变成product_template_id
Where("`product_template_id` = ?", modelId).
Take(&resp).Error
return resp, err
}

View File

@ -6,12 +6,15 @@ import (
"encoding/json"
"errors"
"fmt"
"fusenapi/constants"
"fusenapi/initalize"
"fusenapi/model/gmodel"
"fusenapi/utils/websocket_data"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
"io/ioutil"
"net/http"
"strconv"
"time"
)
@ -102,6 +105,10 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error {
logx.Error("python api 接口没有数据:")
return err
}
mapImageData := make(map[int64]int)
for k, v := range rspInfo.Data {
mapImageData[v.Tid] = k
}
//云渲染日志
title = "2-请求->接收python合成刀版图接口"
now = time.Now().Unix()
@ -140,99 +147,107 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error {
if err != nil {
logx.Error(err)
}
timePinjieBegin := time.Now().UnixMilli()
//获取渲染设置信息
//element, err := allmodels.FsProductTemplateElement
/*
$element = ProductTemplateElement::find()
->andFilterWhere(['in', 'product_template_id', $mids])
->asArray()
->all();
$element = array_column($element, null, 'product_template_id');
$elementTitles = array_column($element, 'title');
$result = [];
$time_pinjie_begin = $render->getMillisecond();
foreach ($templates as $key => $val) {
if(!isset($element[$val['model_id']]) || !isset($imageData[$val['id']])){
continue;
}
//数据拼装
$item = [];
$item['light'] = $element[$val['model_id']]['light'];
$item['refletion'] = $element[$val['model_id']]['refletion'] == '' ? -1 : (int)$element[$val['model_id']]['refletion'];
$item['scale'] = $element[$val['model_id']]['scale'];
$item['sku_id'] = $val['product_id'];
$item['tid'] = $element[$val['model_id']]['title'];
$item['rotation'] = $element[$val['model_id']]['rotation'];
$item['filePath'] = '';//todo 文件路径,针对千人千面
//组装data数据
$tempData = [];
//获取材质模式对应关系
$mode = $element[$val['model_id']]['mode'] ? json_decode($element[$val['model_id']]['mode'], true) : [];
// $base_img = (new ImageService())->base64EncodeImageNoHeader(\Yii::$app->params['baseurl'].$imageData[$val['id']]['imgurl']);
$base_img = \Yii::$app->params['h5Url'].'/storage'.$imageData[$val['id']]['imgurl'];
//判断是否包含base数据 即对应建模那边的model
if($element[$val['model_id']]['base']){
$tempData[] = [
'name' => 'model',
'data' => '0,'.$base_img.','.$element[$val['model_id']]['base'],
'type' => 'other',
'layer' => '0',
'is_update' => 1,
'mode' => $mode['model'],
];
}
if($element[$val['model_id']]['shadow']){
$tempData[] = [
'name' => 'shadow',
'data' => $element[$val['model_id']]['shadow'],
'type' => 'other',
'layer' => '0',
'is_update' => 0,
'mode' => $mode['shadow'],
];
}
if($element[$val['model_id']]['model_p']){
$tempData[] = [
'name' => 'model_P',
'data' => '0,'.$element[$val['model_id']]['model_p'],
'type' => 'other',
'layer' => '0',
'is_update' => 0,
'mode' => $mode['model_P'],
];
}
$item['data'] = $tempData;
$result[] = $item;
}
$log = new CloudRenderLog();
$log->title = '接收到python刀版图 -> 3-组装MQ渲染任务队列';
$log->time = $render->getMillisecond() - $time_pinjie_begin;
$log->user_id = $user_id;
$log->post_data = '';
$log->post_url = '';
$log->result = $res;
$log->tag = $inputData['id'];
$log->ctime = time();
$log->save(false);
}
$sendData = [
'id' => $inputData['id'],
'order_id' => 0,
'user_id' => \Yii::$app->user->id,
'sku_ids' => $inputData['sku_ids'],
'tids' => $elementTitles,
'data' => $result,
'is_thousand_face' => 0,
'folder' => '',//todo 千人千面需要使用
];
return $sendData;*/
element, err := allmodels.FsProductTemplateElement.FindOneByModelId(ctx, *templateInfo.ModelId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
logx.Error("element info is not found,model_id = ?", *templateInfo.ModelId)
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
}
}
baseImage := ""
if index, ok := mapImageData[templateInfo.Id]; ok {
baseImage = constants.H5_URL + "/storage" + rspInfo.Data[index].Imgurl
}
tempData := []map[string]interface{}{}
if element.Base != nil && *element.Base != "" {
tempData = append(tempData, map[string]interface{}{
"name": "model",
"data": "0," + baseImage + "," + *element.Base,
"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,
"sku_id": *templateInfo.ProductId,
"tid": *element.Title,
"rotation": *element.Rotation,
"filePath": "", //todo 文件路径,针对千人千面
"data": tempData,
},
}
timePinjie := time.Now().UnixMilli() - timePinjieBegin
//云渲染日志
title = "接收到python刀版图 -> 3-组装MQ渲染任务队列"
now = time.Now().Unix()
err = allmodels.FsCloudRenderLog.Create(ctx, &gmodel.FsCloudRenderLog{
UserId: &parseInfo.RenderData.UserId,
Title: &title,
Time: &timePinjie,
Tag: &parseInfo.RenderId,
Ctime: &now,
})
if err != nil {
logx.Error(err)
}
sendData := map[string]interface{}{
"id": parseInfo.RenderId,
"order_id": 0,
"user_id": parseInfo.RenderData.UserId,
"sku_ids": []int64{parseInfo.RenderData.ProductId},
"tids": []string{*element.Title},
"data": result,
"is_thousand_face": 0,
"folder": "", //todo 千人千面需要使用
}
b, _ := json.Marshal(sendData)
rabbitmq := initalize.RabbitMqHandle{}
if err = rabbitmq.SendMsg(constants.RABBIT_MQ_TO_UNITY, b); err != nil {
logx.Error("发送渲染组装数据到rabbitmq失败", err)
return err
}
logx.Info("发送渲染组装数据到rabbitmq 成功")
return nil
}