package basesch

import (
	"fmt"
	"strings"
	"time"

	"git.rosy.net.cn/baseapi/platformapi/jdshopapi"

	"git.rosy.net.cn/baseapi/platformapi/dingdingapi"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/baseapi/utils/errlist"
	"git.rosy.net.cn/jx-callback/business/jxstore/cms"
	"git.rosy.net.cn/jx-callback/business/jxutils"
	"git.rosy.net.cn/jx-callback/business/jxutils/ddmsg"
	"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
	"git.rosy.net.cn/jx-callback/business/jxutils/tasksch"
	"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"
)

const (
	autoSelfTakeCode = "135246"
)

const (
	EBaiOrderAddressHide = "[隐私保护]顾客地址已隐藏"
)

func (c *BaseScheduler) CreateWaybillOnProviders(ctx *jxcontext.Context, order *model.GoodsOrder, courierVendorIDs, excludeCourierVendorIDs []int, maxDeliveryFee int64, createOnlyOne bool) (bills []*model.Waybill, err error) {
	storeCourierList, err := dao.GetStoreCourierList2(dao.GetDB(), []int{jxutils.GetSaleStoreIDFromOrder(order)}, courierVendorIDs, model.StoreStatusOpened, []int{model.StoreAuditStatusOnline, model.StoreAuditStatusUpdated})
	if err != nil {
		return nil, err
	}
	courierVendorIDMap := jxutils.IntList2Map(courierVendorIDs)
	excludeCourierVendorIDMap := jxutils.IntList2Map(excludeCourierVendorIDs)
	errList := errlist.New()

	store, _ := dao.GetStoreDetail(dao.GetDB(), order.JxStoreID, order.VendorID, order.VendorOrgCode)
	for _, storeCourier := range storeCourierList {
		switch storeCourier.Status {
		case model.YES:
			if (courierVendorIDs == nil || courierVendorIDMap[storeCourier.VendorID] == 1) &&
				(excludeCourierVendorIDs == nil || excludeCourierVendorIDMap[storeCourier.VendorID] == 0) {
				if handler := partner.GetDeliveryPlatformFromVendorID(storeCourier.VendorID); handler != nil && handler.Use4CreateWaybill {
					courierVendorID := storeCourier.VendorID
					// 创建运单
					bill, err2 := c.CreateWaybill(courierVendorID, order, maxDeliveryFee)
					if err = err2; err == nil {
						// 其实这个bill没啥用,运单的创建是通过回调产生的
						bill.DesiredFee += int64(model.WayBillDeliveryMarkUp + store.FreightMarkup)
						if bill.WaybillVendorID == model.VendorIDFengNiao {
							bill.DesiredFee += model.WayBillDeliveryMarkUp
						}
						bill.DesiredFee += int64(store.FreightMarkup)
						bills = append(bills, bill)
						if createOnlyOne {
							break
						}
					} else {
						errList.AddErr(fmt.Errorf("平台:%s,%s", jxutils.GetVendorName(courierVendorID), err.Error()))
					}
				}
			}
		case model.NO:
			errList.AddErr(fmt.Errorf("平台:%s,发单配送被关闭,打开后发单", jxutils.GetVendorName(storeCourier.VendorID)))
		}
	}
	if len(bills) > 0 {
		err = errList.GetErrListAsOne()
		if err != nil {
			partner.CurOrderManager.OnOrderMsg(order, "创建三方运单部分失败", err.Error())
		}
		err = nil
	} else if errList.GetErrListAsOne() == nil {
		err = fmt.Errorf("orderID:%s过订单发三方配送亏损超6元/当前订单配送方式不是门店自送/没有绑定有效的三方配送门店", order.VendorOrderID)
	} else {
		err = fmt.Errorf("orderID:%s所有运单失败：%s", order.VendorOrderID, errList.GetErrListAsOne().Error())
	}
	return bills, err
}

