package sfps2

import (
	"encoding/json"
	"fmt"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/globals"
	"io/ioutil"
	"net/http"
)

const (
	// 回调url前缀
	UrlIndexRiderStatus    = "rider_status"    //配送状态更改回调
	UrlIndexRiderRecall    = "rider_recall"    //骑士撤单状态回调
	UrlIndexOrderComplete  = "order_complete"  //订单完成回调
	UrlIndexSFCancel       = "sf_cancel"       //顺丰原因订单取消回调
	UrlIndexRiderException = "rider_exception" //订单异常回调
)

// RiderStatus10  配送状态更改回调
type RiderStatus10 struct {
	ArriveShopTime int    `json:"arrive_shop_time"`
	OperatorName   string `json:"operator_name"`  //配送员姓名
	OperatorPhone  string `json:"operator_phone"` //配送员电话
	OrderStatus    int    `json:"order_status"`   //10-配送员接单/改派;12:配送员到店;15:配送员配送中
	PushTime       int    `json:"push_time"`      //状态变更时间
	RiderLng       string `json:"rider_lng"`      //配送员位置经度
	RiderLat       string `json:"rider_lat"`      //配送员位置纬度
	SFOrderID      string `json:"sf_order_id"`    //顺丰订单ID
	SFUCode        string `json:"sf_ucode"`
	ShopId         string `json:"shop_id"`       // 店铺ID
	ShopOrderID    string `json:"shop_order_id"` //商家订单ID
	StatusDesc     string `json:"status_desc"`   //状态描述
	UrlIndex       string `json:"url_index"`     //回调url前缀
}

// RiderStatus  配送状态更改回调 12:配送员到店;15:配送员配送中
type RiderStatus struct {
	ShopId        float64 `json:"shop_id"`        // 店铺ID
	SFOrderID     string  `json:"sf_order_id"`    //顺丰订单ID
	ShopOrderID   string  `json:"shop_order_id"`  //商家订单ID
	UrlIndex      string  `json:"url_index"`      //回调url前缀
	OperatorName  string  `json:"operator_name"`  //配送员姓名
	OperatorPhone string  `json:"operator_phone"` //配送员电话
	RiderLng      float64 `json:"rider_lng"`      //配送员位置经度
	RiderLat      float64 `json:"rider_lat"`      //配送员位置纬度
	OrderStatus   float64 `json:"order_status"`   //10-配送员接单/改派;12:配送员到店;15:配送员配送中
	StatusDesc    string  `json:"status_desc"`    //状态描述
	SFUCode       string  `json:"sf_ucode"`
	PushTime      int     `json:"push_time"` //状态变更时间
}

// RiderRecall  骑士撤单状态回调
type RiderRecall struct {
	OrderStatus float64 `json:"order_status"`  //22-配送员撤单
	PushTime    int     `json:"push_time"`     //状态变更时间
	SFOrderID   string  `json:"sf_order_id"`   //顺丰订单ID
	ShopId      string  `json:"shop_id"`       // 店铺ID
	ShopOrderID string  `json:"shop_order_id"` //商家订单ID
	UrlIndex    string  `json:"url_index"`     //回调url前缀
	StatusDesc  string  `json:"status_desc"`   //状态描述
}

// OrderComplete 订单完成回调
type OrderComplete struct {
	OperatorName  string  `json:"operator_name"`  //配送员姓名
	OperatorPhone string  `json:"operator_phone"` //配送员电话
	OrderStatus   float64 `json:"order_status"`   //17配送员点击完成
	PushTime      int     `json:"push_time"`      //完成时间
	ReceiRptType  float64 `json:"receiRpt_type"`
	RiderLat      float64 `json:"rider_lat"`
	RiderLng      float64 `json:"rider_lng"`
	SfOrderID     string  `json:"sf_order_id"`
	SfUcode       string  `json:"sf_ucode"`
	ShopId        float64 `json:"shop_id"` // 店铺ID
	ShopOrderID   string  `json:"shop_order_id"`
	StatusDesc    string  `json:"status_desc"` //状态描述
	UrlIndex      string  `json:"url_index"`
}

