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") } //根据模板标签获取开启了云渲染的模板列表 templateList, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllCloudRenderTemplateByProductIdsTemplateTag(l.ctx, productIds, req.TemplateTag, "", "id,product_id,model_id") if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product template list") } modelIds := make([]int64, 0, len(templateList)) for _, v := range templateList { modelIds = append(modelIds, *v.ModelId) } //获取模型列表 modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIds(l.ctx, modelIds, "") if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list") } partIds := make([]int64, 0, len(modelList)) for _, v := range modelList { if *v.PartId == 0 { continue } partIds = append(partIds, *v.PartId) } //获取配件列表 partList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIds(l.ctx, partIds, "") //合并模型和配件 modelList = append(modelList, partList...) 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 } //获取配件默认贴图 fittingPublicTemplateIds := make([]int64, 0, len(modelList)) //绑定了公共模板 fittingNotPublicIds := make([]int64, 0, len(modelList)) //没绑定公共模板 for _, v := range modelList { if *v.Tag != constants.TAG_PARTS { continue } //配件绑定公共模板 if *v.OptionTemplate > 0 { fittingPublicTemplateIds = append(fittingPublicTemplateIds, *v.OptionTemplate) } else { fittingNotPublicIds = append(fittingNotPublicIds, v.Id) } } //公共模板配件的模板列表 fittingPublicTemplateList, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByIds(l.ctx, fittingPublicTemplateIds, "id,model_id,material_img") if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get fitting templates!") } mapFittingPublicTemplate := make(map[int64]int) for k, v := range fittingPublicTemplateList { mapFittingPublicTemplate[v.Id] = k } //非公共模板配件的模板列表 fittingNotPublicTemplateList, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByFittingIds(l.ctx, fittingNotPublicIds) if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get fitting templates!!") } mapFittingNotPublicTemplate := make(map[int64]int) for k, v := range fittingNotPublicTemplateList { mapFittingNotPublicTemplate[*v.ModelId] = 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)) } } } fittingMaterialImage := "" //配件有默认贴图 if *v.Tag == constants.TAG_PARTS { switch *v.OptionTemplate { case 0: //没有绑定公共模板的配件 if index, ok := mapFittingNotPublicTemplate[v.Id]; ok { fittingMaterialImage = *fittingNotPublicTemplateList[index].MaterialImg } default: if index, ok := mapFittingPublicTemplate[*v.OptionTemplate]; ok { fittingMaterialImage = *fittingPublicTemplateList[index].MaterialImg } } } mapProductModels[*v.ProductId] = append(mapProductModels[*v.ProductId], types.ModelItem{ Id: v.Id, Type: *v.Tag, Title: *v.Title, Name: *v.Name, MaterialImage: fittingMaterialImage, DesignInfo: modelDesignInfo, LightInfo: types.LightInfo{ Id: *v.Light, LightDesignInfo: 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) // }