abiao 2 роки тому
батько
коміт
f40180d585

+ 16 - 3
go/gopath/src/fohow.com/apps/helpers/promotion_helper.go

@@ -200,11 +200,14 @@ func SetOrderPromotionPro(orderId string, wxUid int64) {
 	if order == nil {
 		return
 	}
+
 	//检查会员是否首次下单
 	list := order_model.GetWxUserOrders(wxUid)
 	if len(list) == 1 {
 		firstOrder = true
 	}
+	var noInovceArr []int64
+	var promotionsArr []int64
 
 	queryDate := time.Now()
 	// 获取所有有效促销记录
@@ -212,10 +215,11 @@ func SetOrderPromotionPro(orderId string, wxUid int64) {
 	for _, item := range effectivePromotions {
 		//beego.Warn("item_name%d", item.Name)
 
+		noInvovedAmount := order_model.GetNotInvovedAmount(item.Id, order.OrderId)
 		firstFlag := true
 		totalFlag := true
 		numsFlag := true
-		if item.MaxTotal > 0 && order.TotalPrice > item.MaxTotal {
+		if item.MaxTotal > 0 && (order.TotalPrice-noInvovedAmount) > item.MaxTotal {
 			totalFlag = false
 			continue
 		}
@@ -223,11 +227,13 @@ func SetOrderPromotionPro(orderId string, wxUid int64) {
 			firstFlag = false
 			continue
 		}
-		if item.MinTotal > 0 && order.TotalPrice < item.MinTotal {
+
+		if item.MinTotal > 0 && (order.TotalPrice-noInvovedAmount) < item.MinTotal {
 			totalFlag = false
 			firstFlag = false
 			continue
 		}
+
 		nums := int64(999)
 		nums1 := int64(999)
 		nums2 := int64(999)
@@ -331,9 +337,10 @@ func SetOrderPromotionPro(orderId string, wxUid int64) {
 			beego.Warn("totalFlag%v", totalFlag)
 			beego.Warn("numsFlag%v", numsFlag)
 			beego.Warn("nums%d", nums)*/
-
 		//满足促销条件
 		if firstFlag && totalFlag && numsFlag {
+			noInovceArr = append(noInovceArr, noInvovedAmount)
+			promotionsArr = append(promotionsArr, item.Id)
 
 			if item.SendProd1 > 0 && item.SendNums1 > 0 {
 				sendNums1 := nums * item.SendNums1
@@ -382,6 +389,12 @@ func SetOrderPromotionPro(orderId string, wxUid int64) {
 				new(cent_model.CentBalance).Create(order.WxUserId, totalCent, source, order.OrderId, remark)
 			}
 		}
+		//更新促销减去金额
+		if len(promotionsArr) > 0 {
+			promotions := tool.CombineInt64ToString(promotionsArr)
+			noinvoves := tool.CombineInt64ToString(noInovceArr)
+			go order_model.UpdateOrderPromotions(promotions, noinvoves, orderId)
+		}
 	}
 }
 

+ 98 - 49
go/gopath/src/fohow.com/apps/models/order_model/order.go

@@ -84,55 +84,57 @@ var ORDER_TYPE_CN_TEXT = map[int64]string{
 }
 
 type Order struct {
-	Id             int64              `orm:"column(id);pk"                                       json:"id"`       // int(11)
-	OrderId        string             `orm:"column(order_id)"                                    json:"order_id"` // varchar(255)
-	OrderType      int64              `orm:"column(order_type)"                                  json:"order_type"`
-	WxUserId       int64              `orm:"column(wx_user_id)"                                  json:"wx_user_id"`
-	UserId         int64              `orm:"column(user_id)"                                     json:"user_id"`          // int(11)
-	TotalPrice     int64              `orm:"column(total_price)"                                 json:"total_price"`      // int(11)
-	PaiedPrice     int64              `orm:"column(paied_price);null"                            json:"paied_price"`      // int(11)
-	CouponPrice    int64              `orm:"column(coupon_price);null"                           json:"coupon_price"`     // int(11)
-	CentPrice      int64              `orm:"column(cent_price);null"                             json:"cent_price"`       // int(11)
-	BuyPrice       int64              `orm:"column(buy_price);null"                              json:"-"`                // int(11)
-	Count          int64              `orm:"column(count);null"                                  json:"count"`            // int(11)
-	PaiedAt        int64              `orm:"column(paied_at);null"                               json:"paied_at"`         // int(11)
-	TradeNo        string             `orm:"column(trade_no);null"                               json:"trade_no"`         // varchar(255)
-	Status         string             `orm:"column(status);null"                                 json:"status"`           // varchar(255)
-	StatusCn       string             `orm:"-"                                                   json:"status_cn"`        // varchar(255)
-	PayWay         string             `orm:"column(pay_way);null"                                json:"pay_way"`          // varchar(255)
-	Source         string             `orm:"column(source);null"                                 json:"source"`           // varchar(255)
-	Tel            string             `orm:"column(tel);null"                                    json:"tel"`              // varchar(255)
-	Address        string             `orm:"column(address);null"                                json:"address"`          // varchar(255)
-	Contact        string             `orm:"column(contact);null"                                json:"contact"`          // varchar(255)
-	ExpressCompany string             `orm:"column(express_company);null"                        json:"express_company"`  // varchar(255)
-	ExpressOrderNo string             `orm:"column(express_order_no);null"                       json:"express_order_no"` // varchar(255)
-	Remark         string             `orm:"column(remark);null"                                 json:"remark"`           // varchar(255)
-	OrderRemark    string             `orm:"column(order_remark);null"                           json:"-"`                // varchar(255)
-	ExpressCode    string             `orm:"column(express_code);null"                           json:"express_code"`     // varchar(255)
-	Sign           string             `orm:"-"                                                   json:"sign"`             // varchar(255)
-	CTime          int64              `orm:"-"                                 json:"ctime"`                              // int(11)
-	DTime          int64              `orm:"-"                                 json:"dtime"`                              // int(11)
-	Freight        int64              `orm:"column(freight);null"                            json:"freight"`              // int(11)
-	Depart         int64              `orm:"column(depart)" json:"-"`                                                     // datetime
-	Pv             int64              `orm:"column(pv)"            json:"pv"`                                             // varchar(255)
-	PickWay        int64              `orm:"column(pick_way)"            json:"pick_way"`                                 // 物流方式
-	PickDept       int64              `orm:"column(pick_dept)"           json:"pick_dept"`                                // 自提部门
-	AddressId      int64              `orm:"column(address_id)"          json:"address_id"`                               // 地址ID
-	DispatchTime   time.Time          `orm:"column(dispatch_time);null;type(datetime)"           json:"dispatch_time"`
-	ReceiveTime    time.Time          `orm:"column(receive_time);null;type(datetime)"            json:"receive_time"`
-	PaiedTime      time.Time          `orm:"column(paied_time);null;type(datetime)"              json:"paied_time"` // int(11)
-	HasCommission  bool               `orm:"has_commission"                                      json:"-"`
-	CreatedAt      time.Time          `orm:"column(created_at);null;auto_now_add;type(datetime)" json:"-"`            // datetime
-	UpdatedAt      time.Time          `orm:"column(updated_at);null;auto_now;type(datetime)"     json:"-"`            // datetime
-	Promotions     string             `orm:"column(promotions);null"                           json:"-"`              // varchar(255)
-	SpecialPro     bool               `orm:"-"                                 json:"special_promotion"`              // int(11)
-	ShutWechat     bool               `orm:"-"                                 json:"shut_wechat"`                    // int(11)
-	PaiedSilver    int64              `orm:"column(paied_silver)"                                json:"paied_silver"` // int(11)
-	TotalSilver    int64              `orm:"column(total_silver)"                                json:"total_silver"` // int(11)
-	DisAmount      int64              `orm:"column(dis_amount)"                                  json:"dis_amount"`   // 折扣
-	PaiedCash      int64              `orm:"column(paied_cash);null"                      json:"paied_cash"`          // int(11)
-	WxUser         *user_model.WxUser `orm:"-"                                 json:"wx_user"`
-	ProductList    []*OrderDetail     `orm:"-"                                 json:"product_list"` // varchar(255)
+	Id                 int64              `orm:"column(id);pk"                                       json:"id"`       // int(11)
+	OrderId            string             `orm:"column(order_id)"                                    json:"order_id"` // varchar(255)
+	OrderType          int64              `orm:"column(order_type)"                                  json:"order_type"`
+	WxUserId           int64              `orm:"column(wx_user_id)"                                  json:"wx_user_id"`
+	UserId             int64              `orm:"column(user_id)"                                     json:"user_id"`          // int(11)
+	TotalPrice         int64              `orm:"column(total_price)"                                 json:"total_price"`      // int(11)
+	PaiedPrice         int64              `orm:"column(paied_price);null"                            json:"paied_price"`      // int(11)
+	CouponPrice        int64              `orm:"column(coupon_price);null"                           json:"coupon_price"`     // int(11)
+	CentPrice          int64              `orm:"column(cent_price);null"                             json:"cent_price"`       // int(11)
+	BuyPrice           int64              `orm:"column(buy_price);null"                              json:"-"`                // int(11)
+	Count              int64              `orm:"column(count);null"                                  json:"count"`            // int(11)
+	PaiedAt            int64              `orm:"column(paied_at);null"                               json:"paied_at"`         // int(11)
+	TradeNo            string             `orm:"column(trade_no);null"                               json:"trade_no"`         // varchar(255)
+	Status             string             `orm:"column(status);null"                                 json:"status"`           // varchar(255)
+	StatusCn           string             `orm:"-"                                                   json:"status_cn"`        // varchar(255)
+	PayWay             string             `orm:"column(pay_way);null"                                json:"pay_way"`          // varchar(255)
+	Source             string             `orm:"column(source);null"                                 json:"source"`           // varchar(255)
+	Tel                string             `orm:"column(tel);null"                                    json:"tel"`              // varchar(255)
+	Address            string             `orm:"column(address);null"                                json:"address"`          // varchar(255)
+	Contact            string             `orm:"column(contact);null"                                json:"contact"`          // varchar(255)
+	ExpressCompany     string             `orm:"column(express_company);null"                        json:"express_company"`  // varchar(255)
+	ExpressOrderNo     string             `orm:"column(express_order_no);null"                       json:"express_order_no"` // varchar(255)
+	Remark             string             `orm:"column(remark);null"                                 json:"remark"`           // varchar(255)
+	OrderRemark        string             `orm:"column(order_remark);null"                           json:"-"`                // varchar(255)
+	ExpressCode        string             `orm:"column(express_code);null"                           json:"express_code"`     // varchar(255)
+	Sign               string             `orm:"-"                                                   json:"sign"`             // varchar(255)
+	CTime              int64              `orm:"-"                                 json:"ctime"`                              // int(11)
+	DTime              int64              `orm:"-"                                 json:"dtime"`                              // int(11)
+	Freight            int64              `orm:"column(freight);null"                            json:"freight"`              // int(11)
+	Depart             int64              `orm:"column(depart)" json:"-"`                                                     // datetime
+	Pv                 int64              `orm:"column(pv)"            json:"pv"`                                             // varchar(255)
+	PickWay            int64              `orm:"column(pick_way)"            json:"pick_way"`                                 // 物流方式
+	PickDept           int64              `orm:"column(pick_dept)"           json:"pick_dept"`                                // 自提部门
+	AddressId          int64              `orm:"column(address_id)"          json:"address_id"`                               // 地址ID
+	DispatchTime       time.Time          `orm:"column(dispatch_time);null;type(datetime)"           json:"dispatch_time"`
+	ReceiveTime        time.Time          `orm:"column(receive_time);null;type(datetime)"            json:"receive_time"`
+	PaiedTime          time.Time          `orm:"column(paied_time);null;type(datetime)"              json:"paied_time"` // int(11)
+	HasCommission      bool               `orm:"has_commission"                                      json:"-"`
+	CreatedAt          time.Time          `orm:"column(created_at);null;auto_now_add;type(datetime)" json:"-"`            // datetime
+	UpdatedAt          time.Time          `orm:"column(updated_at);null;auto_now;type(datetime)"     json:"-"`            // datetime
+	Promotions         string             `orm:"column(promotions);null"                           json:"-"`              // varchar(255)
+	SpecialPro         bool               `orm:"-"                                 json:"special_promotion"`              // int(11)
+	ShutWechat         bool               `orm:"-"                                 json:"shut_wechat"`                    // int(11)
+	PaiedSilver        int64              `orm:"column(paied_silver)"                                json:"paied_silver"` // int(11)
+	TotalSilver        int64              `orm:"column(total_silver)"                                json:"total_silver"` // int(11)
+	DisAmount          int64              `orm:"column(dis_amount)"                                  json:"dis_amount"`   // 折扣
+	PaiedCash          int64              `orm:"column(paied_cash);null"                      json:"paied_cash"`          // int(11)
+	NormalPromotions   string             `orm:"column(normal_promotions);null"                           json:"-"`       // varchar(255)
+	NotPromotionAmount string             `orm:"column(not_promotion_amount);null"                           json:"-"`    // varchar(255)
+	WxUser             *user_model.WxUser `orm:"-"                                 json:"wx_user"`
+	ProductList        []*OrderDetail     `orm:"-"                                 json:"product_list"` // varchar(255)
 }
 
 func (self *Order) TableName() string {
@@ -909,3 +911,50 @@ func GenExpressPassword(expressNo string) string {
 
 	return md5PwdTkey[0:12]
 }
+
+func GetNotInvovedAmount(promotionId int64, orderId string) int64 {
+
+	type PromotionPds struct {
+		ProductId int64 `orm:"column(product_id)"`
+		Nums      int64 `orm:"column(nums)"`
+	}
+
+	sql := `
+		SELECT
+			op.product_id,
+			IF(od.nums IS NULL, 0, od.nums) AS nums
+		FROM
+			products_promotions AS op
+		LEFT JOIN
+			order_details AS od ON od.product_id = op.product_id AND od.order_no =?
+		WHERE
+			op.promotion_id = ?;
+    	AND IF(od.nums IS NULL, 0, od.nums) > 0;
+		`
+
+	var results []PromotionPds
+	db := orm.NewOrm()
+	_, err := db.Raw(sql, promotionId, orderId).QueryRows(&results)
+	if err != nil {
+		beego.BeeLogger.Debug("GetNotInvovedAmount err=%s", err)
+		return 0
+	}
+	total := int64(0)
+	for _, item := range results {
+		item_pd := product_model.GetProductById(item.ProductId, true)
+		total += item.Nums * item_pd.Price
+	}
+
+	return total
+}
+
+// UpdateOrderPromotions 更新 normal_promotions字段
+func UpdateOrderPromotions(promotions, noInvovedAmount, orderId string) {
+	o := orm.NewOrm()
+	//更新wx_users会员关系
+	sql := `
+		update orders set normal_promotions=? ,not_promotion_amount=? where order_id=?
+	`
+	o.Raw(sql, promotions, noInvovedAmount, orderId).Exec()
+
+}

+ 18 - 1
go/gopath/src/fohow.com/libs/tool/string.go

@@ -34,7 +34,7 @@ func ConvertNumToCny(num float64) string {
 	strnum := strconv.FormatFloat(num*100, 'f', 0, 64)
 	sliceUnit := []string{"仟", "佰", "拾", "亿", "仟", "佰", "拾", "万", "仟", "佰", "拾", "元", "角", "分"}
 	// log.Println(sliceUnit[:len(sliceUnit)-2])
-	s := sliceUnit[len(sliceUnit)-len(strnum) : len(sliceUnit)]
+	s := sliceUnit[len(sliceUnit)-len(strnum):]
 	upperDigitUnit := map[string]string{"0": "零", "1": "壹", "2": "贰", "3": "叁", "4": "肆", "5": "伍", "6": "陆", "7": "柒", "8": "捌", "9": "玖"}
 	str := ""
 	for k, v := range strnum[:] {
@@ -86,3 +86,20 @@ func StringsContains(array []string, val string) (index int) {
 	}
 	return index
 }
+
+func AddToString(str string, values ...string) string {
+	values = append(values, str)        // 将字符串追加到值列表中
+	result := strings.Join(values, ",") // 使用逗号连接值
+	return result
+}
+
+func CombineInt64ToString(numbers []int64) string {
+	strNumbers := make([]string, len(numbers))
+
+	for i, num := range numbers {
+		strNumbers[i] = strconv.FormatInt(num, 10)
+	}
+
+	result := strings.Join(strNumbers, ",")
+	return result
+}