Pārlūkot izejas kodu

红包业务逻辑修改

shizhongqi 6 mēneši atpakaļ
vecāks
revīzija
9ac9b3ba9c

+ 1 - 0
app-hd/controllers/HbController.php

@@ -155,6 +155,7 @@ class HbController extends BaseController
 
     /**
      * 更新红包
+     *  - 更新方法包含了:红包退回及其它更新
      */
     public function actionUpdateHb()
     {

+ 30 - 17
app-hd/controllers/OrderController.php

@@ -786,8 +786,38 @@ class OrderController extends BaseController
                 }
             }
 
+            //红包使用
+            $hbId = $post['hbId'] ?? 0;
+            $hb = null;
+            if ($hbId > 0) { // 不使用 $hbId =! 0,因为要用负数来表示红包已取消
+                $hb = HbClass::getById($hbId, true);
+                if (!empty($hb)) {
+                    if ($hb->amount != $post['hbAmount']) {
+                        util::fail('红包金额不相等'); //红包数据可能被串改
+                    }
+
+                    if ($hb->status == 1 || $hb->status == -1) {
+                        util::fail($hb->status == 1 ? '红包已使用' : '红包已作废');
+                    }
+                    if ($hb->endTime < time()) {
+                        util::fail('红包已过期');
+                    }
+                }else{
+                    Yii::error('没有找到红包,hbId: ', $hbId . ' ' . __METHOD__);
+                    util::fail('没有找到红包,hbId: ' . $hbId);
+                }
+            }
+
             //多处调用这个方法,注意同步修改,搜索关键词 createHdNewOrder
             $return = OrderService::createHdOrder($post, $custom, $hasPay);
+
+            if($hb != null){
+                $hb->status = 1;
+                $hb->orderId = $return->id;
+                $hb->save();
+            }
+
+
             $staff = $this->shopAdmin;
             //售后开付款单,库存要变化,关系要建立,多处需要同步修改 sh_pay_change ssh
             OrderClass::shPayChange($return, $shop, $staff);
@@ -814,23 +844,6 @@ class OrderController extends BaseController
                 $order->save();
             }
 
-            //红包使用
-            $hbId = $post['hbId'] ?? 0;
-            if ($hbId > 0) { // 不使用 $hbId =! 0,因为要用负数来表示红包已取消
-                $hb = HbClass::getById($hbId, true);
-                if (!empty($hb)) {
-                    if ($hb->status == 1 || $hb->status == -1) {
-                        util::fail($hb->status == 1 ? '红包已使用' : '红包已作废');
-                    }
-                    $hb->status = 1;
-                    $hb->orderId = $id;
-                    $hb->save();
-                }else{
-                    Yii::error('没有找到红包,hbId: ', $hbId . ' ' . __METHOD__);
-                    util::fail('没有找到红包,hbId: ' . $hbId);
-                }
-            }
-
             $transaction->commit();
 
             if ($order->fromType == 3 || $order->fromType == 4) {

+ 31 - 16
app-mall/controllers/OrderController.php

@@ -412,9 +412,39 @@ class OrderController extends BaseController
                 $modifyPrice = bcadd($modifyPrice, $sendCost, 2);
             }
 
+            //红包使用
+            $hbId = $post['hbId'] ?? 0;
+            $hb = null;
+            if ($hbId > 0) { // 不使用 $hbId =! 0,因为要用负数来表示红包已取消
+                $hb = HbClass::getById($hbId, true);
+                if (!empty($hb)) {
+                    if ($hb->amount != $post['hbAmount']) {
+                        util::fail('红包金额不相等'); //红包数据可能被串改
+                    }
+
+                    if ($hb->status == 1 || $hb->status == -1) {
+                        util::fail($hb->status == 1 ? '红包已使用' : '红包已作废');
+                    }
+                    if ($hb->endTime < time()) {
+                        util::fail('红包已过期');
+                    }
+                    $modifyPrice = bcsub($modifyPrice, $hb->amount, 2);//扣减红包
+                }else{
+                    Yii::error('没有找到红包,hbId', $hbId . ' ' . __METHOD__);
+                    util::fail('没有找到红包,hbId: ' . $hbId);
+                }
+            }
+
             $post['modifyPrice'] = $modifyPrice;
             //多处调用这个方法,注意同步修改,搜索关键词 createHdNewOrder
             $return = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
