From 80f6238d8333aaf38a20c52f2251eb28eedd14f2 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, 11 Aug 2020 17:36:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=AC=E8=A5=BF=E6=8A=98=E6=89=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- business/jxstore/act/act.go | 68 ++++++++++++++++++++++++++++----- business/model/act.go | 4 ++ business/model/dao/dao_order.go | 22 +++++++++++ 3 files changed, 85 insertions(+), 9 deletions(-) diff --git a/business/jxstore/act/act.go b/business/jxstore/act/act.go index 3dd9b95fe..6fbe1e4d1 100644 --- a/business/jxstore/act/act.go +++ b/business/jxstore/act/act.go @@ -1523,21 +1523,71 @@ func ChangeJxPriceByDiscountAct(ctx *jxcontext.Context) { if len(actStoreSkus) == 0 { continue } - var ( - storeIDs, skuIDs []int - ) for _, actStoreSku := range actStoreSkus { - storeIDs = append(storeIDs, actStoreSku.StoreID) - skuIDs = append(skuIDs, actStoreSku.SkuID) - } - storeSkus, _ := dao.GetStoresSkusInfo(db, storeIDs, skuIDs) - for _, _ = range storeSkus { + storeSkus, _ := dao.GetStoresSkusInfo(db, []int{actStoreSku.StoreID}, []int{actStoreSku.SkuID}) + if len(storeSkus) == 0 { + continue + } + var ( + storeSku = storeSkus[0] + shouldStockOut = utils.Float64TwoInt64(math.Ceil(float64(storeSku.Stock) / float64(60))) //每个时间点应出货 = 总库存/60 = N + actualStockOut int64 //每个时间点实际出货 = C + pricePercentage = 0.02 //每次涨跌值为 原价的 2%,即原价100,每个时间点降价为2,涨价也为2 + actualPricePercentage float64 + minJxPrice int64 + ) + if storeSku.Stock == 0 { + continue + } + actualStockOut, err = dao.GetOrderStoreSkusCount(db, actStoreSku.StoreID, actStoreSku.SkuID, time.Now().Add(-time.Minute*10), time.Now()) + dao.Begin(db) + defer func() { + if r := recover(); r != nil || err != nil { + dao.Rollback(db) + if r != nil { + panic(r) + } + } + }() //第一档时间内 if (time.Now().Hour() >= 10 && time.Now().Hour() < 20) || (time.Now().Hour() == 20 && time.Now().Minute() < 1) { - + if actStoreSku.TrendType == model.TrendTypeUp { + actualPricePercentage = float64(actStoreSku.OriginalPrice) * (float64(1) + pricePercentage) + } else if actStoreSku.TrendType == model.TrendTypeDown { + actualPricePercentage = float64(actStoreSku.OriginalPrice) * (float64(1) - pricePercentage) + } else { + actualPricePercentage = 1 + } + if utils.Float64TwoInt64(float64(storeSku.JxPrice)*actualPricePercentage) >= actStoreSku.OriginalPrice { + storeSku.JxPrice = int(actStoreSku.OriginalPrice) + } + //判断活动的折扣类型是最低价还是最低折扣 + if act.DiscountType == model.ActDiscountTypePrice { + minJxPrice = int64(act.DiscountValue1) + } else if act.DiscountType == model.ActDiscountTypePercentage { + minJxPrice = actStoreSku.OriginalPrice * int64(act.DiscountValue1) / 100 + } + if utils.Float64TwoInt64(float64(storeSku.JxPrice)*actualPricePercentage) <= minJxPrice { + storeSku.JxPrice = int(minJxPrice) + } + if _, err = dao.UpdateEntity(db, storeSku, "JxPrice"); err != nil { + dao.Rollback(db) + } + //C >= 2N 涨价趋势,最高涨价到无折扣 + if actualStockOut >= 2*shouldStockOut { + actStoreSku.TrendType = model.TrendTypeUp + } else if actualStockOut < shouldStockOut/2 { //C <= N/2 降价趋势,最低降价到设置到最低折扣 + actStoreSku.TrendType = model.TrendTypeDown + } else { + actStoreSku.TrendType = model.TrendTypeNothing + } + if _, err = dao.UpdateEntity(db, actStoreSku, "TrendType"); err != nil { + dao.Rollback(db) + } } else { //第二档时间内 } + dao.Commit(db) } } } diff --git a/business/model/act.go b/business/model/act.go index f9bd995f6..4dab4391d 100644 --- a/business/model/act.go +++ b/business/model/act.go @@ -16,6 +16,10 @@ const ( ActDiscountTypePrice = 1 //折扣类型是最低价 ActDiscountTypePercentage = 2 //折扣类型是最低折扣 + TrendTypeUp = 1 //涨价趋势 + TrendTypeDown = 2 //降价趋势 + TrendTypeNothing = 0 //不变 + ActOrderBegin = 10 ActOrderMoneyOff = 11 ActOrderMoneyOffCoupon = 12 diff --git a/business/model/dao/dao_order.go b/business/model/dao/dao_order.go index b85b302af..2b2bfd203 100644 --- a/business/model/dao/dao_order.go +++ b/business/model/dao/dao_order.go @@ -1345,3 +1345,25 @@ func GetAfsOrdersByPage(db *DaoDB, vendorOrderID, afsOrderID, userID string, fro } return afsOrderList, totalCount, err } + +func GetOrderStoreSkusCount(db *DaoDB, storeID, skuID int, fromTime, toTime time.Time) (count int64, err error) { + tmpOrderSku := &model.OrderSku{} + sql := ` + SELECT SUM(a.count) count + FROM order_sku a + JOIN goods_order b ON a.vendor_order_id = b.vendor_order_id AND a.vendor_id = b.vendor_id + WHERE IF(b.store_id = 0, b.jx_store_id, b.store_id) = ? + AND a.sku_id = ? + AND b.order_created_at BETWEEN ? AND ? + ` + sqlParams := []interface{}{ + storeID, + skuID, + fromTime, + toTime, + } + if err = GetRow(db, &tmpOrderSku, sql, sqlParams); err == nil { + return int64(tmpOrderSku.Count), nil + } + return count, err +}