diff --git a/model/gmodel/fs_product_template_v2_logic.go b/model/gmodel/fs_product_template_v2_logic.go index d760f398..c29c285d 100755 --- a/model/gmodel/fs_product_template_v2_logic.go +++ b/model/gmodel/fs_product_template_v2_logic.go @@ -139,3 +139,30 @@ func (t *FsProductTemplateV2Model) FindAllByModelIdsTemplateTag(ctx context.Cont err = db.Find(&resp).Error 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{}). + Where("tag = ? and product_id = ? and status = ? and is_del = ?", templateTagId, productId, 1, 0) + if len(fields) > 0 { + db = db.Select(fields[0]) + } + err = db.Find(&resp).Error + return resp, err +} diff --git a/server/product/internal/logic/gettemplatebypidlogic.go b/server/product/internal/logic/gettemplatebypidlogic.go index 18ccb887..876f6274 100644 --- a/server/product/internal/logic/gettemplatebypidlogic.go +++ b/server/product/internal/logic/gettemplatebypidlogic.go @@ -4,12 +4,10 @@ import ( "encoding/json" "errors" "fmt" - "fusenapi/constants" + "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" - "fusenapi/utils/image" "gorm.io/gorm" - "strconv" "strings" "context" @@ -39,13 +37,11 @@ func (l *GetTemplateByPidLogic) GetTemplateByPid(req *types.GetTemplateByPidReq, if req.Pid == "" { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:pid is empty") } - if req.Size > 0 { - req.Size = image.GetCurrentSize(req.Size) - } if req.ProductTemplateTagId <= 0 { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:product_template_tag_id") } - //获取产品信息(只是获取id) + + //获取产品信息(只获取id) productInfo, err := l.svcCtx.AllModels.FsProduct.FindOneBySn(l.ctx, req.Pid, "id") if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { @@ -54,99 +50,46 @@ func (l *GetTemplateByPidLogic) GetTemplateByPid(req *types.GetTemplateByPidReq, logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info") } - //获取尺寸ids(只获取id) - sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByProductIds(l.ctx, []int64{productInfo.Id}, "id") + //获取该产品该模板标签下的模板列表(只获取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 size list") + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get templates") } - if len(sizeList) == 0 { - return resp.SetStatusWithMessage(basic.CodeOK, "success:size list is empty") + if len(templateList) == 0 { + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "templates are not exists") } - sizeIds := make([]int64, 0, len(sizeList)) - for _, v := range sizeList { - sizeIds = append(sizeIds, v.Id) + 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) } - //获取模型数据 - modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllBySizeIdsTag(l.ctx, sizeIds, constants.TAG_MODEL, "id,size_id") if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "template info not found") + } logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list") + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template info") } - if len(modelList) == 0 { - return resp.SetStatusWithMessage(basic.CodeOK, "success:model list is empty") + if templateInfo.TemplateInfo == nil || *templateInfo.TemplateInfo == "" { + return resp.SetStatusWithMessage(basic.CodeJsonErr, "template info is not set") } - modelIds := make([]int64, 0, len(modelList)) - mapModel := make(map[int64]int) - for k, v := range modelList { - modelIds = append(modelIds, v.Id) - mapModel[v.Id] = k - } - //获取模板数据 - productTemplateList, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByModelIdsTemplateTag(l.ctx, modelIds, fmt.Sprintf("%d", req.ProductTemplateTagId), "sort DESC") - if err != nil { + var info interface{} + if err = json.Unmarshal([]byte(*templateInfo.TemplateInfo), &info); err != nil { logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product templates") + return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse json product template info:", templateInfo.Id) } - if len(productTemplateList) == 0 { - return resp.SetStatusWithMessage(basic.CodeOK, "success:product template list is empty") - } - tagIds := make([]int64, 0, len(productTemplateList)) - for _, v := range productTemplateList { - if *v.Tag == "" { - continue - } - tag, err := strconv.ParseInt(*v.Tag, 10, 64) - if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, "template tag is not a number:"+*v.Tag) - } - tagIds = append(tagIds, tag) - } - //获取模板标签列表 - templateTagList, err := l.svcCtx.AllModels.FsProductTemplateTags.GetListByIds(l.ctx, tagIds) - if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template tag list") - } - mapTemplateTag := make(map[string]int) - for k, v := range templateTagList { - mapTemplateTag[fmt.Sprintf("%d", v.Id)] = k - } - mapRsp := make(map[string][]interface{}) - for _, v := range productTemplateList { - //过滤没有设置详细数据的模板 - if v.TemplateInfo == nil || *v.TemplateInfo == "" { - continue - } - var templateInfo map[string]interface{} - if err = json.Unmarshal([]byte(*v.TemplateInfo), &templateInfo); err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse json:template info") - } - if templateInfo["cover"] != nil && templateInfo["cover"].(string) != "" { - cover := strings.Split(templateInfo["cover"].(string), ".") - if req.Size >= 200 && len(cover) >= 2 { - templateInfo["cover"] = fmt.Sprintf("%s_%d.%s", cover[0], req.Size, cover[1]) - } - } else { - templateInfo["cover"] = "" - } - templateInfo["tag_name"] = "" - if tagIndex, ok := mapTemplateTag[*v.Tag]; ok { - templateInfo["tag_name"] = *templateTagList[tagIndex].Title - } - templateInfo["title"] = *v.Title - modelIndex, ok := mapModel[*v.ModelId] - if !ok { - continue - } - key := fmt.Sprintf("_%d", *modelList[modelIndex].SizeId) - if _, ok = mapRsp[key]; ok { - mapRsp[key] = append(mapRsp[key], templateInfo) - } else { - mapRsp[key] = []interface{}{templateInfo} - } - } - return resp.SetStatusWithMessage(basic.CodeOK, "success", mapRsp) + return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetTemplateByPidRsp{ + TemplateId: templateInfo.Id, + TemplateInfo: info, + }) } diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index cf94b40b..8e718fb7 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -341,8 +341,13 @@ type GetSizeByPidRsp struct { type GetTemplateByPidReq struct { Pid string `form:"pid"` - Size uint32 `form:"size"` ProductTemplateTagId int64 `form:"product_template_tag_id"` + ProductSizeId int64 `form:"product_size_id,optional"` +} + +type GetTemplateByPidRsp struct { + TemplateId int64 `json:"template_id"` //模板id + TemplateInfo interface{} `json:"template_info"` //模板信息 } type GetFittingByPidReq struct { diff --git a/server_api/product.api b/server_api/product.api index b4aca200..28ca4ae7 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -50,7 +50,7 @@ service product { //获取产品尺寸列表 @handler GetSizeByPidHandler get /api/product/get_size_by_pid(GetSizeByPidReq) returns (response); - //获取产品模板列表 + //获取产品模板 @handler GetTemplateByPidHandler get /api/product/get_template_by_pid(GetTemplateByPidReq) returns (response); //获取产品配件数据 @@ -383,11 +383,15 @@ type GetSizeByPidRsp { IsPopular bool `json:"is_popular"` //是否受欢迎 MinPrice float64 `json:"min_price"` //最小价格 } -//获取产品模板列表 +//获取产品模板 type GetTemplateByPidReq { Pid string `form:"pid"` - Size uint32 `form:"size"` 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 {