fusenapi/server/product-template/internal/logic/updatetemplatelogic.go

92 lines
2.8 KiB
Go
Raw Normal View History

2023-06-25 03:28:37 +00:00
package logic
import (
2023-06-26 10:27:38 +00:00
"context"
2023-06-25 04:16:01 +00:00
"encoding/json"
2023-06-25 03:28:37 +00:00
"errors"
"fusenapi/model/gmodel"
2023-06-26 10:27:38 +00:00
"fusenapi/utils/auth"
2023-06-25 03:28:37 +00:00
"fusenapi/utils/basic"
"gorm.io/gorm"
2023-06-25 06:11:47 +00:00
"fusenapi/server/product-template/internal/svc"
"fusenapi/server/product-template/internal/types"
2023-06-25 03:28:37 +00:00
"github.com/zeromicro/go-zero/core/logx"
)
type UpdateTemplateLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUpdateTemplateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateTemplateLogic {
return &UpdateTemplateLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
2023-06-26 10:27:38 +00:00
func (l *UpdateTemplateLogic) UpdateTemplate(req *types.UpdateTemplateReq, userInfo *auth.BackendUserInfo) (resp *basic.Response) {
2023-06-25 03:28:37 +00:00
if req.ModelId <= 0 {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param modelId is required")
}
2023-06-25 04:16:01 +00:00
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)
2023-06-25 04:16:40 +00:00
//保存模板宽高
2023-06-25 04:16:01 +00:00
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()
2023-06-25 04:28:06 +00:00
break
2023-06-25 04:16:01 +00:00
}
}
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,
})
2023-06-25 03:28:37 +00:00
}