Преглед изворни кода

order purchase limit logic edit

abiao пре 5 година
родитељ
комит
d83eb53fcb

+ 2 - 0
go/gopath/src/fohow.com/apps/controllers/cron_controller/init.go

@@ -79,6 +79,8 @@ func exec(name string) {
 		go registerWxUser()
 	case "cancel_order": //下单24h未支付改为已取消
 		go OrderCancelAutomatically()
+	case "cancel_order_once": //下单24h未支付改为已取消
+		go OnceOrderCancelAutomatically()
 	case "send_cent": //补发积分执行一次
 		//go patchCent()
 	case "clear_update_orders": //清理过期物流数据

+ 59 - 3
go/gopath/src/fohow.com/apps/controllers/cron_controller/order_receive_automatically.go

@@ -53,6 +53,62 @@ func OrderCompleteDispatchTime(isUpdateTime bool) {
 
 //下单超24h未支付,自动取消订单
 func OrderCancelAutomatically() {
+	payTime, err := beego.AppConfig.Int("PayTime")
+	if err != nil {
+		beego.BeeLogger.Error("%s", err)
+		return
+	}
+	db, err := beego.AppConfig.Int("AliRedisDb")
+	if err != nil {
+		beego.BeeLogger.Error("%s", err)
+		return
+	}
+	client := redis.NewClient(&redis.Options{
+		Addr:     beego.AppConfig.String("AliRedisHost"),
+		Password: beego.AppConfig.String("AliRedisPwd"), //默认空密码
+		DB:       db,                                    //使用默认数据库
+	})
+	defer client.Close() //最后关闭
+	key := lib_redis.GetOrderCancelList()
+	//beego.BeeLogger.Warn("key=%s", key)
+
+	list, _ := client.LRange(key, -1000, -1).Result()
+	for _, orderId := range list {
+		//beego.BeeLogger.Warn("orderId=%s", orderId)
+		order := order_model.GetOrderById(orderId, false)
+		if order == nil {
+			lib_redis.ThrowOutRedisList(key, orderId)
+			continue
+		}
+		if order.PaiedAt > 0 {
+			lib_redis.ThrowOutRedisList(key, orderId)
+			continue
+		}
+		/*		beego.BeeLogger.Warn("payTime=%d", payTime)
+				beego.BeeLogger.Warn("time.Now().Unix()=%d", time.Now().Unix())
+				beego.BeeLogger.Warn("order.CreatedAt.Unix()=%d", order.CreatedAt.Unix())*/
+
+		//如果下单时间超过一天,则取消订单
+		if order.Status == order_model.STATUS_UNPAY && order.CreatedAt.Unix()+int64(payTime) < time.Now().Unix() {
+			if order.CouponPrice > 0 {
+				//退款至提货券账户
+				source := balance_model.BALANCE_SOURCE_ALL_REFUNDED_NAME
+				remark := fmt.Sprintf("取消订单退款")
+				new(balance_model.Balance).Create(order.WxUserId, order.UserId, order.CouponPrice, source, orderId, remark)
+			}
+			order.Status = order_model.STATUS_CLOSED
+			order.Save()
+			lib_redis.ThrowOutRedisList(key, orderId)
+		}
+		if order.Status == order_model.STATUS_CLOSED {
+			lib_redis.ThrowOutRedisList(key, orderId)
+		}
+	}
+	return
+}
+
+//下单超24h未支付,自动取消订单
+func OnceOrderCancelAutomatically() {
 	beego.BeeLogger.Warn("payTime=%d", 12345)
 
 	payTime, err := beego.AppConfig.Int("PayTime")
@@ -74,10 +130,10 @@ func OrderCancelAutomatically() {
 	key := lib_redis.GetOrderCancelList()
 	beego.BeeLogger.Warn("key=%s", key)
 
-	list, _ := client.LRange(key, -1000, -1).Result()
-	for _, orderId := range list {
+	list := order_model.GetUnpayOrders()
+	for _, order := range list {
+		orderId := order.OrderId
 		beego.BeeLogger.Warn("orderId=%s", orderId)
-
 		order := order_model.GetOrderById(orderId, false)
 		if order == nil {
 			lib_redis.ThrowOutRedisList(key, orderId)

+ 2 - 3
go/gopath/src/fohow.com/apps/controllers/live_controller/live_controller.go

@@ -5,7 +5,6 @@ import (
 	// "strings"
 
 	"fohow.com/apps/models/live_model"
-	"github.com/astaxie/beego"
 	"github.com/astaxie/beego/context"
 	"time"
 
@@ -61,8 +60,8 @@ func (self *LiveController) Latest() {
 	count := live_model.GetLatestCount(recommend, ptype, cache)
 	timeNowUnix := time.Now().Unix()
 	for _, live := range lives {
-		beego.BeeLogger.Warn("live_timeNowUnix: %d", timeNowUnix)
-		beego.BeeLogger.Warn("live-BeginDate.Unix: %d", live.BeginDate.Unix())
+		//beego.BeeLogger.Warn("live_timeNowUnix: %d", timeNowUnix)
+		//beego.BeeLogger.Warn("live-BeginDate.Unix: %d", live.BeginDate.Unix())
 		if live.BeginDate.Unix() > timeNowUnix {
 			live.State = "尚未开播"
 		} else if live.BeginDate.Unix() < timeNowUnix && live.EndDate.Unix() > timeNowUnix {

+ 51 - 1
go/gopath/src/fohow.com/apps/controllers/order_controller/order_controller.go

@@ -188,6 +188,23 @@ func (self *OrderController) MultipleCreate() {
 		if product == nil {
 			self.ReturnError(403, apps.NoExist, "", nil)
 		}
+
+		if product.PurchaseLimitCount > 0 {
+			if product.PurchaseLimitCount < cNums {
+				self.ReturnError(403, []string{apps.OverLimitCount[0], fmt.Sprintf("该商品限购%d件", product.PurchaseLimitCount)}, "", nil)
+			} else {
+				purchaseTotalCount := order_model.GetOrderCountByPIdAndWxUId(cartItem.ProductId, wxUId)
+				//历史已经买够到限购数量了
+				if product.PurchaseLimitCount <= purchaseTotalCount {
+					self.ReturnError(403, []string{apps.PurchasedReachLimit[0], fmt.Sprintf("该商品限购%d件", product.PurchaseLimitCount)}, "", nil)
+				}
+				//历史没买够数量,但是历史总数+想购买的数量 超过限购数量
+				if product.PurchaseLimitCount < (purchaseTotalCount + cNums) {
+					canBuyCount := product.PurchaseLimitCount - purchaseTotalCount
+					self.ReturnError(403, []string{apps.PurchasedReachLimit[0], fmt.Sprintf("您还可以购买%d件", canBuyCount)}, "", nil)
+				}
+			}
+		}
 		//创建订单明细
 		totalPrice += product.Price * cNums
 		CreateOrderDetails(product, order, cNums)
@@ -246,7 +263,23 @@ func (self *OrderController) MultShopCreate() {
 		if product.Ptype != product_model.SHOP_SALE {
 			self.ReturnError(403, apps.NoShopSale, "", nil)
 		}
-
+		//商品限购判断
+		if product.PurchaseLimitCount > 0 {
+			if product.PurchaseLimitCount < cNums {
+				self.ReturnError(403, []string{apps.OverLimitCount[0], fmt.Sprintf("该商品限购%d件", product.PurchaseLimitCount)}, "", nil)
+			} else {
+				purchaseTotalCount := order_model.GetOrderCountByPIdAndWxUId(pId, wxUId)
+				//历史已经买够到限购数量了
+				if product.PurchaseLimitCount <= purchaseTotalCount {
+					self.ReturnError(403, []string{apps.PurchasedReachLimit[0], fmt.Sprintf("该商品限购%d件", product.PurchaseLimitCount)}, "", nil)
+				}
+				//历史没买够数量,但是历史总数+想购买的数量 超过限购数量
+				if product.PurchaseLimitCount < (purchaseTotalCount + cNums) {
+					canBuyCount := product.PurchaseLimitCount - purchaseTotalCount
+					self.ReturnError(403, []string{apps.PurchasedReachLimit[0], fmt.Sprintf("您还可以购买%d件", canBuyCount)}, "", nil)
+				}
+			}
+		}
 		//创建订单明细
 		totalPrice += product.Price * cNums
 		CreateOrderDetails(product, order, cNums)
@@ -299,6 +332,23 @@ func (self *OrderController) MultCentCreate() {
 		if product.Ptype != product_model.CENT_SALE {
 			self.ReturnError(403, apps.NoShopSale, "", nil)
 		}
+		//商品限购判断
+		if product.PurchaseLimitCount > 0 {
+			if product.PurchaseLimitCount < cNums {
+				self.ReturnError(403, []string{apps.OverLimitCount[0], fmt.Sprintf("该商品限购%d件", product.PurchaseLimitCount)}, "", nil)
+			} else {
+				purchaseTotalCount := order_model.GetOrderCountByPIdAndWxUId(pId, wxUId)
+				//历史已经买够到限购数量了
+				if product.PurchaseLimitCount <= purchaseTotalCount {
+					self.ReturnError(403, []string{apps.PurchasedReachLimit[0], fmt.Sprintf("该商品限购%d件", product.PurchaseLimitCount)}, "", nil)
+				}
+				//历史没买够数量,但是历史总数+想购买的数量 超过限购数量
+				if product.PurchaseLimitCount < (purchaseTotalCount + cNums) {
+					canBuyCount := product.PurchaseLimitCount - purchaseTotalCount
+					self.ReturnError(403, []string{apps.PurchasedReachLimit[0], fmt.Sprintf("您还可以购买%d件", canBuyCount)}, "", nil)
+				}
+			}
+		}
 		//计算订单总额
 		totalPrice += product.Price * cNums
 	}

+ 18 - 0
go/gopath/src/fohow.com/apps/models/order_model/order.go

@@ -515,6 +515,24 @@ func GetSevenDaysFullOrdersByDispatchTime() (orders []*Order) {
 	return orders
 }
 
+//查找未支付订单
+func GetUnpayOrders() (orders []*Order) {
+
+	sql := `
+		SELECT
+			*
+		FROM
+			orders
+		WHERE
+			STATUS = 'unpay';
+	`
+
+	if _, err := orm.NewOrm().Raw(sql).QueryRows(&orders); err != nil {
+		beego.BeeLogger.Debug("order_model.GetOrdersByDispatchStatusAndDispatchTimeIsNull() err=[%s]", err)
+		return nil
+	}
+	return orders
+}
 func GetOrderByOrderRemark(remark string, useCache bool) (item *Order) {
 	k := fmt.Sprintf("order_model.GetOrderByOrderRemark(%s)", remark)
 	if useCache {