128 lines
3.8 KiB
Go
128 lines
3.8 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"fusenapi/constants"
|
|
"fusenapi/model/gmodel"
|
|
"fusenapi/utils/auth"
|
|
"fusenapi/utils/basic"
|
|
"fusenapi/utils/shopping_cart"
|
|
"math"
|
|
|
|
"fusenapi/server/shopping-cart/internal/svc"
|
|
"fusenapi/server/shopping-cart/internal/types"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type GetCartsLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewGetCartsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCartsLogic {
|
|
return &GetCartsLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
// 处理进入前逻辑w,r
|
|
// func (l *GetCartsLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
|
// }
|
|
|
|
func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
|
if req.CurrentPage <= 0 {
|
|
req.CurrentPage = constants.DEFAULT_PAGE
|
|
}
|
|
limit := 10
|
|
//获取用户购物车列表
|
|
carts, total, err := l.svcCtx.AllModels.FsShoppingCart.GetAllCartsByParam(l.ctx, gmodel.GetAllCartsByParamReq{
|
|
UserId: userinfo.UserId,
|
|
Fields: "",
|
|
Sort: "id DESC",
|
|
Page: req.CurrentPage,
|
|
Limit: limit,
|
|
})
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "system err:failed to get your shopping carts")
|
|
}
|
|
if len(carts) == 0 {
|
|
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetCartsRsp{
|
|
Meta: types.Meta{
|
|
TotalCount: total,
|
|
PageCount: int64(math.Ceil(float64(total) / float64(limit))),
|
|
CurrentPage: req.CurrentPage,
|
|
PerPage: limit,
|
|
},
|
|
CartList: nil,
|
|
})
|
|
}
|
|
lenCarts := len(carts)
|
|
templateIds := make([]int64, 0, lenCarts)
|
|
modelIds := make([]int64, 0, lenCarts) //模型 + 配件
|
|
sizeIds := make([]int64, 0, lenCarts)
|
|
for index := range carts {
|
|
templateIds = append(templateIds, *carts[index].TemplateId)
|
|
modelIds = append(modelIds, *carts[index].ModelId, *carts[index].FittingId)
|
|
sizeIds = append(sizeIds, *carts[index].SizeId)
|
|
}
|
|
//获取尺寸列表
|
|
sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByIds(l.ctx, sizeIds, "")
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get size list")
|
|
}
|
|
mapSize := make(map[int64]gmodel.FsProductSize)
|
|
for _, v := range sizeList {
|
|
mapSize[v.Id] = v
|
|
}
|
|
//获取模型和配件信息
|
|
modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIds(l.ctx, modelIds, "")
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list")
|
|
}
|
|
mapModel := make(map[int64]gmodel.FsProductModel3d)
|
|
for _, v := range modelList {
|
|
mapModel[v.Id] = v
|
|
}
|
|
//获取模板列表
|
|
templateList, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByIds(l.ctx, templateIds)
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template list")
|
|
}
|
|
mapTemplate := make(map[int64]gmodel.FsProductTemplateV2)
|
|
for _, v := range templateList {
|
|
mapTemplate[v.Id] = v
|
|
}
|
|
//定义map收集变更信息
|
|
mapCartChange := make(map[int64]string)
|
|
//校验购物车数据是否变更
|
|
err = shopping_cart.VerifyShoppingCartSnapshotDataChange(shopping_cart.VerifyShoppingCartSnapshotDataChangeReq{
|
|
Carts: carts,
|
|
MapSize: mapSize,
|
|
MapModel: mapModel,
|
|
MapTemplate: mapTemplate,
|
|
MapCartChange: mapCartChange,
|
|
})
|
|
if err != nil {
|
|
logx.Error("VerifyShoppingCartSnapshotDataChange err:", err.Error())
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "system err:failed to check shopping cart change data")
|
|
}
|
|
/*list := make([]types.CartItem, 0, lenCarts)
|
|
for index := range carts {
|
|
|
|
}*/
|
|
return resp.SetStatus(basic.CodeOK)
|
|
}
|
|
|
|
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
|
// func (l *GetCartsLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
|
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
|
// }
|