Browse Source

红包数据保存与更新的调整与bug修复

shizhongqi 6 months ago
parent
commit
1a3c02ebb4

+ 56 - 19
app-hd/controllers/HbController.php

@@ -7,6 +7,7 @@ use biz\market\classes\XrFlClass;
 use biz\shop\classes\ShopClass;
 use bizGhs\custom\classes\CustomClass;
 use bizHd\hb\classes\HbManageClass;
+use bizHd\order\classes\OrderClass;
 use bizHd\recharge\classes\RechargeShbClass;
 use common\components\util;
 use bizHd\hb\classes\HbClass;
@@ -171,28 +172,64 @@ class HbController extends BaseController
             util::fail('不是本门店的红包');
         }
 
-        // 仅更新允许字段,且只更新请求中实际传入的字段(避免用 null 覆盖原值)
-        $allowFields = array_keys($form->attributes);
-        foreach ($allowFields as $field) {
-            if ($field === 'id') {
-                continue;
+        $connection = Yii::$app->db;
+        $transaction = $connection->beginTransaction();
+
+        try {
+            //当是退回红包时,必须检测对应订单 actPrice 为0
+            if (isset($form->status) && $form->status == 0){
+                if($hb->status == 1){
+                    //查询订单数据,校验 actPrice,按情况更新
+                    $order = OrderClass::getById($form->orderId, true, 'id, hbId, hbAmount, actPrice');
+                    if($order->hbId == $id){
+                        if($order->actPrice == 0.00){
+                            //更新订单数据
+                            $order->hbId = -$order->hbId;
+                            $order->save();
+                        } else {
+                            Yii::error('使用红包的订单 actPrice 不等于0,无法执行红包退回。hbId=' . $id);
+                            util::fail('使用红包的订单 actPrice 不等于0,无法执行红包退回');
+                        }
+                    }else{
+                        Yii::error("订单中的红包id与当前红包不匹配。hbId={$id}, orderId={$form->orderId}");
+                        util::fail('订单中的红包id与当前红包不匹配');
+                    }
+                }else{
+                    Yii::error("当前红包状态不是已使用,不能变更未使用。hbId={$id}");
+                    util::fail("当前红包状态不是已使用,不能变更未使用");
+                }
             }
-            if (array_key_exists($field, $rawPost)) {
-                $hb->$field = $form->$field;
+
+            // 仅更新允许字段,且只更新请求中实际传入的字段(避免用 null 覆盖原值)
+            $allowFields = array_keys($form->attributes);
+            foreach ($allowFields as $field) {
+                if ($field === 'id') {
+                    continue;
+                }
+                if (array_key_exists($field, $rawPost)) {
+                    $hb->$field = $form->$field;
+                }
             }
+            // 当操作作废时,status改为-1,同时将endTime改为当前时间
+            if (isset($form->status) && $form->status == -1 && $hb->status != -1) {
+                $hb->endTime = time();
+                //记录作废人
+                $updateData = [
+                    'cancelStaffId' => $this->shopAdminId,
+                    'cancelStaffName' => $this->shopAdminName,
+                    'cancelTime' => date('Y-m-d H:i:s'),
+                ];
+                HbManageClass::updateByCondition(['hbId' => $id], $updateData);
+            }
+            $hb->save();
+            $transaction->commit();
+        } catch (\Exception $e) {
+            $transaction->rollBack();
+            $msg = $e->getMessage();
+            Yii::error('actionUpdateHb', $msg);
+            util::fail($msg);
         }
-        // 当操作作废时,status改为-1,同时将endTime改为当前时间
-        if ($hb->status == -1) {
-            $hb->endTime = time();
-            //记录作废人
-            $updateData = [
-                'cancelStaffId' => $this->shopAdminId,
-                'cancelStaffName' => $this->shopAdminName,
-                'cancelTime' => date('Y-m-d H:i:s'),
-            ];
-            HbManageClass::updateByCondition(['hbId' => $id], $updateData);
-        }
-        $hb->save();
+
         util::success($hb->attributes);
     }
 

+ 1 - 1
app-hd/controllers/OrderController.php

@@ -816,7 +816,7 @@ class OrderController extends BaseController
 
             //红包使用
             $hbId = $post['hbId'] ?? 0;
-            if ($hbId != 0) {
+            if ($hbId > 0) { // 不使用 $hbId =! 0,因为要用负数来表示红包已取消
                 $hb = HbClass::getById($hbId, true);
                 if (!empty($hb)) {
                     if ($hb->status == 1 || $hb->status == -1) {

+ 9 - 2
app-hd/controllers/RefundController.php

@@ -267,8 +267,15 @@ class RefundController extends BaseController
             //红包退款
             $refundHb = $post['refundHb'] ?? 0;
             $hbId = $order->hbId;
-            if ($refundHb == 1 && $hbId != 0 && bccomp($post['price'], $order->orderPrice, 2) == 0) {
-                hbClass::hbBack($order['hbId']);
+            if ($refundHb == 1 && $hbId > 0) { // 不使用 $hbId =! 0,因为要用负数来表示红包已取消
+                $updatedOrder = OrderClass::getById($id, true, 'id, actPrice, hbId, hbAmount');
+                if($updatedOrder->actPrice == 0.00){
+                    hbClass::hbBack($order['hbId']);
+
+                    $updatedOrder->hbId = -$updatedOrder->hbId;
+                    $updatedOrder->hbAmount = 0.00;
+                    $updatedOrder->save();
+                }
             }
 
             //制作单取消通知

+ 10 - 0
app-hd/models/hb/UpdateHbForm.php

@@ -38,6 +38,9 @@ class UpdateHbForm extends BaseForm
     /** @var string 备注 */
     public $remark;
 
+    /** @var int 订单ID */
+    public $orderId;
+
     /**
      * {@inheritdoc}
      */
@@ -58,6 +61,12 @@ class UpdateHbForm extends BaseForm
             [['minConsume'], 'number', 'min' => 0.00, 'tooSmall' => '{attribute}不能小于0'],
 
             [['remark'], 'string'],
+
+            //[['orderId'], 'default', 'value' => 0],
+            [['orderId'], 'integer', 'min' => 1],
+            [['orderId'], 'required', 'when' => function ($model) {
+                return $model->status == '0';
+            }, 'message' => '订单ID不能为空'],
         ];
     }
 
@@ -77,6 +86,7 @@ class UpdateHbForm extends BaseForm
             'willTime' => '预期失效时间',
             'duration' => '有效天数',
             'remark' => '备注',
+            'orderId' => '订单ID',
         ];
     }
 }

+ 1 - 1
app-mall/controllers/OrderController.php

@@ -432,7 +432,7 @@ class OrderController extends BaseController
             }
             //红包使用
             $hbId = $post['hbId'] ?? 0;
-            if ($hbId != 0) {
+            if ($hbId > 0) { // 不使用 $hbId =! 0,因为要用负数来表示红包已取消
                 $hb = HbClass::getById($hbId, true);
                 if (!empty($hb)) {
                     if ($hb->status == 1 || $hb->status == -1) {

+ 0 - 6
biz-hd/order/services/OrderService.php

@@ -302,12 +302,6 @@ class OrderService extends BaseService
         //保存订单
         $returnOrder = OrderClass::addOrder($data);
 
-        //如果 $data['hbId'] 不为空,则把红包消费掉
-        if(isset($data['hbId']) && $data['hbId'] != 0){
-            $hbId = $data['hbId'];
-            HbClass::hbBack($hbId);
-        }
-
         //欠款、已付款、余额支付的直接完成,另外客服下的单要走配送流程,所以是待配送状态
         if (in_array($hasPay, [dict::getDict('hasPay', 'payed'), dict::getDict('hasPay', 'debt'), dict::getDict('hasPay', 'balance')])) {
             $params = [];

+ 9 - 2
console/controllers/OrderController.php

@@ -114,8 +114,15 @@ class OrderController extends Controller
                     $order = OrderClass::getById($id, true);
                     //暂时只考虑花材库存的回滚
                     OrderClass::setExpire($order);
-                    if($order['hbId'] != 0){
-                        hbClass::hbBack($order['hbId']);
+                    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}");