diff --git a/model/gmodel/fs_product_logic.go b/model/gmodel/fs_product_logic.go index 0afa4100..6328daa4 100755 --- a/model/gmodel/fs_product_logic.go +++ b/model/gmodel/fs_product_logic.go @@ -85,3 +85,35 @@ func (p *FsProductModel) GetRandomProductListInIds(ctx context.Context, ids []in err = db.Find(&resp).Error return resp, err } + +type GetProductListByParamsReq struct { + Type []int64 + IsDel *int64 + IsShelf *int64 + Status *int64 + OrderBy string +} + +func (p *FsProductModel) GetProductListByParams(ctx context.Context, req GetProductListByParamsReq) (resp []FsProduct, err error) { + db := p.db.WithContext(ctx).Model(&FsProduct{}) + if len(req.Type) > 0 { + db = db.Where("`type` in (?)", req.Type) + } + if req.IsDel != nil { + db = db.Where("`is_del` = ?", *req.IsDel) + } + if req.IsShelf != nil { + db = db.Where("`is_shelf` = ?", *req.IsShelf) + } + if req.Status != nil { + db = db.Where("`status` = ?", *req.Status) + } + switch req.OrderBy { + case "": + db = db.Order("`id` DESC") + default: + db = db.Order(req.OrderBy) + } + err = db.Find(&resp).Error + return resp, err +} diff --git a/model/gmodel/fs_tags_gen.go b/model/gmodel/fs_tags_gen.go index 9d72c58f..d38c9f64 100644 --- a/model/gmodel/fs_tags_gen.go +++ b/model/gmodel/fs_tags_gen.go @@ -17,6 +17,7 @@ type FsTags struct { Description *string `gorm:"default:'';" json:"description"` // 介绍 Seo RecommendProduct *string `gorm:"default:'';" json:"recommend_product"` // RecommendProductSort *string `gorm:"default:'';" json:"recommend_product_sort"` // + LevelPrefix *string `gorm:"default:'';" json:"level_prefix"` //归属等级前缀 } type FsTagsModel struct{ db *gorm.DB } diff --git a/model/gmodel/fs_tags_logic.go b/model/gmodel/fs_tags_logic.go index 1a77ccd5..c45bcc05 100755 --- a/model/gmodel/fs_tags_logic.go +++ b/model/gmodel/fs_tags_logic.go @@ -29,3 +29,27 @@ func (t *FsTagsModel) GetAllByLevel(ctx context.Context, level int) (resp []FsTa } return } + +type GetAllTagByParamsReq struct { + Ids []int64 + Status *int64 + OrderBy string +} + +func (t *FsTagsModel) GetAllTagByParams(ctx context.Context, req GetAllTagByParamsReq) (resp []FsTags, err error) { + db := t.db.WithContext(ctx).Model(&FsTags{}) + if len(req.Ids) > 0 { + db = db.Where("`id` in (?)", req.Ids) + } + if req.Status != nil { + db = db.Where("`status` = ?", *req.Status) + } + switch req.OrderBy { + case "": + db = db.Order("`id` DESC") + default: + db = db.Order(req.OrderBy) + } + err = db.Find(&resp).Error + return resp, err +} diff --git a/server/product/internal/handler/gettagproductlisthandler.go b/server/product/internal/handler/gettagproductlisthandler.go new file mode 100644 index 00000000..18a2a96c --- /dev/null +++ b/server/product/internal/handler/gettagproductlisthandler.go @@ -0,0 +1,78 @@ +package handler + +import ( + "errors" + "net/http" + + "github.com/zeromicro/go-zero/core/logx" + "github.com/zeromicro/go-zero/rest/httpx" + + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "fusenapi/server/product/internal/logic" + "fusenapi/server/product/internal/svc" + "fusenapi/server/product/internal/types" +) + +func GetTagProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 + claims, err := svcCtx.ParseJwtToken(r) + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 + }) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 + return + } + + if claims != nil { + // 从token中获取对应的用户信息 + userinfo, err = auth.GetUserInfoFormMapClaims(claims) + // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, + Message: "unauthorized", + }) + logx.Info("unauthorized:", err.Error()) + return + } + } else { + // 如果claims为nil,则认为用户身份为白板用户 + userinfo = &auth.UserInfo{UserId: 0, GuestId: 0} + } + + var req types.GetTagProductListReq + // 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据 + if err := httpx.Parse(r, &req); err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 510, + Message: "parameter error", + }) + logx.Info(err) + return + } + // 创建一个业务逻辑层实例 + l := logic.NewGetTagProductListLogic(r.Context(), svcCtx) + resp := l.GetTagProductList(&req, userinfo) + // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; + if resp != nil { + httpx.OkJsonCtx(r.Context(), w, resp) + } else { + err := errors.New("server logic is error, resp must not be nil") + httpx.ErrorCtx(r.Context(), w, err) + logx.Error(err) + } + } +} diff --git a/server/product/internal/handler/routes.go b/server/product/internal/handler/routes.go index 31b80260..059eb87b 100644 --- a/server/product/internal/handler/routes.go +++ b/server/product/internal/handler/routes.go @@ -57,6 +57,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/product/recommand", Handler: GetRecommandProductListHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/api/product/tag_product_list", + Handler: GetTagProductListHandler(serverCtx), + }, }, ) } diff --git a/server/product/internal/logic/gettagproductlistlogic.go b/server/product/internal/logic/gettagproductlistlogic.go new file mode 100644 index 00000000..3868337d --- /dev/null +++ b/server/product/internal/logic/gettagproductlistlogic.go @@ -0,0 +1,248 @@ +package logic + +import ( + "errors" + "fusenapi/model/gmodel" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "fusenapi/utils/format" + "fusenapi/utils/image" + "gorm.io/gorm" + "sort" + "strings" + + "context" + + "fusenapi/server/product/internal/svc" + "fusenapi/server/product/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type GetTagProductListLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetTagProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTagProductListLogic { + return &GetTagProductListLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListReq, userinfo *auth.UserInfo) (resp *basic.Response) { + //获取合适尺寸 + if req.Size > 0 { + req.Size = image.GetCurrentSize(req.Size) + } + //查询用户信息(不用判断存在) + user, err := l.svcCtx.AllModels.FsUser.FindUserById(l.ctx, userinfo.UserId) + if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "get user info err") + } + //查询分类列表 + tStatus := int64(1) + tReq := gmodel.GetAllTagByParamsReq{ + Status: &tStatus, + OrderBy: "`sort` DESC", + } + //传入分类id + if req.Cid > 0 { + tReq.Ids = []int64{req.Cid} + } + tagList, err := l.svcCtx.AllModels.FsTags.GetAllTagByParams(l.ctx, tReq) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get tag list") + } + if len(tagList) == 0 { + return resp.SetStatusWithMessage(basic.CodeOK, "success", []interface{}{}) + } + typeIds := make([]int64, 0, len(tagList)) + for _, v := range tagList { + typeIds = append(typeIds, v.Id) + } + //查询符合的产品列表 + pIsDel := int64(0) + pStatus := int64(1) + pIsShelf := int64(1) + pReq := gmodel.GetProductListByParamsReq{ + Type: typeIds, + IsDel: &pIsDel, + IsShelf: &pIsShelf, + Status: &pStatus, + OrderBy: "`sort` DESC", + } + //获取产品列表 + productList, err := l.svcCtx.AllModels.FsProduct.GetProductListByParams(l.ctx, pReq) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list") + } + productLen := len(productList) + if productLen == 0 { + return resp.SetStatusWithMessage(basic.CodeOK, "success") + } + //提取产品ids + productIds := make([]int64, 0, productLen) + for _, v := range productList { + productIds = append(productIds, v.Id) + } + productPriceList, err := l.svcCtx.AllModels.FsProductPrice.GetSimplePriceListByProductIds(l.ctx, productIds) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product min price list") + } + //存储产品最小价格 + mapProductMinPrice := make(map[int64]int64) + for _, v := range productPriceList { + priceStrSlic := strings.Split(v.Price, ",") + priceSlice, err := format.StrSlicToIntSlice(priceStrSlic) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error()) + } + if len(priceSlice) == 0 { + continue + } + sort.Ints(priceSlice) + mapProductMinPrice[v.ProductId] = int64(priceSlice[0]) + } + //获取模板 + productTemplatesV2, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByProductIds(l.ctx, productIds) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "get product template_v2 err") + } + mapProductTemplate := make(map[int64]struct{}) + for _, v := range productTemplatesV2 { + mapProductTemplate[*v.ProductId] = struct{}{} + } + //获取产品尺寸数量 + productSizeCountList, err := l.svcCtx.AllModels.FsProductSize.GetGroupProductSizeByStatus(l.ctx, productIds, 1) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "get product size count err") + } + mapProductSizeCount := make(map[int64]int64) + for _, v := range productSizeCountList { + mapProductSizeCount[v.ProductId] = v.Num + } + mapTagLevel := make(map[string]types.TagItem) + minLevel := int64(0) //记录最小等级数字 + for _, tagInfo := range tagList { + if (minLevel == 0 && *tagInfo.Level > 0) || (minLevel > *tagInfo.Level) { + minLevel = *tagInfo.Level + } + productListRsp := l.getTagProductList(getTagProductListReq{ + TagInfo: tagInfo, + ProductList: productList, + MapProductMinPrice: mapProductMinPrice, + MapProductTemplate: mapProductTemplate, + MapProductSizeCount: mapProductSizeCount, + Size: req.Size, + User: user, + }) + //加入分类 + tagTem := types.TagItem{ + TagProductList: productListRsp, + TypeName: *tagInfo.Title, + TypeId: tagInfo.Id, + Level: *tagInfo.Level, + BelongPrefix: *tagInfo.LevelPrefix, + Description: *tagInfo.Description, + ChildTagList: []types.TagItem{}, + } + //当前tag保存入map + mapTagLevel[*tagInfo.LevelPrefix] = tagTem + } + //组装等级从属关系 + for prefix, tagItem := range mapTagLevel { + //查看有没有直接父级,有的话则把当前的append到父级的ChildTagList中 + prefixSlice := strings.Split(prefix, "/") + //等于1表示自己是最上级 + if len(prefixSlice) == 1 { + continue + } + parentPrefix := strings.Join(prefixSlice[:len(prefixSlice)-1], "/") + if parent, ok := mapTagLevel[parentPrefix]; ok { + parent.ChildTagList = append(parent.ChildTagList, tagItem) + mapTagLevel[parentPrefix] = parent + } + } + //最终值提取最高级别那一层出来 + finalSlice := make([]interface{}, 0, len(mapTagLevel)) + for _, v := range mapTagLevel { + if v.Level != minLevel { + continue + } + finalSlice = append(finalSlice, v) + } + return resp.SetStatusWithMessage(basic.CodeOK, "success", finalSlice) +} + +type getTagProductListReq struct { + TagInfo gmodel.FsTags + ProductList []gmodel.FsProduct + MapProductMinPrice map[int64]int64 + MapProductTemplate map[int64]struct{} + MapProductSizeCount map[int64]int64 + Size uint32 + User gmodel.FsUser +} + +// 获取对应tag的产品列表 +func (l *GetTagProductListLogic) getTagProductList(req getTagProductListReq) (productListRsp []types.TagProduct) { + //默认给50个容量 + productListRsp = make([]types.TagProduct, 0, 50) + for _, productInfo := range req.ProductList { + //不属于一个类型则跳过 + if *productInfo.Type != req.TagInfo.Id { + continue + } + minPrice, ok := req.MapProductMinPrice[productInfo.Id] + _, tmpOk := req.MapProductTemplate[productInfo.Id] + //无最小价格则不显示 || 没有模板也不显示 + if !ok || !tmpOk { + continue + } + sizeNum := int64(0) + if mapSizeNum, ok := req.MapProductSizeCount[productInfo.Id]; ok { + sizeNum = mapSizeNum + } + item := types.TagProduct{ + ProductId: productInfo.Id, + Sn: *productInfo.Sn, + Title: *productInfo.Title, + Intro: *productInfo.Intro, + IsEnv: *productInfo.IsProtection, + IsMicro: *productInfo.IsMicrowave, + SizeNum: uint32(sizeNum), + MiniPrice: minPrice, + } + //千人千面处理 + r := image.ThousandFaceImageFormatReq{ + Size: int(req.Size), + IsThousandFace: 0, + Cover: *productInfo.Cover, + CoverImg: *productInfo.CoverImg, + CoverDefault: *productInfo.CoverImg, + ProductId: productInfo.Id, + UserId: req.User.Id, + } + if req.User.Id != 0 { + r.IsThousandFace = int(*req.User.IsThousandFace) + } + image.ThousandFaceImageFormat(&r) + item.Cover = r.Cover + item.CoverImg = r.CoverImg + item.CoverDefault = r.CoverDefault + //加入分类产品切片 + productListRsp = append(productListRsp, item) + } + return productListRsp +} diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index f58d7a3f..65880de8 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -23,10 +23,6 @@ type Ob struct { Meta Meta `json:"_meta"` } -type HrefUrl struct { - Href string `json:"href"` -} - type Items struct { Id int64 `json:"id"` Sn string `json:"sn"` @@ -248,6 +244,39 @@ type GetRecommandProductListRsp struct { Intro string `json:"intro"` } +type GetTagProductListReq struct { + Cid int64 `form:"cid,optional"` //分类id + Size uint32 `form:"size,optional"` //尺寸 +} + +type GetTagProductListRsp struct { + TagList []TagItem `json:"tag_list"` +} + +type TagItem struct { + TypeName string `json:"typeName"` + TypeId int64 `json:"type_id"` + Description string `json:"description"` + Level int64 `json:"level"` + BelongPrefix string `json:"belong_prefix"` + TagProductList []TagProduct `json:"tag_product_list"` + ChildTagList []TagItem `json:"child_tag_list"` +} + +type TagProduct struct { + ProductId int64 `json:"product_id"` + Sn string `json:"sn"` + Title string `json:"title"` + Cover string `json:"cover"` + Intro string `json:"intro"` + CoverImg string `json:"cover_img"` + IsEnv int64 `json:"isEnv"` + IsMicro int64 `json:"isMicro"` + SizeNum uint32 `json:"sizeNum"` + MiniPrice int64 `json:"miniPrice"` + CoverDefault string `json:"coverDefault"` +} + type Request struct { } diff --git a/server_api/product.api b/server_api/product.api index e59a9b36..ae8ab5a2 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -37,6 +37,9 @@ service product { //获取详情页推荐产品列表 @handler GetRecommandProductListHandler get /api/product/recommand (GetRecommandProductListReq) returns (response); + //获取分类产品列表 + @handler GetTagProductListHandler + get /api/product/tag_product_list(GetTagProductListReq) returns (response); } //获取产品列表 @@ -55,9 +58,6 @@ type Ob { Items []Items `json:"items"` Meta Meta `json:"_meta"` } -type HrefUrl { - Href string `json:"href"` -} type Items { Id int64 `json:"id"` Sn string `json:"sn"` @@ -259,4 +259,34 @@ type GetRecommandProductListRsp { CoverImg string `json:"cover_img"` CoverDefault string `json:"cover_default"` Intro string `json:"intro"` +} +//获取分类产品列表 +type GetTagProductListReq { + Cid int64 `form:"cid,optional"` //分类id + Size uint32 `form:"size,optional"` //尺寸 +} +type GetTagProductListRsp { + TagList []TagItem `json:"tag_list"` +} +type TagItem { + TypeName string `json:"typeName"` + TypeId int64 `json:"type_id"` + Description string `json:"description"` + Level int64 `json:"level"` + BelongPrefix string `json:"belong_prefix"` + TagProductList []TagProduct `json:"tag_product_list"` + ChildTagList []TagItem `json:"child_tag_list"` +} +type TagProduct { + ProductId int64 `json:"product_id"` + Sn string `json:"sn"` + Title string `json:"title"` + Cover string `json:"cover"` + Intro string `json:"intro"` + CoverImg string `json:"cover_img"` + IsEnv int64 `json:"isEnv"` + IsMicro int64 `json:"isMicro"` + SizeNum uint32 `json:"sizeNum"` + MiniPrice int64 `json:"miniPrice"` + CoverDefault string `json:"coverDefault"` } \ No newline at end of file