This commit is contained in:
laodaming 2023-06-25 12:16:01 +08:00
parent cd8a578dfe
commit 81b315b12b
5 changed files with 75 additions and 20 deletions

View File

@ -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
}

View File

@ -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"

View File

@ -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,
})
}

View File

@ -49,7 +49,7 @@ type AddBaseMapReq struct {
type UpdateTemplateReq struct {
ModelId int64 `json:"modelId"`
TemplateData TemplateData `json:"templateData"`
TemplateData *TemplateData `json:"templateData"`
}
type TemplateData struct {
@ -58,7 +58,7 @@ type TemplateData struct {
Cover string `json:"cover"`
IsPublic bool `json:"isPublic"`
Material string `json:"material"`
MaterialList []interface{} `json:"materialList"`
MaterialList []map[string]interface{} `json:"materialList"`
}
type UpdateTemplateRsp struct {

View File

@ -71,7 +71,7 @@ type AddBaseMapReq {
//更新模板
type UpdateTemplateReq {
ModelId int64 `json:"modelId"`
TemplateData TemplateData `json:"templateData"`
TemplateData *TemplateData `json:"templateData"`
}
type TemplateData {
Id int64 `json:"id"`
@ -79,7 +79,7 @@ type TemplateData {
Cover string `json:"cover"`
IsPublic bool `json:"isPublic"`
Material string `json:"material"`
MaterialList []interface{} `json:"materialList"`
MaterialList []map[string]interface{} `json:"materialList"`
}
type UpdateTemplateRsp {
ModelId int64 `json:"modelId"`