package jdeclp

import (
	"fmt"
	"time"

	"git.rosy.net.cn/baseapi/platformapi/jdeclpapi"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/jxutils"
	"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
	"git.rosy.net.cn/jx-callback/business/model"
	"git.rosy.net.cn/jx-callback/business/model/dao"
	"git.rosy.net.cn/jx-callback/business/partner"
	"git.rosy.net.cn/jx-callback/globals/api"
)

var (
	CurDeliveryHandler *DeliveryHandler
)

type DeliveryHandler struct {
}

func init() {
	CurDeliveryHandler = new(DeliveryHandler)
	partner.RegisterDeliveryPlatform(CurDeliveryHandler, true)
}

func (c *DeliveryHandler) GetVendorID() int {
	return model.VendorIDJDWL
}

func OnWaybillMsg() {
}

func (c *DeliveryHandler) OnWaybillMsg() {

}

func (c *DeliveryHandler) CreateStore(ctx *jxcontext.Context, storeDetail *dao.StoreDetail2) (vendorStoreID string, status int, err error) {
	return vendorStoreID, status, err
}
func (c *DeliveryHandler) GetStore(ctx *jxcontext.Context, storeID int, vendorStoreID string) (storeDetail *dao.StoreDetail2, err error) {
	return storeDetail, err
}
func (c *DeliveryHandler) IsErrStoreNotExist(err error) bool {
	return false
}
func (c *DeliveryHandler) IsErrStoreExist(err error) bool {
	return false
}
func (c *DeliveryHandler) CreateWaybill(order *model.GoodsOrder, maxDeliveryFee int64) (bill *model.Waybill, err error) {
	storeID := jxutils.GetShowStoreIDFromOrder(order)
	stores, err := dao.GetStoreList(dao.GetDB(), []int{storeID}, nil, nil, nil, nil, "")
	if len(stores) == 0 || err != nil {
		return bill, fmt.Errorf("未查询到该门店！ 门店id ：[%v]", storeID)
	}
	vendorWaybillID, err := api.JdEclpAPI.WaybillReceive(&jdeclpapi.WaybillReceiveParam{
		SalePlat:        jdeclpapi.SalePlatSourceDelivery,
		CustomerCode:    jdeclpapi.CustomerCode,
		OrderID:         order.VendorOrderID,
		SenderName:      order.StoreName,
		SenderAddress:   stores[0].Address,
		SenderTel:       stores[0].Tel1,
		ReceiveName:     order.ConsigneeName,
		ReceiveAddress:  order.ConsigneeAddress,
		ReceiveTel:      order.ConsigneeMobile,
		Weight:          utils.Int2Float64(order.Weight),
		Vloumn:          utils.Int2Float64(order.Weight),
		PackageCount:    1,
		Description:     "生鲜",
		Aging:           5,
		PromiseTimeType: 1, //特惠送
	})
	waybill := &model.Waybill{
		VendorOrderID:     order.VendorOrderID,
		OrderVendorID:     model.VendorIDJDShop,
		VendorWaybillID:   vendorWaybillID,
		WaybillVendorID:   model.VendorIDJDWL,
		Status:            model.WaybillStatusDelivering,
		WaybillCreatedAt:  time.Now(),
		StatusTime:        time.Now(),
		WaybillFinishedAt: utils.DefaultTimeValue,
		DeliveryFlag:      model.OrderDeliveryFlagMaskScheduleDisabled,
	}
	dao.CreateEntity(dao.GetDB(), waybill)
	return waybill, err
}
func (c *DeliveryHandler) CancelWaybill(bill *model.Waybill, cancelReasonID int, cancelReason string) (err error) {
	err = api.JdEclpAPI.CancelWayBill(&jdeclpapi.CancelWayBillParam{
		WaybillCode:  bill.VendorWaybillID,
		CustomerCode: jdeclpapi.CustomerCode,
		Source:       "JOS",
		CancelReason: cancelReason,
		OperatorName: "jxadmin",
	})
	return err
}
func (c *DeliveryHandler) GetWaybillFee(order *model.GoodsOrder) (deliveryFeeInfo *partner.WaybillFeeInfo, err error) {
	return deliveryFeeInfo, err
}
func (c *DeliveryHandler) ComplaintRider(bill *model.Waybill, resonID int, resonContent string) (err error) {
	return err
}

func (c *DeliveryHandler) GetRiderInfo(orderId string, deliveryId int64, mtPeisongId string) (rider *utils.RiderInfo, err error) {
	return nil, nil
}
func (c *DeliveryHandler) GetDeliverLiquidatedDamages(orderId string, deliverId string) (money int64, err error) {
	return 0, err
}
func (c *DeliveryHandler) GetPlatformBalance() (balance int64, err error) {
	return 0, fmt.Errorf("暂不支持此功能")
}
func (c *DeliveryHandler) BalanceRecharge(param *utils.RechargeBalance) (url string, err error) {
	return "", fmt.Errorf("此平台暂不支持充值")
}
