package ebaiapi

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

func TestShopList(t *testing.T) {
	result, err := api.ShopList(SysStatusAll)

	if err != nil {
		t.Fatal(err)
	}

	data := make(map[string]int64, 0)
	dataErr := make(map[string]int64, 0)
	vendorKey := make([]int64, 0, 0)
	for _, v := range result {
		if len(v.ShopID) != 6 {
			dataErr[v.ShopID] = v.BaiduShopID
			vendorKey = append(vendorKey, v.BaiduShopID)
		}
	}

	for _, vk := range vendorKey {
		status2, _ := api.ShopBusStatusGet("", vk, "")
		if status2 == 2 || status2 == 3 {
			globals.SugarLogger.Debugf("==vk : %d", vk)
		}
	}
	t.Log(utils.Format4Output(data, false))
	t.Log(utils.Format4Output(dataErr, false))
	t.Log(utils.Format4Output(vendorKey, false))

	//t.Log(utils.Format4Output(data, false))
	//t.Log(utils.Format4Output(dataErr, false))
}

//todo 修改ebai平台与本地映射
func TestShopIDBatchUpdate(t *testing.T) {
	var (
		fBaiduShopIDs []string
		fShopIDs      []string
	)
	key := map[int64]string{
		509107995: "668893",
	}
	for k, v := range key {
		k1 := utils.Int64ToStr(k)
		remoteRel, err := api.ShopGet2("", k)
		fmt.Sprintf("err====%v", err)
		if remoteRel.ShopID != v {
			fBaiduShopIDs = append(fBaiduShopIDs, k1)
			fShopIDs = append(fShopIDs, v)
		}
	}
	fmt.Sprintf("fBaiduIDs=====%s", fBaiduShopIDs)
	fmt.Sprintf("fBaiduIDs=====%s", fShopIDs)
	err := api.ShopIDBatchUpdate(fBaiduShopIDs, fShopIDs)
	//err := api.ShopIDBatchUpdate([]string{utils.Int2Str(2235877567)}, []string{"100920"})
	if err != nil {
		t.Fatal(err)
	}
	// err = api.ShopIDBatchUpdate([]string{utils.Int2Str(testShopBaiduID)}, []string{testShopID})
	// if err != nil {
	// 	t.Fatal(err)
	// }
}

func TestShopIDBatchUpdate2(t *testing.T) {
	data := map[string]int64{
		"667252": 32267779552, "100200": 2233065993, "100489": 2233066035, "100876": 2233066089, "100306": 2233066125, "100805": 2233066147, "100726": 2233066189, "100999": 2233066221, "100972": 2233066288, "100933": 2233066508, "101667": 2235330385, "101691": 2233065954, "101686": 2235811199, "101075": 2235877563, "101793": 2233066050, "101040": 2234549767, "101747": 2235877564, "102091": 2267141526, "101666": 2267141507, "102001": 2267141468, "102316": 2267141488, "102126": 2267141487, "100717": 2233066054, "100203": 2233066065, "102170": 2267121508, "102197": 32267034115, "102404": 32267034132, "102362": 32267034127, "102116": 2267092843, "102130": 32267089386, "102154": 32267089387, "102418": 32267089392, "102501": 32267089401, "102495": 32267089402, "102525": 32267089415, "102505": 32267089419, "102535": 32267089420, "102536": 32267089421, "102547": 32267089424, "100617": 32267105197, "102610": 32267105203, "102615": 32267105205, "102653": 32267105211, "102667": 32267105218, "102709": 32267144438, "102745": 32267144444, "102776": 32267144467, "102086": 2267141477, "102888": 32267184020, "102806": 32267184025, "102869": 32267184037, "102849": 32267184061, "102698": 2233065970, "102575": 2267030904, "102976": 32267258872, "102992": 32267315045, "102293": 32267315069, "103093": 32267358106, "102973": 32267184017, "103088": 32267387505, "100922": 32267388562, "100326": 2235877566, "103062": 32267630601, "102997": 32267358124, "666918": 32267655031, "667176": 32267655039, "667154": 32267715494, "101107": 32267779547, "100162": 32267779555, "667084": 32267674079, "667398": 32267804825, "667421": 32267812971, "103127": 32267387534, "102180": 32267715495, "667339": 32267144482, "667445": 32267089406, "667083": 32267674060, "666916": 32267630644, "103085": 32267105208, "667400": 32267812970, "667139": 32267630624, "102925": 32267804790, "102063": 32267184031, "100117": 32267779369, "666921": 32267742254, "102383": 2234778219, "668786": 508226060, "669232": 512276503, "669201": 1176226197,
	}
	for k, v := range data {
		err := api.ShopIDBatchUpdate([]string{utils.Int64ToStr(v)}, []string{k})
		globals.SugarLogger.Debugf("err := %v", err)
	}
}

