diff --git a/server/marketserver/api/v1/shopcart/shopcart.go b/server/marketserver/api/v1/shopcart/shopcart.go index 54bbdde8..2755aa05 100644 --- a/server/marketserver/api/v1/shopcart/shopcart.go +++ b/server/marketserver/api/v1/shopcart/shopcart.go @@ -6,6 +6,7 @@ import ( "main/constant" "main/common" . "main/global" + "strings" "github.com/gin-gonic/gin" ) @@ -14,6 +15,7 @@ type ShopCartApi struct { } func (this *ShopCartApi) List(c *gin.Context) { + openId := c.MustGet("open_id").(string) rspObj := &struct { ErrCode int32 `json:"errcode"` ErrMsg string `json:"errmsg"` @@ -22,7 +24,7 @@ func (this *ShopCartApi) List(c *gin.Context) { Nft interface{} `json:"nft"` } `json:"data"` }{} - openId := c.MustGet("open_id").(string) + q5.NewSlice(&rspObj.Data, 0, 10) f5.GetGoStyleDb().OrmSelectOne( constant.BCNFT_DB, "t_shopcart", @@ -36,13 +38,15 @@ func (this *ShopCartApi) List(c *gin.Context) { c.JSON(200, rspObj) return } - cartDb := new(common.ShopCart) + cartDb := common.NewShopCart() nfts := []*common.NftDto{} if q5.DecodeJson(ds.GetByName("data"), cartDb) == nil { q5.Sort[common.ShopCartGoods](cartDb.Goods, func (a *common.ShopCartGoods, b *common.ShopCartGoods) bool{ return a.SortIdx < b.SortIdx }) + dirty := false + newGoods := []*common.ShopCartGoods{} for _, val := range cartDb.Goods { var err1 error f5.GetGoStyleDb().OrmSelectOne( @@ -65,6 +69,10 @@ func (this *ShopCartApi) List(c *gin.Context) { p.Payload = map[string]interface{}{} q5.DecodeJson(ds.GetByName("event_data"), &p.Payload) q5.AppendSlice(&nfts, p) + q5.AppendSlice(&newGoods, val) + } else { + dirty = true + val.Flag = 1 } } }) @@ -74,6 +82,10 @@ func (this *ShopCartApi) List(c *gin.Context) { c.JSON(200, rspObj) return } + }//end for goods + if dirty { + cartDb.Goods = newGoods + this.save(openId, cartDb) } } GetCacheMgr().GetNfts(nfts) @@ -92,7 +104,102 @@ func (this *ShopCartApi) Add(c *gin.Context) { } func (this *ShopCartApi) Del(c *gin.Context) { + openId := c.MustGet("open_id").(string) + + rspObj := &struct { + ErrCode int32 `json:"errcode"` + ErrMsg string `json:"errmsg"` + }{} + + reqJson := struct { + NetId interface{} `json:"net_id"` + Tokens []struct { + TokenId interface{} `json:"token_id"` + ContractAddress string `json:"contract_address"` + } `json:"tokens"` + }{} + if err := c.ShouldBindJSON(&reqJson); err != nil { + rspObj.ErrCode = 1 + rspObj.ErrMsg = "json parse error" + c.JSON(200, rspObj) + return + } + + f5.GetGoStyleDb().OrmSelectOne( + constant.BCNFT_DB, + "t_shopcart", + [][]string{ + {"open_id", openId}, + }, + func (err error, ds* f5.DataSet) { + if err != nil { + rspObj.ErrCode = 500 + rspObj.ErrMsg = "server internal error" + c.JSON(200, rspObj) + return + } + if ds.Next() { + cartDb := common.NewShopCart() + dirty := false + if q5.DecodeJson(ds.GetByName("data"), cartDb) == nil { + newGoods := []*common.ShopCartGoods{} + for _, val := range cartDb.Goods { + found := false + for _, val2 := range reqJson.Tokens { + if val.NetId == reqJson.NetId && val.TokenId == val2.TokenId && + strings.ToLower(val.ContractAddress) == strings.ToLower(val2.ContractAddress) { + found = true + } + } + if !found { + q5.AppendSlice(&newGoods, val) + } else { + dirty = true + } + } + } + if dirty { + this.save(openId, cartDb) + } + c.JSON(200, rspObj) + } else { + c.JSON(200, rspObj) + } + }) } func (this *ShopCartApi) Clear(c *gin.Context) { + openId := c.MustGet("open_id").(string) + + rspObj := &struct { + ErrCode int32 `json:"errcode"` + ErrMsg string `json:"errmsg"` + }{} + + cartDb := common.NewShopCart() + this.save(openId, cartDb) + c.JSON(200, rspObj) +} + +func (this *ShopCartApi) save(openId string, cartDb *common.ShopCart) { + nowTime := f5.GetApp().GetRealSeconds() + f5.GetGoStyleDb().Upsert( + constant.BCNFT_DB, + "t_shopcart", + [][]string{ + {"open_id", openId}, + }, + [][]string{ + {"data", q5.EncodeJson(cartDb)}, + {"modifytime", q5.ToString(nowTime)}, + }, + [][]string{ + {"open_id", openId}, + {"data", q5.EncodeJson(cartDb)}, + {"createtime", q5.ToString(nowTime)}, + {"modifytime", q5.ToString(nowTime)}, + }, + func (err error, lastInsertId int64, rowsAffected int64) { + + }) } diff --git a/server/marketserver/common/types.go b/server/marketserver/common/types.go index 3d6e54d0..846e42b1 100644 --- a/server/marketserver/common/types.go +++ b/server/marketserver/common/types.go @@ -67,6 +67,7 @@ type ShopCartGoods struct { TokenId string `json:"token_id"` SortIdx uint32 `json:"idx"` CreateTime int32 `json:"addtime"` + Flag int32 } type ShopCart struct { @@ -115,3 +116,9 @@ func (this* NftDto) GetKey() string { key := fmt.Sprintf("%d_%s_%s", this.NetId, this.ContractAddress, this.TokenId) return key } + +func NewShopCart() *ShopCart { + p := new(ShopCart) + q5.NewSlice(&p.Goods, 0, 10) + return p +}