package tiktok_api

import (
	"fmt"
	"git.rosy.net.cn/baseapi"
	product_editV2_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/product_editV2/request"
	product_editV2_commit_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/product_editV2_commit/request"
	product_listV2_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/product_listV2/request"
	sku_editPrice_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/sku_editPrice/request"
	sku_syncStock_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/sku_syncStock/request"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/globals"
	"go.uber.org/zap"
	"strings"
	"testing"
)

func init() {
	logger, _ := zap.NewDevelopment()
	sugarLogger = logger.Sugar()
	baseapi.Init(sugarLogger)

}

func TestCreateSubProduct(t *testing.T) {
	a.CreateSubProduct(3592508194611683124, 64251631)
}

func TestCategory(t *testing.T) {
	data, err := a.GetShopCategory(20314)
	for _, v := range data {
		fmt.Println(v.Id, "---", v.Name, "---", v.IsLeaf, "---", v.Level)
	}
	fmt.Println("data====", err)
}

func TestGetCatePropertyV2(t *testing.T) {
	data, err := a.GetCatePropertyV2(33622)
	fmt.Println("err=====", err)
	globals.SugarLogger.Debugf("data:=%s", utils.Format4Output(data, false))
	fmt.Println("data====", data) // 202209281558450102081001701D7B32C5
}

func TestInt16(t *testing.T) {
	fmt.Println(utils.Str2Int16(strings.ReplaceAll("08:00", ":", "")))
}

// 查询商品详情(抖音平台id)
func TestGetSkuDetail(t *testing.T) {
	data, err := a.GetSkuDetailLocalID("", "25078")
	fmt.Println(err)
	// 3582171870197365727 3582171870197365727
	globals.SugarLogger.Debugf("====%s", utils.Format4Output(data, false))
}

// 查询商品详情本地商品id
func TestGetSkuDetailLocalId(t *testing.T) {
	data, err := a.GetSkuDetail("3592382914735826936", "")
	fmt.Println(err)
	globals.SugarLogger.Debugf("====%s", utils.Format4Output(data, false))
}

func TestUpdateSkuStore(t *testing.T) {
	err := a.UpdateSkuStock(&sku_syncStock_request.SkuSyncStockParam{
		ProductId:   3602878545761816498,
		OutSkuId:    25250,
		StockNum:    0,
		Incremental: false,
	})
	fmt.Println(err)
}

func TestEiduPrice(t *testing.T) {
	err := a.EditPrice(&sku_editPrice_request.SkuEditPriceParam{
		Price:     1000,
		SkuId:     1746278107578416,
		ProductId: 3605159442921953230,
	})
	fmt.Println(err)
}

// s上架1
func TestEditStoreCommodity(t *testing.T) {
	err := a.EditStoreCommodity(&product_editV2_request.ProductEditV2Param{
		CategoryLeafId:      22428,
		Name:                "净芹菜约350g/份(净菜芹菜摘叶洗净，保鲜膜包装)",
		Pic:                 "https://p3-aio.ecombdimg.com/obj/ecom-shop-material/PTWpUjYg_m_78dbc86c51be55406cd2f51bc863b2e0_sx_32178_www800-800",
		Description:         "https://p3-aio.ecombdimg.com/obj/ecom-shop-material/PTWpUjYg_m_4fdaab3dd1df6ed4aa8d0990b0e99ee7_sx_456574_www750-3527",
		PayType:             1,
		ReduceType:          2,
		Weight:              350,
		WeightUnit:          1,
		DeliveryDelayDay:    9999,
		Mobile:              "18048531223",
		Commit:              true,
		Specs:               "重量|350g",
		SpecPrices:          "[{\"spec_detail_name1\":\"350g\",\"spec_detail_name2\":\"\",\"spec_detail_name3\":\"\",\"stock_num\":9999,\"price\":850,\"code\":\"23010\",\"step_stock_num\":0,\"supplier_id\":\"\",\"outer_sku_id\":\"23010\",\"sku_type\":1,\"stock_num_map\":{\"79226307\":9999},\"delivery_infos\":[{\"info_type\":\"weight\",\"info_value\":\"350\",\"info_unit\":\"g\"}]}]",
		ProductFormatNew:    "{\n\t\"1687\": [\n\t\t{\n\t\t\t\"diy_type\": 0,\n\t\t\t\"name\": \"品牌\",\n\t\t\t\"value\": 789194134\n\t\t}\n\t]\n}",
		StandardBrandId:     789194134,
		NeedCheckOut:        false,
		ForceUseQualityList: false,
		NeedRechargeMode:    false})
	t.Fatal(err)
}

