package jdapi

import (
	"fmt"
	"git.rosy.net.cn/baseapi/utils"
	"io/ioutil"
	"net/http"
	"strings"
	"testing"
)

func TestQuerySingleOrder(t *testing.T) {
	retVal, err := api.QuerySingleOrder("2404201763000323")
	t.Log(utils.Format4Output(retVal, false))
	if err != nil {
		t.Error(err)
	}
	if false {
		t.Log(utils.Format4Output(retVal, false))
	}
	//retVal, err = api.QuerySingleOrder("999999")
	//if err == nil {
	//	t.Error("应该找不到这个订单！")
	//}
}

func TestOrderShoudSettlementService(t *testing.T) {
	api.OrderShoudSettlementService("325908312617")
}

func TestPickUp(t *testing.T) {
	detile, err := api.OrderJDZBSelfDelivery("2223127747000074", "刘磊")
	fmt.Println("detail===", detile)
	fmt.Println("detail===", err)
}

func TestOrderQuery(t *testing.T) {
	jdParams := map[string]interface{}{
		"orderId": "316972178380",
	}
	result, totalCount, err := api.OrderQuery(jdParams)
	if err != nil {
		t.Fatalf("OrderQuery return error:%v", err)
	}
	t.Log(utils.Format4Output(result, false))
	if len(result) == 0 || totalCount == 0 {
		t.Fatal("OrderQuery return empty data")
	}
	buyerCityOk := false
	orderInfo, _ := result[0].(map[string]interface{})
	if buyerCity1, ok := orderInfo["buyerCity"]; ok {
		if buyerCity, ok := buyerCity1.(string); ok && buyerCity == "2376" {
			buyerCityOk = true
		}
	}

	if !buyerCityOk {
		t.Fatal("OrderQuery get data wrong")
	}
}

func TestOrderAcceptOperate(t *testing.T) {
	err := api.OrderAcceptOperate("2318183905000222", true, "")
	if err != nil {
		t.Fatal(err.Error())
	}
}

func TestMyself(t *testing.T) {
	detail, errr := api.OrderJDZBStoreDelivery("2318183905000222", "liulei")
	fmt.Println(detail)
	fmt.Println(errr)
}

func TestOrderJDZBDelivery(t *testing.T) {
	result, err := api.OrderJDZBDelivery("911350836000622", "")
	if err != nil {
		t.Fatal(err.Error())
	}
	sugarLogger.Debug(result)
}

func TestModifySellerDelivery(t *testing.T) {
	result, err := api.ModifySellerDelivery("2320085686000392", "")
	if err != nil {
		t.Fatal(err.Error())
	}
	sugarLogger.Debug(result)
}

func TestOrderSerllerDelivery(t *testing.T) {
	result, err := api.OrderSerllerDelivery("2214740704000086", "自配送")
	if err != nil {
		t.Fatal(err.Error())
	}
	sugarLogger.Debug(result)
}

func TestDeliveryEndOrder(t *testing.T) {
	result, err := api.DeliveryEndOrder("2326939731000094", "")
	if err != nil {
		t.Fatal(err.Error())
	}
	sugarLogger.Debug(result)
}

func TestGetAfsService(t *testing.T) {
	result, err := api.GetAfsService("2225735125000294")
	if err != nil {
		t.Fatal(err.Error())
	}
	sugarLogger.Debug(utils.Format4Output(result, false))
}

func TestGetAfsService2(t *testing.T) {
	result, err := api.GetAfsService2("34778110")
	if err != nil {
		t.Fatal(err.Error())
	}
	sugarLogger.Debug(utils.Format4Output(result, false))
}

func TestOrderShoudSettlementService2(t *testing.T) {
	result, err := api.OrderShoudSettlementService2("334205528418")
	if err != nil {
		t.Fatal(err.Error())
	}
	sugarLogger.Debug(utils.Format4Output(result, false))
}

func TestOrderAddTips(t *testing.T) {
	err := api.OrderAddTips("2116212802000361", 100, "jxadmin")
	if err != nil {
		t.Fatal(err.Error())
	}
}

