223 lines
7.4 KiB
Go
223 lines
7.4 KiB
Go
package logic
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fusenapi/constants"
|
|
"fusenapi/model/gmodel"
|
|
"fusenapi/utils/auth"
|
|
"fusenapi/utils/basic"
|
|
"fusenapi/utils/hash"
|
|
"fusenapi/utils/shopping_cart"
|
|
"math"
|
|
"strings"
|
|
|
|
"context"
|
|
|
|
"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 = VerifyShoppingCartSnapshotDataChange(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)
|
|
}
|
|
|
|
// 校验购物车快照数据跟目前是否一致
|
|
type VerifyShoppingCartSnapshotDataChangeReq struct {
|
|
Carts []gmodel.FsShoppingCart
|
|
MapSize map[int64]gmodel.FsProductSize
|
|
MapModel map[int64]gmodel.FsProductModel3d //模型跟配件都在
|
|
MapTemplate map[int64]gmodel.FsProductTemplateV2
|
|
MapCartChange map[int64]string
|
|
}
|
|
type VerifyShoppingCartSnapshotDataChangeRsp struct {
|
|
CartId int64 //有改变的列表下标
|
|
Descrption string //变更描述信息
|
|
}
|
|
|
|
func VerifyShoppingCartSnapshotDataChange(req VerifyShoppingCartSnapshotDataChangeReq) error {
|
|
for _, cartInfo := range req.Carts {
|
|
var snapShotParseInfo shopping_cart.CartSnapshot
|
|
if err := json.Unmarshal([]byte(*cartInfo.Snapshot), &snapShotParseInfo); err != nil {
|
|
return err
|
|
}
|
|
//快照中模板设计json数据哈希值
|
|
snapshotTemplateJsonHash := hash.JsonHashKey(snapShotParseInfo.TemplateInfo.TemplateJson)
|
|
//快照中模型设计json数据哈希值
|
|
snapshotModelJsonHash := hash.JsonHashKey(snapShotParseInfo.ModelInfo.ModelJson)
|
|
//快照中配件设计json数据哈希值
|
|
snapshotFittingJsonHash := hash.JsonHashKey(snapShotParseInfo.FittingInfo.FittingJson)
|
|
descrptionBuilder := strings.Builder{}
|
|
//有模板验证模板相关
|
|
if *cartInfo.TemplateId > 0 {
|
|
if curTemplateInfo, ok := req.MapTemplate[*cartInfo.TemplateId]; !ok {
|
|
descrptionBuilder.WriteString("<p>the template is lose</p>")
|
|
} else {
|
|
//当前模板设计json数据哈希值
|
|
curTemplateJsonHash := hash.JsonHashKey(*curTemplateInfo.TemplateInfo)
|
|
//模板设计信息改变了
|
|
if snapshotTemplateJsonHash != curTemplateJsonHash {
|
|
descrptionBuilder.WriteString("<p>the template design info has changed</p>")
|
|
}
|
|
//模板标签改变了
|
|
if snapShotParseInfo.TemplateInfo.TemplateTag != *curTemplateInfo.TemplateTag {
|
|
descrptionBuilder.WriteString("<p>the template`s template tag has changed</p>")
|
|
}
|
|
}
|
|
}
|
|
//有模型验证模型相关
|
|
if *cartInfo.ModelId > 0 {
|
|
if curModelInfo, ok := req.MapModel[*cartInfo.ModelId]; !ok { //不存在
|
|
descrptionBuilder.WriteString("<p>the model is lose</p>")
|
|
} else {
|
|
//当前模型设计json数据哈希值
|
|
curModelJsonHash := hash.JsonHashKey(*curModelInfo.ModelInfo)
|
|
if snapshotModelJsonHash != curModelJsonHash {
|
|
descrptionBuilder.WriteString("<p>the model design info has changed</p>")
|
|
}
|
|
}
|
|
}
|
|
//有配件验证配件相关
|
|
if *cartInfo.FittingId > 0 {
|
|
if curFittingInfo, ok := req.MapModel[*cartInfo.FittingId]; !ok { //不存在
|
|
descrptionBuilder.WriteString("<p>the fitting is lose</p>")
|
|
} else {
|
|
//当前配件设计json数据哈希值
|
|
curFittingJsonHash := hash.JsonHashKey(*curFittingInfo.ModelInfo)
|
|
if snapshotFittingJsonHash != curFittingJsonHash {
|
|
descrptionBuilder.WriteString("<p>the fitting design info has changed</p>")
|
|
}
|
|
}
|
|
}
|
|
//验证尺寸相关
|
|
if *cartInfo.SizeId > 0 {
|
|
curSize, ok := req.MapSize[*cartInfo.SizeId]
|
|
if !ok {
|
|
descrptionBuilder.WriteString("<p>the size is lose</p>")
|
|
} else {
|
|
var curSizeTitle shopping_cart.SizeInfo
|
|
if err := json.Unmarshal([]byte(*curSize.Title), &curSizeTitle); err != nil {
|
|
return err
|
|
}
|
|
if snapShotParseInfo.SizeInfo.Inch != curSizeTitle.Inch || snapShotParseInfo.SizeInfo.Cm != curSizeTitle.Cm {
|
|
descrptionBuilder.WriteString("<p>the size design info has changed</p>")
|
|
}
|
|
}
|
|
}
|
|
//收集错误
|
|
descrption := descrptionBuilder.String()
|
|
if descrption != "" {
|
|
req.MapCartChange[cartInfo.Id] = descrption
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
|
// func (l *GetCartsLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
|
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
|
// }
|