// 上架2
func TestEditStoreCommodity2(t *testing.T) {
	a.EditStoreCommodityCommit(&product_editV2_commit_request.ProductEditV2Param2{
		ProductId: 3576377564144516403,
		Commit:    true,
	})
}

// 下架
func TestProductSetOffline(t *testing.T) {
	token := `{"access_token":"a1746210-a8a3-4497-a87b-09d1f10dbb95","expires_in":1665652230,"scope":"SCOPE","shop_id":"","shop_name":"小时达开放平台对接专用店","refresh_token":"c1cf8d88-0983-4f2a-b969-3746fae6b0cd","authority_id":""}`
	a := New("7136048270014416392", "c397aa9f-3927-47c4-8cfe-4d84e02602e0", token)
	a.ProductSetOffline(3576377564144516403)
}

// 解密购买用户电话,名字,地址
func TestBatchDecrypt(t *testing.T) {
	a := New("7136048270014416392", "c397aa9f-3927-47c4-8cfe-4d84e02602e0", token)
	encrypt_post_tel := "##mjApaTO5LNGFDnAU2UmRwu1zhlDQH2+NjIc9kTlQzUYpf1XXrxyxUzd1nWdTPb9H3iFNAdFG66zReSdhf6qUtNVBwNEn5RnIM8wQapxFjh4v+g==*CgkIARCtHCABKAESPgo81dqD65kMclp62fAJBLUpNOP22AKtaL8/7CHDPtDCS8wVtCp/TWhRNLsuFrq/Pmhhz+fe3GDtG8R/L0O0GgA=#1##"
	encrypt_post_receiver := "##Cg8e7Ks78U0rSX7AO6NxoD9pPtISczJdulakYdCJDNqXLsFMfhG/0mz7CoxJJyY7zRvx1dkFKahgRk0NCsAxApns7NaaGDu9tyEXloBu9Sc=*CgkIARCtHCABKAESPgo8ja2mXE+INEhReF2W0+deMN0qb/ZoPtaxS98YbR4a1smRO4WPj+jSiYsBv00+DxiXfJTW3DdxusF6TVS3GgA=#1##"
	encrypt_post_name := ``
	name, tel, address, err := a.OrderUserInfoDecrypt("6917547162766021688", encrypt_post_name, encrypt_post_tel, encrypt_post_receiver)
	fmt.Println(name)
	fmt.Println(tel)
	fmt.Println(address)
	fmt.Println(err)
}

//// 脱敏购买用户电话,名字,地址
//func TestOrderUserInfoSensitive(t *testing.T) {
//	token := `{"access_token":"a1746210-a8a3-4497-a87b-09d1f10dbb95","expires_in":1665652230,"scope":"SCOPE","shop_id":"","shop_name":"小时达开放平台对接专用店","refresh_token":"c1cf8d88-0983-4f2a-b969-3746fae6b0cd","authority_id":""}`
//
//	a := New("7136048270014416392", "c397aa9f-3927-47c4-8cfe-4d84e02602e0", token)
//	a.OrderUserInfoSensitive("4988354190732430566")
//}

var c = []int64{
	87893329,
	87542533,
	86486240,
	86454312,
	85672244,
	85672146,
	85672027,
	85671854,
	85671727,
	85671454,
	85671173,
	85670943,
	84938439,
	84936029,
	83727966,
	81521327,
	80790189}

//668851,
//668841,
//668711,
//668770,
//668709,
//668708,
//668713,
//668707,
//668712,running
//668710,running
//668714,running

//668723, ok
//668790, no
//668789, no
//668785, no
//668512, running
//667481, running

