package jdapi

import (
	"fmt"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/globals"
	"math"
	"strings"
	"testing"
	"time"
)

func TestQueryCategoriesByOrgCode(t *testing.T) {
	result, err := api.QueryCategoriesByOrgCode()
	if err != nil {
		t.Fatal(err)
	}
	if len(result) == 0 {
		t.Fatal("QueryCategoriesByOrgCode category list is empty!")
	}
}

func TestChangeShopCategoryOrder(t *testing.T) {
	var aa = []int64{6836731, 6836749, 6836718, 6836757, 6836770, 6836779, 6836719, 6836720, 6836721, 6836722, 6836793, 6836937, 6836723, 6836724, 6836794, 6836839, 6836725, 6836796, 6836726, 6836797, 6836798, 6836799, 6836727, 6836840, 6836940, 6836800, 6836801, 6836802, 6837303}
	api.ChangeShopCategoryOrder(0, aa)
}

func TestGetSkuCategoryByName(t *testing.T) {
	data, err := api.GetSkuCategoryBySkuName("喜之郎蒟蒻果冻草莓味")
	data2 := strings.Contains("[outSkuId:6100591], 类目ID不存在 level:1, code:11003", "类目ID不存在")
	globals.SugarLogger.Debugf("data:=%s", utils.Format4Output(data, false))
	globals.SugarLogger.Debugf("data:=%v", err)
	globals.SugarLogger.Debugf("data2:=%v", data2)
}

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

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

func TestAddsku(t *testing.T) {
	result, err := api.AddSku2(&OpSkuParam{
		TraceID:        "679DBC742F0611F0B322525400E86DC0,18981810340",
		OutSkuID:       "6099680",
		ShopCategories: []int64{37839055},
		CategoryID:     37090,
		BrandID:        35247,
		SkuName:        "双汇王中王 火腿肠 50g*10根500g/袋",
		SkuPrice:       2460,
		Weight:         0.5,
		FixedStatus:    1,
		IsSale:         -1,
		Upc:            "6666060996802",
		Images:         []string{"https://image.jxc4.com/image/5d4f19b5e2a282e9bf88325ac06d7ec3.jpg"},
		IfViewDesc:     0,
	})
	globals.SugarLogger.Debugf("--------result := %s", utils.Format4Output(result, false))
	globals.SugarLogger.Debugf("--------result := %v", err)
}

func TestNewAddSku(t *testing.T) {
	param := &NewAddSkuParam{
		TraceId:        "277E7786502411F1A173525400E86DC0,18981810340",
		OutSkuId:       "233273898",
		ShopCategories: []int64{0},
		CategoryId:     30066,
		SkuName:        "浪琴谷 L62木质电脑低音炮音响usb迷你家用笔记本小音箱喇叭音响 2个/份",
		SkuPrice:       8600,
		Weight:         math.Round(1*1000) / 1000,
		FixedStatus:    1,
		IsSale:         true,
		Upc:            "6956057720160", //
		Images: []string{
			"http://p0.meituan.net/retailsp/68b0c7cf3d2afd3f5217cbde7cc5d37e125973.jpg",
			"http://p1.meituan.net/retailsp/195403138bd6220c25328a820b1d06f4153320.jpg",
			"http://p0.meituan.net/retailsp/9012e8de839c35279ac9332b9d0477da151461.jpg",
			"http://p0.meituan.net/retailsp/a0172d9c55a1a5f0c9b9a2ae9b5ea21d178262.jpg",
			"http://p0.meituan.net/retailsp/badf205a7cca6615ab1f92fee8cb46ca101364.jpg",
		},
		ProductDesc: "",
		//IfViewDesc: 0,
	}
	if param.Upc == "" {
		productInfo, _ := api.GetJdUpcCodeByName(param.SkuName, "", 1, 10)
		if len(productInfo) != 0 {
			param.Upc = productInfo[0].UpcCode
		}
	}

	brandList, err := api.GetSkuCategoryBySkuName(param.SkuName)
	if brandList == nil || err != nil {
		param.BrandId = 35247
	} else {
		param.BrandId = int64(brandList.BrandId)
		param.CategoryId = int64(brandList.CategoryId)
	}

	result, err := api.NewAddSku(param)
	globals.SugarLogger.Debugf("--------result r1 := %s", result)
	globals.SugarLogger.Debugf("--------err r1 := %v", err)

	err1 := api.UpdateCurrentQty("1122131212", "23378966", utils.Str2Int64(result), 9999)
	if err1 != nil {
		globals.SugarLogger.Debugf("--------err r1 := %v", err1)
	}
	_, err2 := api.UpdateVendibility("225886314785", []*QueryStockRequest{
		&QueryStockRequest{
			StationNo: "23378966",
			SkuId:     utils.Str2Int64(result),
			DoSale:    0,
		},
	})
	if err2 != nil {
		globals.SugarLogger.Debugf("--------err r1 := %v", err2)
	}

	globals.SugarLogger.Debugf("--------result := %s", utils.Format4Output(result, false))
	globals.SugarLogger.Debugf("--------result := %v", err)
}

