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("131006464", "33665")
	fmt.Println(err)
	// 3582171870197365727 3582171870197365727
	globals.SugarLogger.Debugf("====%s", utils.Format4Output(data, false))
}

// 查询商品详情本地商品id
func TestGetSkuDetailLocalId(t *testing.T) {
	data, err := a.GetSkuDetail("3592503538749683119", "")
	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 TestBatchDecrypt(t *testing.T) {
	orderID := "6919995408850359976"
	order, err := a.GetTiktokOrderDetail(orderID)
	if err != nil || order == nil {
		globals.SugarLogger.Debugf("GetTiktokOrderDetail err := %s", utils.Format4Output(order, false))
		return
	}
	name, tel, address, _ := a.OrderUserInfoDecrypt(orderID, order.EncryptPostReceiver, order.EncryptPostTel, order.PostAddr.EncryptDetail)
	fmt.Println(name)
	fmt.Println(tel)
	fmt.Println(address)
	fmt.Println(err)
}

var c = []int64{131006464}

// 删除已经创建商品,重新同步
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: 1, // 1-未提交；2-待审核；3-审核通过；4-审核未通过；5-封禁；7-审核通过待上架
				Page:        int64(i),
				Size:        100,
				StoreId:     v,
				Status:      1,
			})
			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:     63509510,
			})
			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: 3, // 1-未提交；2-待审核；3-审核通过；4-审核未通过；5-封禁；7-审核通过待上架
			Page:        int64(i),
			Size:        100,
			StoreId:     81521327,
		})
		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))
	}
}