func (c *BaseScheduler) SelfDeliveredAndUpdateStatus(ctx *jxcontext.Context, vendorOrderID string, vendorID int, userName string) (err error) {
	jxutils.CallMsgHandler(func() {
		err = func() (err error) {
			order, err := partner.CurOrderManager.LoadOrder(vendorOrderID, vendorID)
			if err == nil {
				if model.IsOrderDeliveryByStore(order) {
					err = c.SelfDeliverDelivered(order, userName)
				} else if model.IsOrderDeliveryByPlatform(order) {
					err = c.Swtich2SelfDelivered(order, userName)
				}
				if err == nil {
					order.Status = model.OrderStatusFinished // todo 是否需要强制设置完成状态？
					if err = dao.SetOrderFlag(dao.GetDB(), ctx.GetUserName(), order.VendorOrderID, order.VendorID, model.OrderFlagMaskSetDelivered); err != nil {
						return err
					}
					order.OrderFinishedAt = time.Now()
					dao.UpdateEntity(dao.GetDB(), order, "Status", "OrderFinishedAt")
				}
			}
			return err
		}()
	}, jxutils.ComposeUniversalOrderID(vendorOrderID, vendorID))
	return err
}

func (c *BaseScheduler) PickupGoodsAndUpdateStatus(ctx *jxcontext.Context, vendorOrderID string, vendorID int, userName string) (err error) {
	jxutils.CallMsgHandler(func() {
		err = func() (err error) {
			order, err := partner.CurOrderManager.LoadOrder(vendorOrderID, vendorID)
			if err == nil {
				flag := model.IsOrderDeliveryByStore(order) || model.IsOrderDeliveryBySelf(order)
				err = c.PickupGoods(order, flag, userName)
				if err == nil {
					partner.CurOrderManager.OnOrderMsg(order, fmt.Sprintf("订单手动拣货成功[%s]", ctx.GetUserName()), "")
					order.Status = model.OrderStatusFinishedPickup
					if err = partner.CurOrderManager.UpdateOrderStatusAndDeliveryFlag(order); err != nil {
						return err
					}
				}
				// 京西商城的自提订单,在拣货完成时自动设置为完成订单
				if model.IsOrderDeliveryBySelf(order) && order.VendorID == model.VendorIDJX {
					order.Status = model.OrderStatusFinished
					order.VendorStatus = utils.Int64ToStr(model.OrderStatusFinished)
					order.Flag = 128
					order.OrderFinishedAt = time.Now()
					dao.UpdateEntity(dao.GetDB(), order, "Status", "VendorStatus", "Flag", "OrderFinishedAt")
				}
			}
			return err
		}()
	}, jxutils.ComposeUniversalOrderID(vendorOrderID, vendorID))
	return err
}

func (c *BaseScheduler) AdjustOrder(ctx *jxcontext.Context, order *model.GoodsOrder, removedSkuList []*model.OrderSku, reason string) (err error) {
	if c.IsReallyCallPlatformAPI {
		err = partner.GetPurchaseOrderHandlerFromVendorID(order.VendorID).AdjustOrder(ctx, order, removedSkuList, reason)
		if err == nil {
			var skuIDs []string
			for _, v := range removedSkuList {
				skuIDs = append(skuIDs, utils.Int2Str(v.SkuID))
			}
			noticeMsg := fmt.Sprintf("商品skuID列表：%v，订单号(点击进入详情)：%v", strings.Join(skuIDs, ","), globals.BackstageHost+"/#/ordermanager/"+order.VendorOrderID)
			user, err := dao.GetUserByID(dao.GetDB(), "mobile", "18982250714")
			if user != nil && err == nil {
				ddmsg.SendUserMessage(dingdingapi.MsgTyeText, user.UserID, "调整单调整商品", noticeMsg)
			}

		}
	}
	return err
}

func (c *BaseScheduler) CancelOrder(ctx *jxcontext.Context, order *model.GoodsOrder, reason string) (err error) {
	if c.IsReallyCallPlatformAPI {
		if globals.IsAddEvent {
			err = cms.AddEventDetail(dao.GetDB(), ctx, model.OperateUpdate, order.StoreID, model.ThingTypeOrder, order.StoreID, order.VendorOrderID, order.StoreName)
		}
		err = partner.GetPurchaseOrderHandlerFromVendorID(order.VendorID).CancelOrder(ctx, order, reason+","+ctx.GetUserName())
	}
	return err
}

func (c *BaseScheduler) AcceptOrRefuseFailedGetOrder(ctx *jxcontext.Context, order *model.GoodsOrder, isAcceptIt bool, reason string) (err error) {
	if c.IsReallyCallPlatformAPI {
		err = partner.GetPurchaseOrderHandlerFromVendorID(order.VendorID).AcceptOrRefuseFailedGetOrder(ctx, order, isAcceptIt)
	}
	if err == nil {
		flag := model.OrderFlagAgreeFailedGetGoods
		if !isAcceptIt {
			flag = model.OrderFlagRefuseFailedGetGoods
		}
		dao.SetOrderFlag(dao.GetDB(), ctx.GetUserName(), order.VendorOrderID, order.VendorID, flag)
	}
	return err
}

func (c *BaseScheduler) CallPMCourier(ctx *jxcontext.Context, order *model.GoodsOrder) (err error) {
	if c.IsReallyCallPlatformAPI {
		err = partner.GetPurchaseOrderHandlerFromVendorID(order.VendorID).CallCourier(ctx, order)
	}
	if err == nil {
		dao.SetOrderFlag(dao.GetDB(), ctx.GetUserName(), order.VendorOrderID, order.VendorID, model.OrderFlagMaskCallPMCourier)
	}
	return err
}

func (c *BaseScheduler) ConfirmReceiveGoods(ctx *jxcontext.Context, order *model.GoodsOrder) (err error) {
	if c.IsReallyCallPlatformAPI {
		err = partner.GetPurchaseOrderHandlerFromVendorID(order.VendorID).ConfirmReceiveGoods(ctx, order)
	}
	if err == nil {
		dao.SetOrderFlag(dao.GetDB(), ctx.GetUserName(), order.VendorOrderID, order.VendorID, model.OrderFlagMaskFailedDeliver)
	}
	return err
}

func (c *BaseScheduler) AgreeOrRefuseCancel(ctx *jxcontext.Context, order *model.GoodsOrder, isAcceptIt bool, reason string) (err error) {
	if c.IsReallyCallPlatformAPI {
		err = partner.GetPurchaseOrderHandlerFromVendorID(order.VendorID).AgreeOrRefuseCancel(ctx, order, isAcceptIt, reason, nil)
	}
	if err == nil {
		flag := model.OrderFlagAgreeUserApplyCancel
		if !isAcceptIt {
			flag = model.OrderFlagRefuseUserApplyCancel
		}
		dao.SetOrderFlag(dao.GetDB(), ctx.GetUserName(), order.VendorOrderID, order.VendorID, flag)
	}
	return err
}

func (c *BaseScheduler) CancelWaybillByID(ctx *jxcontext.Context, vendorWaybillID string, waybillVendorID int, cancelReasonID int, cancelReason string) (err error) {
	bill, err := partner.CurOrderManager.LoadWaybill(vendorWaybillID, waybillVendorID)
	if err != nil {
		return err
	}
	order, _ := partner.CurOrderManager.LoadOrder(bill.VendorOrderID, bill.OrderVendorID)

	if bill.OrderVendorID != bill.WaybillVendorID { // 取消三方配送
		if err = c.CancelWaybill(bill, cancelReasonID, cancelReason); err != nil {
			return err
		}
	} else { // 取消平台配送转自送
		if err = c.Swtich2SelfDeliver(order, ctx.GetUserName()); err != nil {
			return err
		}
	}

	partner.CurOrderManager.OnOrderMsg(order, utils.Int64ToStr(model.WaybillStatusCancel), "商户发起运单取消10")
	return err
}

