package mtwmapi

import (
	"errors"
	"fmt"
	"git.rosy.net.cn/jx-callback/globals"
	"testing"

	"git.rosy.net.cn/baseapi/utils"
)

func TestRetailCatList(t *testing.T) {
	result, err := api.RetailCatList("8694203 ")
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestRetailCatUpdate(t *testing.T) {
	err := api.RetailCatUpdate("8694203", "时令鲜蔬", &Param4UpdateCat{
		CategoryNameOrigin: "1",
		CategoryCodeOrigin: "1",
		CategoryCode:       "1",
		Sequence:           3,
	})
	if err != nil {
		t.Fatal(err)
	}
}
func TestRetailCatDelete(t *testing.T) {
	//var err error
	//errList := errlist.New()
	//info := []string{"11309161", "7379027", "14422863", "12405467", "12398353", "12422620", "12422751", "11437415", "2391979", "11037873", "11646173", "12286931", "11086818", "9000444", "13413407", "10874383", "11674367", "11824223", "11939392", "11810047", "12071134", "11799045", "12848707", "11596643", "14038247", "9676866", "15382980", "8967897", "11566485", "11774697", "11730687", "11798089", "11488932", "11651698", "11637939", "11831005", "11883106", "11011885", "11342761", "12131910", "12833849", "11209533", "12211457", "15874713", "11440630", "12665305", "11437682", "11440628", "9578183", "11796739", "11213162", "12665208", "13887316"}
	//str := []string{"token_gFkmTBCfctlpehYTENBNpw", "token_tnrjZftD5q4lU42KI3RPsQ", "token_ih9ZUCueA_dnrHBC6eBDew", "token_uJenwb2m6hWDe36e7hhAuw", "token_o2bWH171jXW8dIikHjFFog", "token_kfsqhhQ3kPXAwxUueglF_g", "token_h86ZqAP89I-Pe6vMACVLZQ", "token_uzWsBdnSIbv1KBQPoNRKRQ", "token_ucnSaUtKiUbonBnax5dDMQ", "token_o2-KGSi8lbh1ON4seUlEiQ", "token_v0gPiP4rH1vrS51GXgZJBg", "token_k0GWFzpjDtodAW5yFhVBgA", "token_oV2lXBnc1d8kc_JgWvtFxQ", "token_gl5GkKf9XKOO5ZNK-0JPRg", "token_suOF8ngaSTOcFcjNxvdHVA", "token_nf9QokI2udnECY2s1zBIeA", "token_nZjY3PlhFWlkHxdUDr9EsA", "token_kwq5HiBJI4vnavnmDyZBuA", "token_jhE-_ku5e7g6Ys4jWt5J0g", "token_lqAjjKmpEws1pKcspd1ETw", "token_mj31nyxnSIKFfxjViWpN4w", "token_kxjANA3lKNqfJrXApsZEEg", "token_kMttVFAYWLAcPAmZi3FNjg", "token_gd5aruWWQX2Iz8CXEg1Gaw", "token_j1HkNiZHh76aEGLqJT9Pfw", "token_muEkyBPPm6quxE2P2dlNQA", "token_uoHU5jcIZNx_QV0rZI1Now", "token_jj9M4B67tEr0DKgRiidDBQ", "token_gXpPtzRin_m9SEzohBJOFA", "token_rBIwyLoU2O6qfq2_VCdBQg", "token_qY6wArUbm8ypW1IoUc5Frw", "token_m5XZKS8TiL1ILCqy1flFjg", "token_n5xs7bSk96MekhNxWuJGPw", "token_oqbTOzEAZYvr4QgRIypA9w", "token_jmBHMy8LQi-TPa1fOsJJKQ", "token_nNzvPUZFeQ009kWinAhDiw", "token_q4EqYUsEXy726qXH1fdJeA", "token_o0aHQxC55_XjOLvWuYFFCQ", "token_k9vJhV8aAX2fN-39KZRGEA", "token_vdGAQ50zMvm_Ofqz1yJJwg", "token_r-XGhX8HFahkkDMelHxFbQ", "token_nHupX9FsP6nj6HWQm0RN2w", "token_hpNdSt3m1Hs-CKAiWZlMKw", "token_mQ16KE4knE2sdPP2r8ROSw", "token_uUrPsYavDU1ExUwEbuFG8Q", "token_oSSLl9h6zIt9hKz16e1N_g", "token_ifyMH0B_2YipwaBoBmdAdQ", "token_uryS4endUdFgySqEWo9Dwg", "token_kAFq-IIr_5KYQwo5wlhPcg", "token_shBMfRfZtOS1xaNn5MdI0Q", "token_sS0KoWshUu-WlGchoxBNcA"}
	//for _, v := range info {
	//	for _, i := range str {
	//		err = New("5873", "41c479790a76f86326f89e8048964739", "", i).RetailCatDelete(v, "", "国产水果")
	//		if err != nil {
	//			errList.AddErr(err)
	//		}
	//	}
	//}
	//t.Fatal(errList)
	err := api.RetailCatDelete("8694203", "" /*utils.GetUpperUUID()*/, "国产水果")
	t.Fatal(err)
}

func TestRetailList(t *testing.T) {
	result, err := api.RetailList("8967897"+
		"", 0, 20)
	if err != nil {
		t.Fatal(err)
	}
	if len(result) == 0 {
		t.Fatal("should have items")
	}
	t.Log(utils.Format4Output(result, false))
	t.Log(len(result))
}

func TestRetailListAll(t *testing.T) {
	result, err := api.RetailListAll("8967897")
	if err != nil {
		t.Fatal(err)
	}
	if len(result) == 0 {
		t.Fatal("should have items")
	}
	t.Log(utils.Format4Output(result, false))
	t.Log(len(result))
}

func TestRetailGet(t *testing.T) {
	result, err := api.RetailGet(testPoiCode, "4267")
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestRetailGetSpTagIds(t *testing.T) {
	result, err := api.RetailGetSpTagIds()
	if err != nil {
		t.Fatal(err)
	}
	if len(result) == 0 {
		t.Fatal("should have items")
	}
	t.Log(utils.Format4Output(result, false))
}

func TestRetailCatSkuBatchDelete(t *testing.T) {
	err := api.RetailCatSkuBatchDelete2(utils.GetUUID(), testPoiCode, nil, []string{"精品水果2"}, nil, nil, nil)
	t.Log(IsErrCategoryNotExist(err))
	if err != nil {
		t.Fatal(err)
	}
}

//批量更新商品进货价
func TestBatchSetPrice(t *testing.T) {
	param := []*SpuData{
		{
			AppSpuCode:    "25420",
			SkuID:         "11395394828",
			PurchasePrice: "50",
		},
	}
	err := api.BatchSetRestockingPrice(utils.GetUUID(), "12422751", param)
	fmt.Println(err)
}
func TestRetailDelete(t *testing.T) {
	err := api.RetailDelete(utils.GetUUID(), testPoiCode, "32427")
	if err != nil {
		t.Fatal(err)
	}
}

func TestRetailDat(t *testing.T) {
	err := api.RetailInitData("F71D0F0881A211ED9D81525400E86DC0", "16185779", "31173", map[string]interface{}{
		"app_spu_code":      "31173",
		"category_code":     "3039",
		"common_attr_value": "[{\"attrId\":1200000088,\"attrName\":\"品牌\",\"valueList\":[{\"valueId\":1000001,\"value\":\"其他品牌\"}]},{\"attrId\":1200000094,\"attrName\":\"产地\",\"valueList\":[{\"valueId\":100000050,\"value\":\"中国\"}]},{\"attrId\":1200000132,\"attrName\":\"国产/进口\",\"valueList\":[{\"valueId\":1300000003,\"value\":\"国产\"}]},{\"attrId\":1200000210,\"attrName\":\"商品类别\",\"valueList\":[{\"valueId\":1300002991,\"value\":\"水果混合切片\"}]},{\"attrId\":1200000286,\"attrName\":\"果品品牌\",\"valueList\":[{\"valueId\":1300000249,\"value\":\"其他\"}]},{\"attrId\":1200004449,\"attrName\":\"拼数\",\"valueList\":[{\"valueId\":1300015654,\"value\":\"单拼\"}]}]",
		"description":       "",
		"is_sold_out":       0,
		"min_order_count":   1,
		"name":              "【果切】香甜芒果果切1盒约250g",
		"picture":           "https://image.jxc4.com/image/4fd5cd77a6fce23ee26f8d40392d83b1.jpg,https://image.jxc4.com/image/4fd5cd77a6fce23ee26f8d40392d83b1.jpg,https://image.jxc4.com/image/4fd5cd77a6fce23ee26f8d40392d83b1.jpg,https://image.jxc4.com/image/4fd5cd77a6fce23ee26f8d40392d83b1.jpg,https://image.jxc4.com/image/4fd5cd77a6fce23ee26f8d40392d83b1.jpg",
		"price":             13.9,
		"sequence":          494,
		"skus":              "[{\"ladder_box_num\":1,\"ladder_box_price\":0.5,\"price\":13.9,\"sku_id\":\"31173\",\"spec\":\"250g\",\"stock\":\"9999\",\"weight\":250}]",
		"tag_id":            "200002727",
		"unit":              "盒",
	})
	globals.SugarLogger.Debugf("err  %s", utils.Format4Output(err, false))
}

func TestErrList(t *testing.T) {
	data := utils.IsErrMatch(errors.New("无需切自配送 level:0, code:1"), "301251", nil)
	fmt.Println(data)
}

func TestRetailBatchInitData(t *testing.T) {
	failedFoods, err := api.RetailBatchInitData(utils.GetUUID(), testPoiCode, []map[string]interface{}{
		map[string]interface{}{
			"app_food_code":   "23841",
			"box_num":         0,
			"box_price":       0,
			"category_name":   "南北干货",
			"description":     "",
			"is_sold_out":     0,
			"min_order_count": 1,
			"name":            "干腐竹约150g/份",
			"picture":         "http://image.jxc4.com/5f7fba025fc9348796039423c48ac3f5.jpg",
			"price":           1000,
			"skus": []map[string]interface{}{
				map[string]interface{}{
					"price":  1000,
					"sku_id": "23841",
					"spec":   "150g",
					"stock":  "*",
					"upc":    "",
				},
			},
			// "tag_id": "200000380",
			"unit": "份",
		},
		map[string]interface{}{
			"app_food_code":   "23840",
			"box_num":         0,
			"box_price":       0,
			"category_name":   "南北干货",
			"description":     "",
			"is_sold_out":     0,
			"min_order_count": 1,
			"name":            "干腐竹约150g/份",
			"picture":         "http://image.jxc4.com/5f7fba025fc9348796039423c48ac3f5.jpg",
			"price":           1000,
			"skus": []map[string]interface{}{
				map[string]interface{}{
					"price":  1000,
					"sku_id": "23840",
					"spec":   "150g",
					"stock":  "*",
					"upc":    "",
				},
			},
			"tag_id": "200000380",
			"unit":   "份",
		},
	})
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(failedFoods, false))
}

func TestRetailSkuPrice(t *testing.T) {
	result, err := api.RetailSkuPrice(utils.GetUUID(), testPoiCode, []*BareStoreFoodInfo{
		&BareStoreFoodInfo{
			AppFoodCode: "23841",
			Skus: []*BareStoreSkuInfo{
				&BareStoreSkuInfo{
					SkuID: "23841",
					Price: "1.2",
				},
			},
		},
		&BareStoreFoodInfo{
			AppFoodCode: "23840",
			Skus: []*BareStoreSkuInfo{
				&BareStoreSkuInfo{
					SkuID: "23840",
					Price: "1.1",
				},
			},
		},
	})
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestRetailSkuStock(t *testing.T) {
	result, err := api.RetailSkuStock(utils.GetUUID(), testPoiCode, []*BareStoreFoodInfo{
		&BareStoreFoodInfo{
			AppFoodCode: "2212",
			Skus: []*BareStoreSkuInfo{
				&BareStoreSkuInfo{
					SkuID: "2212",
					Price: "1.2",
					Stock: "123",
				},
			},
		},
		&BareStoreFoodInfo{
			AppFoodCode: "23840",
			Skus: []*BareStoreSkuInfo{
				&BareStoreSkuInfo{
					SkuID: "23840",
					Stock: "123",
				},
			},
		},
	})
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestRetailSkuSellStatus(t *testing.T) {
	result, err := api.RetailSkuSellStatus(utils.GetUUID(), testPoiCode, []*BareStoreFoodInfo{
		&BareStoreFoodInfo{
			AppFoodCode: "23841",
			Skus: []*BareStoreSkuInfo{
				&BareStoreSkuInfo{
					SkuID: "23841",
					Price: "1.2",
					Stock: "123",
				},
			},
		},
		&BareStoreFoodInfo{
			AppFoodCode: "23840",
			Skus: []*BareStoreSkuInfo{
				&BareStoreSkuInfo{
					SkuID: "23840",
					Stock: "123",
				},
			},
		},
		&BareStoreFoodInfo{
			AppFoodCode: "2384999",
			Skus: []*BareStoreSkuInfo{
				&BareStoreSkuInfo{
					SkuID: "2384999",
					Stock: "123",
				},
			},
		},
	}, SellStatusOffline)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestRetailSellStatus(t *testing.T) {
	result, err := api.RetailSellStatus(utils.GetUUID(), testPoiCode, []*BareStoreFoodInfo{
		&BareStoreFoodInfo{
			AppFoodCode: "2212",
			Skus: []*BareStoreSkuInfo{
				&BareStoreSkuInfo{
					SkuID: "2212",
					Price: "1.2",
					Stock: "123",
				},
			},
		},
		&BareStoreFoodInfo{
			AppFoodCode: "23840",
			Skus: []*BareStoreSkuInfo{
				&BareStoreSkuInfo{
					SkuID: "23840",
					Stock: "123",
				},
			},
		},
		&BareStoreFoodInfo{
			AppFoodCode: "2384999",
			Skus: []*BareStoreSkuInfo{
				&BareStoreSkuInfo{
					SkuID: "2384999",
					Stock: "123",
				},
			},
		},
	}, SellStatusOnline)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestCategoryAttrList(t *testing.T) {
	result, err := api.CategoryAttrList(200002680)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestCategoryAttrValueList(t *testing.T) {
	result, err := api.CategoryAttrValueList(100002442, "是", 1, 5)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}
