diff --git a/business/jxcallback/orderman/order.go b/business/jxcallback/orderman/order.go index 1c9f3b35d..90010b9f1 100644 --- a/business/jxcallback/orderman/order.go +++ b/business/jxcallback/orderman/order.go @@ -512,9 +512,18 @@ func (c *OrderManager) UpdateOrderStatusAndDeliveryFlag(order *model.GoodsOrder) } func (c *OrderManager) UpdateOrderFields(order *model.GoodsOrder, fieldList []string) (err error) { - db := orm.NewOrm() + db := dao.GetDB() utils.CallFuncLogError(func() error { - _, err = db.Update(order, fieldList...) + if order.ID == 0 { + order2 := *order + if err = dao.GetEntity(db, &order2, model.FieldVendorOrderID, model.FieldVendorID); err == nil { + order.ID = order2.ID + } + err = nil // 强制忽略订单不存在错误 + } + if err == nil && order.ID != 0 { + _, err = db.Db.Update(order, fieldList...) + } return err }, "UpdateOrderFields orderID:%s failed with error:%v", order.VendorOrderID, err) return err diff --git a/business/jxstore/misc/misc.go b/business/jxstore/misc/misc.go index 6520ce94f..a011872bb 100644 --- a/business/jxstore/misc/misc.go +++ b/business/jxstore/misc/misc.go @@ -79,6 +79,49 @@ func RefreshRealMobile(ctx *jxcontext.Context, vendorID int, fromTime, toTime ti return hint, err } +func RefreshOrderFinancial(ctx *jxcontext.Context, fromTime, toTime time.Time, isAsync, isContinueWhenError bool) (hint string, err error) { + sql := ` + SELECT * + FROM goods_order + WHERE status = ? AND total_shop_money = 0 + ` + sqlParams := []interface{}{ + model.OrderStatusFinished, + } + if !utils.IsTimeZero(fromTime) { + sql += " AND order_created_at >= ?" + sqlParams = append(sqlParams, fromTime) + } + if !utils.IsTimeZero(toTime) { + sql += " AND order_created_at <= ?" + sqlParams = append(sqlParams, toTime) + } + var orderList []*model.GoodsOrder + db := dao.GetDB() + if err = dao.GetRows(db, &orderList, sql, sqlParams...); err == nil && len(orderList) > 0 { + task := tasksch.NewParallelTask("misc RefreshOrderFinancial", tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError), ctx, + func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { + order := batchItemList[0].(*model.GoodsOrder) + handler := partner.GetPurchasePlatformFromVendorID(order.VendorID) + if handler != nil { + remoteOrder, err2 := handler.GetOrder(order.VendorOrderID) + if err = err2; err == nil { + order.TotalShopMoney = remoteOrder.TotalShopMoney + order.PmSubsidyMoney = remoteOrder.PmSubsidyMoney + err = partner.CurOrderManager.UpdateOrderFields(order, []string{"TotalShopMoney", "PmSubsidyMoney"}) + } + } + return nil, err + }, orderList) + tasksch.HandleTask(task, nil, true).Run() + hint = task.ID + if !isAsync { + _, err = task.GetResult(0) + } + } + return hint, err +} + func StartDailyWork() { if globals.ReallyCallPlatformAPI { now := time.Now() diff --git a/business/partner/purchase/jd/order.go b/business/partner/purchase/jd/order.go index 1d887dc6f..8d8200d3c 100644 --- a/business/partner/purchase/jd/order.go +++ b/business/partner/purchase/jd/order.go @@ -43,6 +43,21 @@ func (c *PurchaseHandler) OnOrderMsg(msg *jdapi.CallbackOrderMsg) (retVal *jdapi return retVal } +func (c *PurchaseHandler) updateOrderFinancialInfo(orderID string) (err error) { + order := &model.GoodsOrder{ + VendorOrderID: orderID, + VendorID: model.VendorIDJD, + } + orderSettlement, err := api.JdAPI.OrderShoudSettlementService2(orderID) + if err == nil { + if orderSettlement != nil { + updateOrderBySettleMent(order, orderSettlement) + err = partner.CurOrderManager.UpdateOrderFields(order, []string{"TotalShopMoney", "PmSubsidyMoney"}) + } + } + return err +} + func (c *PurchaseHandler) onOrderMsg(msg *jdapi.CallbackOrderMsg) (retVal *jdapi.CallbackResponse) { status := c.callbackMsg2Status(msg) if jdapi.StatusIDNewOrder == msg.StatusID { @@ -53,6 +68,7 @@ func (c *PurchaseHandler) onOrderMsg(msg *jdapi.CallbackOrderMsg) (retVal *jdapi } if msg.MsgURL == jdapi.CallbackMsgOrderAccounting { retVal = c.OnFinancialMsg(msg) + retVal = jdapi.Err2CallbackResponse(c.updateOrderFinancialInfo(msg.BillID), status.VendorStatus) } else if msg.MsgURL == jdapi.CallbackMsgAfterSaleBillStatus { retVal = c.OnAfsOrderMsg(msg) } else { @@ -77,6 +93,13 @@ func (c *PurchaseHandler) onOrderMsg(msg *jdapi.CallbackOrderMsg) (retVal *jdapi return retVal } +func updateOrderBySettleMent(order *model.GoodsOrder, orderSettlement *jdapi.OrderSettlementInfo) { + if orderSettlement != nil { + order.TotalShopMoney = orderSettlement.SettlementAmount + order.PmSubsidyMoney = orderSettlement.PlatOrderGoodsDiscountMoney + orderSettlement.PlatSkuGoodsDiscountMoney + } +} + func (c *PurchaseHandler) getOrder(orderID string) (order *model.GoodsOrder, orderMap map[string]interface{}, err error) { globals.SugarLogger.Debugf("jd getOrder orderID:%s", orderID) var ( @@ -104,10 +127,7 @@ func (c *PurchaseHandler) getOrder(orderID string) (order *model.GoodsOrder, ord task.Run() task.GetResult(0) if order != nil { - if orderSettlement != nil { - order.TotalShopMoney = orderSettlement.SettlementAmount - order.PmSubsidyMoney = orderSettlement.PlatOrderGoodsDiscountMoney + orderSettlement.PlatSkuGoodsDiscountMoney - } + updateOrderBySettleMent(order, orderSettlement) } // if orderMap, err = api.JdAPI.QuerySingleOrder(orderID); err == nil { // globals.SugarLogger.Debugf("jd getOrder2 orderID:%s", orderID) diff --git a/controllers/jx_order.go b/controllers/jx_order.go index 168c51985..bf2c872d2 100644 --- a/controllers/jx_order.go +++ b/controllers/jx_order.go @@ -437,6 +437,26 @@ func (c *OrderController) RefreshOrderRealMobile() { }) } +// @Title 刷新订单平台结算信息 +// @Description 刷新订单平台结算信息 +// @Param token header string true "认证token" +// @Param fromTime formData string true "起始时间" +// @Param toTime formData string false "结束时间" +// @Param isAsync formData bool true "是否异步操作" +// @Param isContinueWhenError formData bool false "单个同步失败是否继续,缺省false" +// @Success 200 {object} controllers.CallResult +// @Failure 200 {object} controllers.CallResult +// @router /RefreshOrderFinancial [put] +func (c *OrderController) RefreshOrderFinancial() { + c.callRefreshOrderFinancial(func(params *tOrderRefreshOrderFinancialParams) (retVal interface{}, errCode string, err error) { + timeList, err2 := jxutils.BatchStr2Time(params.FromTime, params.ToTime) + if err = err2; err == nil { + retVal, err = misc.RefreshOrderFinancial(params.Ctx, timeList[0], timeList[1], params.IsAsync, params.IsContinueWhenError) + } + return retVal, "", err + }) +} + // @Title 设置订单打印状态 // @Description 同步商家SKU类别 // @Param token header string true "认证token" diff --git a/routers/commentsRouter_controllers.go b/routers/commentsRouter_controllers.go index 82f7e70ae..b0c9f0c33 100644 --- a/routers/commentsRouter_controllers.go +++ b/routers/commentsRouter_controllers.go @@ -772,6 +772,15 @@ func init() { Filters: nil, Params: nil}) + beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:OrderController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:OrderController"], + beego.ControllerComments{ + Method: "RefreshOrderFinancial", + Router: `/RefreshOrderFinancial`, + AllowHTTPMethods: []string{"put"}, + MethodParams: param.Make(), + Filters: nil, + Params: nil}) + beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:OrderController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:OrderController"], beego.ControllerComments{ Method: "RefreshOrderRealMobile",