fusenapi/server/collection/internal/logic/getcollectproductlistlogic.go

191 lines
6.2 KiB
Go
Raw Normal View History

package logic
import (
2023-10-08 07:29:03 +00:00
"encoding/json"
"fmt"
"fusenapi/constants"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
2023-10-08 07:29:03 +00:00
"fusenapi/utils/format"
"fusenapi/utils/s3url_to_s3id"
"math"
"context"
"fusenapi/server/collection/internal/svc"
"fusenapi/server/collection/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetCollectProductListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetCollectProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCollectProductListLogic {
return &GetCollectProductListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *GetCollectProductListLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
func (l *GetCollectProductListLogic) GetCollectProductList(req *types.GetCollectProductListReq, userinfo *auth.UserInfo) (resp *basic.Response) {
2023-10-08 07:29:03 +00:00
if req.CurrentPage <= 0 {
req.CurrentPage = constants.DEFAULT_PAGE
}
limit := 10
//查询列表
collectionList, total, err := l.svcCtx.AllModels.FsProductCollection.GetList(l.ctx, userinfo.UserId, userinfo.GuestId, req.CurrentPage, limit, "id DESC")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get collection list")
}
//没有数据
if len(collectionList) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetCollectProductListRsp{
Meta: types.Meta{
TotalCount: total,
PageCount: int64(math.Ceil(float64(total) / float64(limit))),
CurrentPage: req.CurrentPage,
PerPage: limit,
},
List: []types.GetCollectProductListRspItem{},
})
}
productIds := make([]int64, 0, len(collectionList))
for _, v := range collectionList {
productIds = append(productIds, *v.ProductId)
}
//获取产品所有模型+配件来计算最低价
modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByProductIdsTags(l.ctx, productIds, []int{constants.TAG_MODEL, constants.TAG_PARTS}, "id,tag,product_id,part_id,price,step_price")
mapModel := make(map[int64]int)
for k, v := range modelList {
mapModel[v.Id] = k
}
//计算最低价
mapProductMinPrice := make(map[int64]int64)
for _, v := range modelList {
if *v.Tag != constants.TAG_MODEL {
continue
}
var stepPrice gmodel.StepPriceJsonStruct
if err = json.Unmarshal(*v.StepPrice, &stepPrice); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeJsonErr, fmt.Sprintf("failed to parse step price:%d", v.Id))
}
//阶梯最低单价
itemPrice := int64(0)
if len(stepPrice.PriceRange) > 0 {
itemPrice = stepPrice.PriceRange[0].Price
}
//配件价格
if fittingIndex, ok := mapModel[*v.PartId]; ok {
itemPrice += *modelList[fittingIndex].Price
}
//查询比较最低价
if minPrice, ok := mapProductMinPrice[*v.ProductId]; ok {
if minPrice < itemPrice {
continue
}
}
mapProductMinPrice[*v.ProductId] = itemPrice
}
//获取产品列表
productList, err := l.svcCtx.AllModels.FsProduct.GetProductListByIdsWithoutStatus(l.ctx, productIds, "")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product list")
}
mapProduct := make(map[int64]int)
resourceIds := make([]string, 0, len(productList))
for k, v := range productList {
mapProduct[v.Id] = k
resourceIds = append(resourceIds, s3url_to_s3id.GetS3ResourceIdFormUrl(*v.Cover))
}
sizeCounts, err := l.svcCtx.AllModels.FsProductSize.GetGroupProductSizeByStatus(l.ctx, productIds, 1)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product size count")
}
mapProductSizeCount := make(map[int64]int64)
for _, v := range sizeCounts {
mapProductSizeCount[v.ProductId] = v.Num
}
//获取资源列表
resourceList, err := l.svcCtx.AllModels.FsResource.FindAllByResourceIds(l.ctx, resourceIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get resource list")
}
mapResourceMetadata := make(map[string]interface{})
for _, v := range resourceList {
var metadata interface{}
if err = json.Unmarshal(*v.Metadata, &metadata); err != nil {
logx.Error(err)
return resp.SetStatusAddMessage(basic.CodeJsonErr, fmt.Sprintf("failed to parse resource metadata:%s", v.ResourceId))
}
mapResourceMetadata[*v.ResourceUrl] = metadata
}
listRsp := make([]types.GetCollectProductListRspItem, 0, len(collectionList))
for _, collection := range collectionList {
productName := ""
cover := ""
isShelf := int64(0)
isDeleted := int64(0)
var coverMetadata interface{}
if productIndex, ok := mapProduct[*collection.ProductId]; ok {
productName = *productList[productIndex].Title
cover = *productList[productIndex].Cover
isShelf = *productList[productIndex].IsShelf
isDeleted = *productList[productIndex].IsDel
if metadata, ok := mapResourceMetadata[cover]; ok {
coverMetadata = metadata
}
}
sizeCount := int64(0)
if count, ok := mapProductSizeCount[*collection.ProductId]; ok {
sizeCount = count
}
minPrice := ""
if price, ok := mapProductMinPrice[*collection.ProductId]; ok {
minPrice = format.CentitoDollar(price, 3)
}
listRsp = append(listRsp, types.GetCollectProductListRspItem{
Id: collection.Id,
ProductId: *collection.ProductId,
ProductName: productName,
Logo: *collection.Logo,
Cover: cover,
CoverMetadata: coverMetadata,
SelectColorIndex: *collection.SelectColorIndex,
TemplateTag: *collection.TemplateTag,
SizeCount: sizeCount,
MinPrice: minPrice,
IsShelf: isShelf,
IsDeleted: isDeleted,
})
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetCollectProductListRsp{
Meta: types.Meta{
TotalCount: total,
PageCount: int64(math.Ceil(float64(total) / float64(limit))),
CurrentPage: req.CurrentPage,
PerPage: limit,
},
List: listRsp,
})
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *GetCollectProductListLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }