From ae9833361a965bd9d503ac25ba620d6529da932a Mon Sep 17 00:00:00 2001 From: gazebo Date: Thu, 20 Jun 2019 11:20:28 +0800 Subject: [PATCH] =?UTF-8?q?-=20dao.GetPromotionSkuPriceMap=20-=20=E4=BA=AC?= =?UTF-8?q?=E8=A5=BF=E6=B4=BB=E5=8A=A8=E8=A6=81=E6=B1=82=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- business/jxcallback/orderman/order.go | 45 ++++++-------------- business/jxstore/promotion/jd_promotion.go | 24 ++++++++--- business/model/api.go | 11 ++--- business/model/dao/promotion.go | 48 ++++++++++++++++++++++ 4 files changed, 85 insertions(+), 43 deletions(-) create mode 100644 business/model/dao/promotion.go diff --git a/business/jxcallback/orderman/order.go b/business/jxcallback/orderman/order.go index 9ef9aa13a..248931811 100644 --- a/business/jxcallback/orderman/order.go +++ b/business/jxcallback/orderman/order.go @@ -236,29 +236,6 @@ func (c *OrderManager) SaveOrder(order *model.GoodsOrder, isAdjust bool, db *dao return isDuplicated, err } -func getPromotionSkuPriceMap(db *dao.DaoDB, storeID int, skuIDs []int) (skuPriceMap map[int64]*model.PromotionSku, err error) { - sql := ` - SELECT t3.* - FROM promotion t1 - JOIN promotion_store t2 ON t2.promotion_id = t1.id AND t2.store_id = ? - JOIN promotion_sku t3 ON t3.promotion_id = t1.id AND t3.sku_id IN (` + dao.GenQuestionMarks(len(skuIDs)) + `) AND t3.earning_price > 0 - WHERE t1.deleted_at = ? AND (t1.status = ? OR t1.status = ?) AND (t1.begin_at <= NOW() AND t1.end_at >= NOW()) AND t1.vendor_id = ?` - var skuPriceList []*model.PromotionSku - if err = dao.GetRows(db, &skuPriceList, sql, storeID, skuIDs, utils.DefaultTimeValue, model.PromotionStatusLocalCreated, model.PromotionStatusRemoteCreated, model.VendorIDJX); err != nil { - return nil, err - } - skuPriceMap = make(map[int64]*model.PromotionSku) - for _, v := range skuPriceList { - if v.EarningPrice > 0 { - index := jxutils.Combine2Int(v.SkuID, v.Price) - if skuPriceMap[index] == nil || v.EarningPrice < skuPriceMap[index].EarningPrice { - skuPriceMap[index] = v - } - } - } - return skuPriceMap, err -} - func (c *OrderManager) updateOrderSkuOtherInfo(order *model.GoodsOrder, db *dao.DaoDB) (err error) { globals.SugarLogger.Debugf("updateOrderSkuOtherInfo orderID:%s, VendorStoreID:%s", order.VendorOrderID, order.VendorStoreID) jxStoreID := jxutils.GetShowStoreIDFromOrder(order) @@ -309,12 +286,14 @@ func (c *OrderManager) updateOrderSkuOtherInfo(order *model.GoodsOrder, db *dao. skumapper[v.VendorSkuID] = v } - skuPriceMap, err2 := getPromotionSkuPriceMap(db, jxStoreID, jxutils.IntMap2List(skuIDMap)) - if err = err2; err != nil { - globals.SugarLogger.Errorf("updateOrderSkuOtherInfo can not get sku promotion info for orderID:%s, error:%v", order.VendorOrderID, err) - return err + var skuPriceMap map[int]*model.PromotionSku + if len(skuIDMap) > 0 { + skuPriceMap, err = dao.GetPromotionSkuPriceMap(db, []int{jxStoreID}, jxutils.IntMap2List(skuIDMap), time.Now(), time.Now()) + if err != nil { + globals.SugarLogger.Errorf("updateOrderSkuOtherInfo can not get sku promotion info for orderID:%s, error:%v", order.VendorOrderID, err) + return err + } } - for _, v := range orderSkus { v.VendorOrderID = order.VendorOrderID v.VendorID = order.VendorID @@ -335,10 +314,12 @@ func (c *OrderManager) updateOrderSkuOtherInfo(order *model.GoodsOrder, db *dao. } } - if skuID := jxutils.GetSkuIDFromOrderSku(v); skuID > 0 && v.StoreSubName != "" { - index := jxutils.Combine2Int(jxStoreID, int(v.SalePrice)) - if skuPriceMap[index] != nil { - v.EarningPrice = int64(skuPriceMap[index].EarningPrice) + if skuPriceMap != nil { + if skuID := jxutils.GetSkuIDFromOrderSku(v); skuID > 0 /*&& v.StoreSubName != ""*/ { + index := skuID + if skuPriceMap[index] != nil { + v.EarningPrice = int64(skuPriceMap[index].EarningPrice) + } } } } diff --git a/business/jxstore/promotion/jd_promotion.go b/business/jxstore/promotion/jd_promotion.go index 3910a50b2..7bd391e79 100644 --- a/business/jxstore/promotion/jd_promotion.go +++ b/business/jxstore/promotion/jd_promotion.go @@ -283,13 +283,25 @@ func CreateJdPromotion(ctx *jxcontext.Context, vendorID int, isIDJd bool, isAsyn Source: PromotionSourceOpenPlatform, } - if vendorPromotionID == "" && vendorID == model.VendorIDJD { - skuIDs := make([]int, len(params.SkuPrices)) - skuPriceMap := make(map[int64]*SkuPrice) - for k, v := range params.SkuPrices { - skuIDs[k] = v.SkuID - skuPriceMap[int64(v.SkuID)] = v + skuIDs := make([]int, len(params.SkuPrices)) + skuPriceMap := make(map[int64]*SkuPrice) + for k, v := range params.SkuPrices { + skuIDs[k] = v.SkuID + skuPriceMap[int64(v.SkuID)] = v + } + if len(skuIDs) == 0 { + return "", fmt.Errorf("商品列表为空") + } + if vendorID == model.VendorIDJX { + conflictPromotion, err2 := dao.GetPromotionSkuPriceMap(db, params.StoreIDs, skuIDs, promotion.BeginAt, promotion.EndAt) + if err = err2; err != nil { + return "", err } + if len(conflictPromotion) > 0 { + return "", fmt.Errorf("有冲突配置:%s", utils.Format4Output(conflictPromotion, true)) + } + } + if vendorPromotionID == "" && vendorID == model.VendorIDJD { sql := ` SELECT t1.*, t2.jd_id, t3.vendor_store_id FROM store_sku_bind t1 diff --git a/business/model/api.go b/business/model/api.go index 1c5b1d0db..1a3396774 100644 --- a/business/model/api.go +++ b/business/model/api.go @@ -26,11 +26,12 @@ type GoodsOrderExt struct { WaybillCreatedAt time.Time `orm:"type(datetime);index" json:"waybillCreatedAt"` WaybillFinishedAt time.Time `orm:"type(datetime)" json:"waybillFinishedAt"` - SkuID int `orm:"column(sku_id)" json:"skuID,omitempty"` - SkuShopPrice int `json:"skuShopPrice,omitempty"` - SkuSalePrice int `json:"skuSalePrice,omitempty"` - SkuCount2 int `json:"skuCount2,omitempty"` - SkuInfo string `json:"skuInfo,omitempty"` + SkuID int `orm:"column(sku_id)" json:"skuID,omitempty"` + SkuShopPrice int `json:"skuShopPrice,omitempty"` + SkuSalePrice int `json:"skuSalePrice,omitempty"` + SkuEarningPrice int `json:"skuEarningPrice,omitempty"` + SkuCount2 int `json:"skuCount2,omitempty"` + SkuInfo string `json:"skuInfo,omitempty"` } type OrderSkuExt struct { diff --git a/business/model/dao/promotion.go b/business/model/dao/promotion.go new file mode 100644 index 000000000..ce195db4c --- /dev/null +++ b/business/model/dao/promotion.go @@ -0,0 +1,48 @@ +package dao + +import ( + "time" + + "git.rosy.net.cn/baseapi/utils" + "git.rosy.net.cn/jx-callback/business/model" +) + +func GetPromotionSkuPriceMap(db *DaoDB, storeIDs, skuIDs []int, fromTime, toTime time.Time) (skuPriceMap map[int]*model.PromotionSku, err error) { + sql := ` + SELECT t3.* + FROM promotion t1 + JOIN promotion_store t2 ON t2.promotion_id = t1.id + JOIN promotion_sku t3 ON t3.promotion_id = t1.id + WHERE t1.deleted_at = ? AND t1.vendor_id = ? AND (t1.status = ? OR t1.status = ?) AND (t1.begin_at <= ? AND t1.end_at >= ?)` + sqlParams := []interface{}{ + utils.DefaultTimeValue, + model.VendorIDJX, + model.PromotionStatusLocalCreated, + model.PromotionStatusRemoteCreated, + toTime, + fromTime, + } + if len(storeIDs) > 0 { + sql += " AND t2.store_id IN (" + GenQuestionMarks(len(storeIDs)) + ")" + sqlParams = append(sqlParams, storeIDs) + } + if len(skuIDs) > 0 { + sql += " AND t3.sku_id IN (" + GenQuestionMarks(len(skuIDs)) + ")" + sqlParams = append(sqlParams, skuIDs) + } + + var skuPriceList []*model.PromotionSku + if err = GetRows(db, &skuPriceList, sql, sqlParams...); err != nil { + return nil, err + } + skuPriceMap = make(map[int]*model.PromotionSku) + for _, v := range skuPriceList { + if v.EarningPrice > 0 { + index := v.SkuID + if skuPriceMap[index] == nil || v.EarningPrice < skuPriceMap[index].EarningPrice { + skuPriceMap[index] = v + } + } + } + return skuPriceMap, err +}