fusenapi/server/product/internal/logic/gettemplatebypidlogic.go

153 lines
4.9 KiB
Go
Raw Normal View History

2023-07-17 06:40:24 +00:00
package logic
import (
"encoding/json"
"errors"
"fmt"
"fusenapi/constants"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/image"
"gorm.io/gorm"
"strconv"
"strings"
"context"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetTemplateByPidLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetTemplateByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTemplateByPidLogic {
return &GetTemplateByPidLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetTemplateByPidLogic) GetTemplateByPid(req *types.GetTemplateByPidReq, userinfo *auth.UserInfo) (resp *basic.Response) {
req.Pid = strings.Trim(req.Pid, " ")
if req.Pid == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:pid is empty")
}
if req.Size > 0 {
req.Size = image.GetCurrentSize(req.Size)
}
2023-08-08 09:03:54 +00:00
if req.ProductTemplateTagId <= 0 {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:product_template_tag_id")
}
2023-07-17 06:40:24 +00:00
//获取产品信息(只是获取id)
productInfo, err := l.svcCtx.AllModels.FsProduct.FindOneBySn(l.ctx, req.Pid, "id")
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the product is not exists")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info")
}
2023-08-08 09:03:54 +00:00
//获取尺寸ids(只获取id)
2023-07-17 06:40:24 +00:00
sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByProductIds(l.ctx, []int64{productInfo.Id}, "id")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get size list")
}
if len(sizeList) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "success:size list is empty")
}
sizeIds := make([]int64, 0, len(sizeList))
for _, v := range sizeList {
sizeIds = append(sizeIds, v.Id)
}
//获取模型数据
modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllBySizeIdsTag(l.ctx, sizeIds, constants.TAG_MODEL, "id,size_id")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list")
}
if len(modelList) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "success:model list is empty")
}
modelIds := make([]int64, 0, len(modelList))
mapModel := make(map[int64]int)
for k, v := range modelList {
modelIds = append(modelIds, v.Id)
mapModel[v.Id] = k
}
//获取模板数据
2023-08-08 09:03:54 +00:00
productTemplateList, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByModelIdsTemplateTag(l.ctx, modelIds, fmt.Sprintf("%d", req.ProductTemplateTagId), "sort DESC")
2023-07-17 06:40:24 +00:00
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product templates")
}
if len(productTemplateList) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "success:product template list is empty")
}
tagIds := make([]int64, 0, len(productTemplateList))
for _, v := range productTemplateList {
if *v.Tag == "" {
continue
}
tag, err := strconv.ParseInt(*v.Tag, 10, 64)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "template tag is not a number:"+*v.Tag)
}
tagIds = append(tagIds, tag)
}
//获取模板标签列表
templateTagList, err := l.svcCtx.AllModels.FsProductTemplateTags.GetListByIds(l.ctx, tagIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template tag list")
}
mapTemplateTag := make(map[string]int)
for k, v := range templateTagList {
mapTemplateTag[fmt.Sprintf("%d", v.Id)] = k
}
mapRsp := make(map[string][]interface{})
for _, v := range productTemplateList {
//过滤没有设置详细数据的模板
if v.TemplateInfo == nil || *v.TemplateInfo == "" {
continue
}
var templateInfo map[string]interface{}
if err = json.Unmarshal([]byte(*v.TemplateInfo), &templateInfo); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse json:template info")
}
if templateInfo["cover"] != nil && templateInfo["cover"].(string) != "" {
cover := strings.Split(templateInfo["cover"].(string), ".")
if req.Size >= 200 && len(cover) >= 2 {
templateInfo["cover"] = fmt.Sprintf("%s_%d.%s", cover[0], req.Size, cover[1])
}
} else {
templateInfo["cover"] = ""
}
templateInfo["tag_name"] = ""
if tagIndex, ok := mapTemplateTag[*v.Tag]; ok {
templateInfo["tag_name"] = *templateTagList[tagIndex].Title
}
templateInfo["title"] = *v.Title
modelIndex, ok := mapModel[*v.ModelId]
if !ok {
continue
}
key := fmt.Sprintf("_%d", *modelList[modelIndex].SizeId)
if _, ok = mapRsp[key]; ok {
mapRsp[key] = append(mapRsp[key], templateInfo)
} else {
mapRsp[key] = []interface{}{templateInfo}
}
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", mapRsp)
}