package tao_vegetable

import (
	"fmt"
	domain591 "git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability591/domain"
	request591 "git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability591/request"
	"git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/util"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/globals"
	"strings"
	"testing"
	"time"
)

func TestName(t *testing.T) {

}
func TestGetOrderDetail(t *testing.T) {
	requestParam := &request591.AlibabaAelophyOrderGetRequest{OrderGetRequest: &domain591.AlibabaAelophyOrderGetOrderGetRequest{
		StoreId:    utils.String2Pointer("JX668594"),
		BizOrderId: utils.Int64ToPointer(5000894158854290922),
	}}
	data, err := apiTao.QueryOrderDetail(requestParam)
	globals.SugarLogger.Debugf("data := %s", utils.Format4Output(data, false))
	globals.SugarLogger.Debugf("err := %s", utils.Format4Output(err, false))
}

// 接单
func TestDeliveryFinishACCEPTED(t *testing.T) {
	param := &request591.AlibabaAelophyOrderWorkCallbackRequest{}
	param.WorkCallbackRequest = &domain591.AlibabaAelophyOrderWorkCallbackWorkCallbackRequest{
		StoreId:                      utils.String2Pointer("JX668594"),
		BizOrderId:                   utils.Int64ToPointer(5000894158854290922),
		Status:                       utils.String2Pointer("ACCEPTED"),
		StatusRemark:                 nil,
		DelivererName:                nil,
		DelivererPhone:               nil,
		WorkCallbackSubOrderInfoList: nil,
		DelivererCompany:             nil,
		LogisticsNo:                  nil,
	}
	err := apiTao.DeliveryFinish(param)
	fmt.Println(err)
}

// 取消订单
func TestDeliveryFinishREJECTED(t *testing.T) {
	// 发送取消状态,商户取消
	err := apiTao.DeliveryFinish(&request591.AlibabaAelophyOrderWorkCallbackRequest{
		WorkCallbackRequest: &domain591.AlibabaAelophyOrderWorkCallbackWorkCallbackRequest{
			StoreId:    utils.String2Pointer("JX668594"),
			BizOrderId: utils.Int64ToPointer(5000894665550700922),
			Status:     utils.String2Pointer("REJECTED"),
		},
	})

	fmt.Println(err)
}

func TestGetTransactionOrderList(t *testing.T) {
	queryData := time.Now()
	create := time.Date(queryData.Year(), queryData.Month(), queryData.Day(), 0, 0, 0, 0, queryData.Location())
	end := time.Date(queryData.Year(), queryData.Month(), queryData.Day(), 23, 59, 59, 59, queryData.Location())
	createTime := util.LocalTime(create)
	endTime := util.LocalTime(end)
	var pageIndex int64 = 1
	var PageSize int64 = 20
	param := &request591.AlibabaWdkOrderListRequest{
		BatchQueryRequest: &domain591.AlibabaWdkOrderListBatchQueryRequest{
			StoreIds:  &[]string{"JX668594"},
			StartTime: &createTime,
			EndTime:   &endTime,
			OrderFrom: utils.Int64ToPointer(ChannelCome),
			ShopId:    nil,
			StoreId:   utils.String2Pointer("JX668594"),
			BizType:   nil,
		},
	}
	param.BatchQueryRequest.SetPageSize(PageSize)
	param.BatchQueryRequest.SetPageIndex(pageIndex)

	data, err := apiTao.GetTransactionOrderList(param)
	if err != nil {
		globals.SugarLogger.Debugf("err := %s", utils.Format4Output(err, false))
		return
	}

	// 取消订单
	for _, v := range *data.Orders {
		err := apiTao.DeliveryFinish(&request591.AlibabaAelophyOrderWorkCallbackRequest{
			WorkCallbackRequest: &domain591.AlibabaAelophyOrderWorkCallbackWorkCallbackRequest{
				StoreId:    utils.String2Pointer("JX668594"),
				BizOrderId: utils.Int64ToPointer(*v.BizOrderId),
				Status:     utils.String2Pointer("REJECTED"),
			},
		})
		if err != nil {
			fmt.Println(err)
		}
	}
}

func TestCcc(t *testing.T) {
	data := "{\n\t\"refundFee\": 100,\n\t\"merchantCode\": \"CSSJ\",\n\t\"refundReason\": \"退款原因\",\n\t\"subRefundOrders\": [\n\t\t{\n\t\t\t\"refundFee\": 100,\n\t\t\t\"maxRefundFee\": 200,\n\t\t\t\"outSubOrderId\": \"5018100091801\",\n\t\t\t\"refundAmount\": 2\n\t\t}\n\t],\n\t\"storeId\": \"S001\",\n\t\"refundDeliveryFee\": 0,\n\t\"refundPictures\": \"https://open-retail.ele.me/#/msgdoc/detail?topicName=order.partrefund.push\\u0026aopApiCategory=order_msg_group\\u0026type=push_menu\",\n\t\"bizRefundId\": \"50001110022\",\n\t\"outOrderId\": \"500120110121\",\n\t\"refundPackageFee\": 0,\n\t\"orderFrom\": 3,\n\t\"refundId\": \"50181000918\",\n\t\"remarks\": \"备注\"\n}"
	resultParam := strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(data, "\n", ""), "\t", ""), " ", "")

	resultParam2 := strings.ReplaceAll(resultParam, `\u0026`, "&")
	fmt.Println(resultParam2)
}