func (c *BaseScheduler) AgreeOrRefuseRefund(ctx *jxcontext.Context, afsOrderID string, vendorID, approveType int, reason string, mtReview map[string]interface{}) (err error) {
	skus := make([]*model.OrderFinancialSkuExt, 0, 0)
	afsOrder, err := partner.CurOrderManager.LoadAfsOrder(afsOrderID, vendorID)
	if err == nil {
		if c.IsReallyCallPlatformAPI {
			err = partner.GetPurchaseOrderHandlerFromVendorID(vendorID).AgreeOrRefuseRefund(ctx, afsOrder, approveType, reason, mtReview)
		}
		if err == nil {
			flag := model.AfsOrderFlagAgreeUserRefund
			if approveType == partner.AfsApproveTypeRefused {
				flag = model.AfsOrderFlagRefuseUserRefund
				afsOrder.RefuseReason = reason
				partner.CurOrderManager.UpdateAfsOrderFields(afsOrder, []string{"RefuseReason"})
			} else {
				if order, _ := partner.CurOrderManager.LoadOrder(afsOrder.VendorOrderID, afsOrder.VendorID); order != nil {
					var (
						db = dao.GetDB()
					)
					waybills, _ := dao.GetWaybills(db, order.VendorOrderID, nil)
					//美团的订单如果是同意全部退款，要取消所有三方运单并停止调度
					if order.VendorID == model.VendorIDMTWM || order.VendorID == model.VendorIDJX || order.VendorID == model.VendorIDEBAI {
						var (
							afsCount, orderCount int
						)
						skus, _ = dao.GetAfsOrderSkuInfo(db, order.VendorOrderID, afsOrderID, order.VendorID, false)
						for _, v := range skus {
							afsCount += v.Count
						}
						for _, v := range order.Skus {
							orderCount += v.Count
						}
						//如果售后退款的商品数等于订单商品数，我就当是全部退款了
						if afsCount == orderCount && order.VendorID == model.VendorIDMTWM {
							order.DeliveryFlag |= model.OrderDeliveryFlagMaskScheduleDisabled
							partner.CurOrderManager.UpdateOrderFields(order, []string{"DeliveryFlag"})
							for _, v := range waybills {
								c.CancelWaybill(v, partner.CancelWaybillReasonOther, ctx.GetUserName()+":11"+partner.CancelWaybillReasonStrActive)
							}
						}
						if afsCount == orderCount && order.VendorID == model.VendorIDJX {
							order.DeliveryFlag |= model.OrderDeliveryFlagMaskScheduleDisabled
							order.Status = model.OrderStatusCanceled
							order.VendorStatus = utils.Int2Str(model.OrderStatusCanceled)
							partner.CurOrderManager.UpdateOrderFields(order, []string{"DeliveryFlag", "Status", "VendorStatus"})
						}
						if afsCount == orderCount && order.VendorID == model.VendorIDEBAI {
							order.DeliveryFlag |= model.OrderDeliveryFlagMaskScheduleDisabled
							order.Status = model.OrderStatusCanceled
							order.VendorStatus = "10" // 饿百取消订单
							partner.CurOrderManager.UpdateOrderFields(order, []string{"DeliveryFlag", "Status", "VendorStatus"})
							for _, v := range waybills {
								c.CancelWaybill(v, partner.CancelWaybillReasonOther, ctx.GetUserName()+":12"+partner.CancelWaybillReasonStrActive)
							}

						}
					}
					if order.EarningType == model.EarningTypePoints {
						var (
							skuMap = make(map[int]*model.OrderSku)
							diff   int64
						)
						for _, sku := range order.Skus {
							skuMap[sku.SkuID] = sku
						}
						//京东商城和京西要重新算totalshopmoney等
						if order.VendorID == model.VendorIDJDShop || order.VendorID == model.VendorIDJX {
							skus, _ := dao.GetAfsOrderSkuInfo(db, order.VendorOrderID, afsOrderID, order.VendorID, false)
							for _, v := range skus {
								if skuMap[v.SkuID] != nil {
									diff += skuMap[v.SkuID].SalePrice * int64(v.Count)
								}
							}
							order.TotalShopMoney = utils.Float64TwoInt64(float64(float64(order.TotalShopMoney)/jdshopapi.JdsPayPercentage-float64(diff)) * jdshopapi.JdsPayPercentage)
							if len(waybills) > 0 {
								jxutils.RefreshOrderEarningPrice3(order, order.OrderPayPercentage, waybills[0])
							} else {
								jxutils.RefreshOrderEarningPrice2(order, order.OrderPayPercentage)
							}
							dao.UpdateEntity(db, order, "TotalShopMoney", "NewEarningPrice")
						}
					}
				}
			}
			dao.SetAfsOrderFlag(dao.GetDB(), ctx.GetUserName(), afsOrderID, vendorID, flag)
		}
	}
	//if approveType != partner.AfsApproveTypeRefused && err == nil {
	//	storeDetail, err := partner.CurOrderManager.LoadStoreDetail(afsOrder.StoreID, afsOrder.VendorID)
	//	if err != nil {
	//		return err
	//	}
	//	if storeDetail.IsPrintRefundOrder == model.YES { // 打印退款订单
	//		order2, _ := partner.CurOrderManager.LoadOrder(afsOrder.VendorOrderID, afsOrder.VendorID)
	//		order2.Status = model.AfsOrderStatusFinished
	//		_, err = netprinter.PrintRefundOrCancelOrder(jxcontext.AdminCtx, model.NO, order2, afsOrder.StoreID)
	//	}
	//}
	return err
}

