178 lines
8.7 KiB
Go
178 lines
8.7 KiB
Go
package basesch
|
||
|
||
import (
|
||
"git.rosy.net.cn/baseapi/utils"
|
||
"git.rosy.net.cn/jx-callback/business/jxcallback/scheduler"
|
||
"git.rosy.net.cn/jx-callback/business/model"
|
||
"git.rosy.net.cn/jx-callback/business/partner"
|
||
"git.rosy.net.cn/jx-callback/globals"
|
||
)
|
||
|
||
var (
|
||
FixedBaseScheduler *BaseScheduler
|
||
)
|
||
|
||
type BaseScheduler struct {
|
||
IsReallyCallPlatformAPI bool
|
||
}
|
||
|
||
func (c *BaseScheduler) AcceptOrRefuseOrder(order *model.GoodsOrder, isAcceptIt bool, userName string) (err error) {
|
||
globals.SugarLogger.Infof("AcceptOrRefuseOrder orderID:%s, isAcceptIt:%t", order.VendorOrderID, isAcceptIt)
|
||
if order.LockStatus == model.OrderStatusUnknown && order.Status == model.OrderStatusNew {
|
||
if c.IsReallyCallPlatformAPI {
|
||
err = utils.CallFuncLogErrorWithInfo(func() error {
|
||
return partner.GetPurchasePlatformFromVendorID(order.VendorID).AcceptOrRefuseOrder(order, isAcceptIt, userName)
|
||
}, "AcceptOrRefuseOrder orderID:%s, isAcceptIt:%t", order.VendorOrderID, isAcceptIt)
|
||
}
|
||
} else {
|
||
return scheduler.ErrOrderStatusAlreadySatisfyCurOperation
|
||
globals.SugarLogger.Debugf("AcceptOrRefuseOrder orderID:%s, status:%d is not suitable, isAcceptIt:%t", order.VendorOrderID, order.Status, isAcceptIt)
|
||
}
|
||
return err
|
||
}
|
||
|
||
func (c *BaseScheduler) PickupGoods(order *model.GoodsOrder, userName string) (err error) {
|
||
globals.SugarLogger.Infof("PickupGoods orderID:%s", order.VendorOrderID)
|
||
if (order.DeliveryFlag & model.OrderDeliveryFlagMaskPurcahseDisabled) == 0 {
|
||
if order.LockStatus == model.OrderStatusUnknown && order.Status == model.OrderStatusAccepted {
|
||
if c.IsReallyCallPlatformAPI {
|
||
err = utils.CallFuncLogErrorWithInfo(func() error {
|
||
return partner.GetPurchasePlatformFromVendorID(order.VendorID).PickupGoods(order, userName)
|
||
}, "PickupGoods orderID:%s", order.VendorOrderID)
|
||
}
|
||
} else {
|
||
if order.LockStatus != model.OrderStatusUnknown || order.Status < model.OrderStatusAccepted {
|
||
err = scheduler.ErrOrderStatusIsNotSuitable4CurOperation
|
||
globals.SugarLogger.Infof("PickupGoods orderID:%s, status:%d is not suitable", order.VendorOrderID, order.Status)
|
||
} else {
|
||
err = scheduler.ErrOrderStatusAlreadySatisfyCurOperation
|
||
globals.SugarLogger.Debugf("PickupGoods orderID:%s status:%d already ok", order.VendorOrderID, order.Status)
|
||
}
|
||
}
|
||
}
|
||
return err
|
||
}
|
||
|
||
func (c *BaseScheduler) Swtich2SelfDeliver(order *model.GoodsOrder, userName string) (err error) {
|
||
globals.SugarLogger.Infof("Swtich2SelfDeliver orderID:%s", order.VendorOrderID)
|
||
if order.LockStatus == model.OrderStatusUnknown && order.Status == model.OrderStatusFinishedPickup {
|
||
if c.IsReallyCallPlatformAPI {
|
||
err = utils.CallFuncLogErrorWithInfo(func() error {
|
||
return partner.GetPurchasePlatformFromVendorID(order.VendorID).Swtich2SelfDeliver(order, userName)
|
||
}, "Swtich2SelfDeliver orderID:%s", order.VendorOrderID)
|
||
if err == nil { // 因为有些平台转自送后,不会再发送订单在配送中消息过来,所以成功后就强制设置状态为配送中
|
||
order.Status = model.OrderStatusDelivering
|
||
order.DeliveryFlag |= model.OrderDeliveryFlagMaskPurcahseDisabled
|
||
}
|
||
}
|
||
} else {
|
||
if order.LockStatus != model.OrderStatusUnknown || order.Status < model.OrderStatusFinishedPickup || order.VendorID == order.WaybillVendorID {
|
||
err = scheduler.ErrOrderStatusIsNotSuitable4CurOperation
|
||
globals.SugarLogger.Infof("Swtich2SelfDeliver orderID:%s, status:%d is not suitable", order.VendorOrderID, order.Status)
|
||
} else {
|
||
err = scheduler.ErrOrderStatusAlreadySatisfyCurOperation
|
||
globals.SugarLogger.Debugf("Swtich2SelfDeliver orderID:%s status:%d already ok", order.VendorOrderID, order.Status)
|
||
}
|
||
}
|
||
return err
|
||
}
|
||
|
||
func (c *BaseScheduler) Swtich2SelfDelivered(order *model.GoodsOrder, userName string) (err error) {
|
||
globals.SugarLogger.Infof("Swtich2SelfDelivered orderID:%s", order.VendorOrderID)
|
||
if order.LockStatus == model.OrderStatusUnknown && order.Status == model.OrderStatusDelivering {
|
||
if c.IsReallyCallPlatformAPI {
|
||
err = utils.CallFuncLogError(func() error {
|
||
return partner.GetPurchasePlatformFromVendorID(order.VendorID).Swtich2SelfDelivered(order, userName)
|
||
}, "Swtich2SelfDelivered orderID:%s", order.VendorOrderID)
|
||
}
|
||
} else {
|
||
if order.LockStatus != model.OrderStatusUnknown || order.Status < model.OrderStatusDelivering {
|
||
err = scheduler.ErrOrderStatusIsNotSuitable4CurOperation
|
||
globals.SugarLogger.Infof("Swtich2SelfDelivered orderID:%s, status:%d is not suitable", order.VendorOrderID, order.Status)
|
||
} else {
|
||
err = scheduler.ErrOrderStatusAlreadySatisfyCurOperation
|
||
globals.SugarLogger.Debugf("Swtich2SelfDelivered orderID:%s status:%d already ok", order.VendorOrderID, order.Status)
|
||
}
|
||
}
|
||
return err
|
||
}
|
||
|
||
func (c *BaseScheduler) SelfDeliverDelivering(order *model.GoodsOrder, userName string) (err error) {
|
||
globals.SugarLogger.Infof("SelfDeliverDelivering orderID:%s", order.VendorOrderID)
|
||
if order.LockStatus == model.OrderStatusUnknown && order.Status == model.OrderStatusFinishedPickup {
|
||
if c.IsReallyCallPlatformAPI {
|
||
err = utils.CallFuncLogError(func() error {
|
||
return partner.GetPurchasePlatformFromVendorID(order.VendorID).SelfDeliverDelivering(order, userName)
|
||
}, "SelfDeliverDelivering orderID:%s", order.VendorOrderID)
|
||
if err == nil { // 因为有些平台设置配送中后,不会发送订单在配送中消息过来,所以成功后就强制设置状态为配送中
|
||
order.Status = model.OrderStatusDelivering
|
||
}
|
||
}
|
||
} else {
|
||
if order.LockStatus != model.OrderStatusUnknown || order.Status < model.OrderStatusFinishedPickup {
|
||
err = scheduler.ErrOrderStatusIsNotSuitable4CurOperation
|
||
globals.SugarLogger.Infof("SelfDeliverDelivering orderID:%s, status:%d is not suitable", order.VendorOrderID, order.Status)
|
||
} else {
|
||
err = scheduler.ErrOrderStatusAlreadySatisfyCurOperation
|
||
globals.SugarLogger.Debugf("SelfDeliverDelivering orderID:%s, status:%d already ok", order.VendorOrderID, order.Status)
|
||
}
|
||
}
|
||
return err
|
||
}
|
||
|
||
func (c *BaseScheduler) SelfDeliverDelievered(order *model.GoodsOrder, userName string) (err error) {
|
||
globals.SugarLogger.Infof("SelfDeliverDelievered orderID:%s", order.VendorOrderID)
|
||
if order.LockStatus == model.OrderStatusUnknown && order.Status == model.OrderStatusDelivering {
|
||
if c.IsReallyCallPlatformAPI {
|
||
err = utils.CallFuncLogError(func() error {
|
||
return partner.GetPurchasePlatformFromVendorID(order.VendorID).SelfDeliverDelievered(order, userName)
|
||
}, "SelfDeliverDelievered orderID:%s", order.VendorOrderID)
|
||
}
|
||
} else {
|
||
if order.LockStatus != model.OrderStatusUnknown || order.Status < model.OrderStatusDelivering {
|
||
err = scheduler.ErrOrderStatusIsNotSuitable4CurOperation
|
||
globals.SugarLogger.Infof("SelfDeliverDelievered orderID:%s, status:%d is not suitable", order.VendorOrderID, order.Status)
|
||
} else {
|
||
err = scheduler.ErrOrderStatusAlreadySatisfyCurOperation
|
||
globals.SugarLogger.Debugf("SelfDeliverDelievered orderID:%s, status:%d already ok", order.VendorOrderID, order.Status)
|
||
}
|
||
}
|
||
return err
|
||
}
|
||
|
||
func (c *BaseScheduler) CreateWaybill(platformVendorID int, order *model.GoodsOrder, policy func(deliveryFee, addFee int64) error) (bill *model.Waybill, err error) {
|
||
globals.SugarLogger.Infof("CreateWaybill orderID:%s, vendorID:%d", order.VendorOrderID, platformVendorID)
|
||
if !model.IsOrderSolid(order) { // 如果订单是不完整的
|
||
globals.SugarLogger.Warnf("CreateWaybill orderID:%s, vendorID:%d is not solid!!!", order.VendorOrderID, platformVendorID)
|
||
return nil, scheduler.ErrOrderIsNotSolid
|
||
}
|
||
if c.IsReallyCallPlatformAPI {
|
||
handlerInfo := partner.GetDeliveryPlatformFromVendorID(platformVendorID)
|
||
if handlerInfo != nil && handlerInfo.Use4CreateWaybill {
|
||
bill, err = handlerInfo.Handler.CreateWaybill(order, policy)
|
||
if err != nil {
|
||
globals.SugarLogger.Infof("CreateWaybill failed orderID:%s vendorID:%d with error:%v", order.VendorOrderID, platformVendorID, err)
|
||
}
|
||
} else {
|
||
err = scheduler.ErrDeliverProviderWrong
|
||
}
|
||
}
|
||
return bill, err
|
||
}
|
||
|
||
func (c *BaseScheduler) CancelWaybill(bill *model.Waybill, cancelReasonID int, cancelReason string) (err error) {
|
||
globals.SugarLogger.Infof("CancelWaybill bill:%v, cancelReasonID:%d cancelReason:%s", bill, cancelReasonID, cancelReason)
|
||
// 部分快递平台在取消成功后有时会不发运单取消消息过来(比如达达,904200512000442),为避免二次取消报错,添加状态判断
|
||
if c.IsReallyCallPlatformAPI && bill.OrderVendorID != bill.WaybillVendorID && bill.Status != model.WaybillStatusCanceled {
|
||
if handlerInfo := partner.GetDeliveryPlatformFromVendorID(bill.WaybillVendorID); handlerInfo != nil {
|
||
if err = utils.CallFuncLogError(func() error {
|
||
return handlerInfo.Handler.CancelWaybill(bill, cancelReasonID, cancelReason)
|
||
}, "CancelWaybill bill:%v", bill); err == nil {
|
||
bill.Status = model.WaybillStatusCanceled
|
||
}
|
||
globals.SugarLogger.Debugf("CancelWaybill bill:%v canceled by myself", bill)
|
||
}
|
||
}
|
||
return err
|
||
}
|