fusenapi/server/product/internal/logic/getproductmodelslogic.go
laodaming 4f814d7361 fix
2023-11-22 16:04:48 +08:00

121 lines
3.9 KiB
Go

package logic
import (
"encoding/json"
"fmt"
"fusenapi/constants"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/format"
"strings"
"context"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetProductModelsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetProductModelsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetProductModelsLogic {
return &GetProductModelsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *GetProductModelsLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
func (l *GetProductModelsLogic) GetProductModels(req *types.GetProductModelsReq, userinfo *auth.UserInfo) (resp *basic.Response) {
req.ProductIds = strings.Trim(req.ProductIds, " ")
productIds, err := format.StrSlicToInt64Slice(strings.Split(req.ProductIds, ","))
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param(format err)")
}
if len(productIds) > 10 {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "the count of product can`t greater than 10")
}
if len(productIds) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "success", nil)
}
//获取产品信息
productList, err := l.svcCtx.AllModels.FsProduct.GetProductListByIds(l.ctx, productIds, "")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product list")
}
if len(productList) != len(productIds) {
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "some one product is invalid")
}
//获取产品所有的模型以及配件
modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByProductIdsTags(l.ctx, productIds, []int{constants.TAG_MODEL, constants.TAG_PARTS})
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list")
}
mapProductModels := make(map[int64][]types.ModelItem)
lightIds := make([]int64, 0, len(modelList))
for _, v := range modelList {
lightIds = append(lightIds, *v.Light)
}
//获取光源列表
lightList, err := l.svcCtx.AllModels.FsProductModel3dLight.GetAllByIds(l.ctx, lightIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get light list")
}
mapLight := make(map[int64]int)
for k, v := range lightList {
mapLight[v.Id] = k
}
for _, v := range modelList {
//模型设计信息
var modelDesignInfo interface{}
if v.ModelInfo != nil && *v.ModelInfo != "" {
if err = json.Unmarshal([]byte(*v.ModelInfo), &modelDesignInfo); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeJsonErr, fmt.Sprintf("failed to parse model design info[:%d]", v.Id))
}
}
//光源信息
var lightDesignInfo interface{}
if lightIndex, ok := mapLight[*v.Light]; ok {
lightInfo := lightList[lightIndex]
if lightInfo.Info != nil && *lightInfo.Info != "" {
if err = json.Unmarshal([]byte(*lightInfo.Info), &lightDesignInfo); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeJsonErr, fmt.Sprintf("failed to parse light design info[:%d]", lightInfo.Id))
}
}
}
mapProductModels[*v.ProductId] = append(mapProductModels[*v.ProductId], types.ModelItem{
Id: v.Id,
Type: *v.Tag,
Title: *v.Title,
Name: *v.Name,
DesignInfo: modelDesignInfo,
LightInfo: lightDesignInfo,
})
}
mapProductInfo := make(map[int64][]types.ModelItem)
for _, v := range productList {
mapProductInfo[v.Id] = mapProductModels[v.Id]
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", mapProductInfo)
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *GetProductModelsLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }