fusenapi/server/shopping-cart/internal/logic/getcartnumlogic.go
laodaming 42f31ed4cc fix
2023-11-22 18:54:58 +08:00

98 lines
3.1 KiB
Go

package logic
import (
"fusenapi/constants"
"fusenapi/model/gmodel"
"fusenapi/service/repositories"
"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")
}
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,
})
if err != nil {
logx.Error(err)
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")
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetCartNumRsp{
TotalCount: total - int64(len(mapCartChange)), //总数 - 失效的
})
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *GetCartNumLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }