Sfoglia il codice sorgente

app-mall 修改 order/create-order 接口,实现红包功能

shizhongqi 6 mesi fa
parent
commit
93282c1c2b
1 ha cambiato i file con 45 aggiunte e 0 eliminazioni
  1. 45 0
      app-mall/controllers/OrderController.php

+ 45 - 0
app-mall/controllers/OrderController.php

@@ -435,6 +435,9 @@ class OrderController extends BaseController
                         $hb->save();
                         util::fail('红包已失效');
                     }
+                    if ($modifyPrice < $hb->minConsume) {
+                        util::fail("不满足最低消费金额{$hb->minConsume}元");
+                    }
                     $modifyPrice = bcsub($modifyPrice, $hb->amount, 2);//扣减红包
                 } else {
                     Yii::error('没有找到红包,hbId', $hbId . ' ' . __METHOD__);
@@ -726,6 +729,40 @@ class OrderController extends BaseController
             $modifyPrice = stringUtil::calcAdd($sendCost, $goodsPrice);
             //非门店订单
             $post['store'] = 0;
+
+            //红包使用,多有处,关键词 use_hb_action
+            $hbId = $post['hbId'] ?? 0;
+            $hb = null;
+            if ($hbId > 0) { // 不使用 $hbId =! 0,因为要用负数来表示红包已取消
+                $hb = HbClass::getById($hbId, true);
+                if (!empty($hb)) {
+                    if ($hb->customId != $customId) {
+                        util::fail('不是你的红包');
+                    }
+                    if ($hb->amount != $post['hbAmount']) {
+                        util::fail('红包金额不相等'); //红包数据可能被串改
+                    }
+                    if ($hb->status == 1) {
+                        util::fail('红包已使用');
+                    }
+                    if ($hb->status == -1) {
+                        util::fail('红包已作废');
+                    }
+                    if ($hb->endTime < time()) {
+                        $hb->status = -1;
+                        $hb->save();
+                        util::fail('红包已失效');
+                    }
+                    if ($modifyPrice < $hb->minConsume) {
+                        util::fail("不满足最低消费金额{$hb->minConsume}元");
+                    }
+                    $modifyPrice = bcsub($modifyPrice, $hb->amount, 2);//扣减红包
+                } else {
+                    Yii::error('没有找到红包,hbId', $hbId . ' ' . __METHOD__);
+                    util::fail('红包无效');
+                }
+            }
+
             $post['modifyPrice'] = $modifyPrice;
             //非自取订单考虑配送时间
             $post['reachDate'] = !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
@@ -749,6 +786,14 @@ class OrderController extends BaseController
             $product = [['productId' => $goodsId, 'unitType' => 0, 'property' => 0, 'unitPrice' => $unitPrice, 'num' => $goodsNum]];
             $post['product'] = $product;
             $return = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
+
+            //红包保存订单id
+            if (!empty($hb)) {
+                $hb->status = 1;
+                $hb->orderId = $return->id;
+                $hb->save();
+            }
+            
             $orderId = $return->id;
             $orderSn = $return->orderSn;
             $actPrice = $return->actPrice ?? 0;