Browse Source

银豆抵扣逻辑

abiao 3 years ago
parent
commit
f402cf50cb

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

@@ -132,16 +132,26 @@ func (self *OrderController) Create() {
 		order.Promotions = specialstr
 	}
 
-	order.Save()
-
-	//抵扣银豆
+	//实抵银豆
 	var math = helper.NewMath()
 	userLeftSilver := balance_model.GetUserTotalSilver(wxUId)
 	paiedSilver, _ := math.Min(float64(userLeftSilver), float64(product.Silver*count))
 
-	//创建订单明细
-	new(order_model.OrderDetail).Create(order.OrderId, order.Id, product.Id, product.RelateProductId, product.Price, int64(paiedSilver), product.Name, sizeName, colorName, count, wxUser.Depart)
+	//折扣
+	dis := (float64(product.Price*count) - paiedSilver) * float64(product.Pv) / float64(100)
+	disAmount := math.Round(dis)
+
+	//提货券
+	userLeftBalanceCount := balance_model.GetUserTotalBalance(wxUId)
+	if userLeftBalanceCount > int64(0) {
+		disAmount = float64(0)
+	}
 
+	//创建订单明细
+	new(order_model.OrderDetail).Create(order.OrderId, order.Id, product.Id, product.RelateProductId, product.Price, int64(paiedSilver), int64(disAmount), product.Name, sizeName, colorName, count, wxUser.Depart)
+	order.DisAmount = int64(disAmount)
+	order.TotalSilver = product.Silver * count
+	order.Save()
 	//发放赠品
 	helpers.SetOrderPromotionPro(order.OrderId, wxUser.Id)
 
@@ -191,6 +201,7 @@ func (self *OrderController) MultipleCreate() {
 	}
 	totalPrice := int64(0)
 	totalPv := int64(0)
+	totalSilver := int64(0)
 	c_arr := strings.Split(ids, ",")
 	c_nums := strings.Split(nums, ",")
 
@@ -235,15 +246,25 @@ func (self *OrderController) MultipleCreate() {
 				}
 			}
 		}
+		//统计总银豆
+		totalSilver += product.Silver * cNums
 	}
 
+	//统计实抵银豆
+	var math = helper.NewMath()
+	userLeftSilver := balance_model.GetUserTotalSilver(wxUId)
+	paiedSilver, _ := math.Min(float64(userLeftSilver), float64(totalSilver))
+
+	//提货券
+	userLeftBalanceCount := balance_model.GetUserTotalBalance(wxUId)
+
 	//创建订单
 	order := new(order_model.Order).CreateNew(wxUId, wxUser.UserId,
 		totalPrice, int64(0), order_model.ORDER_TYPE_NORMAL, wxUser.Depart, order_model.SOURCE_XCX)
 	if order == nil {
 		self.ReturnError(403, apps.CreateOrderFail, "", nil)
 	}
-
+	totalDisamout := int64(0)
 	for key, s_item := range c_arr {
 		cId, _ := strconv.ParseInt(s_item, 10, 64)
 		cNums := int64(1)
@@ -263,8 +284,20 @@ func (self *OrderController) MultipleCreate() {
 		totalPrice += product.Price * cNums
 		totalPv += product.Pv * cNums
 
+		//统计单件
+		singleTSilver := (float64(product.Silver*cNums) / float64(totalSilver)) * paiedSilver
+		silver := int64(math.Round(singleTSilver))
+
+		dis := (float64(product.Price*cNums) - singleTSilver) * float64(product.Pv) / float64(100)
+		disAmount := int64(math.Round(dis))
+
+		if userLeftBalanceCount > int64(0) {
+			disAmount = int64(0)
+		}
+		totalDisamout += disAmount
+
 		//创建订单明细
-		CreateOrderDetails(product, order, cNums, wxUser.Depart)
+		CreateOrderDetails(product, order, cNums, wxUser.Depart, silver, disAmount)
 	}
 	freight := sys_config.GetFreight()
 	if totalPrice >= sys_config.GetOrderLimit() {
@@ -273,6 +306,8 @@ func (self *OrderController) MultipleCreate() {
 	order.Pv = totalPv
 	order.TotalPrice = totalPrice
 	order.Freight = freight
+	order.DisAmount = totalDisamout
+	order.TotalSilver = totalSilver
 	specialPromotion, specialstr := sys_config.GetSpetialPromotion()
 	if specialPromotion {
 		order.Promotions = specialstr
@@ -358,7 +393,7 @@ func (self *OrderController) MultShopCreate() {
 		}
 		//创建订单明细
 		totalPrice += product.Price * cNums
-		CreateOrderDetails(product, order, cNums, wxUser.Depart)
+		CreateOrderDetails(product, order, cNums, wxUser.Depart, int64(0), int64(0))
 	}
 	freight := sys_config.GetFreight()
 	if totalPrice >= sys_config.GetOrderLimit() {
@@ -466,7 +501,7 @@ func (self *OrderController) MultCentCreate() {
 		if product == nil {
 			self.ReturnError(403, apps.NoExist, "", nil)
 		}
-		CreateOrderDetails(product, order, cNums, wxUser.Depart)
+		CreateOrderDetails(product, order, cNums, wxUser.Depart, int64(0), int64(0))
 	}
 	order.TotalPrice = totalPrice
 	order.Freight = freight
@@ -760,7 +795,7 @@ func (self *OrderController) Operate() {
 	self.ServeJSON()
 }
 
-func CreateOrderDetails(product *product_model.Product, order *order_model.Order, cNums, depart int64) {
+func CreateOrderDetails(product *product_model.Product, order *order_model.Order, cNums, depart, silver, disAmount int64) {
 	//获取商品属性详情
 	sizeName := ""
 	colorName := ""
@@ -776,7 +811,7 @@ func CreateOrderDetails(product *product_model.Product, order *order_model.Order
 			colorName = productColor.Name
 		}
 	}
-	new(order_model.OrderDetail).Create(order.OrderId, order.Id, product.Id, product.RelateProductId, product.Price, product.RoboBalancePrice, product.Name, sizeName, colorName, cNums, depart)
+	new(order_model.OrderDetail).Create(order.OrderId, order.Id, product.Id, product.RelateProductId, product.Price, silver, disAmount, product.Name, sizeName, colorName, cNums, depart)
 }
 
 //用户评论订单明细

+ 5 - 48
go/gopath/src/fohow.com/apps/controllers/pay_controller/pay_exchange_controller.go

@@ -6,8 +6,6 @@ import (
 	"fohow.com/apps/models/cent_model"
 	"fohow.com/apps/models/sys_config"
 	"fohow.com/libs/lib_redis"
-	"math"
-
 	// "net/url"
 	// "strings"
 	"time"
@@ -102,8 +100,7 @@ func (self *PayController) payExchange(oId, payWay, returnUrl, source, remark st
 	total_price := int64(0)
 	total_quan := int64(0)
 	total_weixin := int64(0)
-	total_silver := int64(0)
-	dis_amount := int64(0)
+	paiedSilver := int64(0)
 
 	list := order_model.GetAllDetailsOrderId(order.OrderId, false)
 	for _, item := range list {
@@ -166,8 +163,7 @@ func (self *PayController) payExchange(oId, payWay, returnUrl, source, remark st
 				total_quan += product.Price * item.Count
 			}
 			total_price += product.Price * item.Count
-			total_silver += product.Silver * item.Count
-
+			paiedSilver += item.Silver
 		}
 
 		if _, ok := SaleNumsMap[product.Id]; ok {
@@ -183,7 +179,7 @@ func (self *PayController) payExchange(oId, payWay, returnUrl, source, remark st
 		}
 
 	}
-	//beego.BeeLogger.Error("SaleNumsMap1 %v", SaleNumsMap)
+	dis_amount := order.DisAmount
 
 	resultStore, prdName := FindNotEnoughPrd(storeMap)
 	if resultStore {
@@ -220,14 +216,8 @@ func (self *PayController) payExchange(oId, payWay, returnUrl, source, remark st
 		tp := order.TotalPrice
 
 		needWx := false
+
 		//抵扣银豆
-		paiedSilver := int64(0)
-		userLeftSilver := balance_model.GetUserTotalSilver(wxUId)
-		if userLeftSilver < total_silver {
-			paiedSilver = userLeftSilver
-		} else {
-			paiedSilver = total_silver
-		}
 		if paiedSilver > tp {
 			paiedSilver = tp
 		}
@@ -241,6 +231,7 @@ func (self *PayController) payExchange(oId, payWay, returnUrl, source, remark st
 		total_quan = total_quan + freight - paiedSilver
 
 		quanLittle := false
+		useCoupon = true
 		if useCoupon {
 			if userLeftBalanceCount < total_quan {
 				quanLittle = true
@@ -251,38 +242,6 @@ func (self *PayController) payExchange(oId, payWay, returnUrl, source, remark st
 		}
 		total_weixin = tp - totalCoupon
 
-		//统计折扣
-		if totalCoupon > int64(0) {
-			dis_amount = int64(0)
-		} else {
-			if paiedSilver == int64(0) || total_silver == int64(0) {
-				for _, item := range list {
-					//商品
-					product := product_model.GetProductById(item.ProductId, true)
-					dis_amount += product.Pv * item.Count
-				}
-			} else {
-				totalDis := float64(0)
-				for _, item := range list {
-					//商品
-					product := product_model.GetProductById(item.ProductId, true)
-
-					//非赠品计入价格
-					if !item.Send {
-						beego.BeeLogger.Error("Price error:%d", product.Price)
-						beego.BeeLogger.Error("Pv error:%d", product.Pv)
-
-						singleDis := (float64(product.Price) - float64(product.Pv)*(float64(paiedSilver)/float64(total_silver))) / float64(product.Price)
-
-						beego.BeeLogger.Error("singleDis error:%f", singleDis)
-						totalDis += float64(product.Pv) * float64(singleDis) * float64(item.Count)
-					}
-					beego.BeeLogger.Error("totalDis error:%f", totalDis)
-					dis_amount += int64(math.Floor(totalDis + 0.5))
-				}
-			}
-		}
-
 		//店长先抵扣折扣
 		paiedDis := int64(0)
 		if wxUser.Rank >= user_model.WX_USER_RANK_ONE && dis_amount > int64(0) {
@@ -344,9 +303,7 @@ func (self *PayController) payExchange(oId, payWay, returnUrl, source, remark st
 
 		order.CouponPrice = totalCoupon
 		order.PaiedSilver = paiedSilver
-		order.TotalSilver = total_silver
 		order.PaiedCash = paiedCash
-		order.DisAmount = dis_amount
 
 		switch needWx { // 1.true需要微信支付,2.false不需要微信支付
 		case false: // 提货券抵扣完毕

+ 20 - 21
go/gopath/src/fohow.com/apps/helpers/promotion_helper.go

@@ -187,7 +187,6 @@ func SetOrderPromotion(orderId string, wxUid int64) {
 	}
 }
 
-
 //订单促销
 /*
 	如果促销条件中,设置的项目有一项不满足即视为不满足促销条件
@@ -241,9 +240,9 @@ func SetOrderPromotionPro(orderId string, wxUid int64) {
 			nums1 = int64(0)
 			if prd != nil {
 				detail_nums1 := int64(0)
-				detailItem:=order_model.GetDetailsByOrderIdAndPid(orderId,item.Prod1)
-				if detailItem!=nil{
-					detail_nums1=detailItem.Count
+				detailItem := order_model.GetDetailsByOrderIdAndPid(orderId, item.Prod1)
+				if detailItem != nil {
+					detail_nums1 = detailItem.Count
 				}
 				if detail_nums1 > int64(0) && item.Prod1 > 0 && item.Nums1 > 0 {
 					nums1 = int64(detail_nums1 / item.Nums1)
@@ -255,10 +254,10 @@ func SetOrderPromotionPro(orderId string, wxUid int64) {
 			prd := product_model.GetProductById(item.Prod2, true)
 			nums2 = int64(0)
 			if prd != nil {
-				detail_nums2 :=int64(0)
-				detailItem:=order_model.GetDetailsByOrderIdAndPid(orderId,item.Prod2)
-				if detailItem!=nil{
-					detail_nums2 =detailItem.Count
+				detail_nums2 := int64(0)
+				detailItem := order_model.GetDetailsByOrderIdAndPid(orderId, item.Prod2)
+				if detailItem != nil {
+					detail_nums2 = detailItem.Count
 				}
 				//detail_nums2 := order_model.GetDetailsByOrderIdAndRelatePid(orderId, prd.RelateProductId)
 				if detail_nums2 > int64(0) && item.Prod2 > 0 && item.Nums2 > 0 {
@@ -271,9 +270,9 @@ func SetOrderPromotionPro(orderId string, wxUid int64) {
 			nums3 = int64(0)
 			prd := product_model.GetProductById(item.Prod3, true)
 			if prd != nil {
-				detail_nums3 :=int64(0)
-				detailItem:=order_model.GetDetailsByOrderIdAndPid(orderId,item.Prod3)
-				if detailItem!=nil{
+				detail_nums3 := int64(0)
+				detailItem := order_model.GetDetailsByOrderIdAndPid(orderId, item.Prod3)
+				if detailItem != nil {
 					detail_nums3 = detailItem.Count
 				}
 				//detail_nums3 := order_model.GetDetailsByOrderIdAndRelatePid(orderId, prd.RelateProductId)
@@ -291,8 +290,8 @@ func SetOrderPromotionPro(orderId string, wxUid int64) {
 			nums5 = int64(0)
 			if prd != nil {
 				detail_nums4 := int64(0)
-				detailItem:=order_model.GetDetailsByOrderIdAndPid(orderId,item.Prod4)
-				if detailItem!=nil{
+				detailItem := order_model.GetDetailsByOrderIdAndPid(orderId, item.Prod4)
+				if detailItem != nil {
 					detail_nums4 = detailItem.Count
 				}
 				//detail_nums4 := order_model.GetDetailsByOrderIdAndRelatePid(orderId, prd.RelateProductId)
@@ -306,9 +305,9 @@ func SetOrderPromotionPro(orderId string, wxUid int64) {
 			prd := product_model.GetProductById(item.Prod5, true)
 			nums6 = int64(0)
 			if prd != nil {
-				detail_nums5:=int64(0)
-				detailItem:=order_model.GetDetailsByOrderIdAndPid(orderId,item.Prod5)
-				if detailItem!=nil{
+				detail_nums5 := int64(0)
+				detailItem := order_model.GetDetailsByOrderIdAndPid(orderId, item.Prod5)
+				if detailItem != nil {
 					detail_nums5 = detailItem.Count
 				}
 				//detail_nums5 := order_model.GetDetailsByOrderIdAndRelatePid(orderId, prd.RelateProductId)
@@ -714,7 +713,7 @@ func CreateShopApplyOrder(shopAppplyId int64) {
 				if product != nil {
 					delOrder = false
 					total += sendNums1 * product.Price
-					go new(order_model.OrderDetail).Create(order.OrderId, order.Id, product.Id, product.RelateProductId, product.Price, product.RoboBalancePrice, product.Name, "", "", sendNums1, order.Depart)
+					go new(order_model.OrderDetail).Create(order.OrderId, order.Id, product.Id, product.RelateProductId, product.Price, int64(0), int64(0), product.Name, "", "", sendNums1, order.Depart)
 
 				}
 			}
@@ -725,7 +724,7 @@ func CreateShopApplyOrder(shopAppplyId int64) {
 				if product != nil {
 					delOrder = false
 					total += sendNums2 * product.Price
-					go new(order_model.OrderDetail).Create(order.OrderId, order.Id, product.Id, product.RelateProductId, product.Price, product.RoboBalancePrice, product.Name, "", "", sendNums2, order.Depart)
+					go new(order_model.OrderDetail).Create(order.OrderId, order.Id, product.Id, product.RelateProductId, product.Price, int64(0), int64(0), product.Name, "", "", sendNums2, order.Depart)
 
 				}
 			}
@@ -737,7 +736,7 @@ func CreateShopApplyOrder(shopAppplyId int64) {
 				if product != nil {
 					delOrder = false
 					total += sendNums3 * product.Price
-					go new(order_model.OrderDetail).Create(order.OrderId, order.Id, product.Id, product.RelateProductId, product.Price, product.RoboBalancePrice, product.Name, "", "", sendNums3, order.Depart)
+					go new(order_model.OrderDetail).Create(order.OrderId, order.Id, product.Id, product.RelateProductId, product.Price, int64(0), int64(0), product.Name, "", "", sendNums3, order.Depart)
 
 				}
 			}
@@ -749,7 +748,7 @@ func CreateShopApplyOrder(shopAppplyId int64) {
 				if product != nil {
 					delOrder = false
 					total += sendNums4 * product.Price
-					go new(order_model.OrderDetail).Create(order.OrderId, order.Id, product.Id, product.RelateProductId, product.Price, product.RoboBalancePrice, product.Name, "", "", sendNums4, order.Depart)
+					go new(order_model.OrderDetail).Create(order.OrderId, order.Id, product.Id, product.RelateProductId, product.Price, int64(0), int64(0), product.Name, "", "", sendNums4, order.Depart)
 				}
 			}
 
@@ -760,7 +759,7 @@ func CreateShopApplyOrder(shopAppplyId int64) {
 				if product != nil {
 					delOrder = false
 					total += sendNums5 * product.Price
-					go new(order_model.OrderDetail).Create(order.OrderId, order.Id, product.Id, product.RelateProductId, product.Price, product.RoboBalancePrice, product.Name, "", "", sendNums5, order.Depart)
+					go new(order_model.OrderDetail).Create(order.OrderId, order.Id, product.Id, product.RelateProductId, product.Price, int64(0), int64(0), product.Name, "", "", sendNums5, order.Depart)
 				}
 			}
 

+ 3 - 1
go/gopath/src/fohow.com/apps/models/order_model/order_detail.go

@@ -42,6 +42,7 @@ type OrderDetail struct {
 	Depart               int64          `orm:"column(depart)" json:"-"`                                       // datetime
 	Pv                   int64          `orm:"column(pv)"            json:"pv"`                               // varchar(255)
 	Silver               int64          `orm:"column(silver)"        json:"silver"`                           // varchar(255)
+	DisAmount            int64          `orm:"column(dis_amount)"        json:"dis_amount"`                   // varchar(255)
 	Package              bool           `orm:"-"                                                json:"package"`
 	PacakageList         []*ProductItem `orm:"-"                                                json:"package_list"` // varchar(255)
 }
@@ -51,7 +52,7 @@ func (self *OrderDetail) TableName() string {
 }
 
 //创建订单项
-func (self *OrderDetail) Create(oId string, orderId, pId, relatePId, pPrice, silver int64, pName, sizeName, colorName string,
+func (self *OrderDetail) Create(oId string, orderId, pId, relatePId, pPrice, silver, disAmount int64, pName, sizeName, colorName string,
 	pCount, depart int64) *OrderDetail {
 	item := &OrderDetail{
 		OrderNo:         oId,
@@ -66,6 +67,7 @@ func (self *OrderDetail) Create(oId string, orderId, pId, relatePId, pPrice, sil
 		ColorName:       colorName,
 		Send:            false,
 		Depart:          depart,
+		DisAmount:       disAmount,
 	}
 	id, err := orm.NewOrm().Insert(item)
 	if err != nil {