From 81b315b12ba56516cd94dbfa075c7e435fab0163 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Sun, 25 Jun 2023 12:16:01 +0800 Subject: [PATCH] fix --- model/gmodel/fs_product_template_v2_logic.go | 3 + .../internal/handler/updatetemplatehandler.go | 4 +- .../internal/logic/updatetemplatelogic.go | 56 ++++++++++++++++++- .../internal/types/types.go | 16 +++--- server_api/product-templatev2.api | 16 +++--- 5 files changed, 75 insertions(+), 20 deletions(-) diff --git a/model/gmodel/fs_product_template_v2_logic.go b/model/gmodel/fs_product_template_v2_logic.go index c620be45..bc0d5670 100755 --- a/model/gmodel/fs_product_template_v2_logic.go +++ b/model/gmodel/fs_product_template_v2_logic.go @@ -37,3 +37,6 @@ func (t *FsProductTemplateV2Model) FindByParam(ctx context.Context, id int64, mo err = db.Take(&resp).Error return resp, err } +func (t *FsProductTemplateV2Model) Update(ctx context.Context, id int64, data *FsProductTemplateV2) error { + return t.db.WithContext(ctx).Model(&FsProductTemplateV2{}).Where("`id` = ? ", id).Updates(&data).Error +} diff --git a/server/product-templatev2/internal/handler/updatetemplatehandler.go b/server/product-templatev2/internal/handler/updatetemplatehandler.go index ac02fa3b..2f868d9b 100644 --- a/server/product-templatev2/internal/handler/updatetemplatehandler.go +++ b/server/product-templatev2/internal/handler/updatetemplatehandler.go @@ -4,12 +4,10 @@ import ( "errors" "net/http" + "fusenapi/utils/basic" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/rest/httpx" - "fusenapi/utils/auth" - "fusenapi/utils/basic" - "fusenapi/server/product-templatev2/internal/logic" "fusenapi/server/product-templatev2/internal/svc" "fusenapi/server/product-templatev2/internal/types" diff --git a/server/product-templatev2/internal/logic/updatetemplatelogic.go b/server/product-templatev2/internal/logic/updatetemplatelogic.go index ce0a00cb..814acf7b 100644 --- a/server/product-templatev2/internal/logic/updatetemplatelogic.go +++ b/server/product-templatev2/internal/logic/updatetemplatelogic.go @@ -1,6 +1,7 @@ package logic import ( + "encoding/json" "errors" "fusenapi/model/gmodel" "fusenapi/utils/basic" @@ -43,5 +44,58 @@ func (l *UpdateTemplateLogic) UpdateTemplate(req *types.UpdateTemplateReq, r *ht if req.ModelId <= 0 { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param modelId is required") } - return resp.SetStatus(basic.CodeOK) + if req.TemplateData == nil { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param template data is required") + } + if req.TemplateData.Id <= 0 { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param template data`id is required") + } + //验证模板数据真实性 + templatev2Info, err := l.svcCtx.AllModels.FsProductTemplateV2.FindOne(l.ctx, req.TemplateData.Id) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "template is not exists") + } + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get v2 template info") + } + if *templatev2Info.ModelId != req.ModelId { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "the template`s model id is not match") + } + templateInfoBytes, err := json.Marshal(req.TemplateData) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to marshal template data") + } + templateInfoJson := string(templateInfoBytes) + var logoWidth int64 + var logoHeight int64 + for _, v := range req.TemplateData.MaterialList { + //logo面板需保存宽高 + if v["tag"] == "Logo" { + logoWidth, _ = v["width"].(json.Number).Int64() + logoHeight, _ = v["height"].(json.Number).Int64() + } + } + //保存模板宽高 + isPublic := int64(0) + if req.TemplateData.IsPublic { + isPublic = 1 + } + updData := gmodel.FsProductTemplateV2{ + TemplateInfo: &templateInfoJson, + MaterialImg: &req.TemplateData.Material, + Name: &req.TemplateData.Name, + LogoWidth: &logoWidth, + LogoHeight: &logoHeight, + IsPublic: &isPublic, + } + if err = l.svcCtx.AllModels.FsProductTemplateV2.Update(l.ctx, req.TemplateData.Id, &updData); err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeSaveErr, "failed to update template") + } + return resp.SetStatusWithMessage(basic.CodeOK, "success", types.UpdateTemplateRsp{ + ModelId: req.ModelId, + TemplateId: req.TemplateData.Id, + }) } diff --git a/server/product-templatev2/internal/types/types.go b/server/product-templatev2/internal/types/types.go index 27b639a7..b3214193 100644 --- a/server/product-templatev2/internal/types/types.go +++ b/server/product-templatev2/internal/types/types.go @@ -48,17 +48,17 @@ type AddBaseMapReq struct { } type UpdateTemplateReq struct { - ModelId int64 `json:"modelId"` - TemplateData TemplateData `json:"templateData"` + ModelId int64 `json:"modelId"` + TemplateData *TemplateData `json:"templateData"` } type TemplateData struct { - Id int64 `json:"id"` - Name string `json:"name"` - Cover string `json:"cover"` - IsPublic bool `json:"isPublic"` - Material string `json:"material"` - MaterialList []interface{} `json:"materialList"` + Id int64 `json:"id"` + Name string `json:"name"` + Cover string `json:"cover"` + IsPublic bool `json:"isPublic"` + Material string `json:"material"` + MaterialList []map[string]interface{} `json:"materialList"` } type UpdateTemplateRsp struct { diff --git a/server_api/product-templatev2.api b/server_api/product-templatev2.api index 430b2b60..430c7348 100644 --- a/server_api/product-templatev2.api +++ b/server_api/product-templatev2.api @@ -70,16 +70,16 @@ type AddBaseMapReq { } //更新模板 type UpdateTemplateReq { - ModelId int64 `json:"modelId"` - TemplateData TemplateData `json:"templateData"` + ModelId int64 `json:"modelId"` + TemplateData *TemplateData `json:"templateData"` } type TemplateData { - Id int64 `json:"id"` - Name string `json:"name"` - Cover string `json:"cover"` - IsPublic bool `json:"isPublic"` - Material string `json:"material"` - MaterialList []interface{} `json:"materialList"` + Id int64 `json:"id"` + Name string `json:"name"` + Cover string `json:"cover"` + IsPublic bool `json:"isPublic"` + Material string `json:"material"` + MaterialList []map[string]interface{} `json:"materialList"` } type UpdateTemplateRsp { ModelId int64 `json:"modelId"`