Browse Source

增加查询限购公共调用方法

master v0.4.2
guzeng 3 years ago
parent
commit
99a1ea238c
2 changed files with 154 additions and 0 deletions
  1. +121
    -0
      statistics.go
  2. +33
    -0
      statistics_test.go

+ 121
- 0
statistics.go View File

@ -0,0 +1,121 @@
package orderrpcv2
import (
"encoding/json"
"errors"
"strconv"
)
func GetUserBuyNum(site_id, dbname string, product_id string, user_id string, url ...string) (int64, error) {
if dbname == "" || product_id == "" || user_id == "" {
return 0, errors.New("参数错误")
}
conn, err := rpc_server_conn(url...)
if err != nil {
return 0, err
}
defer conn.Close()
var data map[string]string = make(map[string]string)
data["dbname"] = dbname
data["site_id"] = site_id
data["product_id"] = product_id
data["user_id"] = user_id
req, err := SetReqData(data)
if err != nil {
return 0, err
}
res := &Response{}
err = conn.GetUserBuyNum(req, res)
if err != nil {
return 0, err
}
res_data_de, err := GetResData(res)
if err != nil {
return 0, err
}
if res_data_de == "" {
return 0, errors.New("no data")
}
var res_arr map[string]string
err = json.Unmarshal([]byte(res_data_de), &res_arr)
if err != nil {
return 0, err
}
if _, ok := res_arr["Quantity"]; ok {
return strconv.ParseInt(res_arr["Quantity"], 10, 64)
}
return 0, errors.New("failed")
}
func GetUserBuyTotal(site_id, dbname string, product_id string, user_id string, url ...string) (int64, error) {
if dbname == "" || product_id == "" || user_id == "" {
return 0, errors.New("参数错误")
}
conn, err := rpc_server_conn(url...)
if err != nil {
return 0, err
}
defer conn.Close()
var data map[string]string = make(map[string]string)
data["dbname"] = dbname
data["site_id"] = site_id
data["product_id"] = product_id
data["user_id"] = user_id
req, err := SetReqData(data)
if err != nil {
return 0, err
}
res := &Response{}
err = conn.GetUserBuyTotal(req, res)
if err != nil {
return 0, err
}
res_data_de, err := GetResData(res)
if err != nil {
return 0, err
}
if res_data_de == "" {
return 0, errors.New("no data")
}
var res_arr map[string]string
err = json.Unmarshal([]byte(res_data_de), &res_arr)
if err != nil {
return 0, err
}
if _, ok := res_arr["Total"]; ok {
return strconv.ParseInt(res_arr["Total"], 10, 64)
}
return 0, errors.New("failed")
}

+ 33
- 0
statistics_test.go View File

@ -0,0 +1,33 @@
package orderrpcv2
import (
"testing"
)
func Test_GetUserBuyNum(t *testing.T) {
site_id := "10110"
dbname := "shop_v2"
product_id := "51"
user_id := "2"
ret, err := GetUserBuyNum(site_id, dbname, product_id, user_id)
t.Log(ret)
t.Log(err)
}
func Test_GetUserBuyTotal(t *testing.T) {
site_id := "10110"
dbname := "shop_v2"
product_id := "51"
user_id := "2"
ret, err := GetUserBuyTotal(site_id, dbname, product_id, user_id)
t.Log(ret)
t.Log(err)
}

Loading…
Cancel
Save