|
|
|
|
@@ -4,12 +4,10 @@ import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"fusenapi/constants"
|
|
|
|
|
"fusenapi/model/gmodel"
|
|
|
|
|
"fusenapi/utils/auth"
|
|
|
|
|
"fusenapi/utils/basic"
|
|
|
|
|
"fusenapi/utils/image"
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"context"
|
|
|
|
|
@@ -39,13 +37,11 @@ func (l *GetTemplateByPidLogic) GetTemplateByPid(req *types.GetTemplateByPidReq,
|
|
|
|
|
if req.Pid == "" {
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:pid is empty")
|
|
|
|
|
}
|
|
|
|
|
if req.Size > 0 {
|
|
|
|
|
req.Size = image.GetCurrentSize(req.Size)
|
|
|
|
|
}
|
|
|
|
|
if req.ProductTemplateTagId <= 0 {
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:product_template_tag_id")
|
|
|
|
|
}
|
|
|
|
|
//获取产品信息(只是获取id)
|
|
|
|
|
|
|
|
|
|
//获取产品信息(只获取id)
|
|
|
|
|
productInfo, err := l.svcCtx.AllModels.FsProduct.FindOneBySn(l.ctx, req.Pid, "id")
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
@@ -54,99 +50,46 @@ func (l *GetTemplateByPidLogic) GetTemplateByPid(req *types.GetTemplateByPidReq,
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info")
|
|
|
|
|
}
|
|
|
|
|
//获取尺寸ids(只获取id)
|
|
|
|
|
sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByProductIds(l.ctx, []int64{productInfo.Id}, "id")
|
|
|
|
|
//获取该产品该模板标签下的模板列表(只获取id)
|
|
|
|
|
templateList, err := l.svcCtx.AllModels.FsProductTemplateV2.
|
|
|
|
|
GetListByProductAndTemplateTag(l.ctx, fmt.Sprintf("%d", req.ProductTemplateTagId), productInfo.Id, "id")
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get size list")
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get templates")
|
|
|
|
|
}
|
|
|
|
|
if len(sizeList) == 0 {
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeOK, "success:size list is empty")
|
|
|
|
|
if len(templateList) == 0 {
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "templates are not exists")
|
|
|
|
|
}
|
|
|
|
|
sizeIds := make([]int64, 0, len(sizeList))
|
|
|
|
|
for _, v := range sizeList {
|
|
|
|
|
sizeIds = append(sizeIds, v.Id)
|
|
|
|
|
templateIds := make([]int64, 0, len(templateList))
|
|
|
|
|
for _, v := range templateList {
|
|
|
|
|
templateIds = append(templateIds, v.Id)
|
|
|
|
|
}
|
|
|
|
|
var (
|
|
|
|
|
templateInfo *gmodel.FsProductTemplateV2
|
|
|
|
|
)
|
|
|
|
|
//传了指定物料,则获取该物料的第一个模板
|
|
|
|
|
if req.ProductSizeId > 0 {
|
|
|
|
|
templateInfo, err = l.svcCtx.AllModels.FsProductTemplateV2.GetSizeLatestTemplate(l.ctx, templateIds, productInfo.Id)
|
|
|
|
|
} else { //没有指定物料则获取第一个物料的第一个模板
|
|
|
|
|
templateInfo, err = l.svcCtx.AllModels.FsProductTemplateV2.GetProductLatestTemplate(l.ctx, templateIds, productInfo.Id)
|
|
|
|
|
}
|
|
|
|
|
//获取模型数据
|
|
|
|
|
modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllBySizeIdsTag(l.ctx, sizeIds, constants.TAG_MODEL, "id,size_id")
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "template info not found")
|
|
|
|
|
}
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list")
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template info")
|
|
|
|
|
}
|
|
|
|
|
if len(modelList) == 0 {
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeOK, "success:model list is empty")
|
|
|
|
|
if templateInfo.TemplateInfo == nil || *templateInfo.TemplateInfo == "" {
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeJsonErr, "template info is not set")
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
//获取模板数据
|
|
|
|
|
productTemplateList, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByModelIdsTemplateTag(l.ctx, modelIds, fmt.Sprintf("%d", req.ProductTemplateTagId), "sort DESC")
|
|
|
|
|
if err != nil {
|
|
|
|
|
var info interface{}
|
|
|
|
|
if err = json.Unmarshal([]byte(*templateInfo.TemplateInfo), &info); err != nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product templates")
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse json product template info:", templateInfo.Id)
|
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetTemplateByPidRsp{
|
|
|
|
|
TemplateId: templateInfo.Id,
|
|
|
|
|
TemplateInfo: info,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|