|
|
@@ -10,4 +10,125 @@
|
|
|
- 下图,更新累计消费的动作不会更新: 成长值growth与积分integral
|
|
|
![[Pasted image 20260305095930.png]]
|
|
|
|
|
|
- - 写个脚本,把所有客户的消费记录和成长值、积分刷新刷对一下(等级也要刷新对)
|
|
|
+ - 写个脚本,把所有客户的累计消费和成长值、积分刷新刷对一下(等级也要刷新对)
|
|
|
+
|
|
|
+#### 积分与成长记录表--重新设计
|
|
|
+```
|
|
|
+CREATE TABLE `xhUserGrowth` (
|
|
|
+ `id` int(11) NOT NULL AUTO_INCREMENT,
|
|
|
+ `shopId` int(11) NOT NULL DEFAULT '0',
|
|
|
+ `userId` int(11) NOT NULL DEFAULT '0',
|
|
|
+ `userName` varchar(50) NOT NULL DEFAULT '' COMMENT '用户名',
|
|
|
+ `event` varchar(200) NOT NULL DEFAULT '' COMMENT '原由,事项,解释',
|
|
|
+ `relateId` int(11) NOT NULL DEFAULT '0' COMMENT '关联id',
|
|
|
+ `relateType` tinyint NOT NULL DEFAULT 0 COMMENT '关联的表或类别:0.无关联 1.零售订单',
|
|
|
+ `num` float DEFAULT '0' COMMENT '增加或减少的成长值',
|
|
|
+ `growth` float DEFAULT NULL COMMENT '变动后的成长总值',
|
|
|
+ `operatorId` int(11) NOT NULL DEFAULT '0' COMMENT '操作人,adminId',
|
|
|
+ `remark` varchar(1000) NOT NULL DEFAULT '' COMMENT '备注',
|
|
|
+ `createTime` datetime NOT NULL COMMENT '创建时间',
|
|
|
+ `updateTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
|
|
+ PRIMARY KEY (`id`),
|
|
|
+ KEY `idx_s_u` (`shopId`,`userId`)
|
|
|
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户成长值记录表';
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+CREATE TABLE `xhUserIntegral` (
|
|
|
+ `id` int(11) NOT NULL AUTO_INCREMENT,
|
|
|
+ `shopId` int(11) NOT NULL DEFAULT '0',
|
|
|
+ `userId` int(11) NOT NULL DEFAULT '0',
|
|
|
+ `userName` varchar(50) NOT NULL DEFAULT '' COMMENT '用户名',
|
|
|
+ `event` varchar(200) NOT NULL DEFAULT '' COMMENT '原由,事项,解释',
|
|
|
+ `relateId` int(11) NOT NULL DEFAULT '0' COMMENT '关联id',
|
|
|
+ `relateType` tinyint NOT NULL DEFAULT 0 COMMENT '关联的表或类别:0.无关联 1.零售订单',
|
|
|
+ `num` float DEFAULT '0' COMMENT '增加或减少的积分',
|
|
|
+ `integral` float DEFAULT NULL COMMENT '变动后的积分总值',
|
|
|
+ `operatorId` int(11) NOT NULL DEFAULT '0' COMMENT '操作人,adminId',
|
|
|
+ `remark` varchar(1000) NOT NULL DEFAULT '' COMMENT '备注',
|
|
|
+ `createTime` datetime NOT NULL COMMENT '创建时间',
|
|
|
+ `updateTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
|
|
+ PRIMARY KEY (`id`),
|
|
|
+ KEY `idx_s_u` (`shopId`,`userId`)
|
|
|
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户积分记录表';
|
|
|
+```
|
|
|
+#### 积分与成长值字段修改为 浮点数
|
|
|
+```
|
|
|
+1. xhCustom 表
|
|
|
+ALTER TABLE xhCustom CHANGE integral integral float DEFAULT 0 COMMENT '积分';
|
|
|
+ALTER TABLE xhCustom CHANGE growth growth float DEFAULT 0 COMMENT '成长值';
|
|
|
+
|
|
|
+2. xhUserGrowth 表
|
|
|
+ALTER TABLE xhUserGrowth CHANGE growth growth float DEFAULT 0 COMMENT '成长值';
|
|
|
+xhUserGrowth -- num
|
|
|
+ALTER TABLE xhUserGrowth CHANGE num num float DEFAULT 0 COMMENT '增加或减少的成长值';
|
|
|
+```
|
|
|
+
|
|
|
+
|
|
|
+#### 使用监听者模式来实现积分与成长值的记录
|
|
|
+Custom.php
|
|
|
+```
|
|
|
+<?php
|
|
|
+namespace bizHd\custom\models;
|
|
|
+use bizHd\base\models\Base;
|
|
|
+use bizHd\custom\observers\BuyAmountObserver;
|
|
|
+
|
|
|
+class Custom extends Base
|
|
|
+{
|
|
|
+ // 加上了 xhCustom 的 buyAmount 变动监听(观察者模式),并处理了联动增减逻 -- 该监听依赖 AR 事件触发(如 $model->save())。
|
|
|
+ // 若某处直接用 updateAll()/updateByCondition() 直接改 buyAmount,不会触发该观察者。
|
|
|
+ public function behaviors()
|
|
|
+ {
|
|
|
+ return [
|
|
|
+ BuyAmountObserver::class,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function tableName()
|
|
|
+ {
|
|
|
+ return 'xhCustom';
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+```
|
|
|
+
|
|
|
+
|
|
|
+BuyAmountObserver.php
|
|
|
+```
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace bizHd\custom\observers;
|
|
|
+
|
|
|
+use yii\base\Behavior;
|
|
|
+use yii\db\ActiveRecord;
|
|
|
+use yii\db\AfterSaveEvent;
|
|
|
+
|
|
|
+class BuyAmountObserver extends Behavior
|
|
|
+{
|
|
|
+ public function events()
|
|
|
+ {
|
|
|
+ return [
|
|
|
+ ActiveRecord::EVENT_AFTER_INSERT => 'onAfterSave',
|
|
|
+ ActiveRecord::EVENT_AFTER_UPDATE => 'onAfterSave',
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function onAfterSave(AfterSaveEvent $event)
|
|
|
+ {
|
|
|
+ /** @var ActiveRecord $model */
|
|
|
+ $model = $this->owner;
|
|
|
+ $oldBuyAmount = isset($event->changedAttributes['buyAmount']) ? (string) $event->changedAttributes['buyAmount'] : '0';
|
|
|
+ $newBuyAmount = (string) $model->getAttribute('buyAmount');
|
|
|
+ $delta = bcsub($newBuyAmount, $oldBuyAmount, 2);
|
|
|
+ if (bccomp($delta, '0', 2) === 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ $model::updateAllCounters([
|
|
|
+ 'integral' => (float) $delta,
|
|
|
+ 'growth' => (float) $delta,
|
|
|
+ ], ['id' => $model->getPrimaryKey()]);
|
|
|
+ }
|
|
|
+}
|
|
|
+```
|