Browse Source

add balance ,shop promotion

abiao 4 years ago
parent
commit
8a7a2d1dff

+ 137 - 99
go/gopath/src/fohow.com/apps/helpers/promotion_helper.go

@@ -6,6 +6,8 @@ import (
 	"fohow.com/apps/models/cent_model"
 	"fohow.com/apps/models/order_model"
 	"fohow.com/apps/models/product_model"
+	"fohow.com/apps/models/promotion_model"
+	"fohow.com/apps/models/user_model"
 	"github.com/astaxie/beego"
 	"time"
 )
@@ -31,7 +33,7 @@ func SetOrderPromotion(orderId string, wxUid int64) {
 
 	queryDate := time.Now()
 	// 获取所有有效促销记录
-	effectivePromotions := order_model.GetEffetivePromotions(queryDate, order.OrderType, false)
+	effectivePromotions := promotion_model.GetEffetivePromotions(queryDate, order.OrderType, false)
 	for _, item := range effectivePromotions {
 		//beego.Warn("item_name%d", item.Name)
 
@@ -233,165 +235,201 @@ func min(vals ...int64) int64 {
 */
 
 //充值促销
-/*
-	如果促销条件中,设置的项目有一项不满足即视为不满足促销条件
-*/
-func BalanceOrderPromotion(orderId string, wxUid int64) {
+
+func BalanceOrderPromotion(orderId string, wxUid, depart int64) {
 	beego.BeeLogger.Warn("******* BalanceOrderPromotion orderId:%s wxUid:%d", orderId, wxUid)
 
 	//获取订单明细
-	order := balance_model.GetBalanceOrderByOId(orderId, false)
-	if order == nil {
+	balanceOrder := balance_model.GetBalanceOrderByOId(orderId, false)
+	if balanceOrder == nil {
 		return
 	}
-
 	queryDate := time.Now()
 	// 获取所有有效促销记录
-	effectivePromotions := order_model.GetEffetivePromotions(queryDate, false)
+	effectivePromotions := promotion_model.GetBalanceEffetivePromotions(queryDate, depart, false)
 	for _, item := range effectivePromotions {
 		//beego.Warn("item_name%d", item.Name)
 
-		firstFlag := true
 		totalFlag := true
 		numsFlag := true
-		if item.MaxTotal > 0 && order.TotalPrice > item.MaxTotal {
+		if item.MaxTotal > 0 && balanceOrder.PaiedPrice > item.MaxTotal {
 			totalFlag = false
 			continue
 		}
 
-		if item.MinTotal > 0 && order.TotalPrice < item.MinTotal {
+		if item.MinTotal > 0 && balanceOrder.PaiedPrice < item.MinTotal {
 			totalFlag = false
-			firstFlag = false
 			continue
 		}
-		nums := int64(99)
-		nums1 := int64(99)
-		nums2 := int64(99)
-		nums3 := int64(99)
-		nums4 := int64(99)
-		nums5 := int64(99)
-		nums6 := int64(99)
-		if item.Prod1 > 0 {
-			prd := product_model.GetProductById(item.Prod1, true)
-			nums1 = int64(0)
-			if prd != nil {
-				detail_nums1 := order_model.GetDetailsByOrderIdAndRelatePid(orderId, prd.RelateProductId)
-				if detail_nums1 > int64(0) && item.Prod1 > 0 && item.Nums1 > 0 {
-					nums1 = int64(detail_nums1 / item.Nums1)
-				}
-			}
+
+		count := int64(0)
+
+		if item.MinTotal > 0 {
+			count = int64(balanceOrder.PaiedPrice / item.MinTotal)
 		}
 
-		if item.Prod2 > 0 {
-			prd := product_model.GetProductById(item.Prod2, true)
-			nums2 = int64(0)
-			if prd != nil {
-				detail_nums2 := order_model.GetDetailsByOrderIdAndRelatePid(orderId, prd.RelateProductId)
-				if detail_nums2 > int64(0) && item.Prod2 > 0 && item.Nums2 > 0 {
-					nums2 = int64(detail_nums2 / item.Nums2)
-				}
-			}
+		if count == int64(0) {
+			numsFlag = false
 		}
+		beego.Warn("item_name-1%d", item.Name)
+		beego.Warn("totalFlag%v", totalFlag)
+		beego.Warn("numsFlag%v", numsFlag)
+		beego.Warn("nums%d", count)
 
-		if item.Prod3 > 0 {
-			prd := product_model.GetProductById(item.Prod3, true)
-			if prd != nil {
-				nums3 = int64(0)
-				detail_nums3 := order_model.GetDetailsByOrderIdAndRelatePid(orderId, prd.RelateProductId)
-				if detail_nums3 > 0 && item.Prod3 > 0 && item.Nums3 > 0 {
-					nums3 = int64(detail_nums3 / item.Nums3)
+		//满足促销条件
+		if totalFlag && numsFlag {
+			source := promotion_model.SOURCE_BALANCE
+			remark := fmt.Sprintf("充值订单(%s)充值成功--促销(%s)", orderId, item.Name)
+			if item.SendProd1 > 0 && item.SendNums1 > 0 {
+				sendNums1 := count * item.SendNums1
+				//生成赠品
+				product := product_model.GetProductById(item.SendProd1, true)
+				if product != nil {
+					total := sendNums1 * product.Price
+					go new(promotion_model.Present).CreatePresent(wxUid, product.Price, total, product.Id, sendNums1, source, remark)
 				}
 			}
-		}
-		if item.MinTotal > 0 {
-			nums4 = int64(order.TotalPrice / item.MinTotal)
-		}
-
-		if item.Prod4 > 0 {
-			prd := product_model.GetProductById(item.Prod4, true)
-			nums5 = int64(0)
-			if prd != nil {
-				detail_nums4 := order_model.GetDetailsByOrderIdAndRelatePid(orderId, prd.RelateProductId)
-				if detail_nums4 > 0 && item.Prod4 > 0 && item.Nums4 > 0 {
-					nums5 = int64(detail_nums4 / item.Nums4)
+			if item.SendProd2 > 0 && item.SendNums2 > 0 {
+				sendNums2 := count * item.SendNums2
+				//赠送赠品1
+				product := product_model.GetProductById(item.SendProd2, true)
+				if product != nil {
+					total := sendNums2 * product.Price
+					go new(promotion_model.Present).CreatePresent(wxUid, product.Price, total, product.Id, sendNums2, source, remark)
 				}
 			}
-		}
 
-		if item.Prod5 > 0 {
-			prd := product_model.GetProductById(item.Prod5, true)
-			nums6 = int64(0)
-			if prd != nil {
-				detail_nums5 := order_model.GetDetailsByOrderIdAndRelatePid(orderId, prd.RelateProductId)
-				if detail_nums5 > 0 && item.Prod5 > 0 && item.Nums5 > 0 {
-					nums6 = int64(detail_nums5 / item.Nums5)
+			if item.SendProd3 > 0 && item.SendNums3 > 0 {
+				sendNums3 := count * item.SendNums3
+				//赠送赠品3
+				product := product_model.GetProductById(item.SendProd3, true)
+				if product != nil {
+					total := sendNums3 * product.Price
+					go new(promotion_model.Present).CreatePresent(wxUid, product.Price, total, product.Id, sendNums3, source, remark)
 				}
 			}
+
+			//赠送积分 or 代办费
+			if item.Cash > 0 {
+				totalCash := count * item.Cash
+				source := balance_model.BALANCE_SOURCE_BALANCE_RECHARGE_PROMOTION
+				remark := fmt.Sprintf("充值促销赠送提货券")
+				new(balance_model.Balance).Create(balanceOrder.WxUserId, balanceOrder.UserId, totalCash, source, balanceOrder.OrderId, remark)
+			}
+
+			if item.Cent > 0 {
+				totalCent := count * item.Cent
+				source := cent_model.PROMOTION_SEND
+				remark := fmt.Sprintf("充值促销活动赠送")
+				new(cent_model.CentBalance).Create(balanceOrder.WxUserId, totalCent, source, balanceOrder.OrderId, remark)
+			}
 		}
+	}
+}
 
-		val := []int64{nums1, nums2, nums3, nums4, nums5, nums6}
-		nums = min(val...)
-		if !item.IsMore {
-			val := []int64{nums, 1}
-			nums = min(val...)
+//店长申请促销
+func ShopOrderPromotion(shopApplyId, wxUid, depart int64) {
+	beego.BeeLogger.Warn("******* ShopOrderPromotion shopApplyId:%d wxUid:%d", shopApplyId, wxUid)
+
+	//获取订单明细
+	shopApply := user_model.GetShopApplicationById(shopApplyId)
+	if shopApply == nil {
+		return
+	}
+	queryDate := time.Now()
+	// 获取所有有效促销记录
+	effectivePromotions := promotion_model.GetShopEffetivePromotions(queryDate, depart, false)
+	for _, item := range effectivePromotions {
+		//beego.Warn("item_name%d", item.Name)
+
+		totalFlag := true
+		numsFlag := true
+		if item.Total > 0 && shopApply.Total != item.Total {
+			totalFlag = false
+			continue
 		}
-		if nums == int64(99) || nums == int64(0) {
+		count := int64(0)
+
+		if item.Total > 0 {
+			count = int64(1)
+		}
+
+		if count == int64(0) {
 			numsFlag = false
 		}
-		/*	beego.Warn("item_name-1%d", item.Name)
-			beego.Warn("firstFlag%v", firstFlag)
-			beego.Warn("totalFlag%v", totalFlag)
-			beego.Warn("numsFlag%v", numsFlag)
-			beego.Warn("nums%d", nums)*/
 
-		//满足促销条件
-		if firstFlag && totalFlag && numsFlag {
+		beego.Warn("item_name-1%d", item.Name)
+		beego.Warn("totalFlag%v", totalFlag)
+		beego.Warn("numsFlag%v", numsFlag)
+		beego.Warn("nums%d", count)
 
+		//满足促销条件
+		if totalFlag && numsFlag {
+			orderId := fmt.Sprintf("shopapply-(%d)", shopApply.Id)
+			source := promotion_model.SOURCE_SHOP
+			remark := fmt.Sprintf("店长申请成功(%d)--促销(%s)", shopApply.Id, item.Name)
 			if item.SendProd1 > 0 && item.SendNums1 > 0 {
-				sendNums1 := nums * item.SendNums1
-				//赠送赠品1
+				sendNums1 := count * item.SendNums1
+				//生成赠品
 				product := product_model.GetProductById(item.SendProd1, true)
-				go order_model.SendCreate(order.OrderId, order.Id, product.Id, product.Price, product.Price, product.Name, sendNums1, order.Depart)
+				if product != nil {
+					total := sendNums1 * product.Price
+					go new(promotion_model.Present).CreatePresent(wxUid, product.Price, total, product.Id, sendNums1, source, remark)
+				}
 			}
 			if item.SendProd2 > 0 && item.SendNums2 > 0 {
-				sendNums2 := nums * item.SendNums2
+				sendNums2 := count * item.SendNums2
 				//赠送赠品1
 				product := product_model.GetProductById(item.SendProd2, true)
-				go order_model.SendCreate(order.OrderId, order.Id, product.Id, product.Price, product.Price, product.Name, sendNums2, order.Depart)
+				if product != nil {
+					total := sendNums2 * product.Price
+					go new(promotion_model.Present).CreatePresent(wxUid, product.Price, total, product.Id, sendNums2, source, remark)
+				}
 			}
 
 			if item.SendProd3 > 0 && item.SendNums3 > 0 {
-				sendNums3 := nums * item.SendNums3
-				//赠送赠品1
+				sendNums3 := count * item.SendNums3
+				//赠送赠品3
 				product := product_model.GetProductById(item.SendProd3, true)
-				go order_model.SendCreate(order.OrderId, order.Id, product.Id, product.Price, product.Price, product.Name, sendNums3, order.Depart)
+				if product != nil {
+					total := sendNums3 * product.Price
+					go new(promotion_model.Present).CreatePresent(wxUid, product.Price, total, product.Id, sendNums3, source, remark)
+				}
 			}
 
 			if item.SendProd4 > 0 && item.SendNums4 > 0 {
-				sendNums4 := nums * item.SendNums4
-				//赠送赠品1
+				sendNums4 := count * item.SendNums4
+				//赠送赠品4
 				product := product_model.GetProductById(item.SendProd4, true)
-				go order_model.SendCreate(order.OrderId, order.Id, product.Id, product.Price, product.Price, product.Name, sendNums4, order.Depart)
+				if product != nil {
+					total := sendNums4 * product.Price
+					go new(promotion_model.Present).CreatePresent(wxUid, product.Price, total, product.Id, sendNums4, source, remark)
+				}
 			}
 
 			if item.SendProd5 > 0 && item.SendNums5 > 0 {
-				sendNums5 := nums * item.SendNums5
-				//赠送赠品1
+				sendNums5 := count * item.SendNums5
+				//赠送赠品3
 				product := product_model.GetProductById(item.SendProd5, true)
-				go order_model.SendCreate(order.OrderId, order.Id, product.Id, product.Price, product.Price, product.Name, sendNums5, order.Depart)
+				if product != nil {
+					total := sendNums5 * product.Price
+					go new(promotion_model.Present).CreatePresent(wxUid, product.Price, total, product.Id, sendNums5, source, remark)
+				}
 			}
+
 			//赠送积分 or 代办费
 			if item.Cash > 0 {
-				totalCash := nums * item.Cash
-				source := balance_model.BALANCE_SOURCE_PROMOTION
-				remark := fmt.Sprintf("促销赠送提货券")
-				new(balance_model.Balance).Create(order.WxUserId, order.UserId, totalCash, source, order.OrderId, remark)
+				totalCash := count * item.Cash
+				source := balance_model.BALANCE_SOURCE_SHOP_PROMOTION
+				remark := fmt.Sprintf("店长申请促销赠送提货券")
+				new(balance_model.Balance).Create(shopApply.WxUId, shopApply.UserId, totalCash, source, orderId, remark)
 			}
+
 			if item.Cent > 0 {
-				totalCent := nums * item.Cent
+				totalCent := count * item.Cent
 				source := cent_model.PROMOTION_SEND
-				remark := fmt.Sprintf("促销活动赠送")
-				new(cent_model.CentBalance).Create(order.WxUserId, totalCent, source, order.OrderId, remark)
+				remark := fmt.Sprintf("店长申请促销活动赠送")
+				new(cent_model.CentBalance).Create(shopApply.WxUId, totalCent, source, orderId, remark)
 			}
 		}
 	}

+ 10 - 0
go/gopath/src/fohow.com/apps/models/balance_model/balance.go

@@ -51,6 +51,12 @@ const (
 	BALANCE_SOURCE_PROMOTION      = "promotion"
 	BALANCE_SOURCE_PROMOTION_NAME = "促销赠送"
 
+	BALANCE_SOURCE_SHOP_PROMOTION         = "shop_promotion"
+	BALANCE_SOURCE_BALANCE_PROMOTION_NAME = "店长促销赠送"
+
+	BALANCE_SOURCE_BALANCE_RECHARGE_PROMOTION      = "balance_promotion"
+	BALANCE_SOURCE_BALANCE_RECHARGE_PROMOTION_NAME = "充值订单促销赠送"
+
 	PAY_WAY_TYPE_SERVICE_WXPAY = "service_wxpay"
 )
 
@@ -136,6 +142,10 @@ func (self *Balance) GetSourceName() string {
 		sourceName = SHOP_APPYCATION_NAME
 	case BALANCE_SOURCE_PROMOTION:
 		sourceName = BALANCE_SOURCE_PROMOTION_NAME
+	case BALANCE_SOURCE_SHOP_PROMOTION:
+		sourceName = BALANCE_SOURCE_BALANCE_PROMOTION_NAME
+	case BALANCE_SOURCE_BALANCE_RECHARGE_PROMOTION:
+		sourceName = BALANCE_SOURCE_BALANCE_RECHARGE_PROMOTION_NAME
 	}
 	return sourceName
 }

+ 2 - 2
go/gopath/src/fohow.com/apps/models/promotion_model/order_promotion.go

@@ -54,7 +54,7 @@ func (self *Promotion) TableName() string {
 }
 
 func GetEffetivePromotions(queryDate time.Time, orderType int64, useCache bool) (list []*Promotion) {
-	k := fmt.Sprintf("product_model.GetEffetivePromotions(%s)", queryDate.Format("2006-01-02"))
+	k := fmt.Sprintf("promotion_model.GetEffetivePromotions(%s)", queryDate.Format("2006-01-02"))
 	if useCache {
 		if ret, ok := cache.Cache.Get(k).([]*Promotion); ok {
 			return ret
@@ -69,7 +69,7 @@ func GetEffetivePromotions(queryDate time.Time, orderType int64, useCache bool)
 
 	_, err := orm.NewOrm().Raw(sql, queryDate.Format("2006-01-02 15:04:05"), queryDate.Format("2006-01-02 15:04:05"), orderType).QueryRows(&list)
 	if err != nil {
-		beego.BeeLogger.Warn("product_model.GetSeckillPromotions(%s) err=%s", queryDate.Format("2006-01-02"), err)
+		beego.BeeLogger.Warn("promotion_model.GetEffetivePromotions(%s) err=%s", queryDate.Format("2006-01-02"), err)
 		return nil
 	}
 

+ 3 - 1
go/gopath/src/fohow.com/apps/models/promotion_model/present.go

@@ -9,6 +9,8 @@ import (
 )
 
 const (
+	SOURCE_BALANCE     = "balance"
+	SOURCE_SHOP        = "shop"
 	presents_tablename = "presents"
 )
 
@@ -32,7 +34,7 @@ func (self *Present) TableName() string {
 }
 
 //创建点击统计项
-func (self *Present) Create(wxUId, price, total, prd, nums int64, source, remark string) *Present {
+func (self *Present) CreatePresent(wxUId, price, total, prd, nums int64, source, remark string) *Present {
 	item := &Present{
 		WxUId:    wxUId,
 		Price:    price,

+ 53 - 0
go/gopath/src/fohow.com/apps/models/promotion_model/promotion_model.go

@@ -2,6 +2,11 @@ package promotion_model
 
 import (
 	// "strings"
+	"fmt"
+	"fohow.com/cache"
+	"github.com/astaxie/beego"
+	"github.com/astaxie/beego/orm"
+
 	"time"
 )
 
@@ -63,3 +68,51 @@ func (self *ShopPromotion) TableName() string {
 func (self *BalancePromotion) TableName() string {
 	return balance_promotions_tablename
 }
+
+func GetBalanceEffetivePromotions(queryDate time.Time, depart int64, useCache bool) (list []*BalancePromotion) {
+	k := fmt.Sprintf("promotion_model.GetBalanceEffetivePromotions(%s)", queryDate.Format("2006-01-02"))
+	if useCache {
+		if ret, ok := cache.Cache.Get(k).([]*BalancePromotion); ok {
+			return ret
+		}
+	}
+
+	sql := `
+		select * from
+		balance_promotions
+		where DATE_ADD(begin_time,INTERVAL 8 HOUR) <= ? and DATE_ADD(end_time,INTERVAL 8 HOUR) >= ? and depart = ? and is_enable = 1 ;
+		`
+
+	_, err := orm.NewOrm().Raw(sql, queryDate.Format("2006-01-02 15:04:05"), queryDate.Format("2006-01-02 15:04:05"), depart).QueryRows(&list)
+	if err != nil {
+		beego.BeeLogger.Warn("promotion_model.GetBalanceEffetivePromotions(%s) err=%s", queryDate.Format("2006-01-02"), err)
+		return nil
+	}
+
+	cache.Cache.Put(k, list, 10*time.Minute)
+	return list
+}
+
+func GetShopEffetivePromotions(queryDate time.Time, depart int64, useCache bool) (list []*ShopPromotion) {
+	k := fmt.Sprintf("promotion_model.GetShopEffetivePromotions(%s)", queryDate.Format("2006-01-02"))
+	if useCache {
+		if ret, ok := cache.Cache.Get(k).([]*ShopPromotion); ok {
+			return ret
+		}
+	}
+
+	sql := `
+		select * from
+		shop_promotions
+		where DATE_ADD(begin_time,INTERVAL 8 HOUR) <= ? and DATE_ADD(end_time,INTERVAL 8 HOUR) >= ? and depart = ? and is_enable = 1 ;
+		`
+
+	_, err := orm.NewOrm().Raw(sql, queryDate.Format("2006-01-02 15:04:05"), queryDate.Format("2006-01-02 15:04:05"), depart).QueryRows(&list)
+	if err != nil {
+		beego.BeeLogger.Warn("promotion_model.GetShopEffetivePromotions(%s) err=%s", queryDate.Format("2006-01-02"), err)
+		return nil
+	}
+
+	cache.Cache.Put(k, list, 10*time.Minute)
+	return list
+}