package consumer import ( "context" "encoding/json" "errors" "fmt" "fusenapi/constants" "fusenapi/initalize" "fusenapi/server/render/internal/svc" "fusenapi/service/repositories" "fusenapi/utils/hash" "fusenapi/utils/websocket_data" "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" "strconv" "time" ) // 这里请求的py接口返回数据 type pythonApiRsp struct { Id string `json:"id"` //物料模板的id LogoUrl string `json:"logo_url"` //logo地址 Result string `json:"result"` //图片base64 } // 消费渲染需要组装的数据 type MqConsumerRenderAssemble struct { } func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error { defer func() { if err := recover(); err != nil { logx.Error("MqConsumerRenderAssemble panic:", err) } }() logx.Info("收到需要组装的消息:", string(data)) var parseInfo websocket_data.AssembleRenderData if err := json.Unmarshal(data, &parseInfo); err != nil { logx.Error("MqConsumerRenderAssemble数据格式错误:", err) return nil //不返回错误就删除消息 } val := ctx.Value("svcctx") if val == nil { logx.Error("svcctx is nil") return nil //不返回错误就删除消息 } svcCtx, ok := val.(*svc.ServiceContext) if !ok { logx.Error("svcctx is nil!!") return nil //不返回错误就删除消息 } rabbitmq := initalize.RabbitMqHandle{} //获取模板(模板标签下的对一个物料的的模板) productTemplate, err := svcCtx.AllModels.FsProductTemplateV2.FindOneByProductIdTagIdWithSizeTable(ctx, parseInfo.RenderData.ProductId, fmt.Sprintf("%d", parseInfo.RenderData.TemplateTagId)) 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 nil //不返回错误就删除消息 } time.Now().UTC() resourceKey := hash.JsonHashKey(parseInfo) combineParam := map[string]interface{}{ "logo_url": parseInfo.RenderData.Logo, "website": "", "slogan": "", "address": "", "phone": "", "colors": []string{}, "template_tagid": "b1a", "is_crop": false, "shape": "rectangle", "ratio": 0, "line": "", "other": "", "other1": "", } combineParamBytes, _ := json.Marshal(combineParam) //获取刀版图 res, err := svcCtx.Repositories.ImageHandle.LogoCombine(ctx, &repositories.LogoCombineReq{ ResourceKey: resourceKey, TemplateId: productTemplate.Id, CombineParam: string(combineParamBytes), UserId: parseInfo.RenderData.UserId, GuestId: parseInfo.RenderData.GuestId, }) if err != nil { logx.Error("合成刀版图失败:", err) return nil } combineImage := "" //刀版图 if res != nil && res.ResourceUrl != nil { combineImage = *res.ResourceUrl } else { logx.Error("合成刀版图失败,合成的刀版图是空指针:", err) return nil } logx.Info("合成刀版图成功") //获取渲染设置信息 element, err := svcCtx.AllModels.FsProductTemplateElement.FindOneByModelId(ctx, *productTemplate.ModelId) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { logx.Error("element info is not found,model_id = ?", *productTemplate.ModelId) return nil //不返回错误就删除消息 } logx.Error("failed to get element list,", err) return nil //不返回错误就删除消息 } //组装数据 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 nil //不返回错误就删除消息 } } tempData := make([]map[string]interface{}, 0, 3) if element.Base != nil && *element.Base != "" { tempData = append(tempData, map[string]interface{}{ "name": "model", "data": "0," + combineImage + "," + *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": parseInfo.RenderData.ProductId, "tid": *element.Title, "rotation": *element.Rotation, "filePath": "", //todo 文件路径,针对千人千面 "data": tempData, }, } sendData := map[string]interface{}{ "id": parseInfo.TaskId, "order_id": 0, "user_id": parseInfo.RenderData.UserId, "guest_id": parseInfo.RenderData.GuestId, "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 nil //不返回错误就删除消息 } logx.Info("发送渲染组装数据到unity成功") return nil }