func TestShopCreate(t *testing.T) {
	result, err := api.ShopCreate(map[string]interface{}{
		"shop_id":       "667550",
		"name":          "互利超市(金牛区店)",
		"supplier_id":   "1486975681715",
		"province":      32,
		"city":          75,
		"county":        698,
		"address":       "成都市金牛区二环路北一段8号",
		"phone":         18160030913, // 18980429231,
		"service_phone": 18160030913, //18980429231,
		"ivr_phone":     18160030913, //18048531223,
		"longitude":     104.043677,
		"latitude":      30.692361,
		"shop_logo":     "https://image-star.elemecdn.com/pb/dab294b0d3e9fa4b5fa63007ff02d06e98",
		"coord_type":    "amap",
		"categorys": []map[string]int{
			map[string]int{
				"category1": 166,
				"category2": 222,
			},
		},
		"business_time": []map[string]string{
			map[string]string{
				"start": "7:00",
				"end":   "19:00",
			},
		},
		"business_form_id": 1672214913,
	})
	if err != nil {
		t.Fatal(err)
	} else {
		t.Log(utils.Format4Output(result, false))
	}
}

func TestShopGet(t *testing.T) {
	result, err := api.ShopGet2("", 1138456086) //2233065879 ,22267134648
	if err != nil {
		t.Fatal(err)
	} else {
		t.Log(utils.Format4Output(result, false))
	}

	// result, err = api.ShopGet(testShopID, 0)
	// if err != nil {
	// 	t.Fatal(err)
	// } else {
	// 	t.Log(utils.Format4Output(result, false))
	// }
}

func TestShopAptitudeGet(t *testing.T) {
	result, err := api.ShopAptitudeGet("", 335768207)
	if err != nil {
		t.Fatal(err)
	} else {
		t.Log(utils.Format4Output(result, false))
	}
}

func TestShopUpdate(t *testing.T) {
	params := map[string]interface{}{
		"address":           "四川省成都市双流区东升街道藏卫路中段21号D区23号",
		"baidu_shop_id":     "511574437",
		"business_time":     []map[string]string{{"end": "19:30", "start": "07:30"}},
		"ivr_phone":         "17366937206",
		"package_box_price": 100,
		"service_phone":     "13310841099",
	}
	err := api.ShopUpdate(params)
	if err != nil {
		t.Fatal(err)
	} else {

	}
}

var StoreStatus2 = []int64{32267258880}
var status = map[int]string{1: "休息中", 2: "可预订", 3: "营业中", 4: "停营业"}

func TestShopBusStatusGet(t *testing.T) {
	fmt.Println(len(StoreStatus2))
	for _, v := range StoreStatus2 {
		result, err := api.ShopBusStatusGet("", v, PlatformFlagElm)
		if err != nil {
			globals.SugarLogger.Debugf("storeID[%d],%v", v, err)
		} else {
			if result == 4 {
				globals.SugarLogger.Debugf("storeID[%d],%s", v, status[result])
			}
		}
	}
}

func TestShopOpen(t *testing.T) {
	for _, v := range StoreStatus2 {
		err := api.ShopOpen("", v)
		if err != nil {
			globals.SugarLogger.Debugf("%d,%v", v, err)
		}
	}

}

func TestShopStatusGet(t *testing.T) {
	result, err := api.ShopStatusGet("", 32267034105)
	if err != nil {
		t.Fatal(err)
	} else {
		t.Log(result)
	}
}

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

func TestShopClose(t *testing.T) {
	aa := []int64{1329553284}

	for i := 0; i < len(aa); i++ {
		err := api.ShopClose("", aa[i])
		if err != nil {
			t.Fatal(err)
		}
		t.Logf("id .......%d", aa[i])
	}

}

func TestAbc(t *testing.T) {
	a := "88492098261735"
	fmt.Println(a[len(a)-4:])
}

func TestShopOffline(t *testing.T) {
	err := api.ShopOffline("", testShopBaiduID)
	if err != nil {
		t.Fatal(err)
	}
}

func TestShopAnnouncementSet(t *testing.T) {
	err := api.ShopAnnouncementSet("", 2233065983, "亲爱的顾客祝您用餐愉快我们店里，男人粗手粗脚您懂的，万一出错餐，漏装了，或者眼瞎没有检查出质量问题，亲爱的千万别发飙，恼怒很伤人，咱们该退就退，该换就换，世上没有解决不了的事🤝希望亲给予处理售后的机会！如果餐品还满意，我们需要亲的⑤星鼓励。祝您阖家欢乐，开心每一天！快乐每一刻！幸福每一秒 ps：如果在线消息没能及时回复，麻烦您这边拨打门店电话哦️！")
	if err != nil {
		t.Fatal(err)
	}
}

