package shopping_cart import ( "encoding/json" "fusenapi/model/gmodel" "fusenapi/utils/hash" "strings" ) // 校验购物车快照数据跟目前是否一致 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 MapSnapshot map[int64]CartSnapshot MapProduct map[int64]struct{} } func VerifyShoppingCartSnapshotDataChange(req VerifyShoppingCartSnapshotDataChangeReq) error { for _, cartInfo := range req.Carts { descrptionBuilder := strings.Builder{} //产品下架/删除 if _, ok := req.MapProduct[*cartInfo.ProductId]; !ok { descrptionBuilder.WriteString("
the product is off shelf or deleted
") } var snapShotParseInfo CartSnapshot if err := json.Unmarshal([]byte(*cartInfo.Snapshot), &snapShotParseInfo); err != nil { return err } req.MapSnapshot[cartInfo.Id] = snapShotParseInfo //快照中模板设计json数据哈希值 snapshotTemplateJsonHash := hash.JsonHashKey(snapShotParseInfo.TemplateInfo.TemplateJson) //快照中模型设计json数据哈希值 snapshotModelJsonHash := hash.JsonHashKey(snapShotParseInfo.ModelInfo.ModelJson) //快照中配件设计json数据哈希值 snapshotFittingJsonHash := hash.JsonHashKey(snapShotParseInfo.FittingInfo.FittingJson) //有模板验证模板相关 if *cartInfo.TemplateId > 0 { if curTemplateInfo, ok := req.MapTemplate[*cartInfo.TemplateId]; !ok { descrptionBuilder.WriteString("the template is lose
") } else { //当前模板设计json数据哈希值 curTemplateJsonHash := hash.JsonHashKey(*curTemplateInfo.TemplateInfo) //模板设计信息改变了 if snapshotTemplateJsonHash != curTemplateJsonHash { descrptionBuilder.WriteString("the template design info has changed
") } //模板标签改变了 if snapShotParseInfo.TemplateInfo.TemplateTag != *curTemplateInfo.TemplateTag { descrptionBuilder.WriteString("the template`s template tag has changed
") } } } //有模型验证模型相关 if *cartInfo.ModelId > 0 { if curModelInfo, ok := req.MapModel[*cartInfo.ModelId]; !ok { //不存在 descrptionBuilder.WriteString("the model is lose
") } else { //当前模型设计json数据哈希值 curModelJsonHash := hash.JsonHashKey(*curModelInfo.ModelInfo) if snapshotModelJsonHash != curModelJsonHash { descrptionBuilder.WriteString("the model design info has changed
") } } } //有配件验证配件相关 if *cartInfo.FittingId > 0 { if curFittingInfo, ok := req.MapModel[*cartInfo.FittingId]; !ok { //不存在 descrptionBuilder.WriteString("the fitting is lose
") } else { //当前配件设计json数据哈希值 curFittingJsonHash := hash.JsonHashKey(*curFittingInfo.ModelInfo) if snapshotFittingJsonHash != curFittingJsonHash { descrptionBuilder.WriteString("the fitting design info has changed
") } } } //验证尺寸相关 if *cartInfo.SizeId > 0 { curSize, ok := req.MapSize[*cartInfo.SizeId] if !ok { descrptionBuilder.WriteString("the size is lose
") } else { var curSizeTitle 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("the size design info has changed
") } } } //收集错误 descrption := descrptionBuilder.String() if descrption != "" { req.MapCartChange[cartInfo.Id] = descrption } } return nil }