package ebaiapi

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

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

func TestShopCategoryCreate(t *testing.T) {
	result, err := api.ShopCategoryCreate(testShopID, 0, "绿色蔬菜", 16)
	if err != nil {
		t.Fatal(err)
	} else {
		t.Log(utils.Format4Output(result, false))
	}
}

func TestShopGet222(t *testing.T) {
	data, err := api.ShopGet("", 32267258880)
	fmt.Println(data)
	fmt.Println(err)
}

func TestShopCategoryUpdate(t *testing.T) {
	err := api.ShopCategoryUpdate(testShopID, 153760204017121, "水果2", 2)
	if err != nil {
		t.Fatal(err)
	}
}

func TestShopCategoryDelete(t *testing.T) {
	err := api.ShopCategoryDelete(testShopID, 1538315352106929)
	if err != nil {
		t.Fatal(err)
	}
}

func TestSkuGetItemsByCategoryId(t *testing.T) {
	result, err := api.SkuGetItemsByCategoryId("667544", 153838813210900)
	if err != nil {
		t.Fatal(err)
	} else {
		t.Log(utils.Format4Output(result, false))
	}
}

func TestSkuList(t *testing.T) {

	foodListData := make([]*SkuInfo, 0, 0)
	skuIdOffset := 0
	param := &SkuListParams{
		Page:        1,
		PageSize:    100,
		SkuIdOffset: skuIdOffset,
	}

	for {
		foodList, err := api.SkuList(utils.Int2Str(300394), param)
		if err != nil {
			break
		}

		foodListData = append(foodListData, foodList.List...)
		if len(foodList.List) < 100 {
			break
		}
		param.Page += 1
		skuIdOffset = foodList.SkuIdOffset
	}

}

func TestSkuListDelete(t *testing.T) {
	page := 1
	for {
		param := &SkuListParams{
			Page:     page,
			PageSize: 100,
		}
		foodList, err := api.SkuList(utils.Int2Str(5000112), param)
		if err != nil {
			break
		}

		for k, v := range foodList.List {
			api.AccessAPI2("sku.delete", map[string]interface{}{
				"shop_id": "5000112",
				"sku_id":  v.SkuId,
			}, utils.GetUUID())
			globals.SugarLogger.Debugf("---k := %d,delete_name:= %s", k, v.Name)
		}
		globals.SugarLogger.Debugf("------page := %d", foodList.SkuIdOffset)
		page = page + 1
	}
}

func TestSkuDeleteCategory(t *testing.T) {
	fromCategoryList, _ := api.ShopCategoryGet(utils.Int2Str(5000112))
	for _, v := range fromCategoryList {
		api.ShopCategoryDelete("5000112", v.CategoryID)
		if len(v.Children) != 0 {
			for _, c := range v.Children {
				api.ShopCategoryDelete("5000112", c.CategoryID)
			}
		}
	}
}
func TestSkuUploadRTF(t *testing.T) {
	rtfDetail := BuildRFTFromImgs("https://image.jxc4.com/sijidou.jpg")
	t.Log(rtfDetail)
	result, err := api.SkuUploadRTF("2", rtfDetail)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(result)
}

func TestSkuCreate(t *testing.T) {
	result, err := api.SkuCreate("DD1445C6BC2011EEB00C525400E86DC0", "800437", "20240085", map[string]interface{}{
		"cat3_id":     201218221,
		"category_id": 201217436,
		"left_num":    9999,
		"minimum":     1,
		"name":        "【新品上市】新鲜 洗净智利进口车厘子   500g（JJ）",
		"photos": map[string]interface{}{
			"is_master": 1,
			"url":       "https://img.alicdn.com/imgextra/i4/1/O1CN011UdvjE1BsVJlGQ4ok_!!1-0-tcretail.jpg"},
		"preparation_time":     0,
		"process_type":         0,
		"rtf":                  "https://img.alicdn.com/imgextra/desc/icoss!0764993629621!13916222277",
		"sale_price":           13200,
		"seven_days_no_reason": false,
		"status":               "1",
		"upc":                  "",
		"weight":               "500",
	})
	if err != nil {
		t.Fatal(err)
	} else {
		t.Log(utils.Format4Output(result, false))
	}
}

