From 48afd3105f5778c9524f2791c352a8a934be015d Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 11 Aug 2023 18:59:03 +0800 Subject: [PATCH] fix --- model/gmodel/fs_product_model3d_logic.go | 11 +++ model/gmodel/fs_product_template_v2_gen.go | 1 + model/gmodel/fs_product_template_v2_logic.go | 16 ---- .../internal/logic/gettemplatebypidlogic.go | 91 +++++++++++-------- server_api/product.api | 5 - 5 files changed, 65 insertions(+), 59 deletions(-) diff --git a/model/gmodel/fs_product_model3d_logic.go b/model/gmodel/fs_product_model3d_logic.go index 9cb2fe91..741fdac2 100755 --- a/model/gmodel/fs_product_model3d_logic.go +++ b/model/gmodel/fs_product_model3d_logic.go @@ -101,3 +101,14 @@ func (d *FsProductModel3dModel) FindOneJoinSize(ctx context.Context, productId i Order("s.sort ASC").Take(&resp).Error return resp, err } + +func (d *FsProductModel3dModel) GetOneBySizeIdTag(ctx context.Context, sizeId int64, tag int64, fields ...string) (resp *FsProductModel3d, err error) { + db := d.db.WithContext(ctx).Model(&FsProductModel3d{}). + Where("`size_id` = ? and `tag` = ? and `status` = ?", sizeId, tag, 1). + Order("sort DESC") + if len(fields) != 0 { + db = db.Select(fields[0]) + } + err = db.Take(&resp).Error + return resp, err +} diff --git a/model/gmodel/fs_product_template_v2_gen.go b/model/gmodel/fs_product_template_v2_gen.go index 5e14427f..d72b388a 100644 --- a/model/gmodel/fs_product_template_v2_gen.go +++ b/model/gmodel/fs_product_template_v2_gen.go @@ -22,6 +22,7 @@ type FsProductTemplateV2 struct { Ctime *int64 `gorm:"default:0;" json:"ctime"` // 添加时间 Tag *string `gorm:"default:'';" json:"tag"` // 标签(用户自填) IsDel *int64 `gorm:"default:0;" json:"is_del"` // 是否删除 1删除 + SwitchInfo *string `gorm:"default:'';" json:"switch_info"` // 开关信息 GroupOptions *string `gorm:"default:'';" json:"group_options"` // 颜色分组 Version *int64 `gorm:"default:0;" json:"version"` // } diff --git a/model/gmodel/fs_product_template_v2_logic.go b/model/gmodel/fs_product_template_v2_logic.go index c29c285d..57109ca8 100755 --- a/model/gmodel/fs_product_template_v2_logic.go +++ b/model/gmodel/fs_product_template_v2_logic.go @@ -140,22 +140,6 @@ func (t *FsProductTemplateV2Model) FindAllByModelIdsTemplateTag(ctx context.Cont return resp, err } -// 获取产品某个物料下的第一个模板 -func (t *FsProductTemplateV2Model) GetSizeLatestTemplate(ctx context.Context, ids []int64, productId int64) (resp *FsProductTemplateV2, err error) { - err = t.db.WithContext(ctx).Model(&FsProductTemplateV2{}). - Where("id in (?) and product_id = ? and status = ? and is_del = ?", ids, productId, 1, 0). - Order("sort ASC").Take(&resp).Error - return resp, err -} - -// 获取产品第一个物料下的第一个模板 -func (t *FsProductTemplateV2Model) GetProductLatestTemplate(ctx context.Context, ids []int64, productId int64) (resp *FsProductTemplateV2, err error) { - err = t.db.WithContext(ctx).Model(&FsProductTemplateV2{}). - Where("id in (?) and product_id = ? and status = ? and is_del = ?", ids, productId, 1, 0). - Order("sort ASC").Take(&resp).Error - return resp, err -} - // 获取产品在指定模板标签下的所有模板 func (t *FsProductTemplateV2Model) GetListByProductAndTemplateTag(ctx context.Context, templateTagId string, productId int64, fields ...string) (resp []FsProductTemplateV2, err error) { db := t.db.WithContext(ctx).Model(&FsProductTemplateV2{}). diff --git a/server/product/internal/logic/gettemplatebypidlogic.go b/server/product/internal/logic/gettemplatebypidlogic.go index 876f6274..51432f3d 100644 --- a/server/product/internal/logic/gettemplatebypidlogic.go +++ b/server/product/internal/logic/gettemplatebypidlogic.go @@ -4,7 +4,7 @@ import ( "encoding/json" "errors" "fmt" - "fusenapi/model/gmodel" + "fusenapi/constants" "fusenapi/utils/auth" "fusenapi/utils/basic" "gorm.io/gorm" @@ -40,7 +40,6 @@ func (l *GetTemplateByPidLogic) GetTemplateByPid(req *types.GetTemplateByPidReq, 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 { @@ -50,46 +49,62 @@ func (l *GetTemplateByPidLogic) GetTemplateByPid(req *types.GetTemplateByPidReq, logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info") } - //获取该产品该模板标签下的模板列表(只获取id) - templateList, err := l.svcCtx.AllModels.FsProductTemplateV2. - GetListByProductAndTemplateTag(l.ctx, fmt.Sprintf("%d", req.ProductTemplateTagId), productInfo.Id, "id") - if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get templates") - } - if len(templateList) == 0 { - return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "templates are not exists") - } - templateIds := make([]int64, 0, len(templateList)) - for _, v := range templateList { - templateIds = append(templateIds, v.Id) - } - var ( - templateInfo *gmodel.FsProductTemplateV2 - ) - //传了指定物料,则获取该物料的第一个模板 - if req.ProductSizeId > 0 { - templateInfo, err = l.svcCtx.AllModels.FsProductTemplateV2.GetSizeLatestTemplate(l.ctx, templateIds, productInfo.Id) - } else { //没有指定物料则获取第一个物料的第一个模板 - templateInfo, err = l.svcCtx.AllModels.FsProductTemplateV2.GetProductLatestTemplate(l.ctx, templateIds, productInfo.Id) - } - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "template info not found") + //没有指定物料 + 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获取模型id + modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllBySizeIdsTag(l.ctx, sizeIds, constants.TAG_MODEL, "id") + if err != nil { logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template info") + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list") } - if templateInfo.TemplateInfo == nil || *templateInfo.TemplateInfo == "" { - return resp.SetStatusWithMessage(basic.CodeJsonErr, "template info is not set") + if len(modelList) == 0 { + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "model list is empty") } - var info interface{} - if err = json.Unmarshal([]byte(*templateInfo.TemplateInfo), &info); err != nil { + modelIds := make([]int64, 0, len(modelList)) + for _, v := range modelList { + modelIds = append(modelIds, v.Id) + } + //查询模型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.CodeJsonErr, "failed to parse json product template info:", templateInfo.Id) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template list") } - return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetTemplateByPidRsp{ - TemplateId: templateInfo.Id, - TemplateInfo: info, - }) + rspList := make([]interface{}, 0, len(templateList)) + for _, templateInfo := range templateList { + //没有设置模板据不要 + if templateInfo.TemplateInfo == nil || *templateInfo.TemplateInfo == "" { + continue + } + //基础模板信息 + var info interface{} + if err = json.Unmarshal([]byte(*templateInfo.TemplateInfo), &info); err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse json product template info:", templateInfo.Id) + } + //后台隐藏/显示信息 + var switchInfo interface{} + if templateInfo.SwitchInfo != nil && *templateInfo.SwitchInfo != "" { + _ = json.Unmarshal([]byte(*templateInfo.SwitchInfo), &switchInfo) + } + // todo 继续下周 + rspList = append(rspList, map[string]interface{}{}) + } + return nil } diff --git a/server_api/product.api b/server_api/product.api index 28ca4ae7..e710a978 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -387,11 +387,6 @@ type GetSizeByPidRsp { type GetTemplateByPidReq { Pid string `form:"pid"` ProductTemplateTagId int64 `form:"product_template_tag_id"` - ProductSizeId int64 `form:"product_size_id,optional"` -} -type GetTemplateByPidRsp { - TemplateId int64 `json:"template_id"` //模板id - TemplateInfo interface{} `json:"template_info"` //模板信息 } //获取产品配件数据 type GetFittingByPidReq {