From 061fe163bd880b3d2b92fe33d23e9edc92a140dc Mon Sep 17 00:00:00 2001 From: gazebo Date: Thu, 16 Aug 2018 16:36:49 +0800 Subject: [PATCH] - basic store api added. --- platformapi/jdapi/order_test.go | 2 +- platformapi/jdapi/store.go | 88 +++++++++++++++++++++++++++ platformapi/jdapi/store_test.go | 104 ++++++++++++++++++++++++++++++++ 3 files changed, 193 insertions(+), 1 deletion(-) create mode 100644 platformapi/jdapi/store.go create mode 100644 platformapi/jdapi/store_test.go diff --git a/platformapi/jdapi/order_test.go b/platformapi/jdapi/order_test.go index 7314d0a7..a4b8916e 100644 --- a/platformapi/jdapi/order_test.go +++ b/platformapi/jdapi/order_test.go @@ -4,7 +4,7 @@ import ( "testing" ) -func Test_QueryOassBussMoney(t *testing.T) { +func TestQueryOassBussMoney(t *testing.T) { bussResult, bussErr := jdapi.QueryOassBussMoney("815536199000222") if bussErr != nil { diff --git a/platformapi/jdapi/store.go b/platformapi/jdapi/store.go new file mode 100644 index 00000000..657daba8 --- /dev/null +++ b/platformapi/jdapi/store.go @@ -0,0 +1,88 @@ +package jdapi + +import ( + "git.rosy.net.cn/baseapi/utils" +) + +// https://opendj.jd.com/staticnew/widgets/resources.html?groupid=194&apiid=138426aa19b54c48ae8464af1ca3b681 +func (a API) GetStationsByVenderId() ([]string, error) { + result, err := a.AccessAPINoPage("store/getStationsByVenderId", nil, nil, nil) + if err == nil { + result2 := result.([]interface{}) + retVal := make([]string, len(result2)) + for k, v := range result2 { + retVal[k] = v.(string) + } + return retVal, nil + } + return nil, err +} + +// https://opendj.jd.com/staticnew/widgets/resources.html?groupid=194&apiid=4c48e347027146d5a103e851055cb1a7 +func (a API) GetStoreInfoByStationNo(storeNo string) (map[string]interface{}, error) { + result, err := a.AccessAPINoPage("storeapi/getStoreInfoByStationNo", utils.Params2Map("StoreNo", storeNo), nil, nil) + if err == nil { + return result.(map[string]interface{}), nil + } + return nil, err +} + +// https://opendj.jd.com/staticnew/widgets/resources.html?groupid=194&apiid=2600369a456446f0921e918f3d15e96a +func (a API) UpdateStoreInfo4Open(storeNo, operator string, addParams map[string]interface{}) (string, error) { + jdParams := map[string]interface{}{ + "stationNo": storeNo, + "operator": operator, + } + for k, v := range addParams { + jdParams[k] = v + } + result, err := a.AccessAPINoPage("store/updateStoreInfo4Open", jdParams, nil, nil) + if err == nil { + return result.(string), nil + } + return "", err +} + +func (a API) GetCommentByOrderId(orderId int64) (map[string]interface{}, error) { + jdParams := map[string]interface{}{ + "orderId": orderId, + } + result, err := a.AccessAPINoPage("commentOutApi/getCommentByOrderId", jdParams, nil, nil) + if err != nil { + return nil, err + } + return result.(map[string]interface{}), nil +} + +// https://opendj.jd.com/staticnew/widgets/resources.html?groupid=194&apiid=ea0b466a7fa8489b813e8b197efca2d4? +func (a API) OrgReplyComment(orderID int64, storeID, content, replayPin string) (string, error) { + jdParams := map[string]interface{}{ + "orderId": orderID, + "storeId": storeID, + "content": content, + "replyPin": replayPin, + } + result, err := a.AccessAPINoPage("commentOutApi/orgReplyComment", jdParams, nil, nil) + if err != nil { + return "", err + } + return result.(string), nil +} + +// https://opendj.jd.com/staticnew/widgets/resources.html?groupid=194&apiid=5df446bb5ff14413965b8d702718dc48 +func (a API) UpdateStoreConfig4Open(stationNo string, isAutoOrder bool) (bool, error) { + jdParams := map[string]interface{}{ + "stationNo": stationNo, + } + // 0,1是反的 + if isAutoOrder { + jdParams["isAutoOrder"] = 0 + } else { + jdParams["isAutoOrder"] = 1 + } + result, err := a.AccessAPINoPage("store/updateStoreConfig4Open", jdParams, nil, nil) + if err != nil { + return false, err + } + return result.(bool), nil +} diff --git a/platformapi/jdapi/store_test.go b/platformapi/jdapi/store_test.go new file mode 100644 index 00000000..20f6b6e4 --- /dev/null +++ b/platformapi/jdapi/store_test.go @@ -0,0 +1,104 @@ +package jdapi + +import ( + "testing" + "time" + + "git.rosy.net.cn/baseapi" + "git.rosy.net.cn/baseapi/utils" +) + +const ( + mustExistStoreID = "11738324" +) + +func TestGetStationsByVenderId(t *testing.T) { + result, err := jdapi.GetStationsByVenderId() + if err != nil { + t.Fatal(err) + } + findStore := false + for _, v := range result { + if v == mustExistStoreID { + findStore = true + break + } + } + if !findStore { + baseapi.SugarLogger.Fatal("result have no store:%s", mustExistStoreID) + } +} + +func TestGetStoreInfoByStationNo(t *testing.T) { + result, err := jdapi.GetStoreInfoByStationNo(mustExistStoreID) + if err != nil { + t.Fatal(err) + } + outSystemId := result["outSystemId"].(string) + if outSystemId != "100285" { + baseapi.SugarLogger.Fatal("outSystemId is not correct, its:%s", outSystemId) + } +} + +func TestUpdateStoreInfo4Open(t *testing.T) { + result, err := jdapi.GetStoreInfoByStationNo(mustExistStoreID) + if err != nil { + t.Fatal(err) + } + oldAddress := result["stationAddress"].(string) + testAddress := oldAddress + "T" + addParams := map[string]interface{}{ + "stationAddress": testAddress, + } + updateResult, err := jdapi.UpdateStoreInfo4Open(mustExistStoreID, "test", addParams) + if err != nil { + t.Fatal(err) + } + baseapi.SugarLogger.Debug(updateResult) + time.Sleep(2 * time.Second) + result, err = jdapi.GetStoreInfoByStationNo(mustExistStoreID) + addParams = map[string]interface{}{ + "stationAddress": oldAddress, + } + jdapi.UpdateStoreInfo4Open(mustExistStoreID, "test", addParams) + if err != nil { + t.Fatal(err) + } + restoredAddress := result["stationAddress"].(string) + if restoredAddress != oldAddress { + t.Fatalf("address not match, it's:%s", restoredAddress) + } +} +func TestGetCommentByOrderId(t *testing.T) { + testOrderID := int64(819498819000341) + result, err := jdapi.GetCommentByOrderId(testOrderID) + if err != nil { + t.Fatal(err.Error()) + } + gotOrderID := utils.MustInterface2Int64(result["orderId"]) + if gotOrderID != testOrderID { + t.Fatalf("GetCommentByOrderId wrong, gotOrderID:%d", gotOrderID) + } +} + +func TestUpdateStoreConfig4Open(t *testing.T) { + testStationNo := "11785740" + desiredValue := true + // 马上修改了后,通过GetStoreInfoByStationNo得到的数据不及时,测试不能过的。。。 + result, err := jdapi.UpdateStoreConfig4Open(testStationNo, desiredValue) + if err != nil || !result { + if err != nil { + t.Fatal(err.Error()) + } + t.Fatal(result) + } + time.Sleep(2 * time.Second) + result2, err := jdapi.GetStoreInfoByStationNo(testStationNo) + if err != nil { + t.Fatal(err.Error()) + } + isAutoOrder := int(utils.MustInterface2Int64(result2["isAutoOrder"])) + if isAutoOrder != 0 && desiredValue || isAutoOrder == 0 && !desiredValue { + t.Fatalf("UpdateStoreConfig4Open failed, isAutoOrder:%d", isAutoOrder) + } +}