diff --git a/server/product/internal/handler/getproductmodelshandler.go b/server/product/internal/handler/getproductmodelshandler.go new file mode 100644 index 00000000..4018d46b --- /dev/null +++ b/server/product/internal/handler/getproductmodelshandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/product/internal/logic" + "fusenapi/server/product/internal/svc" + "fusenapi/server/product/internal/types" +) + +func GetProductModelsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.GetProductModelsReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewGetProductModelsLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.GetProductModels(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/product/internal/handler/routes.go b/server/product/internal/handler/routes.go index ef173a82..3a5c2e7c 100644 --- a/server/product/internal/handler/routes.go +++ b/server/product/internal/handler/routes.go @@ -42,6 +42,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/product/get_product_detail", Handler: GetProductDetailHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/api/product/get_product_models", + Handler: GetProductModelsHandler(serverCtx), + }, }, ) } diff --git a/server/product/internal/logic/getproductmodelslogic.go b/server/product/internal/logic/getproductmodelslogic.go new file mode 100644 index 00000000..541e62da --- /dev/null +++ b/server/product/internal/logic/getproductmodelslogic.go @@ -0,0 +1,120 @@ +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) > 20 { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "the count of product can`t greater than 20") + } + 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) +// } diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index e1ab4408..ce8c0175 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -172,6 +172,23 @@ type TemplateTagColorInfo struct { TemplateTagGroups interface{} `json:"template_tag_groups"` //模板标签分组信息 } +type GetProductModelsReq struct { + ProductIds string `form:"product_ids"` +} + +type ProductItem struct { + ModelList []ModelItem `json:"model_list"` +} + +type ModelItem struct { + Id int64 `json:"id"` + Type int64 `json:"type"` //1模型 2配件 3场景 + Title string `json:"title"` + Name string `json:"name"` + DesignInfo interface{} `json:"design_info"` + LightInfo interface{} `json:"light_info"` +} + type Request struct { } diff --git a/server_api/product.api b/server_api/product.api index 6287bd02..22ab024c 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -28,6 +28,9 @@ service product { //获取产品详情 @handler GetProductDetailHandler get /api/product/get_product_detail(GetProductDetailReq) returns (response); + //根据产品获取模型配件列表 + @handler GetProductModelsHandler + get /api/product/get_product_models(GetProductModelsReq) returns (response); } //获取详情页推荐产品列表 @@ -185,4 +188,19 @@ type TemplateTagColorInfo { Colors [][]string `json:"colors"` //传入logo对应的算法颜色组 SelectedColorIndex int `json:"selected_color_index"` //选择的模板标签的颜色索引值 TemplateTagGroups interface{} `json:"template_tag_groups"` //模板标签分组信息 +} +//根据产品获取模型配件列表 +type GetProductModelsReq { + ProductIds string `form:"product_ids"` +} +type ProductItem { + ModelList []ModelItem `json:"model_list"` +} +type ModelItem { + Id int64 `json:"id"` + Type int64 `json:"type"` //1模型 2配件 3场景 + Title string `json:"title"` + Name string `json:"name"` + DesignInfo interface{} `json:"design_info"` + LightInfo interface{} `json:"light_info"` } \ No newline at end of file