func TestOrderQuery2(t *testing.T) {
	orderList, _, err := api.OrderQuery2(&OrderQueryParam{
		OrderPurchaseTimeBegin: "2024-07-20 00:00:00",
		OrderPurchaseTimeEnd:   "2024-07-29 23:59:59",
		DeliveryStationNo:      "11972065",
		PageNo:                 1,
		PageSize:               10,
	})
	//orderList, _, err := api.OrderQuery2(&OrderQueryParam{
	//	OrderID: 2404021632000192,
	//})
	t.Log(utils.Format4Output(orderList, false))
	if err != nil {
		t.Fatal(err.Error())
	}
}

func TestGetByOrderNoForOaos(t *testing.T) {
	orderList, err := api.GetByOrderNoForOaos("2407496361004521")
	t.Log(utils.Format4Output(orderList, false))
	if err != nil {
		t.Fatal(err.Error())
	}
}

func TestGetByOrderNoForOaosNew(t *testing.T) {
	orderList, err := api.GetByOrderNoForOaosNew("2108504597000531")
	t.Log(utils.Format4Output(orderList, false))
	if err != nil {
		t.Fatal(err.Error())
	}
}

func TestCheckSelfPickCode(t *testing.T) {
	err := api.CheckSelfPickCode("135246", "2027170805000062", "jxadmin")
	if err != nil {
		t.Fatal(err.Error())
	}
}

func TestProcessQuestionPic(t *testing.T) {
	for _, v := range [][]string{
		[]string{
			"http://img10.360buyimg.com/o2o/jfs/a.jpg",
			",jfs/a.jpg",
		},
		[]string{
			"http://img10.360buyimg.com/o2o/jfs/a.jpg,https://img10.360buyimg.com/o2o/jfs/b.jpg",
			" ,  ,  jfs/a.jpg,https:jfs/b.jpg",
		},
		[]string{
			"https://img10.360buyimg.com/o2o/jfs/a.jpg",
			",https:jfs/a.jpg",
		},
		[]string{
			"",
			",https:",
		},
	} {
		str := ProcessQuestionPic(v[1])
		if str != v[0] {
			t.Fatalf("%s, desired:%s, but its:%s", v[1], v[0], str)
		}
	}
}

func TestCancelAndRefund(t *testing.T) {
	err := api.CancelAndRefund("2423124647018693", "jxadmin", "协商一致")
	if err != nil {
		t.Fatal(err.Error())
	}
}

func TestGenerateReverseOrder(t *testing.T) {
	err := api.GenerateReverseOrder("2114820876000231", []*RefDiffAdjustSkuList{
		&RefDiffAdjustSkuList{
			SkuId:           "2036475569",
			SkuActualWeight: 230,
		},
	})
	if err != nil {
		t.Fatal(err.Error())
	}
}

func TestConfirmReceiveGoods(t *testing.T) {
	err := api.ConfirmReceiveGoods("2115980474000061")
	if err != nil {
		t.Fatal(err.Error())
	}
}

func TestAfsOpenApprove(t *testing.T) {
	err := api.AfsOpenApprove("90879887", 1, "同意退款", "jxadmin-13689038670")
	if err != nil {
		t.Fatal(err.Error())
	}
}

func TestUpdateBoxFee(t *testing.T) {
	url := "http://www.jxc4.com/v2/sku/BatchSetBoxPrice"
	storeIds := []int{
		668545, 668548, 668567, 668568, 668577, 668580, 668583, 668599, 668601, 668607, 668614, 668617, 668620,
		668622, 668623, 668624, 668631, 668639, 668642, 668650, 668651, 668659, 668665, 668668, 668681, 668682, 668686, 668700, 668701,
		668746, 668757, 668759, 668766, 668767, 668772, 668773, 668795, 668815, 668838, 668839, 668843, 668849, 668850, 668851, 668853,
		668860, 668868, 668878, 668882, 668886, 668891, 668894, 668897, 668900, 668905, 668906, 668908, 668909, 668910, 668915, 668922,
	}
	for _, v := range storeIds {
		payload := strings.NewReader(fmt.Sprintf("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"storeIDs\"\r\n\r\n[%d]\r\n-----011000010111000001101001--\r\n\r\n", v))
		req, _ := http.NewRequest("POST", url, payload)
		req.Header.Add("content-type", "multipart/form-data; boundary=---011000010111000001101001")
		res, _ := http.DefaultClient.Do(req)
		defer res.Body.Close()
		body, _ := ioutil.ReadAll(res.Body)
		fmt.Println(v)
		fmt.Println(res)
		fmt.Println(string(body))
	}

}
