102 lines
3.2 KiB
Go
102 lines
3.2 KiB
Go
package logic
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fusenapi/model/gmodel"
|
|
"fusenapi/utils/basic"
|
|
"gorm.io/gorm"
|
|
"net/http"
|
|
|
|
"context"
|
|
|
|
"fusenapi/server/product-templatev2/internal/svc"
|
|
"fusenapi/server/product-templatev2/internal/types"
|
|
|
|
"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,
|
|
}
|
|
}
|
|
|
|
func (l *UpdateTemplateLogic) UpdateTemplate(req *types.UpdateTemplateReq, r *http.Request) (resp *basic.Response) {
|
|
authKey := r.Header.Get("Auth-Key")
|
|
genentModel := gmodel.NewFsGerentModel(l.svcCtx.MysqlConn)
|
|
_, err := genentModel.Find(l.ctx, authKey)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first..")
|
|
}
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeUnAuth, "failed to get user info")
|
|
}
|
|
if req.ModelId <= 0 {
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param modelId is required")
|
|
}
|
|
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,
|
|
})
|
|
}
|