把server归到统一文件夹
This commit is contained in:
174
server/product/internal/logic/getproductlistlogic.go
Normal file
174
server/product/internal/logic/getproductlistlogic.go
Normal file
@@ -0,0 +1,174 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"fusenapi/constants"
|
||||
"fusenapi/model"
|
||||
"fusenapi/product/internal/svc"
|
||||
"fusenapi/product/internal/types"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/format"
|
||||
"fusenapi/utils/image"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type GetProductListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetProductListLogic {
|
||||
return &GetProductListLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 获取产品列表
|
||||
func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp *types.Response) {
|
||||
resp = &types.Response{}
|
||||
loginInfo := auth.GetUserInfoFormCtx(l.ctx)
|
||||
if loginInfo.UserId == 0 {
|
||||
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.SetStatusWithMessage(basic.CodeServiceErr, "demo data format err")
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", demo)
|
||||
}
|
||||
if req.Page <= 0 {
|
||||
req.Page = 1
|
||||
}
|
||||
//获取合适尺寸
|
||||
if req.Size > 0 {
|
||||
req.Size = image.GetCurrentSize(req.Size)
|
||||
}
|
||||
//查询用户信息
|
||||
userModel := model.NewFsUserModel(l.svcCtx.MysqlConn)
|
||||
userInfo, err := userModel.FindOne(l.ctx, loginInfo.UserId)
|
||||
if err != nil && !errors.Is(err, sqlc.ErrNotFound) {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get user info err")
|
||||
}
|
||||
if userInfo == nil {
|
||||
return resp.SetStatusWithMessage(basic.CodeUnAuth, "user not exists")
|
||||
}
|
||||
//查询符合的产品列表
|
||||
productModel := model.NewFsProductModel(l.svcCtx.MysqlConn)
|
||||
productList, err := productModel.GetProductListByConditions(l.ctx, []string{fmt.Sprintf("%d", req.Cid)}, "sort-desc")
|
||||
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([]string, 0, productLen)
|
||||
for _, v := range productList {
|
||||
productIds = append(productIds, fmt.Sprintf("%d", v.Id))
|
||||
}
|
||||
productPriceModel := model.NewFsProductPriceModel(l.svcCtx.MysqlConn)
|
||||
productPriceList, err := productPriceModel.GetPriceListByProductIds(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])
|
||||
}
|
||||
//获取模板
|
||||
productTemplateModel := model.NewFsProductTemplateV2Model(l.svcCtx.MysqlConn)
|
||||
productTemplatesV2, err := productTemplateModel.FindAllByCondition(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{}{}
|
||||
}
|
||||
//获取分类
|
||||
tagsModel := model.NewFsTagsModel(l.svcCtx.MysqlConn)
|
||||
tagInfo, err := tagsModel.FindOne(l.ctx, req.Cid)
|
||||
if err != nil && !errors.Is(err, sqlc.ErrNotFound) {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get tag err")
|
||||
}
|
||||
if tagInfo == nil {
|
||||
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.SetStatusWithMessage(basic.CodeServiceErr, "get product size count err")
|
||||
}
|
||||
//拼接返回
|
||||
itemList := make([]types.Items, 0, productLen)
|
||||
for _, v := range productList {
|
||||
minPrice, ok := mapProductMinPrice[v.Id]
|
||||
_, tmpOk := mapProductTemplate[v.Id]
|
||||
//无最小价格则不显示 || 没有模板也不显示
|
||||
if !ok || !tmpOk {
|
||||
continue
|
||||
}
|
||||
item := types.Items{
|
||||
Id: v.Id,
|
||||
Sn: v.Sn,
|
||||
Title: v.Title,
|
||||
Intro: v.Intro.String,
|
||||
IsEnv: v.IsProtection,
|
||||
IsMicro: v.IsMicrowave,
|
||||
SizeNum: uint32(productSizeCount),
|
||||
MiniPrice: format.CentoDollar(minPrice),
|
||||
}
|
||||
//千人千面处理
|
||||
thousandFaceImageFormatReq := image.ThousandFaceImageFormatReq{
|
||||
Size: int(req.Size),
|
||||
IsThousandFace: int(userInfo.IsThousandFace),
|
||||
Cover: v.Cover,
|
||||
CoverImg: v.CoverImg,
|
||||
CoverDefault: v.CoverImg,
|
||||
ProductId: v.Id,
|
||||
UserInfo: *userInfo,
|
||||
}
|
||||
image.ThousandFaceImageFormat(&thousandFaceImageFormatReq)
|
||||
item.Cover = thousandFaceImageFormatReq.Cover
|
||||
item.CoverImg = thousandFaceImageFormatReq.CoverImg
|
||||
item.CoverDefault = thousandFaceImageFormatReq.CoverDefault
|
||||
itemList = append(itemList, item)
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetProductListRsp{
|
||||
Ob: types.Ob{
|
||||
Items: itemList,
|
||||
}, TypeName: tagInfo.Title, Description: tagInfo.Description,
|
||||
})
|
||||
}
|
||||
193
server/product/internal/logic/getsizebyproductlogic.go
Normal file
193
server/product/internal/logic/getsizebyproductlogic.go
Normal file
@@ -0,0 +1,193 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"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))
|
||||
price, ok := mapProductPrice[productSize.Id]
|
||||
//无对应尺寸价格
|
||||
if !ok {
|
||||
childrenObjList = append(childrenObjList, types.ChildrenObj{
|
||||
Id: productSize.Id,
|
||||
Name: productSize.Capacity,
|
||||
PriceList: []types.PriceObj{
|
||||
{Num: 1, Price: 0},
|
||||
{Num: 1, Price: 0},
|
||||
{Num: 1, Price: 0},
|
||||
},
|
||||
})
|
||||
continue
|
||||
}
|
||||
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
|
||||
}
|
||||
if len(stepNum) > len(stepPrice) {
|
||||
return nil, errors.New(fmt.Sprintf("stepNum count not eq stepPrice count: product size id :%d ,product price id :%d", productSize.Id, price.Id))
|
||||
}
|
||||
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(minBuyNum int, stepNum []int, stepPrice []int) float64 {
|
||||
for k, v := range stepNum {
|
||||
if minBuyNum <= v {
|
||||
return float64(stepPrice[k]) / float64(100)
|
||||
}
|
||||
}
|
||||
return float64(stepPrice[len(stepPrice)-1]) / float64(100)
|
||||
}
|
||||
89
server/product/internal/logic/getsuccessrecommandlogic.go
Normal file
89
server/product/internal/logic/getsuccessrecommandlogic.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fusenapi/model"
|
||||
"fusenapi/product/internal/svc"
|
||||
"fusenapi/product/internal/types"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/image"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
type GetSuccessRecommandLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetSuccessRecommandLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSuccessRecommandLogic {
|
||||
return &GetSuccessRecommandLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 获取推荐的产品列表
|
||||
func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessRecommandReq) (resp *types.Response) {
|
||||
resp = &types.Response{}
|
||||
loginInfo := auth.GetUserInfoFormCtx(l.ctx)
|
||||
if loginInfo.UserId == 0 {
|
||||
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.SetStatusWithMessage(basic.CodeServiceErr, "failed to get user info")
|
||||
}
|
||||
if userInfo == nil {
|
||||
return resp.SetStatusWithMessage(basic.CodeUnAuth, "failed to get user info")
|
||||
}
|
||||
if req.Num == 0 || req.Num > 500 {
|
||||
req.Num = 8
|
||||
}
|
||||
if req.Size > 0 {
|
||||
req.Size = image.GetCurrentSize(req.Size)
|
||||
}
|
||||
//随机取8个产品
|
||||
productModel := model.NewFsProductModel(l.svcCtx.MysqlConn)
|
||||
productList, err := productModel.GetRandomProductList(l.ctx, int(req.Num))
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list")
|
||||
}
|
||||
//没有推荐产品就返回
|
||||
if len(productList) == 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success")
|
||||
}
|
||||
list := make([]types.GetSuccessRecommandRsp, 0, len(productList))
|
||||
for _, v := range productList {
|
||||
data := types.GetSuccessRecommandRsp{
|
||||
Title: v.Title,
|
||||
Sn: v.Sn,
|
||||
Id: v.Id,
|
||||
SkuId: 0, //???????
|
||||
}
|
||||
//千人千面处理
|
||||
thousandFaceImageFormatReq := image.ThousandFaceImageFormatReq{
|
||||
Size: int(req.Size),
|
||||
IsThousandFace: int(userInfo.IsThousandFace),
|
||||
Cover: v.Cover,
|
||||
CoverImg: v.CoverImg,
|
||||
CoverDefault: v.CoverImg,
|
||||
ProductId: v.Id,
|
||||
UserInfo: *userInfo,
|
||||
}
|
||||
image.ThousandFaceImageFormat(&thousandFaceImageFormatReq)
|
||||
data.Cover = thousandFaceImageFormatReq.Cover
|
||||
data.CoverImg = thousandFaceImageFormatReq.CoverImg
|
||||
data.CoverDefault = thousandFaceImageFormatReq.CoverDefault
|
||||
list = append(list, data)
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", list)
|
||||
}
|
||||
Reference in New Issue
Block a user