package fnpsapi

import (
	"fmt"
	"git.rosy.net.cn/baseapi"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/model"
	"git.rosy.net.cn/jx-callback/globals"
	"go.uber.org/zap"
	"testing"
)

var (
	api         *API
	sugarLogger *zap.SugaredLogger
)

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

	api = New("6705486294797503379", "c1e6c280-e618-4103-9d0a-673bc54fb22e", "51658", "")
	//token, _ := api.GetAccessToken()
	api.accessToken = "2f2898e8-bf14-438a-944f-a1a0df7e6d21"
}

func TestGetAccessToken(t *testing.T) {
	api = New("6705486294797503379", "c1e6c280-e618-4103-9d0a-673bc54fb22e", "51658", "o86wptdoBZfFLHLbGt7xQH")
	token, err := api.GetAccessToken()
	globals.SugarLogger.Debug("err: %s", utils.Format4Output(err, false))
	globals.SugarLogger.Debug("token: %s", utils.Format4Output(token, false))
}

// 查询单个门店
func TestQueryOneStore(t *testing.T) {
	data, err := api.GetStore("805224")
	fmt.Println(data)
	fmt.Println(err)
}

func TestHttpToGuoYuanFN(t *testing.T) {
	HttpToGuoYuanFN(map[string]interface{}{"token": "c21e7a3b-e51e-4fe6-a02a-a751a011f5ee_1111", "refresh_token": "72227d0d-42eb-452e-abc6-80a30ac6f410"}, "token", model.ServerTypeFruits)
}

// 正式下单
func TestCreateOrder(t *testing.T) {
	api = New("6705486294797503379", "c1e6c280-e618-4103-9d0a-673bc54fb22e", "5375691", "")
	token, err := api.GetAccessToken()
	fmt.Println("22222222222", err)
	api.accessToken = token.BusinessDataObj.AccessToken

	aa := []*GoodsItemsList{
		{"白菜", 1, 100, 100, "30011", 1, "备注:大白菜"},
		//{"白菜2", 1, 100, 100, "30012", 1, "备注:大白菜2"},
		//	{"白菜3", 1, 100, 100, "1004", 1, "备注:大白菜2"},
	}
	_, err = api.CreateOrder(&CreateOrderReqParam{
		PartnerOrderCode:      "232232992229uue21",
		OrderType:             1,
		PositionSource:        3,
		ReceiverAddress:       "四川成都",
		ReceiverLongitude:     116.307892,
		ReceiverLatitude:      40.039115,
		GoodsTotalAmountCent:  100,
		GoodsActualAmountCent: 100,
		GoodsWeight:           0.25,
		GoodsCount:            1,
		GoodsItemList:         aa,
		ReceiverName:          "刘磊",
		ReceiverPrimaryPhone:  "18981810340",
		OutShopCode:           "",
		ChainStoreId:          "209476483",
	})
	fmt.Println(err)
}

// 创建门店,
func TestCreateStore(t *testing.T) {
	api = New("6705486294797503379", "c1e6c280-e618-4103-9d0a-673bc54fb22e", "5375691", "cabrXQf9eFMVWVYg4hNlwu")
	token, _ := api.GetAccessToken()
	api.accessToken = token.BusinessDataObj.AccessToken
	data, err := api.CreateStore(&CreateStoreBaseInfo{
		HeadShopName:           "刘磊测试门店10",
		ContactPhone:           "18981810340",
		Address:                "四川成都",
		Longitude:              104.094555,
		Latitude:               30.661382,
		PositionSource:         3,
		OutShopCode:            "6379110",
		CategoryID:             "12",
		OwnerName:              "刘磊",
		OwnerIDNum:             "511324199308263974",
		HandheldLicencePicHash: "d7c64022f6458f9aa76968e01f5686c5.jpeg",
		OwnerIDPicFrontHash:    "d7c64022f6458f9aa76968e01f5686c5.jpeg",
		OwnerIDPicBackHash:     "d7c64022f6458f9aa76968e01f5686c5.jpeg",
		CreditCode:             "12345",
		BusinessLicencePicHash: "d7c64022f6458f9aa76968e01f5686c5.jpeg",

		BranchShopName:                       "",
		ChainstoreType:                       2,
		SettlementModel:                      "1",
		SettlementAccountID:                  "",
		FoodLicensePicHash:                   "d7c64022f6458f9aa76968e01f5686c5.jpeg",
		SecondMedicalEquipmentLicensePicHash: "",
		MedicalInstitutionLicensePicHash:     "",
		MedicalEquipmentLicensePicHash:       "",
		MedicineLicensePicHash:               "",
		TabacooLicensePicHash:                "",
	})
	fmt.Println(err)
	fmt.Println(data)
}