+
+            if($hb != null){
+                $hb->status = 1;
+                $hb->orderId = $return->id;
+                $hb->save();
+            }
+
             if (isset($return->shopId)) {
                 if ($return->shopId == 7610) {
                     if ($return->actPrice < 200) {
@@ -430,22 +460,7 @@ class OrderController extends BaseController
                     util::fail('本店暂不能使用跑腿呢');
                 }
             }
-            //红包使用
-            $hbId = $post['hbId'] ?? 0;
-            if ($hbId > 0) { // 不使用 $hbId =! 0,因为要用负数来表示红包已取消
-                $hb = HbClass::getById($hbId, true);
-                if (!empty($hb)) {
-                    if ($hb->status == 1 || $hb->status == -1) {
-                        util::fail($hb->status == 1 ? '红包已使用' : '红包已作废');
-                    }
-                    $hb->status = 1;
-                    $hb->orderId = $return->id;
-                    $hb->save();
-                }else{
-                    Yii::error('没有找到红包,hbId', $hbId . ' ' . __METHOD__);
-                    util::fail('没有找到红包,hbId: ' . $hbId);
-                }
-            }
+
             $transaction->commit();
             
             $orderSn = $return->orderSn ?? '';

+ 11 - 2
biz-hd/hb/classes/HbClass.php

@@ -40,13 +40,22 @@ class HbClass extends BaseClass
     /**
      * 红包退回
      * @param $id
+     * @param string $returnType 出错时的返回方式:fail - 使用 util::fail(), return - return返回
+     * @return bool
+     * @throws \Exception
      */
-    public static function hbBack($id)
+    public static function hbBack($id, $returnType='fail')
     {
         $hb = self::getById($id, true);
         if (empty($hb)) {
             Yii::error('没有找到红包', __METHOD__);
-            util::fail('没有找到红包');
+            if($returnType == 'fail'){
+                util::fail('没有找到红包');
+            }elseif($returnType == 'return'){
+                return false;
+            }else{
+                new \Exception('没有找到红包' . $id);
+            }
         }
         $hb->status = 0;
         $hb->orderId = 0;

+ 11 - 0
biz-hd/order/classes/OrderClass.php

@@ -13,6 +13,7 @@ use bizGhs\order\classes\CheckOrderClass;
 use bizGhs\product\classes\ProductClass;
 use bizGhs\stock\classes\StockRecordClass;
 use bizHd\custom\classes\CustomClass;
+use bizHd\hb\classes\HbClass;
 use bizHd\item\classes\ItemClass;
 use bizHd\merchant\classes\SjClass;
 use bizHd\refund\classes\HdRefundClass;
@@ -636,6 +637,16 @@ class OrderClass extends BaseClass
         if ($order->status != 1) {
             return false;
         }
+
+        // 红包退回
+        if($order->hbId > 0){ // 不使用 $order->hbId =! 0,因为要用负数来表示红包已取消
+            HbClass::hbBack($order->hbId, 'return');
+        }
+
+        // hbId变更为负数,表示红包已退回(取消)
+        if($order->hbId > 0) {
+            $order->hbId = -$order->hbId;
+        }
         $order->status = 5;
         $order->cancelTime = date("Y-m-d H:i:s");
         $order->save();

+ 4 - 2
biz-hd/order/services/OrderService.php

@@ -8,7 +8,6 @@ use biz\sj\services\SjCapitalService;
 use biz\sj\services\MerchantService;
 use bizHd\custom\classes\CustomClass;
 use bizHd\goods\classes\GoodsClass;
-use bizHd\hb\classes\HbClass;
 use bizHd\order\classes\OrderClass;
 use bizHd\order\classes\OrderGoodsClass;
 use bizHd\order\classes\OrderItemClass;
@@ -219,7 +218,6 @@ class OrderService extends BaseService
 
                 //库存变化在这里
                 OrderItemClass::addData($currentItemData, $custom);
-
             }
         }
         //运费
