| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- namespace addons\Recommen\common\models;
- use addons\Recommen\common\enums\RecommenEnum;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- use common\models\base\User;
- use Exception;
- use Yii;
- /**
- * This is the model class for table "{{%addons_recommen_log}}".
- *
- * @property int $id
- * @property int|null $mall_id 商城ID
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int|null $user_id 会员ID
- * @property int|null $parent_id 推荐者ID
- * @property int|null $active_id 活动ID
- * @property int|null $new_num 新人数量
- * @property int|null $created_at
- * @property int|null $updated_at
- */
- class RecommenLog extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_recommen_log}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'status', 'user_id', 'parent_id', 'active_id', 'new_num', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'user_id' => '会员ID',
- 'parent_id' => '推荐者ID',
- 'active_id' => '活动ID',
- 'new_num' => '新人数量',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- /**
- * 保存数据
- * @Author: ltm
- * @DateTime: 2022/5/16 0022 17:13
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params
- * @param int $type 活动类型 0注册,4订单完成
- * @param bool $is_add_log 是否需要添加新的用户 为true的时候一定是需要发放推荐奖励的
- * @return RecommenLog|bool|array
- */
- public static function setData(array $params, int $type, bool $is_add_log){
- try{
- if ($is_add_log) {
- //推荐人信息 开始
- $model = self::findOne(['active_id'=>$params['active_id'],'user_id'=>$params['user_id'],'status' => StatusEnum::ENABLED]);
- if (!$model) {
- $model = new self();
- $model->loadDefaultValues();
- }else{
- // 注册的时候记录了,但是下单的时候还是得判断一下能不能获得奖励
- if ($type == RecommenEnum::RECOMMEND_TYPE_REG) {
- throw new Exception("记录已存在,不可以在领取: " . var_export($params, true));
- } else { // 如果是下单,那么就可以直接计算能否奖励
- $recommen_log = self::findOne(['active_id'=>$params['active_id'],'user_id'=>$params['parent_id'],'status' => StatusEnum::ENABLED]);
- return $recommen_log ? [$recommen_log] : false;
- }
- }
- $model->mall_id = $params['mall_id'];
- if(!$model->load($params,'') || !$model->save()){
- throw new Exception($model->getErrorMessage());
- }
- }
- //记录推荐人信息开始 - 如果是订单完成,那么就先计数再做奖励 注册的时候计数,订单完成的时候添加日志
- $recommen_log = self::findOne(['active_id'=>$params['active_id'],'user_id'=>$params['parent_id'],'status' => StatusEnum::ENABLED]);
- if($params['parent_id'] && $type == RecommenEnum::RECOMMEND_TYPE_REG){
- // 如果是注册,那么就需要累加人数
- $is_new = 0;
- if(!$recommen_log){
- $recommen_log = new self();
- $recommen_log->loadDefaultValues();
- $recommen_log->user_id = $params['parent_id'];
- $parent_id = User::getOne([['id'=>$params['parent_id']]],true);
- $recommen_log->parent_id = $parent_id['parent_id'];
- $recommen_log->new_num = 1;
- $is_new = 1;
- }else{
- $recommen_log->new_num = $recommen_log->new_num + 1;
- }
- unset($params['parent_id'],$params['user_id']);
- $recommen_log->mall_id = $params['mall_id'];
- if(!$recommen_log->load($params,'') || !$recommen_log->save()){
- throw new Exception($recommen_log->getErrorMessage());
- }
- $update = Recommen::findOne(['id'=>$params['active_id']]);
- if($is_new == 1){
- $update->num = $update->num + 1;
- }
- $update->new_num = $update->new_num + 1;
- $update->save();
- } elseif($params['parent_id'] && $type == RecommenEnum::RECOMMEND_TYPE_ORDER_FINISH) {
- // 如果是下单,那么就可以直接计算上级是否参与活动
- if (!$recommen_log) { // 初始下单时上级的记录不存在
- $recommen_log = new self();
- $recommen_log->loadDefaultValues();
- $recommen_log->user_id = $params['parent_id'];
- $parent_id = User::getOne([['id'=>$params['parent_id']]],true);
- $recommen_log->parent_id = $parent_id['parent_id'];
- $recommen_log->new_num = 0;
- if(!$recommen_log->load($params,'') || !$recommen_log->save()){
- throw new Exception($recommen_log->getErrorMessage());
- }
- }
- }
- //记录被推荐人信息 结束
- return $recommen_log;
- }catch(\Exception $e){
- self::$static_error = $e->getMessage();
- Yii::error(FormatHelper::exception($e, __METHOD__));
- return false;
- }
- }
- /**
- * 关联用户信息
- * @Author:ltm
- * @DateTime: 2022/5/6 0022 17:13
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \yii\db\ActiveQuery
- */
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,nickname,username,mobile,avatar_url');
- }
- /**
- * 关联活动信息
- * @Author:ltm
- * @DateTime: 2022/5/6 0022 17:13
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \yii\db\ActiveQuery
- */
- public function getRecommen()
- {
- return $this->hasOne(Recommen::class, ['id' => 'active_id']);
- }
- }
|