Browse Source

buyAmount变动记录

shizhongqi 5 tháng trước cách đây
mục cha
commit
5a83857039

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

@@ -266,7 +266,7 @@ class RefundController extends BaseController
             $custom = CustomClass::getLockById($order->customId);
             if (!empty($custom)) {
                 $custom->buyAmount = bcsub($custom->buyAmount, $post['price'], 2);
-                $custom->growthEventRemark = '退款';
+                $custom->eventRemark = '退款';
                 $custom->mainId = $this->mainId;
                 $custom->save();
 

+ 1 - 1
biz-hd/custom/classes/CustomClass.php

@@ -240,7 +240,7 @@ class CustomClass extends BaseClass
         //成长值变更
         $growth = $current['amount'] ?? 0;
         $custom->growth = $growth;
-        // $custom->growthEventRemark = '等级变更';
+        // $custom->eventRemark = '等级变更';
         $custom->save();
 
         //成长值变动记录

+ 8 - 0
biz-hd/custom/classes/UserConsumeClass.php

@@ -0,0 +1,8 @@
+<?php
+namespace bizHd\custom\classes;
+use bizHd\base\classes\BaseClass;
+
+class UserConsumeClass extends BaseClass
+{
+    public static $baseFile = '\bizHd\custom\models\UserConsume';
+}

+ 3 - 3
biz-hd/custom/models/Custom.php

@@ -6,7 +6,7 @@ use bizHd\custom\observers\BuyAmountObserver;
 class Custom extends Base
 {
     /**
-     * xhCustom 的 buyAmount 变动监听的总开头(默认是开启)
+     * xhCustom 的 buyAmount 变动监听的开关(默认是开启)
      *
      * @var bool
      */
@@ -36,13 +36,13 @@ class Custom extends Base
     public $relateType = 0;
 
 	/**
-	 * 成长值流水事件文案(运行时临时属性,不入库)。
+	 * 流水事件文案(运行时临时属性,不入库)。
 	 *
 	 * 业务侧可在 save() 前赋值,供 BuyAmountObserver 使用。
 	 *
 	 * @var string
 	 */
-	public $growthEventRemark = '';
+	public $eventRemark = '';
 
 	// 加上了 xhCustom 的 buyAmount 变动监听(观察者模式),并处理了联动增减逻辑 -- 该监听依赖 AR 事件触发(如 $model->save())。
 	// 若某处直接用 updateAll()/updateByCondition() 直接改 buyAmount,不会触发该观察者。

+ 11 - 0
biz-hd/custom/models/UserConsume.php

@@ -0,0 +1,11 @@
+<?php
+namespace bizHd\custom\models;
+use bizHd\base\models\Base;
+
+class UserConsume extends Base
+{
+    public static function tableName()
+    {
+        return 'xhUserConsume';
+    }
+}

+ 35 - 14
biz-hd/custom/observers/BuyAmountObserver.php

@@ -8,6 +8,7 @@ use yii\base\Behavior;
 use yii\db\ActiveRecord;
 use yii\db\AfterSaveEvent;
 use bizHd\user\classes\UserGrowthClass;
+use bizHd\custom\classes\UserConsumeClass;
 
 class BuyAmountObserver extends Behavior
 {
@@ -32,11 +33,31 @@ class BuyAmountObserver extends Behavior
 		if (bccomp($newBuyAmount, $oldBuyAmount, 2) === 0) {
 			return;
 		}
+		
+		$buyAmountDelta = bcsub($newBuyAmount, $oldBuyAmount, 2);
+		$shopId = $model->getAttribute('shopId');
+		$userId = $model->getAttribute('userId');
+		$userName = $model->getAttribute('name');
+		$relateId = $model->relateId;
+		$relateType = $model->relateType;
+		$eventRemark = isset($model->eventRemark) ? trim((string) $model->eventRemark) : '';
+		//buyAmount变动记录
+		$consumeData = [
+			'shopId' => $shopId,
+			'userId' => $userId,
+			'userName' => $userName,
+			'event' => $eventRemark,
+			'relateId' => $relateId,
+			'relateType' => $relateType,
+			'num' => $buyAmountDelta,
+			'buyAmount' => $newBuyAmount,
+			'createTime' => date('Y-m-d H:i:s'),
+		];
+		UserConsumeClass::add($consumeData);
 
 		$oldGrowth = array_key_exists('growth', $event->changedAttributes)
 			? $event->changedAttributes['growth']
 			: $model->getAttribute('growth'); // changedAttributes 里没有 growth 时,说明这次更新没改 growth,这时直接取模型当前值即可(它就等于旧值)。
-		$buyAmountDelta = bcsub($newBuyAmount, $oldBuyAmount, 2);
 		$newGrowth = bcadd($oldGrowth, $buyAmountDelta, 2);
 
 		$oldIntegral = array_key_exists('integral', $event->changedAttributes)
@@ -66,14 +87,14 @@ class BuyAmountObserver extends Behavior
 		}
 
 		$createTime = date('Y-m-d H:i:s');
-		$growthEventRemark = isset($model->growthEventRemark) ? trim((string) $model->growthEventRemark) : '';
+		//成长值变动记录
 		$growthData = [
-		    'shopId' => $model->getAttribute('shopId'),
-			'userId' => $model->getAttribute('userId'),
-			'userName' => $model->getAttribute('name'),
-			'event' => $growthEventRemark !== '' ? $growthEventRemark . ',成长值同步变更' : '累计消费变更同步成长值',
-			'relateId' => $model->relateId,
-			'relateType' => $model->relateType,
+		    'shopId' => $shopId,
+			'userId' => $userId,
+			'userName' => $userName,
+			'event' => $eventRemark !== '' ? $eventRemark . ',成长值同步变更' : '累计消费变更同步成长值',
+			'relateId' => $relateId,
+			'relateType' => $relateType,
 			'num' => $buyAmountDelta,
 			'growth' => $newGrowth,
 			'operatorId' => 0,
@@ -83,12 +104,12 @@ class BuyAmountObserver extends Behavior
 
 		//积分变动记录
 		$integralData = [
-            'shopId' => $model->getAttribute('shopId'),
-			'userId' => $model->getAttribute('userId'),
-			'userName' => $model->getAttribute('name'),
-			'event' => $growthEventRemark !== '' ? $growthEventRemark . ',积分同步变更' : '累计消费变更同步积分',
-			'relateId' => $model->relateId,
-			'relateType' => $model->relateType,
+            'shopId' => $shopId,
+			'userId' => $userId,
+			'userName' => $userName,
+			'event' => $eventRemark !== '' ? $eventRemark . ',积分同步变更' : '累计消费变更同步积分',
+			'relateId' => $relateId,
+			'relateType' => $relateType,
 			'num' => $buyAmountDelta,
 			'integral' => $newIntegral,
 			'operatorId' => 0,

+ 2 - 2
biz-hd/order/classes/OrderClass.php

@@ -457,10 +457,10 @@ class OrderClass extends BaseClass
                 $myCustom->recentExpend = date("Y-m-d H:i:s");
                 $myCustom->visitTime = date("Y-m-d H:i:s");
                 $myCustom->buyAmount = bcadd($myCustom->buyAmount, $order->realPrice, 2);
-                $myCustom->growthEventRemark = '累计消费变更';
+                $myCustom->eventRemark = '消费新增';
             } elseif ($order->forward == 1) { //1 售后付款单(红冲)
                 $myCustom->buyAmount = bcsub($myCustom->buyAmount, $order->realPrice, 2);
-                $myCustom->growthEventRemark = '累计消费回退';
+                $myCustom->eventRemark = '消费回退';
             } else {
                 util::fail('订单类型不存在');
             }

+ 0 - 1
biz-hd/user/models/UserGrowth.php

@@ -1,5 +1,4 @@
 <?php
-
 namespace bizHd\user\models;
 
 use bizHd\base\models\Base;