fusenapi/server/product/internal/logic/getsizebyproductlogic.go

189 lines
6.0 KiB
Go
Raw Normal View History

2023-06-07 09:27:17 +00:00
package logic
import (
"context"
2023-06-07 10:09:54 +00:00
"errors"
2023-06-07 09:27:17 +00:00
"fmt"
"fusenapi/constants"
2023-06-12 08:47:48 +00:00
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
2023-06-07 09:27:17 +00:00
"fusenapi/utils/basic"
"fusenapi/utils/format"
2023-06-13 09:47:48 +00:00
"fusenapi/utils/step_price"
2023-06-07 09:27:17 +00:00
"strings"
2023-06-08 03:03:20 +00:00
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
2023-06-07 09:27:17 +00:00
"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,
}
}
// 获取分类下的产品以及尺寸
2023-06-15 04:00:32 +00:00
func (l *GetSizeByProductLogic) GetSizeByProduct(userinfo *auth.UserInfo) (resp *basic.Response) {
if userinfo.GetIdType() != auth.IDTYPE_User {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first")
}
2023-06-07 09:27:17 +00:00
//获取所有网站目录
2023-06-12 08:47:48 +00:00
tagsModel := gmodel.NewFsTagsModel(l.svcCtx.MysqlConn)
2023-06-09 12:06:24 +00:00
tagsList, err := tagsModel.GetAllByLevel(l.ctx, constants.TYPE_WEBSITE)
2023-06-07 09:27:17 +00:00
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")
}
2023-06-12 08:47:48 +00:00
tagIds := make([]int64, 0, len(tagsList))
2023-06-07 09:27:17 +00:00
for _, v := range tagsList {
2023-06-12 08:47:48 +00:00
tagIds = append(tagIds, v.Id)
2023-06-07 09:27:17 +00:00
}
//获取这些类型的产品
2023-06-12 08:47:48 +00:00
productModel := gmodel.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetProductListByIds(l.ctx, tagIds, "sort-desc")
2023-06-07 09:27:17 +00:00
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get tag product list")
}
2023-06-12 08:47:48 +00:00
productIds := make([]int64, 0, len(productList))
2023-06-07 09:27:17 +00:00
for _, v := range productList {
2023-06-12 08:47:48 +00:00
productIds = append(productIds, v.Id)
2023-06-07 09:27:17 +00:00
}
2023-06-12 08:47:48 +00:00
productSizeModel := gmodel.NewFsProductSizeModel(l.svcCtx.MysqlConn)
2023-07-14 08:48:04 +00:00
productSizeList, err := productSizeModel.GetAllByProductIds(l.ctx, productIds, "sort DESC")
2023-06-07 09:27:17 +00:00
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product size list")
}
2023-06-12 08:47:48 +00:00
sizeIds := make([]int64, 0, len(productSizeList))
2023-06-07 09:27:17 +00:00
for _, v := range productSizeList {
2023-06-12 08:47:48 +00:00
sizeIds = append(sizeIds, v.Id)
2023-06-07 09:27:17 +00:00
}
//获取价格列表
2023-06-12 08:47:48 +00:00
productPriceModel := gmodel.NewFsProductPriceModel(l.svcCtx.MysqlConn)
2023-06-07 09:27:17 +00:00
productPriceList, err := productPriceModel.GetPriceListBySizeIds(l.ctx, sizeIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product proce list")
}
2023-06-12 08:47:48 +00:00
mapProductPrice := make(map[int64]gmodel.FsProductPrice)
2023-06-07 09:27:17 +00:00
for _, v := range productPriceList {
2023-06-12 08:47:48 +00:00
mapProductPrice[*v.SizeId] = v
2023-06-07 09:27:17 +00:00
}
//组装返回
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,
2023-06-12 08:47:48 +00:00
Name: *tag.Title,
2023-06-07 09:27:17 +00:00
Children: firstChildrenList,
}
list = append(list, data)
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", list)
}
// 第一层子层
2023-06-29 03:43:07 +00:00
func (l *GetSizeByProductLogic) GetFirstChildrenList(tag gmodel.FsTags, productList []gmodel.FsProduct, productSizeList []gmodel.FsProductSize, mapProductPrice map[int64]gmodel.FsProductPrice) (childrenList []types.Children, err error) {
childrenList = make([]types.Children, 0, len(productList))
2023-06-07 09:27:17 +00:00
for _, product := range productList {
2023-06-12 08:47:48 +00:00
if *product.Type != tag.Id {
2023-06-07 09:27:17 +00:00
continue
}
2023-06-12 08:47:48 +00:00
childrenObjList, err := l.GetSecondChildrenList(product, productSizeList, mapProductPrice)
2023-06-07 09:27:17 +00:00
if err != nil {
return nil, err
}
//获取第二层子类
2023-06-29 03:43:07 +00:00
data := types.Children{
2023-06-07 09:27:17 +00:00
Id: product.Id,
2023-06-12 08:47:48 +00:00
Name: *product.Title,
Cycle: int(*product.DeliveryDays + *product.ProduceDays),
2023-06-07 09:27:17 +00:00
ChildrenList: childrenObjList,
}
childrenList = append(childrenList, data)
}
return
}
// 第2层子层
2023-06-29 03:43:07 +00:00
func (l *GetSizeByProductLogic) GetSecondChildrenList(product gmodel.FsProduct, productSizeList []gmodel.FsProductSize, mapProductPrice map[int64]gmodel.FsProductPrice) (childrenObjList []types.ChildrenObj, err error) {
childrenObjList = make([]types.ChildrenObj, 0, len(productSizeList))
2023-06-07 09:27:17 +00:00
for _, productSize := range productSizeList {
2023-06-12 08:47:48 +00:00
if product.Id != *productSize.ProductId {
2023-06-07 09:27:17 +00:00
continue
}
2023-06-29 03:43:07 +00:00
priceList := make([]types.PriceObj, 0, len(productSizeList))
2023-06-07 10:09:54 +00:00
price, ok := mapProductPrice[productSize.Id]
2023-06-07 09:27:17 +00:00
//无对应尺寸价格
2023-06-07 10:09:54 +00:00
if !ok {
2023-06-29 09:48:07 +00:00
priceList = []types.PriceObj{
{Num: 1, Price: 0},
{Num: 1, Price: 0},
{Num: 1, Price: 0},
2023-06-20 10:37:56 +00:00
}
2023-06-29 03:43:07 +00:00
childrenObjList = append(childrenObjList, types.ChildrenObj{
2023-06-20 10:37:56 +00:00
Id: productSize.Id,
Name: *productSize.Capacity,
PriceList: priceList,
2023-06-07 10:09:54 +00:00
})
continue
}
2023-06-12 08:47:48 +00:00
if price.StepNum == nil || price.StepPrice == nil {
2023-06-07 10:09:54 +00:00
continue
}
2023-06-12 08:47:48 +00:00
*price.StepNum = strings.Trim(*price.StepNum, " ")
*price.StepPrice = strings.Trim(*price.StepPrice, " ")
2023-06-07 10:09:54 +00:00
//阶梯数量切片
2023-06-12 08:47:48 +00:00
stepNum, err := format.StrSlicToIntSlice(strings.Split(*price.StepNum, ","))
2023-06-07 10:09:54 +00:00
if err != nil {
return nil, err
}
//阶梯价格切片
2023-06-12 08:47:48 +00:00
stepPrice, err := format.StrSlicToIntSlice(strings.Split(*price.StepPrice, ","))
2023-06-07 10:09:54 +00:00
if err != nil {
return nil, err
}
2023-06-08 02:41:57 +00:00
if len(stepNum) == 0 || len(stepPrice) == 0 {
return nil, errors.New(fmt.Sprintf("stepNum count or stepPrice count is empty: product size id :%d ,product price id :%d", productSize.Id, price.Id))
2023-06-07 10:09:54 +00:00
}
2023-06-08 02:41:57 +00:00
index := 0
// 最小购买数量小于 最大阶梯数量+5
2023-06-12 08:47:48 +00:00
for int(*price.MinBuyNum) < (stepNum[len(stepNum)-1]+5) && index < 3 {
2023-06-29 03:43:07 +00:00
priceList = append(priceList, types.PriceObj{
2023-06-12 08:47:48 +00:00
Num: int(*price.MinBuyNum * *price.EachBoxNum),
2023-06-13 09:47:48 +00:00
Price: step_price.GetStepPrice(int(*price.MinBuyNum), stepNum, stepPrice),
2023-06-08 02:41:57 +00:00
})
2023-06-12 08:47:48 +00:00
*price.MinBuyNum++
2023-06-08 02:41:57 +00:00
index++
2023-06-07 09:27:17 +00:00
}
2023-06-29 03:43:07 +00:00
data := types.ChildrenObj{
2023-06-07 09:27:17 +00:00
Id: productSize.Id,
2023-06-12 08:47:48 +00:00
Name: *productSize.Capacity,
2023-06-07 09:27:17 +00:00
PriceList: priceList,
}
childrenObjList = append(childrenObjList, data)
}
return
}