|
@@ -5,6 +5,7 @@ import (
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"log"
|
|
|
+ "math"
|
|
|
. "mongodb"
|
|
|
"pay"
|
|
|
qutil "qfw/util"
|
|
@@ -82,11 +83,13 @@ func (this *jyCourseOnline) PayCallBack(param *CallBackParam) bool {
|
|
|
|
|
|
//生成订单
|
|
|
func (this *jyCourseOnline) NewOrder(param frpc.JyPayOrderParam) (ordercode string, err error) {
|
|
|
+ log.Println("p:--------", param)
|
|
|
productSign, ok := pay.PayWayAndSign["onlineCourse"][param.PayWay]
|
|
|
// 获取分销口令
|
|
|
disWordStr := ""
|
|
|
+ userid := param.Userid
|
|
|
// 查询用户携带的分销口令
|
|
|
- userfilter, ok := util.MQFW.FindById("user", param.Userid, "")
|
|
|
+ userfilter, ok := util.MQFW.FindById("user", userid, "")
|
|
|
log.Println("用户查询结果:", ok, userfilter)
|
|
|
if ok && userfilter != nil {
|
|
|
startTime := qutil.Int64All((*userfilter)["startTime"])
|
|
@@ -109,14 +112,36 @@ func (this *jyCourseOnline) NewOrder(param frpc.JyPayOrderParam) (ordercode stri
|
|
|
}
|
|
|
//插入数据库
|
|
|
now := time.Now()
|
|
|
- ordercode = pay.GetOrderCode(param.Userid)
|
|
|
+ ordercode = pay.GetOrderCode(userid)
|
|
|
detailByte, marshalErr := json.Marshal(param.Detail)
|
|
|
if marshalErr != nil {
|
|
|
err = marshalErr
|
|
|
return
|
|
|
}
|
|
|
+ original_price, discount_price := param.Price, 0 //原价,优惠价格
|
|
|
+ totalfee := param.Price
|
|
|
+ lotteryId := param.LotteryId
|
|
|
+ userLotteryId := "" //使用的卡券
|
|
|
+ if lotteryId != "" {
|
|
|
+ lotteryId = qutil.SE.Encode2Hex(lotteryId)
|
|
|
+ products := param.UseProduct
|
|
|
+ full_price, reduce_price, discount, userLotteryId_ := util.GetCouponInfo(userid, lotteryId, products)
|
|
|
+ userLotteryId = userLotteryId_
|
|
|
+ if full_price <= totalfee {
|
|
|
+ if discount == 100 { //满减
|
|
|
+ totalfee = totalfee - reduce_price
|
|
|
+ discount_price = reduce_price
|
|
|
+ } else { //满折
|
|
|
+ disCount_int := int(math.Ceil(discount * 100))
|
|
|
+ discount_price = totalfee - totalfee*disCount_int/1000
|
|
|
+ totalfee = totalfee * disCount_int / 1000
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ userLotteryId = ""
|
|
|
+ }
|
|
|
+ }
|
|
|
insertMap := map[string]interface{}{
|
|
|
- "order_money": param.Price,
|
|
|
+ "order_money": totalfee,
|
|
|
"order_status": 0,
|
|
|
"user_openid": param.Openid,
|
|
|
"prepay_id": prepayid,
|
|
@@ -126,13 +151,14 @@ func (this *jyCourseOnline) NewOrder(param frpc.JyPayOrderParam) (ordercode stri
|
|
|
"product_type": "中标必听课",
|
|
|
"create_time": qutil.FormatDate(&now, qutil.Date_Full_Layout),
|
|
|
"prepay_time": qutil.FormatDate(&now, qutil.Date_Full_Layout),
|
|
|
- "original_price": param.Price,
|
|
|
+ "original_price": original_price,
|
|
|
"filter_id": qutil.If(param.Detail != nil && param.Detail["_id"] != nil, param.Detail["_id"], ""), //课程id
|
|
|
"pay_way": param.PayWay,
|
|
|
- "user_id": param.Userid,
|
|
|
+ "user_id": userid,
|
|
|
"filter": string(detailByte), //订单详情
|
|
|
"dis_word": disWordStr, //分销口令
|
|
|
-
|
|
|
+ "discount_price": discount_price,
|
|
|
+ "d_relation_id": userLotteryId,
|
|
|
}
|
|
|
if param.Phone != "" {
|
|
|
insertMap["user_phone"] = param.Phone
|
|
@@ -143,6 +169,15 @@ func (this *jyCourseOnline) NewOrder(param frpc.JyPayOrderParam) (ordercode stri
|
|
|
err = errors.New("订单插入失败")
|
|
|
return
|
|
|
}
|
|
|
+ //绑定卡卷
|
|
|
+ if userLotteryId != "" && lotteryId != "" {
|
|
|
+ go func(userId, userLotteryId, order_code string) {
|
|
|
+ phone, nickname := util.GetMyPhoneAndName(userId)
|
|
|
+ if !util.UpdateCouponState(userId, userLotteryId, nickname, phone, order_code, "中标必听课", "", 3, 0) {
|
|
|
+ log.Println(fmt.Sprintf("单号%s-绑定失败-卡卷%s", order_code, userLotteryId))
|
|
|
+ }
|
|
|
+ }(userid, userLotteryId, ordercode)
|
|
|
+ }
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -193,6 +228,16 @@ func (this *jyCourseOnline) CloseOrder(ordercode string) error {
|
|
|
}, map[string]interface{}{
|
|
|
"order_status": -2,
|
|
|
}) {
|
|
|
+ res := util.Mysql.FindOne("dataexport_order", map[string]interface{}{
|
|
|
+ "order_status": ordercode,
|
|
|
+ }, "id,order_code,create_time,pay_way,out_trade_no,prepay_time,order_status,discount_price,d_relation_id,user_id,filter,product_type", "")
|
|
|
+ user_id := qutil.ObjToString((*res)["user_id"])
|
|
|
+ userLotteryId := qutil.ObjToString((*res)["d_relation_id"])
|
|
|
+ go func(userId, userLotteryId, order_code string) {
|
|
|
+ if !util.UpdateCouponState(userId, userLotteryId, "", "", order_code, qutil.ObjToString((*res)["product_type"]), "0", 0, 1) {
|
|
|
+ log.Println(fmt.Sprintf("单号%s-解绑失败-卡卷%s", order_code, userLotteryId))
|
|
|
+ }
|
|
|
+ }(user_id, userLotteryId, ordercode)
|
|
|
return errors.New(fmt.Sprintf("%s关闭订单更新数据库出错", ordercode))
|
|
|
}
|
|
|
return nil
|