| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace addons\ScoreExpansion\common\models;
- use addons\Redpack\common\models\RedpackUserCountDay;
- use common\enums\StatusEnum;
- use common\models\user\User;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_redpack_invite}}".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $rid qimall_addons_score_expansion.id
- * @property int $user_id 被分享人用户id
- * @property int $parent_id 上级用户id
- * @property int $is_valid 是否有效[0:无效,1有效]
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class ScoreExpansionInvite extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_score_expansion_invite}}';
- }
- public static function briefTableName()
- {
- return 'addons_score_expansion_invite';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id'], 'required'],
- [['mall_id', 'rid', 'user_id', 'parent_id', 'is_valid', 'status', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'rid' => 'qimall_addons_score_expansion.id',
- 'user_id' => '被分享人用户id',
- 'parent_id' => '上级用户id',
- 'is_valid' => '是否有效[0:无效,1有效]',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,avatar_url,parent_id,username');
- }
- /**
- * 检查是否分享邀请用户
- * @Author: lun
- * @DateTime: 2022/3/21 0021 20:01
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $rid
- * @param $user_id
- * @param $parent_id
- * @return bool
- */
- public static function checkRedpackInvite($mall_id, $rid, $user_id, $parent_id, $is_valid)
- {
- try {
- $model = self::findOne(['rid' => $rid, 'user_id' => $user_id, 'parent_id' => $parent_id, 'status' => StatusEnum::ENABLED]);
- if (empty($model)) {
- $model = new self();
- $model->mall_id = $mall_id;
- $model->rid = $rid;
- $model->user_id = $user_id;
- $model->parent_id = $parent_id;
- $model->is_valid = $is_valid;
- $model->loadDefaultValues();
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- // 修改用户邀请数据,有效用户才会进行统计
- if ($is_valid) {
- $res = ScoreExpansionUserCountDay::setData([
- 'mall_id' => $mall_id,
- 'rid' => $rid,
- 'user_id' => $parent_id,
- 'is_inviter' => 1,
- ]);
- if ($res == false) throw new \Exception(ScoreExpansionUserCountDay::getStaticError());
- }
- }
- return true;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
|