package jdshopapi

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

//Creat by hang At 2022-03-28
//用于处理获得消息后对于订单的Api调用 jdsOrderApi和jdsOrder2Api分别对应shop_jxcs2021和shop——jxcs2022两个订单 由于api分为两个店铺 因此对一个OrderId查询需要同时引入两个aou

type GetEnOrderResult struct {
	OrderStateRemark  string                  `json:"orderStateRemark"`
	OrderRemark       string                  `json:"orderRemark"`
	OrderSellerPrice  string                  `json:"orderSellerPrice"`
	OrderState        string                  `json:"orderState"`
	OrderType         string                  `json:"orderType"`
	ConsigneeInfo     *CallBackConsigneeInfo  `json:"consigneeInfo"`
	OrderPayment      string                  `json:"orderPayment"`
	PayType           string                  `json:"payType"`
	ItemInfoList      []*CallBackItemInfoList `json:"itemInfoList"`
	StoreID           string                  `json:"storeId"`
	OrderTotalPrice   string                  `json:"orderTotalPrice"`
	OrderExt          string                  `json:"orderExt"`
	StoreOrder        string                  `json:"storeOrder"`
	OrderStartTime    string                  `json:"orderStartTime"`
	OrderID           string                  `json:"orderId"`
	OrderSource       string                  `json:"orderSource"`
	FreightPrice      string                  `json:"freightPrice"`
	Pin               string                  `json:"pin"`
	IDSopShipmenttype string                  `json:"idSopShipmenttype"`
	ScDT              string                  `json:"scDT"`
	SellerDiscount    string                  `json:"sellerDiscount"`
	MenDianID         string                  `json:"menDianId"`
	BalanceUsed       string                  `json:"balanceUsed"`
}

//根据订单ID查询订单 京东换了新的接口 原来的接口变为收费接口
//https://open.jd.com/home/home#/doc/api?apiCateId=55&apiId=2366&apiName=jingdong.pop.oto.locorderinfo.enget
//目前消息队列同时订阅2011和2012的账号但是对应的是两个accessToken所以对于一个订单id要先查一个api如果查不到后再查一个api
//另外由于现在的接口对于返回的数据中手机和pin都不支持解密详见工单 手机号已脱敏改造
//https://open.jd.com/home/home#/support/my-ask-detail/17522185
func (a *API) GetOrderById(orderID int64, isStatus bool) (getOrderResult *GetEnOrderResult, err error) {
	params := make(map[string]interface{})
	params["order_id"] = orderID
	params["optional_fields"] = `orderType,payType,orderTotalPrice,orderSellerPrice,
	orderPayment,freightPrice,orderState,orderStateRemark,
	orderStartTime,orderEndTime,orderRemark,consigneeInfo,
	itemInfoList,pauseBizInfo,pin,idSopShipmenttype,scDT,sellerDiscount,menDianId,storeId,balanceUsed,orderExt`
	if isStatus {
		params["order_state"] = "WAIT_SELLER_STOCK_OUT,TRADE_CANCELED,PAUSE,WAIT_GOODS_RECEIVE_CONFIRM,FINISHED_L"
	}

	result, err := a.AccessAPI("jingdong.pop.order.enGet", prodURL2, params)
	if err == nil {
		utils.Map2StructByJson(result["jingdong_pop_order_enGet_responce"].(map[string]interface{})["orderDetailInfo"].(map[string]interface{})["orderInfo"], &getOrderResult, false)
	}
	return getOrderResult, err
}

func (a *API) GetOrderById2(orderID int64, isStatus bool) (getOrderResult *GetEnOrderResult, err error) {
	params := make(map[string]interface{})
	params["order_id"] = orderID
	params["code_type"] = `0`
	if isStatus {
		params["order_state"] = "WAIT_SELLER_STOCK_OUT,TRADE_CANCELED,PAUSE,WAIT_GOODS_RECEIVE_CONFIRM,FINISHED_L"
	}

	result, err := a.AccessAPI("jingdong.pop.oto.locorderinfo.enget", prodURL2, params)
	if err == nil {
		utils.Map2StructByJson(result["jingdong_pop_order_enGet_responce"].(map[string]interface{})["orderDetailInfo"].(map[string]interface{})["orderInfo"], &getOrderResult, false)
	}
	return getOrderResult, err
}
