This commit is contained in:
laodaming 2023-08-24 18:02:00 +08:00
parent 4a41cb77e9
commit 04ffa3cf9c

View File

@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"fusenapi/constants"
"fusenapi/model/gmodel"
"fusenapi/service/repositories"
"fusenapi/utils/curl"
"fusenapi/utils/hash"
@ -102,15 +103,56 @@ func (w *wsConnectItem) consumeRenderCache(data []byte) {
//用户id赋值
renderImageData.RenderData.UserId = w.userId
renderImageData.RenderData.GuestId = w.guestId
//生成任务id(需要把user_id,guest_id设为0)
hashVal := renderImageData.RenderData
hashVal.UserId = 0
hashVal.GuestId = 0
hashByte, _ := json.Marshal(hashVal)
var hashData map[string]interface{}
_ = json.Unmarshal(hashByte, &hashData)
taskId := hash.JsonHashKey(hashData)
//获取产品第一个尺寸
productFirstSize, err := w.logic.svcCtx.AllModels.FsProductSize.GetProductFirstSize(w.logic.ctx, renderImageData.RenderData.ProductId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "product first size is not exists", w.userId, w.guestId, 0, 0, 0)
logx.Error("product first size is not found")
return
}
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "failed to get product first size", w.userId, w.guestId, 0, 0, 0)
logx.Error("failed to get product first size:", err)
return
}
//获取模型(只是获取id)
model3dInfo, err := w.logic.svcCtx.AllModels.FsProductModel3d.GetOneBySizeIdTag(w.logic.ctx, productFirstSize.Id, constants.TAG_MODEL, "id")
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "product model is not exists", w.userId, w.guestId, 0, 0, productFirstSize.Id)
logx.Error("product model is not found")
return
}
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "failed to get product model", w.userId, w.guestId, 0, 0, productFirstSize.Id)
logx.Error("failed to get product model:", err)
return
}
//获取模板
productTemplate, err := w.logic.svcCtx.AllModels.FsProductTemplateV2.FindFirstOneByProductIdModelIdTemplateTag(w.logic.ctx, renderImageData.RenderData.ProductId, model3dInfo.Id, renderImageData.RenderData.TemplateTag)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "product template is not exists", w.userId, w.guestId, 0, model3dInfo.Id, productFirstSize.Id)
logx.Error("template info is not found")
return
}
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "failed to get product template", w.userId, w.guestId, 0, model3dInfo.Id, productFirstSize.Id)
logx.Error("failed to get template info:", err)
return
}
//获取渲染设置信息
element, err := w.logic.svcCtx.AllModels.FsProductTemplateElement.FindOneByModelId(w.logic.ctx, *productTemplate.ModelId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "render element is not exists", w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productFirstSize.Id)
logx.Error("element info is not found,model_id = ", *productTemplate.ModelId)
return
}
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "failed to get render element", w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productFirstSize.Id)
logx.Error("failed to get element list,", err)
return
}
//获取唯一id
taskId := w.genRenderTaskId(renderImageData, model3dInfo, productTemplate, element)
//查询有没有缓存的资源,有就返回######################
resource, err := w.logic.svcCtx.AllModels.FsResource.FindOneById(w.logic.ctx, taskId)
if err != nil {
@ -139,50 +181,40 @@ func (w *wsConnectItem) consumeRenderCache(data []byte) {
RenderId: renderImageData.RenderId,
})
//组装数据
if err = w.assembleRenderData(taskId, renderImageData); err != nil {
if err = w.assembleRenderData(taskId, renderImageData, productTemplate, model3dInfo, element, productFirstSize); err != nil {
logx.Error("组装数据失败:", err)
return
}
}
// 组装渲染任务id
func (w *wsConnectItem) genRenderTaskId(renderImageData websocket_data.RenderImageReqMsg, model3dInfo *gmodel.FsProductModel3d, productTemplate *gmodel.FsProductTemplateV2, element *gmodel.FsProductTemplateElement) string {
//生成任务id(需要把user_id,guest_id设为0)
incomeHashParam := renderImageData.RenderData
incomeHashParam.UserId = 0 //设为0(渲染跟用户id无关)
incomeHashParam.GuestId = 0 //设为0(渲染跟用户id无关)
incomeHashBytes, _ := json.Marshal(incomeHashParam)
modelHashStr := ""
templateHashStr := ""
if model3dInfo.ModelInfo != nil {
modelHashStr = *model3dInfo.ModelInfo
}
if productTemplate.TemplateInfo != nil {
templateHashStr = *productTemplate.TemplateInfo
}
elementHashBytes, _ := json.Marshal(element)
hashMap := map[string]interface{}{
"income_param": incomeHashBytes,
"model_info": modelHashStr,
"template_info": templateHashStr,
"render_element": elementHashBytes,
}
return hash.JsonHashKey(hashMap)
}
// 组装数据发送给unity
func (w *wsConnectItem) assembleRenderData(taskId string, info websocket_data.RenderImageReqMsg) error {
//获取产品第一个尺寸
productFirstSize, err := w.logic.svcCtx.AllModels.FsProductSize.GetProductFirstSize(w.logic.ctx, info.RenderData.ProductId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "product first size is not exists", w.userId, w.guestId, 0, 0, 0)
logx.Error("product first size is not found")
return err
}
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "failed to get product first size", w.userId, w.guestId, 0, 0, 0)
logx.Error("failed to get product first size:", err)
return err
}
//获取模型(只是获取id)
model3dInfo, err := w.logic.svcCtx.AllModels.FsProductModel3d.GetOneBySizeIdTag(w.logic.ctx, productFirstSize.Id, constants.TAG_MODEL, "id")
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "product model is not exists", w.userId, w.guestId, 0, 0, productFirstSize.Id)
logx.Error("product model is not found")
return err
}
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "failed to get product model", w.userId, w.guestId, 0, 0, productFirstSize.Id)
logx.Error("failed to get product model:", err)
return err
}
//获取模板
productTemplate, err := w.logic.svcCtx.AllModels.FsProductTemplateV2.FindFirstOneByProductIdModelIdTemplateTag(w.logic.ctx, info.RenderData.ProductId, model3dInfo.Id, info.RenderData.TemplateTag)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "product template is not exists", w.userId, w.guestId, 0, model3dInfo.Id, productFirstSize.Id)
logx.Error("template info is not found")
return err
}
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "failed to get product template", w.userId, w.guestId, 0, model3dInfo.Id, productFirstSize.Id)
logx.Error("failed to get template info:", err)
return err
}
func (w *wsConnectItem) assembleRenderData(taskId string, info websocket_data.RenderImageReqMsg, productTemplate *gmodel.FsProductTemplateV2, model3dInfo *gmodel.FsProductModel3d, element *gmodel.FsProductTemplateElement, productFirstSize *gmodel.FsProductSize) error {
//记录刀版图合成开始时间
w.modifyRenderTaskTimeConsuming(renderImageControlChanItem{
Option: 2,
@ -225,18 +257,6 @@ func (w *wsConnectItem) assembleRenderData(taskId string, info websocket_data.Re
},
})
logx.Info("合成刀版图成功,合成刀版图数据:", combineReq, ",logo图片:", info.RenderData.Logo, " 刀版图:", *res.ResourceUrl)
//获取渲染设置信息
element, err := w.logic.svcCtx.AllModels.FsProductTemplateElement.FindOneByModelId(w.logic.ctx, *productTemplate.ModelId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "render element is not exists", w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productFirstSize.Id)
logx.Error("element info is not found,model_id = ", *productTemplate.ModelId)
return err
}
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "failed to get render element", w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productFirstSize.Id)
logx.Error("failed to get element list,", err)
return err
}
//组装数据
refletion := -1
if element.Refletion != nil && *element.Refletion != "" {