func TestUpdateCurrentQty(t *testing.T) {
	err1 := api.UpdateCurrentQty(utils.Int64ToStr(time.Now().Unix()), "23988364", 10214378230898, 9999)
	fmt.Println(err1)
}

func TestNewAddSku2(t *testing.T) {
	param := map[string]interface{}{
		"traceId":        "33F7CD45502711F190B1525400E86DC0,18981810340",
		"outSkuId":       "233273863",
		"shopCategories": []int64{104299412},
		"categoryId":     28303,
		"brandId":        35247,
		"skuName":        "魔小二2G U盘储存盘 约20g/卡",
		"skuPrice":       2600,
		"weight":         0.02,
		"upc":            "6972701260325",
		"images": []string{
			"http://p0.meituan.net/retailsp/be1da6491c7fb22ed6c75ab0c35a29d561181.jpg",
			"http://p1.meituan.net/retailsp/f1e763a7cf8f796c3c4aab116e9a627481307.jpg",
			"http://p1.meituan.net/retailsp/82ace5f81ed14810a7d498cea8c7dc4871311.jpg",
		},
		"productDesc": "",
		"fixedStatus": 1,
		"isSale":      true,
	}
	brandList, err := api.GetSkuCategoryBySkuName("浪琴谷 L62木质电脑低音炮音响usb迷你家用笔记本小音箱喇叭音响 2个/份")
	fmt.Println(brandList)
	fmt.Println(err)
	api.NewAddSku222(param)
}

// 根据品名获取到家品牌编号ID
func TestQueryPageBrandInfo(t *testing.T) {
	brandList, _, err := api.QueryPageBrandInfo(1, 20, 0, "青竹丙烯美丽乡村")
	globals.SugarLogger.Debugf("--------result := %s", utils.Format4Output(brandList, false))
	globals.SugarLogger.Debugf("--------result := %v", err)
}

func TestQueryChildCategoriesForOP(t *testing.T) {
	api.QueryChildCategoriesForOP(0)
}
func TestDelete(t *testing.T) {
	var searchAfterSkuId string = ""
	var jdSkus = make(map[string]int, 0)
	for i2 := 0; i2 < 85; i2++ {
		globals.SugarLogger.Debugf("----i:= %d", i2)
		idList, afterSkuId, err := api.AuerySkuInfoList(int64(i2), 20, searchAfterSkuId)
		if err != nil {
			globals.SugarLogger.Debugf("--------err:%v", err)
			return
		}
		time.Sleep(time.Second * 1)
		searchAfterSkuId = utils.Int64ToStr(afterSkuId)
		for _, ifd := range idList {
			jdSkus[ifd] = 1
		}
		if len(idList) < 20 {
			break
		}
	}
}