func (c *BaseScheduler) ConfirmReceivedReturnGoods(ctx *jxcontext.Context, afsOrderID string, vendorID int) (err error) {
	afsOrder, err := partner.CurOrderManager.LoadAfsOrder(afsOrderID, vendorID)
	if err == nil {
		if c.IsReallyCallPlatformAPI {
			err = partner.GetPurchaseOrderHandlerFromVendorID(vendorID).ConfirmReceivedReturnGoods(ctx, afsOrder)
		}
		if err == nil {
			dao.SetAfsOrderFlag(dao.GetDB(), ctx.GetUserName(), afsOrderID, vendorID, model.AfsOrderFlagMaskReturnGoods)
		}
	}
	return err
}

func (c *BaseScheduler) PartRefundOrder(ctx *jxcontext.Context, order *model.GoodsOrder, refundSkuList []*model.OrderSku, reason string) (err error) {
	if c.IsReallyCallPlatformAPI {
		err = partner.GetPurchaseOrderHandlerFromVendorID(order.VendorID).PartRefundOrder(ctx, order, refundSkuList, reason)
	}
	return err
}

func (c *BaseScheduler) RefundOrder(ctx *jxcontext.Context, order *model.GoodsOrder, reason string) (err error) {
	if c.IsReallyCallPlatformAPI {
		err = partner.GetPurchaseOrderHandlerFromVendorID(order.VendorID).RefundOrder(ctx, order, reason)
	}
	return err
}

func (c *BaseScheduler) ConfirmSelfTake(ctx *jxcontext.Context, vendorOrderID string, vendorID int, selfTakeCode string) (err error) {
	order, err2 := partner.CurOrderManager.LoadOrder(vendorOrderID, vendorID)
	if err = err2; err == nil {
		err = c.confirmSelfTake(ctx, order, selfTakeCode)
	}
	return err
}

func (c *BaseScheduler) SetOrderWaybillTip(ctx *jxcontext.Context, vendorOrderID string, vendorID int, tipFee int64, isPay int) (errCode string, err error) {
	order, err := partner.CurOrderManager.LoadOrder(vendorOrderID, vendorID)
	if err != nil {
		return "", err
	}
	errCode, err = c.CheckStoreBalanceWithTip(ctx, order, tipFee, isPay)
	if err != nil {
		return errCode, err
	}
	err = c.SetOrderWaybillTipByOrder(ctx, order, tipFee)
	return errCode, err
}

func (c *BaseScheduler) CheckStoreBalanceWithTip(ctx *jxcontext.Context, order *model.GoodsOrder, tipFee int64, isPay int) (errCode string, err error) {
	var (
		db = dao.GetDB()
	)
	roundTipFee := tipFee / 100 * 100
	if roundTipFee != tipFee {
		return model.ErrCodeOnePayTipFeeMore, fmt.Errorf("小费必须是1元的整数倍")
	}
	//if order.WaybillTipMoney >= tipFee {
	//	return model.ErrCodeOnePayTipFeeMore, fmt.Errorf("本次小费金额不能小于已经支付的小费")
	//}
	if order.CreateDeliveryType == model.YES {
		//加小费只判断余额
		storeAcct, err := cms.GetStoreAcctBalance(ctx, jxutils.GetSaleStoreIDFromOrder(order))
		if err != nil {
			return errCode, fmt.Errorf("获取账户余额失败！")
		}
		if tipFee > int64(storeAcct.AccountBalance) {
			return model.ErrCodeAccountBalanceNotEnough, fmt.Errorf("门店账户余额不足，不能加小费！")
		}
		if tipFee > 1000 {
			return model.ErrCodeAccountBalanceNotEnough, fmt.Errorf("小费单次加价金额大于十元")
		}
	} else {
		// 品牌发单
		storeDetail, err := dao.GetStoreDetail(db, order.JxStoreID, order.VendorID, order.VendorOrgCode)
		if err != nil || storeDetail == nil {
			return model.ErrCodeAccountBalanceNotEnough, fmt.Errorf("根据订单获取门店详情错误:%v", err)
		}

		brandAcct, err := dao.GetBrandBalance(db, storeDetail.BrandID)
		if err != nil {
			return "", err
		}
		if tipFee > int64(brandAcct) {
			return model.ErrCodeAccountBalanceNotEnough, fmt.Errorf("品牌账户余额不足，不能加小费！")
		}
		if tipFee > 1000 {
			return model.ErrCodeOnePayTipFeeMore, fmt.Errorf("小费单次加价金额大于十元")
		}
	}
	// 是否确认支付
	if isPay != model.YES {
		return model.ErrCodeIsPaySure, fmt.Errorf("此订单已经支付小费[%d]元,本次增加小费[%d]元,总支出小费[%d]元", order.WaybillTipMoney/100, tipFee/100, (order.WaybillTipMoney+tipFee)/100)
	}

	return errCode, err
}

