From db9c2f3cc73dd70b70919d2b2594d8b0012c2226 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 21 Jun 2023 12:11:43 +0800 Subject: [PATCH] fix --- model/gmodel/fs_product_model3d_logic.go | 16 +- .../gmodel/fs_product_template_tags_logic.go | 2 +- .../internal/logic/gettemplatevdetaillogic.go | 160 ++++++++++-------- utils/format/str_to_int_slice.go | 24 ++- 4 files changed, 118 insertions(+), 84 deletions(-) diff --git a/model/gmodel/fs_product_model3d_logic.go b/model/gmodel/fs_product_model3d_logic.go index 945656ff..c216c086 100755 --- a/model/gmodel/fs_product_model3d_logic.go +++ b/model/gmodel/fs_product_model3d_logic.go @@ -8,23 +8,21 @@ func (d *FsProductModel3dModel) FindOne(ctx context.Context, id int64) (resp *Fs err = d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` = ? ", id).Find(&resp).Error return resp, err } -func (d *FsProductModel3dModel) GetAllByIds(ctx context.Context, ids []int64) (resp []FsProductModel3d, err error) { +func (d *FsProductModel3dModel) GetAllByIds(ctx context.Context, ids []int64, fields ...string) (resp []FsProductModel3d, err error) { if len(ids) == 0 { return } - err = d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` in (?) and `status` = ?", ids, 1).Find(&resp).Error - if err != nil { - return nil, err + db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` in (?) and `status` = ?", ids, 1) + if len(fields) > 0 { + db = db.Select(fields[0]) } - return + err = db.Find(&resp).Error + return resp, err } func (d *FsProductModel3dModel) GetAllByIdsTag(ctx context.Context, ids []int64, tag int64) (resp []FsProductModel3d, err error) { if len(ids) == 0 { return } err = d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` in (?) and `status` = ? and `tag` = ?", ids, 1, tag).Find(&resp).Error - if err != nil { - return nil, err - } - return + return resp, err } diff --git a/model/gmodel/fs_product_template_tags_logic.go b/model/gmodel/fs_product_template_tags_logic.go index 7793673f..f5c87b39 100755 --- a/model/gmodel/fs_product_template_tags_logic.go +++ b/model/gmodel/fs_product_template_tags_logic.go @@ -20,6 +20,6 @@ func (pt *FsProductTemplateTagsModel) FindOne(ctx context.Context, id int64, fie if len(fields) != 0 { db = db.Select(fields[0]) } - err = db.Take(resp).Error + err = db.Take(&resp).Error return resp, err } diff --git a/server/product-templatev2/internal/logic/gettemplatevdetaillogic.go b/server/product-templatev2/internal/logic/gettemplatevdetaillogic.go index 30e66731..17bd3cf3 100644 --- a/server/product-templatev2/internal/logic/gettemplatevdetaillogic.go +++ b/server/product-templatev2/internal/logic/gettemplatevdetaillogic.go @@ -34,7 +34,6 @@ func NewGetTemplatevDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) } func (l *GetTemplatevDetailLogic) GetTemplatevDetail(req *types.GetTemplatevDetailReq, r *http.Request) (resp *basic.Response) { - // authKey := r.Header.Get("Auth-Key") if authKey == "" { return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first") @@ -62,7 +61,7 @@ func (l *GetTemplatevDetailLogic) GetTemplatevDetail(req *types.GetTemplatevDeta return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "product model info is not exists") } logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product model info") + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product model info") } //查询产品模板并检测数据完整 productTemplatev2Model := gmodel.NewFsProductTemplateV2Model(l.svcCtx.MysqlConn) @@ -73,7 +72,7 @@ func (l *GetTemplatevDetailLogic) GetTemplatevDetail(req *types.GetTemplatevDeta return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "template info is not exists") } logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product template info") + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product template info") } //获取模板标签 templateTagModel := gmodel.NewFsProductTemplateTagsModel(l.svcCtx.MysqlConn) @@ -88,10 +87,13 @@ func (l *GetTemplatevDetailLogic) GetTemplatevDetail(req *types.GetTemplatevDeta return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "template tag info is not exists") } logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product template tag info") + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product template tag info") } //配件ids - partIds, err := format.StrSlicToIntSlice(strings.Split(*model3dInfo.PartList, ",")) + partIds := make([]int64, 0, 10) + if *model3dInfo.PartList != "" { + partIds, err = format.StrSlicToInt64Slice(strings.Split(*model3dInfo.PartList, ",")) + } //灯光ids var lightIds []int64 if err = json.Unmarshal([]byte(*model3dInfo.LightList), &lightIds); err != nil { @@ -103,70 +105,88 @@ func (l *GetTemplatevDetailLogic) GetTemplatevDetail(req *types.GetTemplatevDeta model3dLightList, err := productModel3dLightModel.GetAllByIds(l.ctx, lightIds) if err != nil { logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product model light list") + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product model light list") } - /* - - //产品模型数据解析model_info - $productModel['model_info'] = $productModel['model_info'] ? json_decode($productModel['model_info']) : null; - - //产品模板数据解析template_info - if ($productTemplate) { - $productTemplate['template_info'] = $productTemplate['template_info'] ? json_decode($productTemplate['template_info']) : ''; - } - //获取灯光列表 - $lightList = ProductModelLight::find()->where(['in', 'id', $lightListArr]) - ->select(['id', 'info']) - ->asArray()->all(); - - //产品模型灯光数据解析 - foreach ($lightList as $key => $val) { - $lightList[$key]['info'] = json_decode($val['info'], true); - } - - //查询使用该选项的模板 - if ($partIds) { - $optionModelInfo = ProductModel::find() - ->where(['in', 'id', $partIds]) - ->select('id,model_info,option_template') - ->asArray() - ->all(); - - //公共模板数据 - $optionIds = array_column($optionModelInfo, 'option_template'); - $optionTemplates = ProductTemplateV2::find()->where(['in', 'id', $optionIds]) - ->asArray() - ->all(); - $optionTemplates = array_column($optionTemplates, null, 'id'); - - //处理数据 - $optionModelInfoArr = []; - foreach ($optionModelInfo as $item => $row) { - $rowInfo = json_decode($row['model_info'], true); - if (isset($optionTemplates[$row['option_template']])) { - $rowInfo['material_img'] = $optionTemplates[$row['option_template']]['material_img']; - } - $rowInfo['id'] = $row['id']; - $optionModelInfoArr[] = $rowInfo; - } - } - - //是否是公共模板 - if ($productTemplate) { - if ($productTemplate['is_public'] == 1) { - $productTemplate['is_public'] = true; - } else { - $productTemplate['is_public'] = false; - } - } - - //返回的数据组装 - return [ - 'product_model_info' => $productModel['model_info'], - 'product_template' => $productTemplate ?? null, - 'light_list' => $lightList ?? [], - 'option_model_info' => $optionModelInfoArr ?? [], - 'tag' => $productModel['tag'], - ];*/ - return resp.SetStatus(basic.CodeOK) + //组装灯光信息 + lightListRsp := make([]*types.Light, 0, len(model3dLightList)) + for _, v := range model3dLightList { + var info interface{} + if err = json.Unmarshal([]byte(*v.Info), &info); err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse light info") + } + lightListRsp = append(lightListRsp, &types.Light{ + Id: v.Id, + Info: info, + }) + } + //产品模型数据解析model_info + var productModelInfoRsp interface{} + if model3dInfo.ModelInfo != nil { + if err = json.Unmarshal([]byte(*model3dInfo.ModelInfo), &productModelInfoRsp); err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse product 3d model info") + } + } + //产品模板数据解析template_info + templateInfoRsp := make(map[string]interface{}) + if templatev2Info.TemplateInfo != nil { + if err = json.Unmarshal([]byte(*templatev2Info.TemplateInfo), &templateInfoRsp); err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse product v2 template info") + } + } + templateInfoRsp["tag"] = map[string]interface{}{ + "id": templateTagInfo.Id, + "title": *templateTagInfo.Title, + } + response := types.GetTemplatevDetailRsp{ + ProductModelInfo: productModelInfoRsp, + ProductTemplate: templateInfoRsp, + LightList: lightListRsp, + OptionModelInfo: nil, + Tag: *model3dInfo.Tag, + } + //查询使用该选项的模板 + if len(partIds) > 0 { + model3dList, err := productModel3dModel.GetAllByIds(l.ctx, partIds, "id,model_info,option_template") + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product 3d model list") + } + optionIds := make([]int64, 0, len(model3dList)) + for _, v := range model3dList { + if v.OptionTemplate != nil { + optionIds = append(optionIds, *v.OptionTemplate) + } + } + //获取公共模板信息 + optionTemplateList, err := productTemplatev2Model.FindAllByIds(l.ctx, optionIds) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product v2 template list") + } + mapOptionTemplate := make(map[int64]int) + for k, v := range optionTemplateList { + mapOptionTemplate[v.Id] = k + } + //处理数据 + optionModelInfoList := make([]interface{}, 0, len(model3dList)) + for _, v := range model3dList { + info := make(map[string]interface{}) + if v.ModelInfo != nil { + if err = json.Unmarshal([]byte(*v.ModelInfo), &info); err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse model info") + } + if optionTemplateIndex, ok := mapOptionTemplate[*v.OptionTemplate]; ok { + info["material_img"] = optionTemplateList[optionTemplateIndex].MaterialImg + } + info["id"] = v.Id + } + optionModelInfoList = append(optionModelInfoList, info) + } + response.OptionModelInfo = optionModelInfoList + } + return resp.SetStatusWithMessage(basic.CodeOK, "success", response) } diff --git a/utils/format/str_to_int_slice.go b/utils/format/str_to_int_slice.go index e6f28713..8ddfc4b5 100644 --- a/utils/format/str_to_int_slice.go +++ b/utils/format/str_to_int_slice.go @@ -6,16 +6,32 @@ import ( // 字符串切片转int切片 func StrSlicToIntSlice(input []string) ([]int, error) { - priceSlic := make([]int, 0, len(input)) + newSlic := make([]int, 0, len(input)) for _, p := range input { if p == "" { continue } - price, err := strconv.Atoi(p) + val, err := strconv.Atoi(p) if err != nil { return nil, err } - priceSlic = append(priceSlic, price) + newSlic = append(newSlic, val) } - return priceSlic, nil + return newSlic, nil +} + +// 字符串切片转int64切片 +func StrSlicToInt64Slice(input []string) ([]int64, error) { + newSlic := make([]int64, 0, len(input)) + for _, p := range input { + if p == "" { + continue + } + val, err := strconv.ParseInt(p, 10, 64) + if err != nil { + return nil, err + } + newSlic = append(newSlic, val) + } + return newSlic, nil }