// 删除已经创建商品,重新同步
func TestDeleteSkuAndUploadSku(t *testing.T) {
	var errList = make([]error, 0, 0)
	for _, v := range c {
		for i := 1; i < 100; i++ {
			result, err := a.GetSkuDetailList(&product_listV2_request.ProductListV2Param{
				CheckStatus: 3, // 1-未提交；2-待审核；3-审核通过；4-审核未通过；5-封禁；7-审核通过待上架
				Page:        int64(i),
				Size:        100,
				StoreId:     v,
			})
			if err != nil {
				globals.SugarLogger.Debugf("=index := %d ,err := %s", i, utils.Format4Output(err, false))
				continue
			}
			if len(result.Data) == 0 {
				globals.SugarLogger.Debugf("====没有商品的门店 %d", v)
				break
			}
			for _, v := range result.Data {
				err := a.DeleteStoreCommodity(v.ProductId)
				if err != nil {
					errList = append(errList, err)
				}
			}
		}
	}

	for _, v := range errList {
		globals.SugarLogger.Debugf("err := %s", utils.Format4Output(v, false))
	}
}

// 删除已经创建商品,重新同步封禁
func TestDeleteSkuAndUploadSkuFengJinBiXu222(t *testing.T) {
	var errList = make([]error, 0, 0)
	for i := 1; i < 100; i++ {
		result, err := a.GetSkuDetailList(&product_listV2_request.ProductListV2Param{
			Status:      0,
			CheckStatus: 3, // 1-未提交；2-待审核；3-审核通过；4-审核未通过；5-封禁；7-审核通过待上架
			Page:        int64(i),
			Size:        100,
			StoreId:     63465423,
		})
		if err != nil {
			globals.SugarLogger.Debugf("=index := %d ,err := %s", i, utils.Format4Output(err, false))
			continue
		}
		if len(result.Data) == 0 {
			break
		}
		for _, v := range result.Data {
			err := a.DeleteStoreCommodity(v.ProductId)
			if err != nil {
				errList = append(errList, err)
			}
		}
	}

	for _, v := range errList {
		globals.SugarLogger.Debugf("err := %s", utils.Format4Output(v, false))
	}
}

// 删除已经创建商品,重新同步
func TestDeleteSkuAndUploadSku55(t *testing.T) {
	var errList = make([]error, 0, 0)
	for _, v := range c {
		for i := 1; i < 100; i++ {
			result, err := a.GetSkuDetailList(&product_listV2_request.ProductListV2Param{
				CheckStatus: 3, // 1-未提交；2-待审核；3-审核通过；4-审核未通过；5-封禁；7-审核通过待上架
				Page:        int64(i),
				Size:        100,
				StoreId:     v,
			})
			if err != nil {
				globals.SugarLogger.Debugf("=index := %d ,err := %s", i, utils.Format4Output(err, false))
				continue
			}
			if len(result.Data) == 0 {
				globals.SugarLogger.Debugf("====没有商品的门店 %d", v)
				break
			}
			for _, v := range result.Data {
				err := a.DeleteStoreCommodity(v.ProductId)
				if err != nil {
					errList = append(errList, err)
				}
			}
		}
	}

	for _, v := range errList {
		globals.SugarLogger.Debugf("err := %s", utils.Format4Output(v, false))
	}
}

// 删除已经创建商品,重新同步
func TestDeleteSkuAndUploadSku7(t *testing.T) {
	var errList = make([]error, 0, 0)
	for _, v := range c {
		for i := 1; i < 100; i++ {
			result, err := a.GetSkuDetailList(&product_listV2_request.ProductListV2Param{
				CheckStatus: 3, // 1-未提交；2-待审核；3-审核通过；4-审核未通过；5-封禁；7-审核通过待上架
				Page:        int64(i),
				Size:        100,
				StoreId:     v,
			})
			if err != nil {
				globals.SugarLogger.Debugf("=index := %d ,err := %s", i, utils.Format4Output(err, false))
				continue
			}
			if len(result.Data) == 0 {
				globals.SugarLogger.Debugf("====没有商品的门店 %d", v)
				break
			}
			for _, v := range result.Data {
				err := a.DeleteStoreCommodity(v.ProductId)
				if err != nil {
					errList = append(errList, err)
				}
			}
		}
	}

	for _, v := range errList {
		globals.SugarLogger.Debugf("err := %s", utils.Format4Output(v, false))
	}
}

/***********************************/
// 删除已经创建商品,重新同步 下架
func TestDeleteSkuAndUploadSkuXiaJiaBiXu(t *testing.T) {
	var errList = make([]error, 0, 0)
	for _, v := range c {
		for i := 1; i < 100; i++ {
			result, err := a.GetSkuDetailList(&product_listV2_request.ProductListV2Param{
				Status:      1,
				CheckStatus: 1, // 1-未提交；2-待审核；3-审核通过；4-审核未通过；5-封禁；7-审核通过待上架
				Page:        int64(i),
				Size:        100,
				StoreId:     v,
			})
			if err != nil {
				globals.SugarLogger.Debugf("=index := %d ,err := %s", i, utils.Format4Output(err, false))
				continue
			}
			if len(result.Data) == 0 {
				globals.SugarLogger.Debugf("====没有商品的门店 %d", v)
				break
			}
			for _, v := range result.Data {
				err := a.DeleteStoreCommodity(v.ProductId)
				if err != nil {
					errList = append(errList, err)
				}
			}
		}
	}

	for _, v := range errList {
		globals.SugarLogger.Debugf("err := %s", utils.Format4Output(v, false))
	}
}

// 删除已经创建商品,重新同步封禁
func TestDeleteSkuAndUploadSkuFengJinBiXu(t *testing.T) {
	var errList = make([]error, 0, 0)
	for _, v := range c {
		for i := 1; i < 100; i++ {
			result, err := a.GetSkuDetailList(&product_listV2_request.ProductListV2Param{
				Status:      1,
				CheckStatus: 5, // 1-未提交；2-待审核；3-审核通过；4-审核未通过；5-封禁；7-审核通过待上架
				Page:        int64(i),
				Size:        100,
				StoreId:     v,
			})
			if err != nil {
				globals.SugarLogger.Debugf("=index := %d ,err := %s", i, utils.Format4Output(err, false))
				continue
			}
			if len(result.Data) == 0 {
				globals.SugarLogger.Debugf("====没有商品的门店 %d", v)
				break
			}
			for _, v := range result.Data {
				err := a.DeleteStoreCommodity(v.ProductId)
				if err != nil {
					errList = append(errList, err)
				}
			}
		}
	}

	for _, v := range errList {
		globals.SugarLogger.Debugf("err := %s", utils.Format4Output(v, false))
	}
}

// 删除已经创建商品,重新同步延迟上架
func TestDeleteSkuAndUploadSkuYanChiBiXu(t *testing.T) {
	var errList = make([]error, 0, 0)
	for _, v := range c {
		for i := 1; i < 100; i++ {
			result, err := a.GetSkuDetailList(&product_listV2_request.ProductListV2Param{
				Status:      0,
				CheckStatus: 7, // 1-未提交；2-待审核；3-审核通过；4-审核未通过；5-封禁；7-审核通过待上架
				Page:        int64(i),
				Size:        100,
				StoreId:     v,
			})
			if err != nil {
				globals.SugarLogger.Debugf("=index := %d ,err := %s", i, utils.Format4Output(err, false))
				continue
			}
			if len(result.Data) == 0 {
				globals.SugarLogger.Debugf("====没有商品的门店 %d", 111)
				break
			}
			for _, v := range result.Data {
				err := a.DeleteStoreCommodity(v.ProductId)
				if err != nil {
					errList = append(errList, err)
				}
			}
		}
	}
	for _, v := range errList {
		globals.SugarLogger.Debugf("err := %s", utils.Format4Output(v, false))
	}
}

// 删除已经创建商品,重新同步封禁
func TestDeleteSkuAndUploadSkuFengJinBiXu22332(t *testing.T) {
	var errList = make([]error, 0, 0)
	for i := 1; i < 100; i++ {
		result, err := a.GetSkuDetailList(&product_listV2_request.ProductListV2Param{
			Status:      0,
			CheckStatus: 4, // 1-未提交；2-待审核；3-审核通过；4-审核未通过；5-封禁；7-审核通过待上架
			Page:        int64(i),
			Size:        100,
		})
		if err != nil {
			globals.SugarLogger.Debugf("=index := %d ,err := %s", i, utils.Format4Output(err, false))
			continue
		}
		if len(result.Data) == 0 {
			break
		}
		for _, v := range result.Data {
			err := a.DeleteStoreCommodity(v.ProductId)
			if err != nil {
				errList = append(errList, err)
			}
		}
	}

	for _, v := range errList {
		globals.SugarLogger.Debugf("err := %s", utils.Format4Output(v, false))
	}
}