func isWaybillCanAddTip(waybill *model.Waybill) (isCan bool) {
	isCan = waybill.Status >= model.WaybillStatusNew && waybill.Status < model.WaybillStatusAccepted && partner.GetWaybillTipUpdater(waybill.WaybillVendorID) != nil
	return isCan
}

func (c *BaseScheduler) SetOrderWaybillTipByOrder(ctx *jxcontext.Context, order *model.GoodsOrder, tipFee int64) (err error) {
	db := dao.GetDB()
	thisTimeTipFee := tipFee - order.WaybillTipMoney
	storeDetail, _ := dao.GetStoreDetail(db, jxutils.GetSaleStoreIDFromOrder(order), order.VendorID, "")
	flag := false
	// 如果平台支持设置配送小费，必须要成功设置
	if handler := partner.GetWaybillTipUpdater(order.VendorID); handler != nil {
		if err = handler.UpdateWaybillTip(ctx, order.VendorOrgCode, order.VendorStoreID, order.VendorOrderID, "", "", utils.Int2Str(storeDetail.CityCode), thisTimeTipFee); err != nil {
			globals.SugarLogger.Debugf("订单id[%s],平台id[%d],427,err :[%v]", order.VendorOrderID, order.VendorID, err)
		} else {
			//加小费成功扣钱
			if order.CreateDeliveryType == model.YES {
				if err = partner.CurStoreAcctManager.InsertStoreAcctExpendAndUpdateStoreAcctBalance(ctx, jxutils.GetSaleStoreIDFromOrder(order), int(thisTimeTipFee), partner.StoreAcctTypeExpendCreateWaybillTip, order.VendorOrderID, "", 0); err == nil {
					flag = true
				}
			} else if order.CreateDeliveryType == model.NO {
				if err = partner.CurStoreAcctManager.InsertBrandBill(ctx, storeDetail.BrandID, int(thisTimeTipFee), model.BrandBillTypeExpend, model.BrandBillFeeTypeTipFee, order.VendorOrderID, ""); err == nil {
					flag = true
				}
			}
		}
	} //有可能进else，没加得起平台小费，就要到下面扣账户

	order.WaybillTipMoney = tipFee
	partner.CurOrderManager.UpdateOrderFields(order, []string{"WaybillTipMoney"})

	waybills, err := dao.GetWayBillByOrderID(db, 0, order.VendorID, 0, order.VendorOrderID)
	if err == nil {
		var waybills2 []*model.Waybill
		for _, v := range waybills {
			// 必须是三方配送
			if !model.IsWaybillPlatformOwn(v) && isWaybillCanAddTip(v) {
				waybills2 = append(waybills2, v)
			}
		}
		if len(waybills2) > 0 {
			task := tasksch.NewParallelTask("SetOrderWaybillTipByOrder", nil, ctx,
				func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
					waybill := batchItemList[0].(*model.Waybill)
					handler := partner.GetWaybillTipUpdater(waybill.WaybillVendorID)
					if waybill.WaybillVendorID == model.VendorIDDada {
						err = handler.UpdateWaybillTip(ctx, waybill.VendorOrgCode, storeDetail.VendorStoreID, waybill.VendorOrderID, waybill.VendorWaybillID, waybill.VendorWaybillID2, utils.Int2Str(storeDetail.CityCode), tipFee)
						globals.SugarLogger.Debugf("订单id[%s],平台id[%d],462,err :[%v]", order.VendorOrderID, order.VendorID, err)
					} else {
						err = handler.UpdateWaybillTip(ctx, waybill.VendorOrgCode, storeDetail.VendorStoreID, waybill.VendorOrderID, waybill.VendorWaybillID, waybill.VendorWaybillID2, utils.Int2Str(storeDetail.CityCode), thisTimeTipFee)
						globals.SugarLogger.Debugf("订单id[%s],平台id[%d],465,err :[%v]", order.VendorOrderID, order.VendorID, err)
					}
					return nil, err
				}, waybills2)
			tasksch.HandleTask(task, nil, false).Run()
			_, err = task.GetResult(0)
			if err == nil {
				//加起了至少只扣一次钱
				if !flag {
					//加小费成功扣钱
					if order.CreateDeliveryType == model.YES {
						partner.CurStoreAcctManager.InsertStoreAcctExpendAndUpdateStoreAcctBalance(ctx, jxutils.GetSaleStoreIDFromOrder(order), int(thisTimeTipFee), partner.StoreAcctTypeExpendCreateWaybillTip, order.VendorOrderID, "", 0)
					} else if order.CreateDeliveryType == model.NO {
						partner.CurStoreAcctManager.InsertBrandBill(ctx, storeDetail.BrandID, int(thisTimeTipFee), model.BrandBillTypeExpend, model.BrandBillFeeTypeTipFee, order.VendorOrderID, "")
					}
				}
			}
		}
	}
	return err
}