// SFCancel  顺丰原因订单取消回调
type SFCancel struct {
	CancelReason  string  `json:"cancel_reason"`  //取消原因
	CancelCode    float64 `json:"cancel_code"`    //取消码
	OperatorName  string  `json:"operator_name"`  //配送员姓名
	OperatorPhone string  `json:"operator_phone"` //配送员电话
	OrderStatus   float64 `json:"order_status"`   //2:订单取消
	PushTime      int     `json:"push_time"`      //状态变更时间
	RiderLng      string  `json:"rider_lng"`      //配送员位置经度
	RiderLat      string  `json:"rider_lat"`      //配送员位置纬度
	SFOrderID     string  `json:"sf_order_id"`    //顺丰订单ID
	SFUCode       string  `json:"sf_ucode"`
	ShopId        string  `json:"shop_id"`       // 店铺ID
	ShopOrderID   string  `json:"shop_order_id"` //商家订单ID
	UrlIndex      string  `json:"url_index"`     //回调url前缀
	StatusDesc    string  `json:"status_desc"`   //状态描述
}

// RiderException  订单异常回调
type RiderException struct {
	ShopId        float64 `json:"shop_id"`        // 店铺ID
	SFOrderID     string  `json:"sf_order_id"`    //顺丰订单ID
	ShopOrderID   string  `json:"shop_order_id"`  //商家订单ID
	UrlIndex      string  `json:"url_index"`      //回调url前缀
	OperatorName  string  `json:"operator_name"`  //配送员姓名
	OperatorPhone string  `json:"operator_phone"` //配送员电话
	OrderStatus   float64 `json:"order_status"`   //固定为91
	StatusDesc    string  `json:"status_desc"`    //状态描述
	ExID          int     `json:"ex_id"`          //异常ID
	ExContent     string  `json:"ex_content"`     //异常详情
	ExpectTime    string  `json:"expect_time"`    //新的预计送达时间
	PushTime      int     `json:"push_time"`      //顺丰推送时间
}

type CallbackResponse struct {
	ErrorCode int    `json:"error_code"`
	ErrorMsg  string `json:"error_msg"`
}

var (
	SuccessResponse = &CallbackResponse{ErrorCode: SuccessCode, ErrorMsg: SuccessMsg}
)

func Err2CallbackResponse(err error) *CallbackResponse {
	if err == nil {
		return SuccessResponse
	}
	return &CallbackResponse{
		ErrorCode: -1,
		ErrorMsg:  fmt.Sprint(err),
	}
}

// GetCallbackUrlIndex   配送状态更改回调
func (a *API) GetCallbackUrlIndex(request *http.Request) (map[string]interface{}, *CallbackResponse) {
	data, err := ioutil.ReadAll(request.Body)
	if err != nil {
		return nil, CallbackResponseErr(false)
	}

	var (
		urlIndex = ""
		temp     = map[string]interface{}{}
	)
	if err = json.Unmarshal(data, &temp); err == nil {
		urlIndex = temp["url_index"].(string)
	}
	result := make(map[string]interface{}, 0)
	switch urlIndex {
	case UrlIndexRiderStatus:
		retVal := RiderStatus{}
		if err = json.Unmarshal(data, &retVal); err != nil {
			return nil, CallbackResponseErr(false)
		}
		result[UrlIndexRiderStatus] = retVal
	case UrlIndexRiderRecall:
		retVal := RiderRecall{}
		if err := json.Unmarshal(data, &retVal); err != nil {
			return nil, CallbackResponseErr(false)
		}
		result[UrlIndexRiderRecall] = retVal
	case UrlIndexOrderComplete:
		retVal := OrderComplete{}
		if err = json.Unmarshal(data, &retVal); err != nil {
			return nil, CallbackResponseErr(false)
		}
		result[UrlIndexOrderComplete] = retVal
	case UrlIndexSFCancel:
		retVal := SFCancel{}
		if err = json.Unmarshal(data, &retVal); err != nil {
			return nil, CallbackResponseErr(false)
		}
		result[UrlIndexSFCancel] = retVal
	default:
		return nil, CallbackResponseErr(false)
	}
	globals.SugarLogger.Debugf("GetCallbackUrlIndex result=%s", utils.Format4Output(result, false))
	return result, SuccessResponse
}

// GetRiderExceptionCallback    订单异常回调
func (a *API) GetRiderExceptionCallback(request *http.Request) (riderException *RiderException, response *CallbackResponse) {
	data, err := ioutil.ReadAll(request.Body)
	if err != nil {
		response = &CallbackResponse{ErrorCode: -1}
		return nil, response
	}

	if err = json.Unmarshal(data, &riderException); err != nil {
		response = &CallbackResponse{ErrorCode: -1}
		return nil, response
	}
	return riderException, SuccessResponse
}
