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

98 lines
3.1 KiB
Go
Raw Normal View History

2023-10-19 10:42:37 +00:00
package logic
import (
2023-11-07 04:17:35 +00:00
"fusenapi/constants"
"fusenapi/model/gmodel"
"fusenapi/service/repositories"
2023-10-19 10:42:37 +00:00
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"context"
"fusenapi/server/shopping-cart/internal/svc"
"fusenapi/server/shopping-cart/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetCartNumLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetCartNumLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCartNumLogic {
return &GetCartNumLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *GetCartNumLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
func (l *GetCartNumLogic) GetCartNum(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) {
if !userinfo.IsUser() {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in")
}
2023-11-07 04:17:35 +00:00
currentPage := constants.DEFAULT_PAGE
limit := 1000
//获取用户购物车列表
carts, total, err := l.svcCtx.AllModels.FsShoppingCart.GetAllCartsByParam(l.ctx, gmodel.GetAllCartsByParamReq{
UserId: userinfo.UserId,
Sort: "id DESC",
Page: currentPage,
Limit: limit,
})
2023-10-19 10:42:37 +00:00
if err != nil {
logx.Error(err)
2023-11-07 04:17:35 +00:00
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "system err:failed to get your shopping carts")
}
var (
mapSize = make(map[int64]gmodel.FsProductSize)
mapModel = make(map[int64]gmodel.FsProductModel3d)
mapTemplate = make(map[int64]gmodel.FsProductTemplateV2)
mapProduct = make(map[int64]gmodel.FsProduct)
mapResourceMetadata = make(map[string]interface{})
)
//获取相关信息
err = NewGetCartsLogic(l.ctx, l.svcCtx).GetRelationInfo(GetRelationInfoReq{
Carts: carts,
MapSize: mapSize,
MapModel: mapModel,
MapTemplate: mapTemplate,
MapProduct: mapProduct,
MapResourceMetadata: mapResourceMetadata,
})
if err != nil {
return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
}
//定义map收集变更信息
mapCartChange := make(map[int64]string)
mapSnapshot := make(map[int64]gmodel.CartSnapshot)
//校验购物车数据是否变更
err = l.svcCtx.Repositories.NewShoppingCart.VerifyShoppingCartSnapshotDataChange(repositories.VerifyShoppingCartSnapshotDataChangeReq{
Carts: carts,
MapSize: mapSize,
MapModel: mapModel,
MapTemplate: mapTemplate,
MapCartChange: mapCartChange,
MapSnapshot: mapSnapshot,
MapProduct: mapProduct,
})
if err != nil {
logx.Error("VerifyShoppingCartSnapshotDataChange err:", err.Error())
return resp.SetStatusWithMessage(basic.CodeServiceErr, "system err:failed to check shopping cart change data")
2023-10-19 10:42:37 +00:00
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetCartNumRsp{
2023-11-22 10:54:58 +00:00
TotalCount: total - int64(len(mapCartChange)), //总数 - 失效的
2023-10-19 10:42:37 +00:00
})
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *GetCartNumLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }