Browse Source

update sold count static function

abiao 4 years ago
parent
commit
5175fbaad5

+ 11 - 11
go/gopath/src/fohow.com/apps/controllers/cron_controller/update_sold_count.go

@@ -26,21 +26,21 @@ func updateSoldCount() {
 	beego.BeeLogger.Warn("updateSoldCount.len(list):%d", len(list))
 	for _, product := range list {
 		//统计商品总销量
-		soldCount := order_model.GetSoldCountByPId(product.Id, false)
+		soldCount := order_model.GetDetailSoldCountByPId(product.Id, false)
 		product.SaleNums = soldCount
 		beego.BeeLogger.Warn("updateSoldCount.productId:%d", product.Id)
 		beego.BeeLogger.Warn("updateSoldCount.SaleNums:%d", soldCount)
 		product.Save()
-		//套装商品拆分更新销量
-		if product.Package {
-			package_list := product_model.GetPackageList(product.Id, true)
-			for _, item := range package_list {
-				item_pd := product_model.GetProductById(item.ItemId, true)
-				count := item.Nums * soldCount
-				item_pd.SaleNums += count
-				item_pd.Save()
-			}
-		}
+		/*	//套装商品拆分更新销量
+			if product.Package {
+				package_list := product_model.GetPackageList(product.Id, true)
+				for _, item := range package_list {
+					item_pd := product_model.GetProductById(item.ItemId, true)
+					count := item.Nums * soldCount
+					item_pd.SaleNums += count
+					item_pd.Save()
+				}
+			}*/
 	}
 
 }

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

@@ -457,8 +457,8 @@ func (self *OrderController) Detail() {
 		beego.BeeLogger.Error("order not exist id=[%s]", oId)
 		self.ReturnError(404, apps.OrderNotExist, "", nil)
 	}
-	orderList := order_model.GetAllDetailsOrderId(o.OrderId, false)
-	for _, item := range orderList {
+	orderDtList := order_model.GetAllDetailsOrderId(o.OrderId, false)
+	for _, item := range orderDtList {
 		product := product_model.GetProductById(item.ProductId, cache)
 		if product == nil {
 			self.ReturnError(403, apps.ProductNotExist, "", nil)
@@ -469,7 +469,7 @@ func (self *OrderController) Detail() {
 		}
 		item.Package = product.Package
 		if product.Package {
-			item.PacakageList = order_model.GetOrderDetailPackages(product.Id, true)
+			item.PacakageList = order_model.GetOrderDetailPackages(item.Id, item.Count, true)
 		}
 		o.Count += item.Count
 		item.Cover = product.Cover
@@ -517,22 +517,22 @@ func (self *OrderController) List() {
 
 	for _, item := range orders {
 
-		orderList := order_model.GetAllDetailsOrderId(item.OrderId, false)
-		for _, orderItem := range orderList {
-			product := product_model.GetProductById(orderItem.ProductId, cache)
+		orderDtList := order_model.GetAllDetailsOrderId(item.OrderId, false)
+		for _, orderDtItem := range orderDtList {
+			product := product_model.GetProductById(orderDtItem.ProductId, cache)
 			if product == nil {
 				continue
 			}
-			orderItem.Package = product.Package
-			item.Count += orderItem.Count
-			if orderItem.Commend || item.Status != order_model.STATUS_COMPLETE {
-				orderItem.Commend = true
+			orderDtItem.Package = product.Package
+			item.Count += orderDtItem.Count
+			if orderDtItem.Commend || item.Status != order_model.STATUS_COMPLETE {
+				orderDtItem.Commend = true
 			}
 			if product.Package {
-				orderItem.PacakageList = order_model.GetOrderDetailPackages(product.Id, true)
+				orderDtItem.PacakageList = order_model.GetOrderDetailPackages(orderDtItem.Id, item.Count, true)
 			}
-			orderItem.Cover = product.Cover
-			item.ProductList = append(item.ProductList, orderItem)
+			orderDtItem.Cover = product.Cover
+			item.ProductList = append(item.ProductList, orderDtItem)
 		}
 	}
 	type Ret struct {

+ 38 - 9
go/gopath/src/fohow.com/apps/models/order_model/order_dt_item.go

@@ -75,23 +75,52 @@ func GenerateOrderDtItem(detail_item *OrderDetail) error {
 }
 
 //生成订单明细套装
-func GetOrderDetailPackages(productId int64, useCache bool) (list []*ProductItem) {
-	k := fmt.Sprintf("order_model.GetOrderDetailPackages(%d)", productId)
+func GetOrderDetailPackages(orderDtId, nums int64, useCache bool) (list []*ProductItem) {
+	k := fmt.Sprintf("order_model.GetOrderDetailPackages(%d)", orderDtId)
 	if useCache {
 		if ret, ok := cache.Cache.Get(k).([]*ProductItem); ok {
 			return ret
 		}
 	}
-	sql := `
-		select product_id,item_id,item_title,nums from
-		product_items
-		where product_id = ? ;
-		`
-	_, err := orm.NewOrm().Raw(sql, productId).QueryRows(&list)
+	//sql := fmt.Sprintf("select total_price as investment, user_id, paied_at as invest_time from %s where project_id = ? and state = ? and is_refunded = ? and is_deleted = ? order by invest_time desc, id desc limit %d, %d",
+
+	sql :=
+		fmt.Sprintf(`
+		select product_id as item_id,title as item_title,nums/%d from
+		order_base_details
+		where order_dt_id = ?  ;
+		`, nums)
+	_, err := orm.NewOrm().Raw(sql, orderDtId).QueryRows(&list)
 	if err != nil {
-		beego.BeeLogger.Warn("order_model.GetOrderDetailPackages(%d) err=%s", productId, err)
+		beego.BeeLogger.Warn("order_model.GetOrderDetailPackages(%d) err=%s", orderDtId, err)
 		return nil
 	}
 	cache.Cache.Put(k, list, 10*time.Minute)
 	return list
 }
+
+//获取某个商品明细已售总数
+func GetDetailSoldCountByPId(pId int64, useCache bool) int64 {
+	k := fmt.Sprintf("order_model.GetDetailSoldCountByPId(%d)", pId)
+	if useCache {
+		if ret, ok := cache.Cache.Get(k).(int64); ok {
+			return ret
+		}
+	}
+	type Ret struct {
+		Count int64
+	}
+	ret := &Ret{}
+	o := orm.NewOrm()
+	new_tbn := "order_base_details"
+	tbn := new(Order).TableName()
+	sql := fmt.Sprintf("SELECT sum(`nums`) as count FROM `%s` WHERE order_no in ( select order_id from `%s` where  status not in (?, ?) ) and product_id=?;", new_tbn, tbn)
+	err := o.Raw(sql, STATUS_CLOSED, STATUS_UNPAY, pId).QueryRow(ret)
+	if err != nil {
+		beego.BeeLogger.Error("GetSoldCountByPId err=[%s]", err)
+		return 0
+	}
+
+	cache.Cache.Put(k, ret.Count, 10*time.Minute)
+	return ret.Count
+}