From 53a194a135198e33f5e0150cf414ad6e714f5abd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E5=B0=B9=E5=B2=9A?= <770236076@qq.com> Date: Tue, 5 Nov 2019 16:47:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=8C=89=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E5=88=B7=E6=96=B0=E5=8E=86=E5=8F=B2=E8=AE=A2=E5=8D=95=E7=BB=93?= =?UTF-8?q?=E7=AE=97=E4=BB=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- business/jxcallback/orderman/order.go | 49 +++++++++++++++++++++++++++ business/model/dao/dao_order.go | 49 +++++++++++++++++++++++++++ controllers/jx_order.go | 17 ++++++++++ routers/commentsRouter_controllers.go | 9 +++++ 4 files changed, 124 insertions(+) diff --git a/business/jxcallback/orderman/order.go b/business/jxcallback/orderman/order.go index b1c52f003..f46447cf7 100644 --- a/business/jxcallback/orderman/order.go +++ b/business/jxcallback/orderman/order.go @@ -1,14 +1,19 @@ package orderman import ( + "errors" "fmt" + "math" "strings" "time" + "git.rosy.net.cn/jx-callback/business/jxutils/tasksch" + "git.rosy.net.cn/baseapi" "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/business/jxcallback/scheduler" "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" @@ -605,3 +610,47 @@ func (c *OrderManager) UpdateOrderFields(order *model.GoodsOrder, fieldList []st }, "UpdateOrderFields orderID:%s failed with error:%v", order.VendorOrderID, err) return err } + +func RefreshHistoryOrdersEarningPrice(ctx *jxcontext.Context, fromDate string, toDate string, isAsync, isContinueWhenError bool) (err error) { + db := dao.GetDB() + fromDateParm := utils.Str2Time(fromDate) + toDateParm := utils.Str2Time(toDate) + //若时间间隔大于10天则不允许查询 + if math.Ceil(toDateParm.Sub(fromDateParm).Hours()/24) > 10 { + return errors.New(fmt.Sprintf("查询间隔时间不允许大于10天!: 时间范围:[%v] 至 [%v]", fromDate, toDate)) + } + actStoreSkuList, err := dao.GetEffectiveActStoreSkuInfo(db, 0, []int{}, []int{}, []int{}, fromDateParm, toDateParm) + task := tasksch.NewSeqTask("按订单刷新历史订单结算价", ctx, + func(task *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) { + switch step { + case 0: + task1 := tasksch.NewParallelTask("更新order_sku", tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError), ctx, + func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { + if actStoreSkuMap := jxutils.NewActStoreSkuMap(actStoreSkuList, false); actStoreSkuMap != nil { + v := batchItemList[0].(*model.ActStoreSku2) + num, err := dao.UpdateOrderSkuEariningPrice(db, v, fromDateParm, toDateParm) + if err != nil && !isContinueWhenError { + return "", err + } else { + globals.SugarLogger.Debug(fmt.Sprintf("更新order_sku , 行数:%d, storeid :%d ,skuid : %d, vendoreid : %d, earningPrice : %v, store_sub_id : %d", num, v.StoreID, v.SkuID, v.VendorID, v.EarningPrice, v.ActID)) + } + } + return retVal, err + }, actStoreSkuList) + tasksch.HandleTask(task1, task, true).Run() + case 1: + num2, err2 := dao.UpdateGoodOrderEaringPrice(db, fromDateParm, toDateParm) + if err2 != nil && !isContinueWhenError { + return "", err2 + } else { + globals.SugarLogger.Debug(fmt.Sprintf("更新goods_order , 行数:%d, 时间: %v 至 %v", num2, fromDateParm, toDateParm)) + } + } + return result, err + }, 2) + tasksch.HandleTask(task, nil, true).Run() + if !isAsync { + _, err = task.GetResult(0) + } + return err +} diff --git a/business/model/dao/dao_order.go b/business/model/dao/dao_order.go index 5933c8406..020856b5f 100644 --- a/business/model/dao/dao_order.go +++ b/business/model/dao/dao_order.go @@ -589,3 +589,52 @@ func GetRiskOrderCount(db *DaoDB, dayNum int, includeToday bool) (storeOrderList return storeOrderList, GetRows(db, &storeOrderList, sql, sqlParams) } + +func UpdateOrderSkuEariningPrice(db *DaoDB, actStoreSku2 *model.ActStoreSku2, fromDateParm, toDateParm time.Time) (num int64, err error) { + sql := ` + UPDATE order_sku t1 + JOIN goods_order tt1 ON tt1.vendor_order_id = t1.vendor_order_id + AND tt1.vendor_id = ? + AND t1.sku_id = ? + AND tt1.jx_store_id = ? + AND tt1.order_created_at BETWEEN ? and ? + SET t1.earning_price = ?,t1.store_sub_id = ? + WHERE t1.store_sub_id = 0 + AND t1.earning_price <> 0 + ` + sqlParams := []interface{}{ + actStoreSku2.VendorID, + actStoreSku2.SkuID, + actStoreSku2.StoreID, + fromDateParm, + toDateParm, + actStoreSku2.EarningPrice, + actStoreSku2.ActID, + } + return ExecuteSQL(db, sql, sqlParams...) +} + +func UpdateGoodOrderEaringPrice(db *DaoDB, fromDateParm, toDateParm time.Time) (num int64, err error) { + sql := ` + UPDATE goods_order t1 + JOIN( + SELECT + IF(t0.jx_store_id > 0, t0.jx_store_id, t0.store_id) store_id, + t0.vendor_id, + t0.vendor_order_id, + CAST(SUM(t1.count * IF(t1.earning_price <> 0, t1.earning_price, IF(t1.shop_price <> 0 && t1.shop_price < t1.sale_price, t1.shop_price, t1.sale_price) * IF(t5.pay_percentage > 0, t5.pay_percentage, 70) / 100)) AS SIGNED) earning_price + FROM goods_order t0 + JOIN order_sku t1 ON t1.vendor_order_id = t0.vendor_order_id AND t1.vendor_id = t0.vendor_id + LEFT JOIN store t5 ON t5.id = IF(t0.jx_store_id <> 0, t0.jx_store_id, t0.store_id) + WHERE t0.order_created_at BETWEEN ? AND ? + GROUP BY 1,2,3 + ) t2 ON t2.vendor_order_id = t1.vendor_order_id AND t2.vendor_id = t1.vendor_id + SET t1.earning_price = t2.earning_price + WHERE t1.earning_price <> t2.earning_price + ` + sqlParams := []interface{}{ + fromDateParm, + toDateParm, + } + return ExecuteSQL(db, sql, sqlParams...) +} diff --git a/controllers/jx_order.go b/controllers/jx_order.go index 703b4571c..a2d2320f5 100644 --- a/controllers/jx_order.go +++ b/controllers/jx_order.go @@ -741,6 +741,23 @@ func (c *OrderController) AmendMissingOrders() { }) } +// @Title 同步刷新历史订单的结算价按订单 +// @Description 同步刷新历史订单的结算价按订单 +// @Param token header string true "认证token" +// @Param fromDate formData string true "订单起始日期" +// @Param toDate formData string true "订单结束日期" +// @Param isAsync formData bool true "是否异步操作" +// @Param isContinueWhenError formData bool false "单个失败是否继续,缺省true" +// @Success 200 {object} controllers.CallResult +// @Failure 200 {object} controllers.CallResult +// @router /RefreshHistoryOrdersEarningPrice [post] +func (c *OrderController) RefreshHistoryOrdersEarningPrice() { + c.callRefreshHistoryOrdersEarningPrice(func(params *tOrderRefreshHistoryOrdersEarningPriceParams) (retVal interface{}, errCode string, err error) { + err = orderman.RefreshHistoryOrdersEarningPrice(params.Ctx, params.FromDate, params.ToDate, params.IsAsync, params.IsContinueWhenError) + return retVal, "", err + }) +} + // @Title 商家主动发起部分退款售后 // @Description 商家主动发起部分退款售后 // @Param token header string true "认证token" diff --git a/routers/commentsRouter_controllers.go b/routers/commentsRouter_controllers.go index c8a8106c4..a4d2a9e54 100644 --- a/routers/commentsRouter_controllers.go +++ b/routers/commentsRouter_controllers.go @@ -918,6 +918,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: "RefreshHistoryOrdersEarningPrice", + Router: `/RefreshHistoryOrdersEarningPrice`, + AllowHTTPMethods: []string{"post"}, + 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: "RefreshOrderFinancial",