diff --git a/initalize/service.go b/initalize/service.go index 7e486360..b503c6f1 100644 --- a/initalize/service.go +++ b/initalize/service.go @@ -9,6 +9,7 @@ import ( type Repositories struct { ImageHandle repositories.ImageHandle + NewResource repositories.Resource } type NewAllRepositorieData struct { @@ -20,5 +21,6 @@ type NewAllRepositorieData struct { func NewAllRepositories(newData *NewAllRepositorieData) *Repositories { return &Repositories{ ImageHandle: repositories.NewImageHandle(newData.GormDB, newData.BLMServiceUrl, newData.AwsSession), + NewResource: repositories.NewResource(newData.GormDB, newData.BLMServiceUrl, newData.AwsSession), } } diff --git a/model/gmodel/fs_product_template_v2_logic.go b/model/gmodel/fs_product_template_v2_logic.go index 5a51fd2a..a277bf80 100755 --- a/model/gmodel/fs_product_template_v2_logic.go +++ b/model/gmodel/fs_product_template_v2_logic.go @@ -117,7 +117,7 @@ func (t *FsProductTemplateV2Model) GetProductTemplateListByParams(ctx context.Co func (t *FsProductTemplateV2Model) FindFirstOneCloudRenderByProductIdModelIdTemplateTag(ctx context.Context, productId, modelId int64, templateTag string) (resp *FsProductTemplateV2, err error) { err = t.db.WithContext(ctx).Model(&FsProductTemplateV2{}). Where("product_id = ? and model_id = ? and template_tag = ? ", productId, modelId, templateTag). - Where("status = ? and is_del = ? and element_model_id != ?", 1, 0, 0). + Where("status = ? and is_del = ?", 1, 0). Order("sort ASC"). Take(&resp).Error return resp, err diff --git a/server/resource/internal/logic/logoresizelogic.go b/server/resource/internal/logic/logoresizelogic.go index 0f32f469..a1b128e6 100644 --- a/server/resource/internal/logic/logoresizelogic.go +++ b/server/resource/internal/logic/logoresizelogic.go @@ -3,7 +3,9 @@ package logic import ( "bytes" "encoding/json" + "fmt" "fusenapi/model/gmodel" + "fusenapi/service/repositories" "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/file" @@ -96,6 +98,7 @@ func (l *LogoResizeLogic) LogoResize(req *types.LogoResizeReq, userinfo *auth.Us hashKeyDataB, _ := json.Marshal(req) json.Unmarshal(hashKeyDataB, &hashKeyDataMap) var resourceId string = hash.JsonHashKey(hashKeyDataMap) + fmt.Println(resourceId) // 上传文件 var upload = file.Upload{ @@ -116,10 +119,28 @@ func (l *LogoResizeLogic) LogoResize(req *types.LogoResizeReq, userinfo *auth.Us logx.Errorf("upload UploadFileByByte err: %v", err) return resp.SetStatus(basic.CodeFileNoFoundErr) } + metadataChild := make(map[string]interface{}, 1) + metadataChildData := make(map[string]interface{}, 1) + metadataChildKey := fmt.Sprintf("%d*%d", req.Width, req.Height) + metadataChildData[metadataChildKey] = repositories.Cropping{ + ResourceId: uploadRes.ResourceId, + ResourceUrl: uploadRes.ResourceUrl, + Width: req.Width, + Height: req.Height, + } + metadataChild["cropping"] = metadataChildData // 原图metadata 更新 + _, err = l.svcCtx.Repositories.NewResource.UpdateMetadata(l.ctx, &repositories.UpdateMetadataReq{ + ResourceId: req.ResourceId, + MetadataChild: metadataChild, + }) - // 返回成功的响应和上传URL + if err != nil { + return resp.SetStatus(basic.CodeServiceErr, "原图metadata更新失败") + } + + // 返回成功的响应 return resp.SetStatus(basic.CodeOK, map[string]interface{}{ "resource_id": uploadRes.ResourceId, "resource_url": uploadRes.ResourceUrl, diff --git a/server/websocket/internal/logic/ws_render_image.go b/server/websocket/internal/logic/ws_render_image.go index 9948c7a8..39ab91bc 100644 --- a/server/websocket/internal/logic/ws_render_image.go +++ b/server/websocket/internal/logic/ws_render_image.go @@ -92,7 +92,7 @@ func (w *wsConnectItem) renderImage(data []byte) { userMaterial, err := w.logic.svcCtx.AllModels.FsUserMaterial.FindLatestOne(w.logic.ctx, w.userId, w.guestId) if err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { - w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "failed to get user logo", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0) + w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取用户上传logo素材失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0) logx.Error("failed to get user logo") return } @@ -100,10 +100,10 @@ func (w *wsConnectItem) renderImage(data []byte) { userMaterialDefault, err := w.logic.svcCtx.AllModels.FsUserMaterial.FindOneById(w.logic.ctx, 0) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { - w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "default logo is not exists", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0) + w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "默认logo不存在", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0) return } - w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "failed to get default logo", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0) + w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取默认logo失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0) logx.Error("default logo is not exists") return } @@ -123,11 +123,11 @@ func (w *wsConnectItem) renderImage(data []byte) { } if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { - w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "product first size is not exists", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, renderImageData.RenderData.ProductSizeId, 0) + w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "找不到产品的第一个尺寸", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, renderImageData.RenderData.ProductSizeId, 0) logx.Error("product size is not found") return } - w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "failed to get product first size", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, renderImageData.RenderData.ProductSizeId, 0) + w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取产品的第一个尺寸失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, renderImageData.RenderData.ProductSizeId, 0) logx.Error("failed to get product size:", err) return } @@ -135,11 +135,11 @@ func (w *wsConnectItem) renderImage(data []byte) { model3dInfo, err := w.logic.svcCtx.AllModels.FsProductModel3d.GetOneBySizeIdTag(w.logic.ctx, productSize.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", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, productSize.Id, 0) + w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "产品第一个尺寸对应的模型不存在", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, productSize.Id, 0) logx.Error("product model is not found") return } - w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "failed to get product model", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, productSize.Id, 0) + w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取产品第一个尺寸对应的模型失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, productSize.Id, 0) logx.Error("failed to get product model:", err) return } @@ -147,23 +147,28 @@ func (w *wsConnectItem) renderImage(data []byte) { productTemplate, err := w.logic.svcCtx.AllModels.FsProductTemplateV2.FindFirstOneCloudRenderByProductIdModelIdTemplateTag(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", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, model3dInfo.Id, productSize.Id, 0) + w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "找不到对应模板", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, model3dInfo.Id, productSize.Id, 0) logx.Error("template info is not found") return } - w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "failed to get product template", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, model3dInfo.Id, productSize.Id, 0) + w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取对应模板失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, model3dInfo.Id, productSize.Id, 0) logx.Error("failed to get template info:", err) return } + //如果未开启云渲染 + if *productTemplate.ElementModelId <= 0 { + w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "模板未开启云渲染", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId) + return + } //获取渲染设置信息 element, err := w.logic.svcCtx.AllModels.FsProductTemplateElement.FindOneByModelId(w.logic.ctx, *productTemplate.ElementModelId) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { - w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "render element is not exists", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId) + w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "云渲染设置不存在", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId) logx.Error("element info is not found,element_model_id = ", 0) return } - w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "failed to get render element", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId) + w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取云渲染设置失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId) logx.Error("failed to get element ,", err) return } @@ -182,7 +187,7 @@ func (w *wsConnectItem) renderImage(data []byte) { } res, err := w.logic.svcCtx.Repositories.ImageHandle.LogoCombine(w.logic.ctx, &combineReq) if err != nil { - w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "failed to combine image:"+err.Error(), renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId) + w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "合成刀版图失败:"+err.Error(), renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId) logx.Error("合成刀版图失败,合成请求数据:", combineReq, "错误信息:", err) return } @@ -190,7 +195,7 @@ func (w *wsConnectItem) renderImage(data []byte) { if res != nil && res.ResourceUrl != nil { combineImage = *res.ResourceUrl } else { - w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "combine image is empty", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId) + w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "合成的刀版图是空的地址", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId) logx.Error("合成刀版图失败,合成的刀版图是空指针:", err) return } @@ -207,7 +212,7 @@ func (w *wsConnectItem) renderImage(data []byte) { resource, err := w.logic.svcCtx.AllModels.FsResource.FindOneById(w.logic.ctx, taskId) if err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { - w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, taskId, "failed to get render cache", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId) + w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, taskId, "获取unity云渲染缓存失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId) logx.Error("failed to find render resource:", err) return } @@ -258,7 +263,7 @@ func (w *wsConnectItem) assembleRenderDataToUnity(taskId string, combineImage st refletion, err = strconv.Atoi(*element.Refletion) if err != nil { logx.Error("err refletion:set default -1") - w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "parse element.Refletion from string to number err", info.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId) + w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "解析云渲染设置,把Refletion字段值从字符串转数字失败", info.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId) return err } } @@ -267,7 +272,7 @@ func (w *wsConnectItem) assembleRenderDataToUnity(taskId string, combineImage st 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) - w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "parse element.Mode err", info.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId) + w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "解析云渲染设置字段 mode 为map对象失败", info.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId) return err } } @@ -342,7 +347,7 @@ func (w *wsConnectItem) assembleRenderDataToUnity(taskId string, combineImage st unityRenderBeginTime := time.Now().UTC().UnixMilli() _, err = curl.ApiCall(url, "POST", header, bytes.NewReader(postDataBytes), time.Second*10) if err != nil { - w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "request unity api err", info.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId) + w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "请求unity接口失败", info.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId) logx.Error("failed to send data to unity") return err } @@ -480,69 +485,72 @@ func (w *wsConnectItem) operationRenderTask() { case data := <-w.extendRenderProperty.renderImageTaskCtlChan: switch data.option { case 0: //渲染结果回调,删除任务 - //存在任务,则发送渲染结果给前端 - if taskData, ok := w.extendRenderProperty.renderImageTask[data.taskId]; ok { - CombineTakesTime := "0ms" - UnityRenderTakesTime := "0ms" - uploadCombineImageTakesTime := "0ms" - uploadUnityRenderImageTakesTime := "0ms" - //合图时间 - if taskData.combineTakesTime > 0 { - CombineTakesTime = fmt.Sprintf("%dms", taskData.combineTakesTime) - } - //上传刀版图时间 - if taskData.uploadCombineImageTakesTime > 0 { - uploadCombineImageTakesTime = fmt.Sprintf("%dms", taskData.uploadCombineImageTakesTime) - } - //unity渲染时间 - if taskData.unityRenderBeginTime > 0 && taskData.unityRenderEndTime > 0 { - UnityRenderTakesTime = fmt.Sprintf("%dms", taskData.unityRenderEndTime-taskData.unityRenderBeginTime) - } - //上传unity渲染图耗时 - if taskData.uploadUnityRenderImageTakesTime > 0 { - uploadUnityRenderImageTakesTime = fmt.Sprintf("%dms", taskData.uploadUnityRenderImageTakesTime) - } - //发送到出口 - w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE, websocket_data.RenderImageRspMsg{ - RenderId: taskData.renderId, - Image: data.renderNotifyImageUrl, - RenderProcessTime: websocket_data.RenderProcessTime{ - CombineTakesTime: CombineTakesTime, - UnityRenderTakesTime: UnityRenderTakesTime, - UploadCombineImageTakesTime: uploadCombineImageTakesTime, - UploadUnityRenderImageTakesTime: uploadUnityRenderImageTakesTime, - }, - })) + taskData, ok := w.extendRenderProperty.renderImageTask[data.taskId] + if !ok { + continue } + //存在任务,则发送渲染结果给前端 + CombineTakesTime := "0ms" + UnityRenderTakesTime := "0ms" + uploadCombineImageTakesTime := "0ms" + uploadUnityRenderImageTakesTime := "0ms" + //合图时间 + if taskData.combineTakesTime > 0 { + CombineTakesTime = fmt.Sprintf("%dms", taskData.combineTakesTime) + } + //上传刀版图时间 + if taskData.uploadCombineImageTakesTime > 0 { + uploadCombineImageTakesTime = fmt.Sprintf("%dms", taskData.uploadCombineImageTakesTime) + } + //unity渲染时间 + if taskData.unityRenderBeginTime > 0 && taskData.unityRenderEndTime > 0 { + UnityRenderTakesTime = fmt.Sprintf("%dms", taskData.unityRenderEndTime-taskData.unityRenderBeginTime) + } + //上传unity渲染图耗时 + if taskData.uploadUnityRenderImageTakesTime > 0 { + uploadUnityRenderImageTakesTime = fmt.Sprintf("%dms", taskData.uploadUnityRenderImageTakesTime) + } + //发送到出口 + w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE, websocket_data.RenderImageRspMsg{ + RenderId: taskData.renderId, + Image: data.renderNotifyImageUrl, + RenderProcessTime: websocket_data.RenderProcessTime{ + CombineTakesTime: CombineTakesTime, + UnityRenderTakesTime: UnityRenderTakesTime, + UploadCombineImageTakesTime: uploadCombineImageTakesTime, + UploadUnityRenderImageTakesTime: uploadUnityRenderImageTakesTime, + }, + })) //删除任务 delete(w.extendRenderProperty.renderImageTask, data.taskId) case 1: //新增任务 w.extendRenderProperty.renderImageTask[data.taskId] = &renderTask{ renderId: data.renderId, } - case 2: //修改任务属性 - if taskData, ok := w.extendRenderProperty.renderImageTask[data.taskId]; ok { - //合图耗时 - if data.taskProperty.combineTakesTime != 0 { - taskData.combineTakesTime = data.taskProperty.combineTakesTime - } - //上传合图耗时 - if data.taskProperty.uploadCombineImageTakesTime != 0 { - taskData.uploadCombineImageTakesTime = data.taskProperty.uploadCombineImageTakesTime - } - //上传渲染结果图耗时 - if data.taskProperty.uploadUnityRenderImageTakesTime != 0 { - taskData.uploadUnityRenderImageTakesTime = data.taskProperty.uploadUnityRenderImageTakesTime - } - //发送unity时间 - if data.taskProperty.unityRenderBeginTime != 0 { - taskData.unityRenderBeginTime = data.taskProperty.unityRenderBeginTime - } - //收到unity返回的时间 - if data.taskProperty.unityRenderEndTime != 0 { - taskData.unityRenderEndTime = data.taskProperty.unityRenderEndTime - } + taskData, ok := w.extendRenderProperty.renderImageTask[data.taskId] + if !ok { + continue + } + //合图耗时 + if data.taskProperty.combineTakesTime != 0 { + taskData.combineTakesTime = data.taskProperty.combineTakesTime + } + //上传合图耗时 + if data.taskProperty.uploadCombineImageTakesTime != 0 { + taskData.uploadCombineImageTakesTime = data.taskProperty.uploadCombineImageTakesTime + } + //上传渲染结果图耗时 + if data.taskProperty.uploadUnityRenderImageTakesTime != 0 { + taskData.uploadUnityRenderImageTakesTime = data.taskProperty.uploadUnityRenderImageTakesTime + } + //发送unity时间 + if data.taskProperty.unityRenderBeginTime != 0 { + taskData.unityRenderBeginTime = data.taskProperty.unityRenderBeginTime + } + //收到unity返回的时间 + if data.taskProperty.unityRenderEndTime != 0 { + taskData.unityRenderEndTime = data.taskProperty.unityRenderEndTime } } } diff --git a/service/repositories/image_handle.go b/service/repositories/image_handle.go index c0f396ed..5af0c7dc 100644 --- a/service/repositories/image_handle.go +++ b/service/repositories/image_handle.go @@ -193,9 +193,17 @@ func (l *defaultImageHandle) LogoCombine(ctx context.Context, in *LogoCombineReq productTemplateV2Info, err := productTemplateV2Model.FindOne(ctx, in.TemplateId) if err != nil { - logx.Error(err) + logc.Errorf(ctx, "productTemplateV2Model.FindOne:%v", err) return nil, err } + + productTemplateTagInfo, err := gmodel.NewFsProductTemplateTagsModel(l.MysqlConn).FindOneByTagName(ctx, in.TemplateTag, "groups") + + if err != nil { + logc.Errorf(ctx, "NewFsProductTemplateTagsModel.FindOneByTagName:%v", err) + return nil, err + } + var groupOptions map[string]interface{} var materialList []interface{} if productTemplateV2Info.TemplateInfo != nil { @@ -216,6 +224,13 @@ func (l *defaultImageHandle) LogoCombine(ctx context.Context, in *LogoCombineReq moduleDataMap["groupOptions"] = groupOptions moduleDataMap["materialList"] = materialList + var tagDataMap []interface{} + err = json.Unmarshal([]byte(*productTemplateTagInfo.Groups), &tagDataMap) + if err != nil { + logc.Errorf(ctx, "Unmarshal tagDataMap:%v", err) + return nil, err + } + var combineParam map[string]interface{} json.Unmarshal([]byte(*resLogoInfo.Metadata), &combineParam) combineParam["template_tagid"] = in.TemplateTag @@ -227,6 +242,7 @@ func (l *defaultImageHandle) LogoCombine(ctx context.Context, in *LogoCombineReq var postMap = make(map[string]interface{}, 2) postMap["module_data"] = moduleDataMap + postMap["tag_data"] = tagDataMap postMap["param_data"] = combineParam logc.Infof(ctx, "合图--算法请求--合图--开始时间:%v", time.Now().UTC()) diff --git a/service/repositories/resource.go b/service/repositories/resource.go new file mode 100644 index 00000000..d6eb0aca --- /dev/null +++ b/service/repositories/resource.go @@ -0,0 +1,122 @@ +package repositories + +import ( + "context" + "encoding/json" + "fusenapi/model/gmodel" + + "github.com/aws/aws-sdk-go/aws/session" + "github.com/zeromicro/go-zero/core/logc" + "gorm.io/gorm" +) + +func NewResource(gormDB *gorm.DB, bLMServiceUrl *string, awsSession *session.Session) Resource { + return &defaultResource{ + MysqlConn: gormDB, + BLMServiceUrl: bLMServiceUrl, + AwsSession: awsSession, + } +} + +type ( + defaultResource struct { + MysqlConn *gorm.DB + BLMServiceUrl *string + AwsSession *session.Session + } + Resource interface { + // 更新metadata + UpdateMetadata(ctx context.Context, in *UpdateMetadataReq) (*UpdateMetadataRes, error) + } +) + +// 全局 +type ( + Cropping struct { + ResourceId string `json:"resource_id"` + ResourceUrl string `json:"resource_url"` + Width int64 `json:"width"` + Height int64 `json:"height"` + } +) + +/* 更新metadata */ +type ( + UpdateMetadataReq struct { + ResourceId string `json:"resource_id"` + MetadataChild map[string]interface{} `json:"metadata_child"` + } + UpdateMetadataRes struct{} +) + +// 更新metadata +func (d *defaultResource) UpdateMetadata(ctx context.Context, in *UpdateMetadataReq) (*UpdateMetadataRes, error) { + // 事务处理 + err := d.MysqlConn.Transaction(func(tx *gorm.DB) error { + var NewFsResourceModel = gmodel.NewFsResourceModel(tx) + oldResourceWhere := tx.Where("resource_id = ?", in.ResourceId) + oldResource, err := NewFsResourceModel.FindOneByQuery(ctx, oldResourceWhere, nil) + if err != nil || oldResource.ResourceId == "" { + logc.Errorf(ctx, "FindOneByQuery oldResource error: %v", err) + return err + } + var newResourceMetadata string + if oldResource.Metadata != nil && *oldResource.Metadata != "{}" { + var oldResourceMetadata map[string]interface{} + err = json.Unmarshal([]byte(*oldResource.Metadata), &oldResourceMetadata) + if err != nil { + logc.Errorf(ctx, "Unmarshal oldResourceMetadata error: %v", err) + return err + } + for k, v := range in.MetadataChild { + switch val := v.(type) { + case map[string]interface{}: + oldResourceMetadataData := oldResourceMetadata[k].(map[string]interface{}) + for key := range val { + oldResourceMetadataData[key] = val + } + oldResourceMetadata[k] = oldResourceMetadataData + } + } + oldResourceMetadataB, err := json.Marshal(oldResourceMetadata) + if err != nil { + logc.Errorf(ctx, "Marshal oldResourceMetadata error: %v", err) + return err + } + newResourceMetadata = string(oldResourceMetadataB) + } else { + var resourceMetadata = make(map[string]interface{}, len(in.MetadataChild)) + for k, v := range in.MetadataChild { + switch val := v.(type) { + case map[string]interface{}: + var resourceMetadataData = make(map[string]interface{}, len(val)) + for key := range val { + resourceMetadataData[key] = val + } + resourceMetadata[k] = resourceMetadataData + } + } + newResourceMetadataB, err := json.Marshal(resourceMetadata) + if err != nil { + logc.Errorf(ctx, "Marshal oldResourceMetadata error: %v", err) + return err + } + newResourceMetadata = string(newResourceMetadataB) + } + oldResource.Metadata = &newResourceMetadata + _, err = NewFsResourceModel.BuilderUpdate(ctx, tx, oldResource) + if err != nil { + logc.Errorf(ctx, "Transaction UpdateMetadata error: %v", err) + return err + } + return nil + + }) + if err != nil { + logc.Errorf(ctx, "BuilderUpdate oldResource error: %v", err) + return nil, err + } + return nil, nil +} + +/* 更新metadata */