fusenapi/server/product/internal/logic/gettemplatebypidlogic.go
laodaming bdd9a70a2d fix
2023-08-14 14:21:14 +08:00

126 lines
4.1 KiB
Go

package logic
import (
"encoding/json"
"errors"
"fmt"
"fusenapi/constants"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"gorm.io/gorm"
"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.ProductTemplateTagId <= 0 {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:product_template_tag_id")
}
//获取产品信息(只获取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")
}
//没有指定物料
sizeIds := make([]int64, 0, 10)
if req.ProductSizeId <= 0 {
//获取产品所有物料
sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByProductIds(l.ctx, []int64{productInfo.Id}, "id")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "failed to get product size list")
}
if len(sizeList) == 0 {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "product size list is empty")
}
for _, v := range sizeList {
sizeIds = append(sizeIds, v.Id)
}
} else { //指定物料
sizeIds = append(sizeIds, req.ProductSizeId)
}
//根据尺寸id获取模型
modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllBySizeIdsTag(l.ctx, sizeIds, constants.TAG_MODEL)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list")
}
if len(modelList) == 0 {
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "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
}
//查询模型ids下对应tag标签的模板
templateList, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByModelIdsTemplateTag(l.ctx, modelIds, fmt.Sprintf("%d", req.ProductTemplateTagId), "")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template list")
}
rsp := make(map[string]interface{})
for _, templateInfo := range templateList {
//没有设置模板据不要
if templateInfo.TemplateInfo == nil || *templateInfo.TemplateInfo == "" {
continue
}
modelIndex, ok := mapModel[*templateInfo.ModelId]
if !ok {
continue
}
//基础模板信息
var info map[string]map[string]interface{}
if err = json.Unmarshal([]byte(*templateInfo.TemplateInfo), &info); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeJsonErr, fmt.Sprintf("failed to parse json product template info(may be old data):%d", templateInfo.Id))
}
//后台隐藏/显示信息
var switchInfo interface{}
if templateInfo.SwitchInfo != nil && *templateInfo.SwitchInfo != "" {
_ = json.Unmarshal([]byte(*templateInfo.SwitchInfo), &switchInfo)
}
modelInfo := modelList[modelIndex]
var material interface{}
if info["module_data"] != nil && info["module_data"]["material"] != nil {
material = info["module_data"]["material"]
}
mapKey := fmt.Sprintf("_%d", *modelInfo.SizeId)
rsp[mapKey] = map[string]interface{}{
"id": templateInfo.Id,
"material": material,
"material_data": switchInfo,
}
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", rsp)
}