package jdapi

import (
	"fmt"
	"sync"
	"testing"

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

const (
	mustExistSkuID   = 2023747677
	mustExistSkuJXID = "5246"
)

func TestGetStationInfoList(t *testing.T) {
	result, err := api.GetStationInfoList("11910721", []int64{2023432263})
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestDelVipPrice(t *testing.T) {
	err := api.DelVipPrice("667281", []*SkuIdEntity{
		&SkuIdEntity{
			OutSkuId: "31196",
		},
	})
	if err != nil {
		t.Fatal(err)
	}
	t.Fatal(err)
	//t.Log(utils.Format4Output(result, false))
}

//func TestQueryOpenUseable(t *testing.T) {
//	result, err := api.QueryOpenUseable([]*BaseStockCenterRequest{
//		&BaseStockCenterRequest{
//			StationNo: mustExistStoreID,
//			SkuId:     mustExistSkuID,
//		},
//	})
//	if err != nil {
//		t.Fatal(err)
//	}
//	t.Log(utils.Format4Output(result, false))
//}

//func TestQueryStockCenter(t *testing.T) {
//	result, err := api.QueryStockCenter(mustExistStoreJXID, []*SkuIdEntity{
//		&SkuIdEntity{
//			OutSkuId: mustExistSkuJXID,
//		},
//	}, "test")
//	if err != nil {
//		t.Fatal(err)
//	}
//	t.Log(utils.Format4Output(result, false))
//}

func TestBatchUpdateVendibility(t *testing.T) {
	result, err := api.BatchUpdateVendibility("", "800302", "", []*StockVendibility{
		&StockVendibility{
			OutSkuId: "6042945",
			DoSale:   true,
		},
	}, "test")
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
	//baseapi.SugarLogger.Debug(utils.Format4Output(result, false))
}

func TestUpdateVendibility(t *testing.T) {
	result, err := api.UpdateVendibility("", []*QueryStockRequest{
		&QueryStockRequest{
			StationNo: "11053496",
			SkuId:     2012224772,
			DoSale:    1,
		},
	})
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
	//baseapi.SugarLogger.Debug(utils.Format4Output(result, false))
}

func TestUpdateVendorStationPrice(t *testing.T) {
	result, err := api.UpdateVendorStationPrice("", "2", "", []*SkuPriceInfo{
		&SkuPriceInfo{
			OutSkuId: "02",
			Price:    200,
		},
		&SkuPriceInfo{
			OutSkuId: "01",
			Price:    200,
		},
	})
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
	//baseapi.SugarLogger.Debug(utils.Format4Output(result, false))
}

func TestBatchUpdateCurrentQtys(t *testing.T) {
	result, err := api.BatchUpdateCurrentQtys("", "2", "", []*SkuStock{
		&SkuStock{
			OutSkuId: "01",
			StockQty: 111,
		},
		&SkuStock{
			OutSkuId: "2212",
			StockQty: 111,
		},
	}, "")
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
	//baseapi.SugarLogger.Debug(utils.Format4Output(result, false))
}

type Object2222 struct {
	Clients map[string]int
	*sync.RWMutex
}

func NewTcpClient222() *Object2222 {
	t := &Object2222{
		Clients: make(map[string]int),
	}
	t.RWMutex = new(sync.RWMutex)
	return t
}
func TestLock(t *testing.T) {
	obj := NewTcpClient222()
	go func() {
		fmt.Println("1")
		for i := 0; i < 100000; i++ {
			obj.setParam(utils.Int2Str(i), i)
		}
	}()

	go func() {
		fmt.Println("2")
		for i := 0; i < 100000; i++ {
			fmt.Println(obj.getParam(utils.Int2Str(i)))
		}
	}()

	for i := 0; i < 100000; i++ {
		fmt.Println(obj.Clients[utils.Int2Str(i)])
	}
}

func (t *Object2222) setParam(key string, v int) {
	t.Lock()
	defer t.Unlock()
	t.Clients[key] = v
}

func (t *Object2222) getParam(key string) int {
	t.RLock()
	defer t.RUnlock()
	return t.Clients[key]
}