func TestUpdataStore(t *testing.T) {
	api = New("6705486294797503379", "c1e6c280-e618-4103-9d0a-673bc54fb22e", "5375691", "cabrXQf9eFMVWVYg4hNlwu")
	token, _ := api.GetAccessToken()
	api.accessToken = token.BusinessDataObj.AccessToken
	err := api.UpdateStore(&UpdateStoreParam{
		ChainStoreID:           209636747,
		HeadShopName:           "刘磊测试门店",
		ContactPhone:           "18981810340",
		Address:                "四川成都",
		Longitude:              104.094555,
		Latitude:               30.661382,
		PositionSource:         3,
		OutShopCode:            "637910",
		CategoryID:             "12",
		OwnerName:              "刘磊",
		OwnerIDNum:             "511324199308263974",
		HandheldLicencePicHash: "d7c64022f6458f9aa76968e01f5686c5.jpeg",
		OwnerIDPicFrontHash:    "d7c64022f6458f9aa76968e01f5686c5.jpeg",
		OwnerIDPicBackHash:     "d7c64022f6458f9aa76968e01f5686c5.jpeg",
		CreditCode:             "12345",
		BusinessLicencePicHash: "d7c64022f6458f9aa76968e01f5686c5.jpeg",

		BranchShopName:                       "",
		ChainstoreType:                       2,
		FoodLicensePicHash:                   "d7c64022f6458f9aa76968e01f5686c5.jpeg",
		SecondMedicalEquipmentLicensePicHash: "",
		MedicalInstitutionLicensePicHash:     "",
		MedicalEquipmentLicensePicHash:       "",
		MedicineLicensePicHash:               "",
		TabacooLicensePicHash:                "",
	})
	fmt.Println(err)
}

func TestClient(t *testing.T) {
	api = New("6705486294797503379", "c1e6c280-e618-4103-9d0a-673bc54fb22e", "51658", "4W4hqacKND6NOct5gCyjbT")
	token, err := api.GetAccessToken()
	api.accessToken = token.BusinessDataObj.AccessToken
	fmt.Println("token===", token.BusinessDataObj.AccessToken)
	fmt.Println("err=====", err)
}

// 预下单
func TestGetOrder(t *testing.T) {
	api = New("6705486294797503379", "c1e6c280-e618-4103-9d0a-673bc54fb22e", "51658", "tMNyQk52Q7VA36Q9XXbTdX")
	//token, _ := api.GetAccessToken()
	api.accessToken = "a3e04dde-4930-4145-871d-8c2a99039d55"

	aa := []*GoodsItemsList{
		{"白菜", 1, 100, 100, "30011", 1, "备注:大白菜"},
		{"白菜2", 1, 100, 100, "30012", 1, "备注:大白菜2"},
		//	{"白菜3", 1, 100, 100, "1004", 1, "备注:大白菜2"},
	}
	deliveryFee, baseDeliveryFee, err := api.PreCreateByShopFn(&PreCreateOrder{
		PartnerOrderCode:      "2102193962599091789",
		OrderType:             1,
		PositionSource:        3,
		ReceiverAddress:       "浙江省宁波市鄞州区邱隘镇回龙村盛莫路以西回龙村农产品集中交易点（地铁工地后面（回龙菜市场）",
		ReceiverLongitude:     121.627465,
		ReceiverLatitude:      29.818844,
		GoodsTotalAmountCent:  100,
		GoodsActualAmountCent: 100,
		GoodsWeight:           0.25,
		GoodsCount:            1,
		GoodsItemList:         aa,
		//ServiceGoodsId:        nil,
		//BaseGoodsId:           nil,
		//OutShopCode:           "637910",
		ChainStoreID: "220418619",
	})
	fmt.Println(err)
	fmt.Println(deliveryFee)
	fmt.Println(baseDeliveryFee)
}

func TestQueryOrder(t *testing.T) {
	data, err := api.QueryOrder("4201952231529175730") // 1100537991119839487  1100536591119839487
	globals.SugarLogger.Debug("data:= %s", utils.Format4Output(data, false))
	globals.SugarLogger.Debug("err:= %s", utils.Format4Output(err, false))
}

func TestCancelOrder(t *testing.T) {
	api = New("6705486294797503379", "c1e6c280-e618-4103-9d0a-673bc54fb22e", "51658", "")
	api.accessToken = "f5716fd7-f69b-491f-b63c-d0167c9097b6"
	param := CancelOrderReq{
		GetOrderDetailReq:      GetOrderDetailReq{PartnerOrderCode: "88421859678001"},
		OrderCancelCode:        9,
		OrderCancelRole:        2,
		OrderCancelOtherReason: "不想要了",
	}
	err := api.CancelOrder(&param) // 1100537991119839487  1100536591119839487
	globals.SugarLogger.Debug("err:= %s", utils.Format4Output(err, false))
}

func TestComplaintRider(t *testing.T) {
	api = New("6705486294797503379", "c1e6c280-e618-4103-9d0a-673bc54fb22e", "51658", "")
	api.accessToken = "0d572b0b-9742-4e2b-9d00-5a6609867dd3"
	err := api.ComplaintRider("1100643132821623946", "300000718145112173", 160, "1")
	fmt.Println(err)
}

func TestQueryBillBalance(t *testing.T) {
	api.QueryBillBalance()
}
