fusenapi/server/shopping-cart/internal/logic/calculatecartpricelogic.go

87 lines
2.7 KiB
Go
Raw Normal View History

2023-09-15 10:19:01 +00:00
package logic
import (
"context"
"fmt"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/server/shopping-cart/internal/svc"
"fusenapi/server/shopping-cart/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type CalculateCartPriceLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewCalculateCartPriceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CalculateCartPriceLogic {
return &CalculateCartPriceLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *CalculateCartPriceLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPriceReq, userinfo *auth.UserInfo) (resp *basic.Response) {
if !userinfo.IsUser() {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in")
}
if len(req.CalculateList) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.CalculateCartPriceRsp{CalculateResultList: []types.CalculateResultItem{}})
}
cartIds := make([]int64, 0, len(req.CalculateList))
for _, v := range req.CalculateList {
cartIds = append(cartIds, v.CartId)
}
//获取购物车列表
carts, _, err := l.svcCtx.AllModels.FsShoppingCart.GetAllCartsByParam(l.ctx, gmodel.GetAllCartsByParamReq{
Ids: cartIds,
Fields: "id,size_id,product_id",
UserId: userinfo.UserId,
Page: 1,
Limit: len(cartIds),
})
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get cart list")
}
sizeIds := make([]int64, 0, len(carts))
productIds := make([]int64, 0, len(carts))
fittingIds := make([]int64, 0, len(carts))
for _, v := range carts {
sizeIds = append(sizeIds, *v.SizeId)
productIds = append(productIds, *v.ProductId)
if *v.FittingId > 0 {
fittingIds = append(fittingIds, *v.FittingId)
}
}
//根据sizeid获取价格列表
priceList, err := l.svcCtx.AllModels.FsProductPrice.GetPriceListByProductIdsSizeIds(l.ctx, productIds, sizeIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get price list")
}
mapPrice := make(map[string]int)
for k, v := range priceList {
mapPrice[fmt.Sprintf("%d_%d", *v.ProductId, *v.SizeId)] = k
}
//获取配件列表
// todo 下周写
/*fittingList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIdsTag()*/
return resp.SetStatus(basic.CodeOK)
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *CalculateCartPriceLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }