package jd

import (
	"strings"
	"testing"

	"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
	"git.rosy.net.cn/jx-callback/business/model"

	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/model/dao"
)

const (
	TestStoreNo   = 100164
	TestJdStoreNo = "11736989"
)

func TestReadStore(t *testing.T) {
	result, err := new(PurchaseHandler).ReadStore(jxcontext.AdminCtx, "", TestJdStoreNo)
	if err != nil {
		t.Fatal(err.Error())
	}
	t.Log(utils.Format4Output(result, false))
}

func TestUpdateStore(t *testing.T) {
	handler := new(PurchaseHandler)
	result, err := handler.ReadStore(jxcontext.AdminCtx, "", TestJdStoreNo)

	// result := &model.Store{}
	// result.ID = 100164
	// err := dao.GetEntity(nil, result)
	db := dao.GetDB()
	if err != nil {
		t.Fatal(err.Error())
	}
	result.Name += "h"
	newName := result.Name
	err = handler.UpdateStore(db, TestStoreNo, "autotest")
	if err != nil {
		t.Fatal(err.Error())
	}

	// same
	result, err = handler.ReadStore(jxcontext.AdminCtx, "", TestJdStoreNo)
	if result.Name != newName {
		t.Fatalf("result is not same, desired newName:%s, newName:%s", newName, result.Name)
	}

	// restore
	result.Name = strings.Trim(result.Name, "h")
	err = handler.UpdateStore(db, TestStoreNo, "autotest")
	if err != nil {
		t.Fatal(err.Error())
	}
}

func TestUpdateStore2(t *testing.T) {
	handler := new(PurchaseHandler)

	// result := &model.Store{}
	// result.ID = 100164
	// err := dao.GetEntity(nil, result)
	db := dao.GetDB()
	// restore
	err := handler.UpdateStore(db, 1, "autotest")
	if err != nil {
		t.Fatal(err.Error())
	}
}

// func TestCoordRangeConversion(t *testing.T) {
// 	jxRange := "108841759,34332892;108842271,34330820;108846013,34331422;108846110,34333189;108847722,34331853;108856703,34331729;108866149,34327507;108873423,34320980;108877737,34312856;108877727,34299624;108870105,34287988;108855137,34290911;108867884,34286298;108858260,34281316;108854162,34283490;108853803,34280145;108846110,34279291;108830587,34282539;108818806,34291500;108814493,34299624;108813596,34308465;108818797,34320980;108830582,34329941;108841759,34332892"
// 	jdRange := "108.841759,34.332892;108.842271,34.330820;108.846013,34.331422;108.846110,34.333189;108.847722,34.331853;108.856703,34.331729;108.866149,34.327507;108.873423,34.320980;108.877737,34.312856;108.877727,34.299624;108.870105,34.287988;108.855137,34.290911;108.867884,34.286298;108.858260,34.281316;108.854162,34.283490;108.853803,34.280145;108.846110,34.279291;108.830587,34.282539;108.818806,34.291500;108.814493,34.299624;108.813596,34.308465;108.818797,34.320980;108.830582,34.329941;108.841759,34.332892"

// 	if JdRange2JxRange(jdRange) != jxRange {
// 		t.Fatal("result doesn't match")
// 	}

// 	if JxRange2JdRange(jxRange) != jdRange {
// 		t.Fatal("result doesn't match")
// 	}
// }

func TestSyncQualify(t *testing.T) {
	storeDetail, err := dao.GetStoreDetail(dao.GetDB(), 102610, model.VendorIDJD)
	if err != nil {
		t.Fatal(err.Error())
	}
	err = CurPurchaseHandler.SyncQualify(jxcontext.AdminCtx, storeDetail)
	if err != nil {
		t.Fatal(err.Error())
	}
}
