This commit is contained in:
laodaming 2023-06-07 17:27:17 +08:00
parent 3d21e2491f
commit 9007cc8369
13 changed files with 353 additions and 30 deletions

14
constants/tag_level.go Normal file
View File

@ -0,0 +1,14 @@
package constants
// tag分类等级
const (
TYPE_QRCODE = iota + 1 //1
TYPE_WEBSITE //2
TYPE_DESIGN_TOOL //3
)
var MAP_TAG_LEVEL = map[int]string{
TYPE_QRCODE: "二维码分类",
TYPE_WEBSITE: "网站目录",
TYPE_DESIGN_TOOL: "设计工具",
}

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"strings"
)
var _ FsProductModel = (*customFsProductModel)(nil)
@ -13,7 +14,7 @@ type (
// and implement the added methods in customFsProductModel.
FsProductModel interface {
fsProductModel
GetProductListByConditions(ctx context.Context, productType int, sort string) ([]FsProduct, error)
GetProductListByConditions(ctx context.Context, productTypes []string, sort string) ([]FsProduct, error)
GetRandomProductList(ctx context.Context, limit int) (resp []FsProduct, err error)
}
@ -28,8 +29,8 @@ func NewFsProductModel(conn sqlx.SqlConn) FsProductModel {
defaultFsProductModel: newFsProductModel(conn),
}
}
func (m *defaultFsProductModel) GetProductListByConditions(ctx context.Context, productType int, sort string) (resp []FsProduct, err error) {
query := fmt.Sprintf("select %s from %s where `type` = ? and `is_del` =? and `is_shelf` = ?",
func (m *defaultFsProductModel) GetProductListByConditions(ctx context.Context, productTypes []string, sort string) (resp []FsProduct, err error) {
query := fmt.Sprintf("select %s from %s where `type` in (?) and `is_del` =? and `is_shelf` = ? and `status` =?",
fsProductRows, m.table)
switch sort {
case "sort-asc":
@ -39,7 +40,7 @@ func (m *defaultFsProductModel) GetProductListByConditions(ctx context.Context,
default:
query = fmt.Sprintf("%s order by sort DESC", query)
}
err = m.conn.QueryRowsCtx(ctx, &resp, query, productType, 0, 1)
err = m.conn.QueryRowsCtx(ctx, &resp, query, strings.Join(productTypes, ","), 0, 1, 1)
if err != nil {
return nil, err
}

View File

@ -15,7 +15,8 @@ type (
// and implement the added methods in customFsProductPriceModel.
FsProductPriceModel interface {
fsProductPriceModel
GetPriceList(ctx context.Context, productIds []string) ([]GetPriceListRsp, error)
GetPriceListByProductIds(ctx context.Context, productIds []string) (resp []GetPriceListRsp, err error)
GetPriceListBySizeIds(ctx context.Context, sizeIds []string) (resp []FsProductPrice, err error)
FindOneByProductIdMaterialIdSizeId(ctx context.Context, productId int64, materialId int64, sizeId int64) (*FsProductPrice, error)
}
@ -36,7 +37,7 @@ type GetPriceListRsp struct {
Price string `json:"price"`
}
func (m *defaultFsProductPriceModel) GetPriceList(ctx context.Context, productIds []string) (resp []GetPriceListRsp, err error) {
func (m *defaultFsProductPriceModel) GetPriceListByProductIds(ctx context.Context, productIds []string) (resp []GetPriceListRsp, err error) {
if len(productIds) == 0 {
return nil, nil
}
@ -59,3 +60,14 @@ func (m *defaultFsProductPriceModel) FindOneByProductIdMaterialIdSizeId(ctx cont
return nil, err
}
}
func (m *defaultFsProductPriceModel) GetPriceListBySizeIds(ctx context.Context, sizeIds []string) (resp []FsProductPrice, err error) {
if len(sizeIds) == 0 {
return nil, nil
}
query := fmt.Sprintf("select %s from %s where `size_id` in (?) and `status` = ? ", fsProductPriceRows, m.table)
if err = m.conn.QueryRowsCtx(ctx, &resp, query, strings.Join(sizeIds, ","), 1); err != nil {
return nil, err
}
return
}

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"strings"
)
var _ FsProductSizeModel = (*customFsProductSizeModel)(nil)
@ -14,7 +15,7 @@ type (
FsProductSizeModel interface {
fsProductSizeModel
CountByStatus(ctx context.Context, status int) (total int, err error)
FindAllByStatus(ctx context.Context, status int, sort int) ([]FsProductSize, error)
FindAllByProductIds(ctx context.Context, productIds []string, sort int) (resp []FsProductSize, err error)
}
customFsProductSizeModel struct {
@ -36,15 +37,15 @@ func (m *defaultFsProductSizeModel) CountByStatus(ctx context.Context, status in
}
return
}
func (m *defaultFsProductSizeModel) FindAllByStatus(ctx context.Context, status int, sort int) (resp []FsProductSize, err error) {
query := fmt.Sprintf("select %s from %s where `status` = ? ", fsProductSizeRows, m.table)
func (m *defaultFsProductSizeModel) FindAllByProductIds(ctx context.Context, productIds []string, sort int) (resp []FsProductSize, err error) {
query := fmt.Sprintf("select %s from %s where `product_id` in(?) and `status` = ? ", fsProductSizeRows, m.table)
switch sort {
case 1:
query = fmt.Sprintf("%s order by `sort` ASC", query)
case 2:
query = fmt.Sprintf("%s order by `sort` DESC", query)
}
err = m.conn.QueryRowsCtx(ctx, &resp, query, status)
err = m.conn.QueryRowsCtx(ctx, &resp, query, strings.Join(productIds, ","), 1)
if err != nil {
return nil, err
}

View File

@ -1,6 +1,10 @@
package model
import "github.com/zeromicro/go-zero/core/stores/sqlx"
import (
"context"
"fmt"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ FsTagsModel = (*customFsTagsModel)(nil)
@ -9,6 +13,7 @@ type (
// and implement the added methods in customFsTagsModel.
FsTagsModel interface {
fsTagsModel
ListAllByLevelStatus(ctx context.Context, level int, status int) (resp []FsTags, err error)
}
customFsTagsModel struct {
@ -22,3 +27,12 @@ func NewFsTagsModel(conn sqlx.SqlConn) FsTagsModel {
defaultFsTagsModel: newFsTagsModel(conn),
}
}
func (m *defaultFsTagsModel) ListAllByLevelStatus(ctx context.Context, level int, status int) (resp []FsTags, err error) {
query := fmt.Sprintf("select %s from %s where `level` = ? and `status` = ?", fsTagsRows, m.table)
err = m.conn.QueryRowsCtx(ctx, &resp, query, level, status)
if err != nil {
return nil, err
}
return
}

View File

@ -0,0 +1,26 @@
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/product/internal/logic"
"fusenapi/product/internal/svc"
)
func GetSizeByProductHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := logic.NewGetSizeByProductLogic(r.Context(), svcCtx)
resp := l.GetSizeByProduct()
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)
}
}
}

View File

@ -25,4 +25,14 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
},
rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
)
server.AddRoutes(
[]rest.Route{
{
Method: http.MethodGet,
Path: "/product/get-size-by-product",
Handler: GetSizeByProductHandler(serverCtx),
},
},
)
}

View File

@ -38,14 +38,14 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp
resp = &types.Response{}
loginInfo := auth.GetUserInfoFormCtx(l.ctx)
if loginInfo.UserId == 0 {
return resp.SetStatus(basic.CodeServiceErr, "get login user info err")
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get login user info err")
}
//如果是demo
if req.IsDemo == 1 {
var demo types.GetProductListRsp
if err := json.Unmarshal([]byte(constants.PRODUCT_LIST_DEMO), &demo); err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeServiceErr, "demo data format err")
return resp.SetStatusWithMessage(basic.CodeServiceErr, "demo data format err")
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", demo)
}
@ -61,21 +61,21 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp
userInfo, err := userModel.FindOne(l.ctx, loginInfo.UserId)
if err != nil && !errors.Is(err, sqlc.ErrNotFound) {
logx.Error(err)
return resp.SetStatus(basic.CodeServiceErr, "get user info err")
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get user info err")
}
if userInfo == nil {
return resp.SetStatus(basic.CodeUnAuth, "user not exists")
return resp.SetStatusWithMessage(basic.CodeUnAuth, "user not exists")
}
//查询符合的产品列表
productModel := model.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetProductListByConditions(l.ctx, int(req.Cid), "sort-desc")
productList, err := productModel.GetProductListByConditions(l.ctx, []string{fmt.Sprintf("%d", req.Cid)}, "sort-desc")
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeServiceErr, "failed to get product list")
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list")
}
productLen := len(productList)
if productLen == 0 {
return resp.SetStatus(basic.CodeOK, "success")
return resp.SetStatusWithMessage(basic.CodeOK, "success")
}
//提取产品ids
productIds := make([]string, 0, productLen)
@ -83,10 +83,10 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp
productIds = append(productIds, fmt.Sprintf("%d", v.Id))
}
productPriceModel := model.NewFsProductPriceModel(l.svcCtx.MysqlConn)
productPriceList, err := productPriceModel.GetPriceList(l.ctx, productIds)
productPriceList, err := productPriceModel.GetPriceListByProductIds(l.ctx, productIds)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeServiceErr, "failed to get product min price list")
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product min price list")
}
//存储产品最小价格
mapProductMinPrice := make(map[int64]int64)
@ -95,7 +95,7 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp
priceSlice, err := format.StrSlicToIntSlice(priceStrSlic)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeServiceErr, err.Error())
return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
}
if len(priceSlice) == 0 {
continue
@ -108,7 +108,7 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp
productTemplatesV2, err := productTemplateModel.FindAllByCondition(l.ctx, productIds)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeServiceErr, "get product template_v2 err")
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get product template_v2 err")
}
mapProductTemplate := make(map[int64]struct{})
for _, v := range productTemplatesV2 {
@ -119,17 +119,17 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp
tagInfo, err := tagsModel.FindOne(l.ctx, req.Cid)
if err != nil && !errors.Is(err, sqlc.ErrNotFound) {
logx.Error(err)
return resp.SetStatus(basic.CodeServiceErr, "get tag err")
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get tag err")
}
if tagInfo == nil {
return resp.SetStatus(basic.CodeServiceErr, "tag is not exists")
return resp.SetStatusWithMessage(basic.CodeServiceErr, "tag is not exists")
}
//获取产品尺寸数量
productSizeModel := model.NewFsProductSizeModel(l.svcCtx.MysqlConn)
productSizeCount, err := productSizeModel.CountByStatus(l.ctx, 1)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeServiceErr, "get product size count err")
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get product size count err")
}
//拼接返回
itemList := make([]types.Items, 0, productLen)