func TestShopAptitudeUpload(t *testing.T) {
	err := api.ShopAptitudeUpload("", 335768207, map[string]interface{}{
		"aptitude": []map[string]interface{}{
			map[string]interface{}{
				"type_1":                    1,
				"type_2":                    101,
				"license_number":            "92510106L597085088",
				"license_address":           "金牛区二环路北一段8号1（A-2、A-3、A-4）楼",
				"long_term_valid":           1,
				"legal_representative_name": "张应友",
				"license_name":              "营业执照",
				"photos": []map[string]interface{}{
					map[string]interface{}{
						"photo_url":        "http://image.jxc4.com/image/c436338a2af4d87e30b770cfc1bb8041.jpg",
						"waterprinter_url": "http://image.jxc4.com/image/c436338a2af4d87e30b770cfc1bb8041.jpg",
					},
				},
			},
			map[string]interface{}{
				"type_1":                    2,
				"type_2":                    202,
				"license_number":            "JY15101060104947（1-1）",
				"license_validdate":         "2023-10-09",
				"license_name":              "食品经营许可证",
				"license_address":           "金牛区二环路北一段8号1（A-2、A-3、A-4）楼",
				"legal_representative_name": "张应友",
				"photos": []map[string]interface{}{
					map[string]interface{}{
						"photo_url":        "http://image.jxc4.com/image/93d2126adfd28350385b5067e2770edf.jpg",
						"waterprinter_url": "http://image.jxc4.com/image/93d2126adfd28350385b5067e2770edf.jpg",
					},
				},
			},
			map[string]interface{}{
				"type_1":                    3,
				"type_2":                    301,
				"license_number":            "510921196608255211",
				"long_term_valid":           1,
				"license_name":              "身份证",
				"legal_representative_name": "张应友",
				"photos": []map[string]interface{}{
					map[string]interface{}{
						"photo_url":        "http://image.jxc4.com/image/ebdaf32e771a6c050e397624c3fae369.jpg",
						"waterprinter_url": "http://image.jxc4.com/image/ebdaf32e771a6c050e397624c3fae369.jpg",
					},
					map[string]interface{}{
						"photo_url":        "http://image.jxc4.com/image/75fad719d61326cedd73f17b1af6338b.jpg",
						"waterprinter_url": "http://image.jxc4.com/image/75fad719d61326cedd73f17b1af6338b.jpg",
					},
				},
			},
			map[string]interface{}{
				"type_1": 5,
				"type_2": 501,
				"photos": []map[string]interface{}{
					map[string]interface{}{
						"photo_url":        "http://image.jxc4.com/image/53bd1275a8a9cd5898912fa78857f284.jpg",
						"waterprinter_url": "http://image.jxc4.com/image/53bd1275a8a9cd5898912fa78857f284.jpg",
					},
				},
			},
			map[string]interface{}{
				"type_1": 5,
				"type_2": 502,
				"photos": []map[string]interface{}{
					map[string]interface{}{
						"photo_url":        "http://image.jxc4.com/image/74c371be5bf39d795cf6c02507e7ca84.jpg",
						"waterprinter_url": "http://image.jxc4.com/image/74c371be5bf39d795cf6c02507e7ca84.jpg",
					},
				},
			},
		},
	})
	if err != nil {
		t.Fatal(err)
	}
}

func TestShopDeliveryinfoGet(t *testing.T) {
	vendorStoreID := "1128314567"
	store, err := api.ShopGet2("", utils.Str2Int64(vendorStoreID))
	globals.SugarLogger.Debugf("-:=%s", utils.Format4Output(store, false))
	globals.SugarLogger.Debugf("-:=%v", err)

	list, err := api.ShopDeliveryinfoGet("", utils.Str2Int64(vendorStoreID))
	if err == nil && len(list) > 0 {
		deliveryRange := EbaiDeliveryRegion2Jx2(list[0])
		globals.SugarLogger.Debugf("-:=%s", deliveryRange)
	}

}

func EbaiDeliveryRegion2Jx2(deliveryRegion map[string]interface{}) string {
	if len(deliveryRegion) > 0 {
		if deliveryRegion["delivery_areas"] == nil {
			return ""
		}
		region := deliveryRegion["delivery_areas"].([]interface{})[0].(map[string]interface{})["coordinates"].([]interface{})
		coords := make([]string, len(region))
		for k, v := range region {
			mapV := v.(map[string]interface{})
			coords[k] = fmt.Sprintf("%.6f,%.6f", utils.MustInterface2Float64(mapV["longitude"]), utils.MustInterface2Float64(mapV["latitude"]))
		}
		return strings.Join(coords, ";")
	}
	return ""
}