func TestSkuUpdate(t *testing.T) {
	// 15579787500720732 高级

	result, err := api.SkuUpdate(utils.GetUUID(), "2", 1557043939079105, map[string]interface{}{
		// "name": "高级商品2015a333约1100g/份",
		// "rtf": "http://www.rosy.net.cn/rtf.html",
		"shelf_number": 12,
	})
	if err != nil {
		t.Fatal(err)
	} else {
		t.Log(utils.Format4Output(result, false))
	}
}

func TestSkuShopCategoryMap(t *testing.T) {
	err := api.SkuShopCategoryMap(testShopID, 0, "2212", 15637192270716, 100)
	if err != nil {
		t.Fatal(err)
	}
}

func TestSkuDelete(t *testing.T) {
	const (
		notExistSkuID = 12345678
		existSkuID    = 156406677407848
	)
	opResult, err := api.SkuDelete(utils.GetUUID(), testShopID, []int64{notExistSkuID}, nil)
	t.Log(utils.Format4Output(opResult, false))
	if err == nil {
		t.Log("应该要报错")
	}
	if opResult == nil || len(opResult.FailedList) != 1 || opResult.FailedList[0].SkuID != notExistSkuID {
		t.Logf("错误结果中应该要包含:%d", notExistSkuID)
	}
	opResult, err = api.SkuDelete(utils.GetUUID(), testShopID, []int64{existSkuID}, nil)
	t.Log(utils.Format4Output(opResult, false))
	if err != nil {
		t.Fatal(err)
	}
	if opResult == nil || len(opResult.SuccessList) != 1 || opResult.SuccessList[0].SkuID != existSkuID {
		t.Logf("成功结果中应该要包含:%d", existSkuID)
	}
}

func TestSkuOnline(t *testing.T) {
	const (
		notExistSkuID = 12345678
		existSkuID    = 156406688807623
	)

	opResult, err := api.SkuOnline(utils.GetUUID(), testShopID, []int64{notExistSkuID, existSkuID}, nil, nil)
	t.Log(utils.Format4Output(opResult, false))
	t.Log(err.Error())
	if err == nil {
		t.Log("应该要报错")
	}
	if opResult == nil || len(opResult.FailedList) != 1 || opResult.FailedList[0].SkuID != notExistSkuID {
		t.Logf("错误结果中应该要包含:%d", notExistSkuID)
	}
	if opResult == nil || len(opResult.SuccessList) != 1 || opResult.SuccessList[0].SkuID != existSkuID {
		t.Logf("成功结果中应该要包含:%d", existSkuID)
	}
}

func TestName(t *testing.T) {
	err := fmt.Errorf("{\"failed_list\":[{\"custom_sku_id\":\"\",\"error_msg\":\"商品禁止售卖不允许上架，请去商品审核模块进行修改。\",\"error_no\":1,\"item_id\":\"624098832982\",\"sku_id\":\"1596531171227780\",\"upc\":\"\"},{\"custom_sku_id\":\"\",\"error_msg\":\"商品禁止售卖不允许上架，请去商品审核模块进行修改。\",\"error_no\":1,\"item_id\":\"624979359986\",\"sku_id\":\"1596531148227028\",\"upc\":\"\"},{\"custom_sku_id\":\"\",\"error_msg\":\"商品禁止售卖不允许上架，请去商品审核模块进行修改。\",\"error_no\":1,\"item_id\":\"627603029676\",\"sku_id\":\"160058318622375\",\"upc\":\"\"},{\"custom_sku_id\":\"\",\"error_msg\":\"商品禁止售卖不允许上架，请去商品审核模块进行修改。\",\"error_no\":1,\"item_id\":\"681456084928\",\"sku_id\":\"1661361999222422\",\"upc\":\"\"}],\"success_list\":[]}")
	opResult, err := handleShopSkuBatchErr(err)
	fmt.Println(opResult)
	fmt.Println(err)
}

func TestSkuOnlineOne(t *testing.T) {
	err := api.SkuOnlineOne(utils.GetUUID(), testShopID, 13211, "", "")
	if err == nil {
		t.Fatal("应该要报错才对")
	}
}

func TestSkuOffline(t *testing.T) {
	opResult, err := api.SkuOffline(utils.GetUUID(), testShopID, []int64{1564049914071288, 156389470507185}, nil, nil)
	t.Log(utils.Format4Output(opResult, false))
	if err != nil {
		t.Fatal(err)
	}
}