View File

@ -0,0 +1,184 @@
package logic
import (
"context"
"fmt"
"fusenapi/constants"
"fusenapi/model"
"fusenapi/utils/basic"
"fusenapi/utils/format"
"strings"
"fusenapi/product/internal/svc"
"fusenapi/product/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetSizeByProductLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetSizeByProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSizeByProductLogic {
return &GetSizeByProductLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 获取分类下的产品以及尺寸
func (l *GetSizeByProductLogic) GetSizeByProduct() (resp *types.Response) {
//获取所有网站目录
tagsModel := model.NewFsTagsModel(l.svcCtx.MysqlConn)
tagsList, err := tagsModel.ListAllByLevelStatus(l.ctx, constants.TYPE_WEBSITE, 1)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get website tags")
}
if len(tagsList) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "tag list is null")
}
tagIds := make([]string, 0, len(tagsList))
for _, v := range tagsList {
tagIds = append(tagIds, fmt.Sprintf("%d", v.Id))
}
//获取这些类型的产品
productModel := model.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetProductListByConditions(l.ctx, tagIds, "sort-desc")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get tag product list")
}
productIds := make([]string, 0, len(productList))
for _, v := range productList {
productIds = append(productIds, fmt.Sprintf("%d", v.Id))
}
productSizeModel := model.NewFsProductSizeModel(l.svcCtx.MysqlConn)
productSizeList, err := productSizeModel.FindAllByProductIds(l.ctx, productIds, 2)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product size list")
}
sizeIds := make([]string, 0, len(productSizeList))
for _, v := range productSizeList {
sizeIds = append(sizeIds, fmt.Sprintf("%d", v.Id))
}
//获取价格列表
productPriceModel := model.NewFsProductPriceModel(l.svcCtx.MysqlConn)
productPriceList, err := productPriceModel.GetPriceListBySizeIds(l.ctx, sizeIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product proce list")
}
mapProductPrice := make(map[int64]model.FsProductPrice)
for _, v := range productPriceList {
mapProductPrice[v.SizeId] = v
}
//组装返回
list := make([]types.GetSizeByProductRsp, 0, len(tagsList))
for _, tag := range tagsList {
//获取第一层子类
firstChildrenList, err := l.GetFirstChildrenList(tag, productList, productSizeList, mapProductPrice)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get first level children list")
}
data := types.GetSizeByProductRsp{
Id: tag.Id,
Name: tag.Title,
Children: firstChildrenList,
}
list = append(list, data)
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", list)
}
// 第一层子层
func (l *GetSizeByProductLogic) GetFirstChildrenList(tag model.FsTags, productList []model.FsProduct, productSizeList []model.FsProductSize, mapProductPrice map[int64]model.FsProductPrice) (childrenList []types.Children, err error) {
childrenList = make([]types.Children, 0, len(productList))
for _, product := range productList {
if product.Type != tag.Id {
continue
}
childrenObjList, err := l.GetSecondChildrenList(tag, product, productSizeList, mapProductPrice)
if err != nil {
return nil, err
}
//获取第二层子类
data := types.Children{
Id: product.Id,
Name: product.Title,
Cycle: int(product.DeliveryDays + product.ProduceDays),
ChildrenList: childrenObjList,
}
childrenList = append(childrenList, data)
}
return
}
// 第2层子层
func (l *GetSizeByProductLogic) GetSecondChildrenList(tag model.FsTags, product model.FsProduct, productSizeList []model.FsProductSize, mapProductPrice map[int64]model.FsProductPrice) (childrenObjList []types.ChildrenObj, err error) {
childrenObjList = make([]types.ChildrenObj, 0, len(productSizeList))
for _, productSize := range productSizeList {
if product.Id != productSize.ProductId {
continue
}
priceList := make([]types.PriceObj, 0, len(productSizeList))
//无对应尺寸价格
if price, ok := mapProductPrice[productSize.Id]; !ok {
priceList = []types.PriceObj{
{Num: 1, Price: 0},
{Num: 1, Price: 0},
{Num: 1, Price: 0},
}
} else {
price.StepNum = strings.Trim(price.StepNum, " ")
price.StepPrice = strings.Trim(price.StepPrice, " ")
if price.StepNum == "" || price.StepPrice == "" {
continue
}
//阶梯数量切片
stepNum, err := format.StrSlicToIntSlice(strings.Split(price.StepNum, ","))
if err != nil {
return nil, err
}
//阶梯价格切片
stepPrice, err := format.StrSlicToIntSlice(strings.Split(price.StepPrice, ","))
if err != nil {
return nil, err
}
for i := 0; i < 3; i++ {
// 最小购买数量小于 最大阶梯数量+5
if int(price.MinBuyNum) < (stepNum[len(stepNum)-1] + 5) {
priceList = append(priceList, types.PriceObj{
Num: int(price.MinBuyNum * price.EachBoxNum),
Price: l.GetPrice(int(price.MinBuyNum), stepNum, stepPrice),
})
}
price.MinBuyNum++
}
//如果不够三个则追加伪数据
for len(priceList) < 3 {
priceList = append(priceList, types.PriceObj{Num: 1, Price: 0})
}
}
data := types.ChildrenObj{
Id: productSize.Id,
Name: productSize.Capacity,
PriceList: priceList,
}
childrenObjList = append(childrenObjList, data)
}
return
}
func (l *GetSizeByProductLogic) GetPrice(num int, stepNum []int, stepPrice []int) float64 {
for _, v := range stepNum {
if num <= v {
return float64(v) / float64(100)
}
}
return float64(stepPrice[len(stepPrice)-1]) / float64(100)
}

