fusenapi/product/internal/logic/getproductlistlogic.go

197 lines
5.5 KiB
Go
Raw Normal View History

2023-06-01 07:32:28 +00:00
package logic
import (
"context"
2023-06-02 11:24:58 +00:00
"encoding/json"
"errors"
"fmt"
"fusenapi/constants"
2023-06-01 10:34:41 +00:00
"fusenapi/model"
2023-06-01 07:32:28 +00:00
"fusenapi/product/internal/svc"
"fusenapi/product/internal/types"
2023-06-02 04:12:51 +00:00
"fusenapi/utils/auth"
2023-06-02 11:24:58 +00:00
"fusenapi/utils/format"
2023-06-01 10:52:15 +00:00
"fusenapi/utils/image"
2023-06-05 04:21:15 +00:00
"github.com/zeromicro/go-zero/core/stores/sqlc"
2023-06-02 11:24:58 +00:00
"sort"
"strings"
"time"
2023-06-01 07:32:28 +00:00
"github.com/zeromicro/go-zero/core/logx"
)
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,
}
}
// 获取产品列表
2023-06-05 10:14:46 +00:00
func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, loginInfo auth.UserInfo) (resp *types.Response) {
2023-06-06 08:14:02 +00:00
resp = &types.Response{}
2023-06-02 04:12:51 +00:00
//校验前台登录情况
if loginInfo.UserId == 0 {
2023-06-06 08:14:02 +00:00
resp.Set(constants.CODE_UNAUTH, "please sign in")
return
2023-06-02 04:12:51 +00:00
}
2023-06-02 11:24:58 +00:00
//如果是demo
if req.IsDemo == 1 {
var demo types.GetProductListRsp
2023-06-06 07:08:17 +00:00
if err := json.Unmarshal([]byte(constants.PRODUCT_LIST_DEMO), &demo); err != nil {
2023-06-02 11:24:58 +00:00
logx.Error(err)
2023-06-06 08:14:02 +00:00
resp.Set(constants.CODE_SERVICE_ERR, "demo data format err")
2023-06-02 11:24:58 +00:00
}
2023-06-06 08:14:02 +00:00
resp.SetWithData(constants.CODE_OK, "success", demo)
return
2023-06-02 11:24:58 +00:00
}
if req.Page <= 0 {
req.Page = 1
}
2023-06-01 07:32:28 +00:00
//获取合适尺寸
if req.Size > 0 {
req.Size = image.GetCurrentSize(req.Size)
}
2023-06-02 11:24:58 +00:00
//查询用户信息
2023-06-01 10:34:41 +00:00
userModel := model.NewFsUserModel(l.svcCtx.MysqlConn)
2023-06-02 04:12:51 +00:00
userInfo, err := userModel.FindOne(l.ctx, loginInfo.UserId)
2023-06-05 04:21:15 +00:00
if err != nil && !errors.Is(err, sqlc.ErrNotFound) {
2023-06-02 11:24:58 +00:00
logx.Error(err)
2023-06-06 08:14:02 +00:00
resp.Set(constants.CODE_SERVICE_ERR, "get user info err")
return
2023-06-02 11:24:58 +00:00
}
if userInfo == nil {
2023-06-06 08:14:02 +00:00
resp.Set(constants.CODE_UNAUTH, "user not exists")
return
2023-06-02 11:24:58 +00:00
}
//查询符合的产品列表
productModel := model.NewFsProductModel(l.svcCtx.MysqlConn)
2023-06-06 10:17:03 +00:00
productList, err := productModel.GetProductListByConditions(l.ctx, int(req.Cid), "sort-desc")
2023-06-01 10:34:41 +00:00
if err != nil {
2023-06-02 11:24:58 +00:00
logx.Error(err)
2023-06-06 08:14:02 +00:00
resp.Set(constants.CODE_SERVICE_ERR, "failed to get product list")
return
2023-06-01 10:34:41 +00:00
}
2023-06-02 11:24:58 +00:00
productLen := len(productList)
if productLen == 0 {
2023-06-06 08:14:02 +00:00
resp.Set(constants.CODE_OK, "success")
return
2023-06-02 11:24:58 +00:00
}
//提取产品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.GetPriceList(l.ctx, productIds)
if err != nil {
logx.Error(err)
2023-06-06 08:14:02 +00:00
resp.Set(constants.CODE_SERVICE_ERR, "failed to get product min price list")
return
2023-06-02 11:24:58 +00:00
}
//存储产品最小价格
mapProductMinPrice := make(map[int64]int64)
for _, v := range productPriceList {
2023-06-05 02:18:31 +00:00
priceStrSlic := strings.Split(v.Price, ",")
priceSlice, err := format.StrSlicToIntSlice(priceStrSlic)
2023-06-02 11:24:58 +00:00
if err != nil {
logx.Error(err)
2023-06-06 08:14:02 +00:00
resp.Set(constants.CODE_SERVICE_ERR, err.Error())
return
2023-06-02 11:24:58 +00:00
}
2023-06-05 02:18:31 +00:00
if len(priceSlice) == 0 {
continue
}
sort.Ints(priceSlice)
mapProductMinPrice[v.ProductId] = int64(priceSlice[0])
2023-06-01 10:34:41 +00:00
}
2023-06-02 11:24:58 +00:00
//获取模板
productTemplateModel := model.NewFsProductTemplateV2Model(l.svcCtx.MysqlConn)
2023-06-05 10:56:23 +00:00
productTemplatesV2, err := productTemplateModel.FindAllByCondition(l.ctx, productIds)
2023-06-02 11:24:58 +00:00
if err != nil {
logx.Error(err)
2023-06-06 08:14:02 +00:00
resp.Set(constants.CODE_SERVICE_ERR, "get product template_v2 err")
return
2023-06-02 11:24:58 +00:00
}
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)
2023-06-05 04:21:15 +00:00
if err != nil && !errors.Is(err, sqlc.ErrNotFound) {
2023-06-02 11:24:58 +00:00
logx.Error(err)
2023-06-06 08:14:02 +00:00
resp.Set(constants.CODE_SERVICE_ERR, "get tag err")
return
2023-06-02 11:24:58 +00:00
}
if tagInfo == nil {
2023-06-05 10:14:46 +00:00
return &types.Response{Code: 510, Message: "classification not exists "}
2023-06-02 11:24:58 +00:00
}
2023-06-05 09:13:05 +00:00
//获取产品尺寸数量
productSizeModel := model.NewFsProductSizeModel(l.svcCtx.MysqlConn)
productSizeCount, err := productSizeModel.CountByStatus(l.ctx, 1)
if err != nil {
logx.Error(err)
2023-06-06 08:14:02 +00:00
resp.Set(constants.CODE_SERVICE_ERR, "get product size count err")
return
2023-06-05 09:13:05 +00:00
}
2023-06-02 11:24:58 +00:00
//拼接返回
itemList := make([]types.Items, 0, productLen)
for _, v := range productList {
minPrice, ok := mapProductMinPrice[v.Id]
//无最小价格则不显示
if !ok {
continue
}
_, ok = mapProductTemplate[v.Id]
//没有模板也不显示
if !ok {
continue
}
item := types.Items{
Id: v.Id,
Sn: v.Sn,
Title: v.Title,
Cover: v.Cover,
Intro: v.Intro.String,
CoverImg: v.CoverImg,
2023-06-05 04:21:15 +00:00
IsEnv: v.IsProtection,
IsMicro: v.IsMicrowave,
2023-06-05 09:13:05 +00:00
SizeNum: uint32(productSizeCount),
2023-06-05 04:21:15 +00:00
MiniPrice: format.CentoDollar(minPrice),
2023-06-02 11:24:58 +00:00
}
if req.Size > 0 {
coverSlice := strings.Split(v.Cover, ".")
coverImgSlice := strings.Split(v.CoverImg, ".")
if req.Size >= 200 {
item.Cover = fmt.Sprintf("%s_%d.%s", coverSlice[0], req.Size, coverSlice[1])
item.CoverImg = fmt.Sprintf("%s_%d.%s", coverImgSlice[0], req.Size, coverImgSlice[1])
}
//千人千面处理
if userInfo.IsThousandFace == 1 {
v.Cover = ""
item.CoverDefault = item.CoverImg
if req.Size >= 200 {
item.CoverImg = fmt.Sprintf("%s/test/%d/%d_%d.png?%d", constants.DOMAIN_RENDER_IMG_NAME, userInfo.Id, userInfo.Id, v.Id, time.Now().Unix())
item.CoverDefault = fmt.Sprintf("%s_%d.%s", coverImgSlice[0], req.Size, coverImgSlice[1])
}
}
}
itemList = append(itemList, item)
}
2023-06-06 08:14:02 +00:00
resp.SetWithData(constants.CODE_OK, "success", types.GetProductListRsp{
2023-06-06 07:08:17 +00:00
Ob: types.Ob{
Items: itemList,
}, TypeName: tagInfo.Title, Description: tagInfo.Description,
2023-06-06 08:14:02 +00:00
})
2023-06-01 07:32:28 +00:00
return
}