@@ -234,6 +232,10 @@ class OrderService extends BaseService
         //美团服务费和手续费
         $serviceFee = isset($data['serviceFee']) && $data['serviceFee'] > 0 ? $data['serviceFee'] : 0;
         $totalPrice = bcadd($totalPrice, $serviceFee, 2);
+        //扣除红包的减免
+        $hbAmount = isset($data['hbAmount']) && $data['hbAmount'] > 0 ? $data['hbAmount'] : 0;
+        $totalPrice = bcsub($totalPrice, $hbAmount, 2);
+
         $modifyPrice = isset($data['modifyPrice']) && $data['modifyPrice'] > 0 ? $data['modifyPrice'] : 0;
         //优惠
         if ($modifyPrice < $totalPrice) {

+ 4 - 3
biz-hd/recharge/classes/RechargeClass.php

@@ -190,6 +190,7 @@ class RechargeClass extends BaseClass
                     }
                 }
                 if (!empty($maxHbRule)) {
+                    $currentTime = time();
                     //给用户生成红包
                     $data = [
                         'mainId' => $mainId,
@@ -198,9 +199,9 @@ class RechargeClass extends BaseClass
                         'customId' => $customId,
                         'amount' => $maxHbRule['hbAmount'],
                         'minConsume' => $maxHbRule['miniCost'],
-                        'beginTime' => time(),
-                        'endTime' => 4102416000,
-                        'willTime' => 4102416000,
+                        'beginTime' => $currentTime,
+                        'endTime' => $maxHbRule['duration'] == 0 ? 4102416000 : ($currentTime + 86400 * $maxHbRule['duration']),
+                        'willTime' => $maxHbRule['duration'] == 0 ? 4102416000 : ($currentTime + 86400 * $maxHbRule['duration']),
                         'duration' => $maxHbRule['duration'],
                         'getType' => 2, //取得方式 0新人领取 1门店发放 2充值赠送
                         'remark' => '充值赠送红包',

+ 3 - 13
console/controllers/OrderController.php

@@ -105,25 +105,15 @@ class OrderController extends Controller
         $query->where(['status' => 1])->andWhere(['>', 'deadline', $startTime])->andWhere(['<=', 'deadline', $endTime]);
         $query->orderBy('deadline ASC');
         foreach ($query->batch() as $batchOrder) {
-            foreach ($batchOrder as $order) {
+            foreach ($batchOrder as $orderItem) {
                 $connection = Yii::$app->db;
                 $transaction = $connection->beginTransaction();
-                $id = $order['id'] ?? 0;
-                $orderSn = $order['orderSn'] ?? '';
+                $id = $orderItem['id'] ?? 0;
+                $orderSn = $orderItem['orderSn'] ?? '';
                 try {
                     $order = OrderClass::getById($id, true);
                     //暂时只考虑花材库存的回滚
                     OrderClass::setExpire($order);
-                    if($order['hbId'] > 0){ // 不使用 $order['hbId'] =! 0,因为要用负数来表示红包已取消
-                        $hb = HbClass::getById($id, true);
-                        if (empty($hb)) {
-                            Yii::error('没有找到红包', __METHOD__);
-                            continue;
-                        }
-                        $hb->status = 0;
-                        $hb->orderId = 0;
-                        $hb->save();
-                    }
                     $transaction->commit();
                     //noticeUtil::push("零售订单未付款,库存已回滚,单号:{$orderSn}");
                 } catch (\Exception $e) {