| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace common\models\user;
- use addons\ConsumeGift\common\enums\GiftEnum;
- use addons\ConsumeGift\common\listeners\ConsumeGiftListener;
- use common\enums\AddonsEnum;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\helpers\ErrHelper;
- use common\helpers\FormatHelper;
- use common\models\mall\MallAddons;
- use common\models\order\Order;
- use Yii;
- use yii\db\Exception;
- use function Qcloud\Cos\startWith;
- /**
- * This is the model class for table "{{%user_action_record}}".
- *
- *
- * @property int $id ID
- * @property int $mall_id 商城ID
- * @property int $user_id 用户ID
- * @property int $is_buy_again 是否复购:0=否,1=是
- * @property int $is_share 是否分享过商城:0=未分享,1=已分享
- * @property int $last_share_at 最后分享时间
- * @property int $follow 关注商城:0=未关注,1=已关注,2=取消关注
- * @property int $follow_at 关注商城时间,根据follow字段变化
- * @property int $last_login_at 最后登录时间
- * @property int $first_pay_at 首次消费时间
- * @property int $last_pay_at 最后消费时间
- * @property int $last_create_order_at 最后下单时间
- * @property int $status 状态【1启用 0禁用 -1删除】
- * @property int $created_at 创建时间
- * @property int $updated_at 上次更新数据时间
- */
- class UserActionRecord extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%user_action_record}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'is_buy_again', 'is_share', 'last_share_at', 'follow', 'follow_at', 'last_login_at', 'first_pay_at', 'last_pay_at', 'last_create_order_at', 'status', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'user_id' => '用户ID',
- 'is_buy_again' => '是否复购:0=否,1=是',
- 'is_share' => '是否分享过商城:0=未分享,1=已分享',
- 'last_share_at' => '最后分享时间',
- 'follow' => '关注商城:0=未关注,1=已关注,2=取消关注',
- 'follow_at' => '关注商城时间,根据follow字段变化',
- 'last_login_at' => '最后登录时间',
- 'first_pay_at' => '首次消费时间',
- 'last_pay_at' => '最后消费时间',
- 'last_create_order_at' => '最后下单时间',
- 'status' => '状态【1启用 0禁用 -1删除】',
- 'created_at' => '创建时间',
- 'updated_at' => '上次更新数据时间',
- ];
- }
- /**
- * 用户行为数据维护
- * @Author: lun
- * @DateTime: 2021/11/19 0019 15:57
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $user_id
- * @param $data
- */
- public static function setData($mall_id, $user_id, $data) {
- try {
- $model = self::findOne(['mall_id' => $mall_id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
- if (empty($model)) {
- $model = new self();
- $model->mall_id = $mall_id;
- $model->user_id = $user_id;
- $model->loadDefaultValues();
- }
- // 支付行为记录
- if (!empty($data['last_pay_at'])) {
- // 首次消费时间
- if ($model->first_pay_at == 0) {
- $model->first_pay_at = $data['last_pay_at'];
- // 触发首单奖励礼包
- if (MallAddons::isInstall($mall_id,AddonsEnum::ADDONS_NAME_CONSUMEGIFT)) {
- $gift['mall_id'] = $mall_id;
- $gift['user_ids'] = $user_id;
- $gift['gift_type'] = GiftEnum::GIFT_TYPE_FIRSTPAY;
- Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, $gift, ConsumeGiftListener::class, 'async_handle');
- }
- }
- // 复购
- if ($model->is_buy_again == 0) {
- $count_order = Order::find()->select('id')->where(['mall_id' => $mall_id, 'user_id' => $user_id, 'pay_status' => StatusEnum::ENABLED])->count();
- if ($count_order >= 2) $model->is_buy_again = 1;
- }
- }
- if (!$model->load($data, '') || !$model->save()) throw new Exception($model->getErrorMessage());
- return true;
- } catch (\Exception $e) {
- $exception = FormatHelper::exception($e, "用户行为数据维护");
- echo $exception;
- self::setStaticError($exception);
- // Yii::error($exception);
- ErrHelper::log('用户行为数据维护', $e);
- return false;
- }
- }
- }
|