View File

@ -32,17 +32,17 @@ func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessReco
resp = &types.Response{}
loginInfo := auth.GetUserInfoFormCtx(l.ctx)
if loginInfo.UserId == 0 {
return resp.SetStatus(basic.CodeUnAuth, "get login user info err")
return resp.SetStatusWithMessage(basic.CodeUnAuth, "get login user info err")
}
//获取用户信息
userModel := model.NewFsUserModel(l.svcCtx.MysqlConn)
userInfo, err := userModel.FindOne(l.ctx, loginInfo.UserId)
if err != nil && errors.Is(err, sqlx.ErrNotFound) {
logx.Error(err)
return resp.SetStatus(basic.CodeServiceErr, "failed to get user info")
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get user info")
}
if userInfo == nil {
return resp.SetStatus(basic.CodeUnAuth, "failed to get user info")
return resp.SetStatusWithMessage(basic.CodeUnAuth, "failed to get user info")
}
if req.Num == 0 || req.Num > 500 {
req.Num = 8
@ -55,11 +55,11 @@ func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessReco
productList, err := productModel.GetRandomProductList(l.ctx, int(req.Num))
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeServiceErr, "failed to get product list")
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list")
}
//没有推荐产品就返回
if len(productList) == 0 {
return resp.SetStatus(basic.CodeOK, "success")
return resp.SetStatusWithMessage(basic.CodeOK, "success")
}
list := make([]types.GetSuccessRecommandRsp, 0, len(productList))
for _, v := range productList {

View File

@ -72,6 +72,30 @@ type GetSuccessRecommandRsp struct {
CoverDefault string `json:"coverDefault"`
}
type GetSizeByProductRsp struct {
Id int64 `json:"id"`
Name string `json:"name"`
Children []Children `json:"children"`
}
type Children struct {
Id int64 `json:"id"`
Name string `json:"name"`
Cycle int `json:"cycle"`
ChildrenList []ChildrenObj `json:"children"`
}
type ChildrenObj struct {
Id int64 `json:"id"`
Name string `json:"name"`
PriceList []PriceObj `json:"price_list"`
}
type PriceObj struct {
Num int `json:"num"`
Price float64 `json:"price"`
}
type Response struct {
Code int `json:"code"`
Message string `json:"msg"`

7
product/product_test.go Normal file
View File

@ -0,0 +1,7 @@
package main
import "testing"
func TestMain(t *testing.T) {
main()
}

View File

@ -7,6 +7,7 @@ info (
email: ""
)
import "basic.api"
//验证登录
@server(
jwt: Auth
)
@ -19,6 +20,13 @@ service product {
get /product/success-recommand (GetSuccessRecommandReq) returns (response);
}
//非登录接口
service product {
//获取分类下的产品以及尺寸
@handler GetSizeByProduct
get /product/get-size-by-product () returns (response);
}
//获取产品列表
type GetProductListReq {
Cid int64 `form:"cid"`
@ -78,4 +86,26 @@ type GetSuccessRecommandRsp {
Id int64 `json:"id"`
SkuId int64 `json:"skuId"`
CoverDefault string `json:"coverDefault"`
}
//获取分类下的产品以及尺寸
type GetSizeByProductRsp {
Id int64 `json:"id"`
Name string `json:"name"`
Children []Children `json:"children"`
}
type Children {
Id int64 `json:"id"`
Name string `json:"name"`
Cycle int `json:"cycle"`
ChildrenList []ChildrenObj `json:"children"`
}
type ChildrenObj {
Id int64 `json:"id"`
Name string `json:"name"`
PriceList []PriceObj `json:"price_list"`
}
type PriceObj {
Num int `json:"num"`
Price float64 `json:"price"`
}