func (c *BaseScheduler) confirmSelfTake(ctx *jxcontext.Context, order *model.GoodsOrder, selfTakeCode string) (err error) {
	vendorID := order.VendorID
	// if vendorID == model.VendorIDJD || vendorID == model.VendorIDJX {
	if vendorID != model.VendorIDJX {
		handler := partner.GetPurchaseOrderHandlerFromVendorID(vendorID)
		if selfTakeCode == autoSelfTakeCode {
			if selfTakeCode, err = handler.GetSelfTakeCode(ctx, order); err != nil {
				return fmt.Errorf("获取订单:%s自提货码失败，原始错误:%s", order.VendorOrderID, err.Error())
			}
			if selfTakeCode == "" {
				return fmt.Errorf("订单:%s 自动提货失败，请手动输入自提码", order.VendorOrderID)
			}
		}
		err = handler.ConfirmSelfTake(ctx, order, selfTakeCode)
	} else {
		if selfTakeCode != order.ConsigneeMobile && selfTakeCode != order.VendorOrderID[len(order.VendorOrderID)-4:] {
			return fmt.Errorf("京西平台自提单,自提码为订单后四位或完整的下单电话")
		}
		orderStatus := &model.OrderStatus{
			VendorOrderID:    order.VendorOrderID,
			VendorID:         model.VendorIDJX,
			OrderType:        model.OrderTypeOrder,
			RefVendorOrderID: order.VendorOrderID,
			RefVendorID:      model.VendorIDJX,
			VendorStatus:     utils.Int2Str(model.OrderStatusFinished),
			Status:           model.OrderStatusFinished,
			StatusTime:       time.Now(),
			Remark:           "自提完成",
		}
		jxutils.CallMsgHandlerAsync(func() {
			err = partner.CurOrderManager.OnOrderStatusChanged("", orderStatus)
		}, jxutils.ComposeUniversalOrderID(order.VendorOrderID, model.VendorIDJX))
	}
	// } else {
	// 	err = fmt.Errorf("自提核销不支持%s平台订单", model.VendorChineseNames[order.VendorID])
	// }
	return err
}

func (c *BaseScheduler) ConfirmSelfTakeOrders(ctx *jxcontext.Context, vendorIDs []int, orderCreatedAfter, orderCreatedBefore time.Time, isAsync, isContinueWhenError bool) (hint string, err error) {
	orderList, err := dao.GetPendingFakeOrders(dao.GetDB(), vendorIDs, orderCreatedAfter, orderCreatedBefore)
	if err == nil {
		if len(orderList) > 0 {
			task := tasksch.NewParallelTask(fmt.Sprintf("自动完成内部自提单%v,%s", vendorIDs, utils.Time2Str(orderCreatedAfter)), tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError), ctx,
				func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
					order := batchItemList[0].(*model.GoodsOrder)
					if order.Status == model.OrderStatusAccepted {
						if handler := partner.GetPurchaseOrderHandlerFromVendorID(order.VendorID); handler != nil {
							err = handler.AcceptOrRefuseOrder(order, true, ctx.GetUserName())
							time.Sleep(2 * time.Second)
						}
					}
					if err == nil {
						if err = c.confirmSelfTake(ctx, order, autoSelfTakeCode); err == nil {
							retVal = []int{1}
						}
					}
					return retVal, err
				}, orderList)
			tasksch.HandleTask(task, nil, true).Run()
			if isAsync {
				hint = task.GetID()
			} else {
				resultList, err2 := task.GetResult(0)
				if err = err2; err == nil {
					hint = utils.Int2Str(len(resultList))
				}
			}
		}
	}
	return hint, err
}