func TestSkuPriceUpdateBatch(t *testing.T) {
	opResult, err := api.SkuPriceUpdateBatch(utils.GetUUID(), testShopID, ShopSkuInfoList{
		&ShopSkuInfo{
			SkuID:     156369111807787,
			SalePrice: 100,
		},
		&ShopSkuInfo{
			SkuID:     156369111707770,
			SalePrice: 100,
		},
		&ShopSkuInfo{
			SkuID:     12321,
			SalePrice: 100,
		},
	}, SkuIDTypeSkuID)
	t.Log(utils.Format4Output(opResult, false))
	if err == nil {
		t.Fatal("应该要报错才对")
	}
	if len(opResult.FailedList) != 3 {
		t.Fatal("错误列表不正确")
	}
}

func TestSkuStockUpdateBatch(t *testing.T) {
	const (
		notExistSkuID = 12345678
		existSkuID    = 156406688807623

		leftNum = 123
	)

	opResult, err := api.SkuStockUpdateBatch(utils.GetUUID(), testShopID, ShopSkuInfoList{
		&ShopSkuInfo{
			SkuID: notExistSkuID,
			Stock: leftNum,
		},
		&ShopSkuInfo{
			SkuID: existSkuID,
			Stock: leftNum,
		},
	}, SkuIDTypeSkuID)
	t.Log(utils.Format4Output(opResult, false))
	if err == nil {
		t.Fatal("应该要报错才对")
	}
	if len(opResult.FailedList) != 1 {
		t.Fatal("错误列表不正确")
	}

	skuInfo, err := api.SkuList(testShopID, &SkuListParams{
		SkuID: existSkuID,
	})
	if err != nil {
		t.Fatal(err)
	}
	if skuInfo.List[0].LeftNum != leftNum {
		t.Fatal("设置库存失败")
	}
}

func TestSkuStockUpdateOne(t *testing.T) {
	const (
		notExistSkuID = 12345678
		existSkuID    = 156406688807623

		leftNum = 234
	)
	err := api.SkuStockUpdateOne(utils.GetUUID(), testShopID, &ShopSkuInfo{
		SkuID: existSkuID,
		Stock: leftNum,
	})
	if err != nil {
		t.Fatal(err)
	}
	skuInfo, err := api.SkuList(testShopID, &SkuListParams{
		SkuID: existSkuID,
	})
	if err != nil {
		t.Fatal(err)
	}
	if skuInfo.List[0].LeftNum != leftNum {
		t.Fatal("设置库存失败")
	}
}
func TestGetEbaiCatIDFromName(t *testing.T) {
	ebaiCatID := api.GetEbaiCatIDFromName("300280", "应季水果新鲜美味")
	t.Log(ebaiCatID)
}

func TestGetEbaiSkuIDFromCustomID(t *testing.T) {
	ebaiCatID := api.GetEbaiSkuIDFromCustomID("300001", "31554")
	t.Log(ebaiCatID)
}

func TestDeleteStoreSku(t *testing.T) {
	fmt.Println(101 / 50)
}

// TestDeleteSku 删除饿了么商品
func TestDeleteSku(t *testing.T) {
	shopId := "667453"
	param1 := &SkuListParams{
		Page:     1,
		PageSize: 100,
	}
	skuInfo, err := api.SkuList(shopId, param1)
	if err != nil {
		fmt.Println(err)
		return
	}
	skuIds := make([]int64, 0, 0)

	for i := 1; i <= skuInfo.Pages; i++ {
		param := &SkuListParams{
			Page:     i,
			PageSize: 100,
		}
		skuInfo2, err2 := api.SkuList(shopId, param)
		if err2 != nil {
			fmt.Println(err2)
		}
		for _, v := range skuInfo2.List {
			skuIds = append(skuIds, v.SkuId)
		}
	}
	for i := 1; i <= skuInfo.Pages; i++ {
		data, err := api.SkuDelete(utils.GetUUID(), shopId, skuIds[i-1:i*100], nil)
		fmt.Println(data)
		fmt.Println(err)
	}

	//categoryList, _ := api.ShopCategoryGet(shopId)
	//for _, v := range categoryList {
	//	if v.Children != nil {
	//		for _, v2 := range v.Children {
	//			api.ShopCategoryDelete(shopId, v2.CategoryID)
	//		}
	//	}
	//	api.ShopCategoryDelete(shopId, v.CategoryID)
	//}
}

func TestCategoryAttrValueList(t *testing.T) {
	api.CategoryAttrValueList(201232923, "666667")
}
