UserActionRecord.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace common\models\user;
  3. use addons\ConsumeGift\common\enums\GiftEnum;
  4. use addons\ConsumeGift\common\listeners\ConsumeGiftListener;
  5. use common\enums\AddonsEnum;
  6. use common\enums\RabbitMqEnum;
  7. use common\enums\StatusEnum;
  8. use common\helpers\ErrHelper;
  9. use common\helpers\FormatHelper;
  10. use common\models\mall\MallAddons;
  11. use common\models\order\Order;
  12. use Yii;
  13. use yii\db\Exception;
  14. use function Qcloud\Cos\startWith;
  15. /**
  16. * This is the model class for table "{{%user_action_record}}".
  17. *
  18. *
  19. * @property int $id ID
  20. * @property int $mall_id 商城ID
  21. * @property int $user_id 用户ID
  22. * @property int $is_buy_again 是否复购:0=否,1=是
  23. * @property int $is_share 是否分享过商城:0=未分享,1=已分享
  24. * @property int $last_share_at 最后分享时间
  25. * @property int $follow 关注商城:0=未关注,1=已关注,2=取消关注
  26. * @property int $follow_at 关注商城时间,根据follow字段变化
  27. * @property int $last_login_at 最后登录时间
  28. * @property int $first_pay_at 首次消费时间
  29. * @property int $last_pay_at 最后消费时间
  30. * @property int $last_create_order_at 最后下单时间
  31. * @property int $status 状态【1启用 0禁用 -1删除】
  32. * @property int $created_at 创建时间
  33. * @property int $updated_at 上次更新数据时间
  34. */
  35. class UserActionRecord extends \common\models\base\BaseActiveRecord
  36. {
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public static function tableName()
  41. {
  42. return '{{%user_action_record}}';
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function rules()
  48. {
  49. return [
  50. [['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'],
  51. ];
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function attributeLabels()
  57. {
  58. return [
  59. 'id' => 'ID',
  60. 'mall_id' => '商城ID',
  61. 'user_id' => '用户ID',
  62. 'is_buy_again' => '是否复购:0=否,1=是',
  63. 'is_share' => '是否分享过商城:0=未分享,1=已分享',
  64. 'last_share_at' => '最后分享时间',
  65. 'follow' => '关注商城:0=未关注,1=已关注,2=取消关注',
  66. 'follow_at' => '关注商城时间,根据follow字段变化',
  67. 'last_login_at' => '最后登录时间',
  68. 'first_pay_at' => '首次消费时间',
  69. 'last_pay_at' => '最后消费时间',
  70. 'last_create_order_at' => '最后下单时间',
  71. 'status' => '状态【1启用 0禁用 -1删除】',
  72. 'created_at' => '创建时间',
  73. 'updated_at' => '上次更新数据时间',
  74. ];
  75. }
  76. /**
  77. * 用户行为数据维护
  78. * @Author: lun
  79. * @DateTime: 2021/11/19 0019 15:57
  80. * @Copyright: copyright (c) 2021 广东七件事集团
  81. * @param $mall_id
  82. * @param $user_id
  83. * @param $data
  84. */
  85. public static function setData($mall_id, $user_id, $data) {
  86. try {
  87. $model = self::findOne(['mall_id' => $mall_id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
  88. if (empty($model)) {
  89. $model = new self();
  90. $model->mall_id = $mall_id;
  91. $model->user_id = $user_id;
  92. $model->loadDefaultValues();
  93. }
  94. // 支付行为记录
  95. if (!empty($data['last_pay_at'])) {
  96. // 首次消费时间
  97. if ($model->first_pay_at == 0) {
  98. $model->first_pay_at = $data['last_pay_at'];
  99. // 触发首单奖励礼包
  100. if (MallAddons::isInstall($mall_id,AddonsEnum::ADDONS_NAME_CONSUMEGIFT)) {
  101. $gift['mall_id'] = $mall_id;
  102. $gift['user_ids'] = $user_id;
  103. $gift['gift_type'] = GiftEnum::GIFT_TYPE_FIRSTPAY;
  104. Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, $gift, ConsumeGiftListener::class, 'async_handle');
  105. }
  106. }
  107. // 复购
  108. if ($model->is_buy_again == 0) {
  109. $count_order = Order::find()->select('id')->where(['mall_id' => $mall_id, 'user_id' => $user_id, 'pay_status' => StatusEnum::ENABLED])->count();
  110. if ($count_order >= 2) $model->is_buy_again = 1;
  111. }
  112. }
  113. if (!$model->load($data, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  114. return true;
  115. } catch (\Exception $e) {
  116. $exception = FormatHelper::exception($e, "用户行为数据维护");
  117. echo $exception;
  118. self::setStaticError($exception);
  119. // Yii::error($exception);
  120. ErrHelper::log('用户行为数据维护', $e);
  121. return false;
  122. }
  123. }
  124. }