RecommenLog.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace addons\Recommen\common\models;
  3. use addons\Recommen\common\enums\RecommenEnum;
  4. use common\enums\StatusEnum;
  5. use common\helpers\FormatHelper;
  6. use common\models\base\User;
  7. use Exception;
  8. use Yii;
  9. /**
  10. * This is the model class for table "{{%addons_recommen_log}}".
  11. *
  12. * @property int $id
  13. * @property int|null $mall_id 商城ID
  14. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  15. * @property int|null $user_id 会员ID
  16. * @property int|null $parent_id 推荐者ID
  17. * @property int|null $active_id 活动ID
  18. * @property int|null $new_num 新人数量
  19. * @property int|null $created_at
  20. * @property int|null $updated_at
  21. */
  22. class RecommenLog extends \common\models\base\BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%addons_recommen_log}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['mall_id', 'status', 'user_id', 'parent_id', 'active_id', 'new_num', 'created_at', 'updated_at'], 'integer'],
  38. ];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'id' => 'ID',
  47. 'mall_id' => '商城ID',
  48. 'status' => '状态[-1:删除;0:禁用;1启用]',
  49. 'user_id' => '会员ID',
  50. 'parent_id' => '推荐者ID',
  51. 'active_id' => '活动ID',
  52. 'new_num' => '新人数量',
  53. 'created_at' => 'Created At',
  54. 'updated_at' => 'Updated At',
  55. ];
  56. }
  57. /**
  58. * 保存数据
  59. * @Author: ltm
  60. * @DateTime: 2022/5/16 0022 17:13
  61. * @Copyright: copyright (c) 2021 广东七件事集团
  62. * @param array $params
  63. * @param int $type 活动类型 0注册,4订单完成
  64. * @param bool $is_add_log 是否需要添加新的用户 为true的时候一定是需要发放推荐奖励的
  65. * @return RecommenLog|bool|array
  66. */
  67. public static function setData(array $params, int $type, bool $is_add_log){
  68. try{
  69. if ($is_add_log) {
  70. //推荐人信息 开始
  71. $model = self::findOne(['active_id'=>$params['active_id'],'user_id'=>$params['user_id'],'status' => StatusEnum::ENABLED]);
  72. if (!$model) {
  73. $model = new self();
  74. $model->loadDefaultValues();
  75. }else{
  76. // 注册的时候记录了,但是下单的时候还是得判断一下能不能获得奖励
  77. if ($type == RecommenEnum::RECOMMEND_TYPE_REG) {
  78. throw new Exception("记录已存在,不可以在领取: " . var_export($params, true));
  79. } else { // 如果是下单,那么就可以直接计算能否奖励
  80. $recommen_log = self::findOne(['active_id'=>$params['active_id'],'user_id'=>$params['parent_id'],'status' => StatusEnum::ENABLED]);
  81. return $recommen_log ? [$recommen_log] : false;
  82. }
  83. }
  84. $model->mall_id = $params['mall_id'];
  85. if(!$model->load($params,'') || !$model->save()){
  86. throw new Exception($model->getErrorMessage());
  87. }
  88. }
  89. //记录推荐人信息开始 - 如果是订单完成,那么就先计数再做奖励 注册的时候计数,订单完成的时候添加日志
  90. $recommen_log = self::findOne(['active_id'=>$params['active_id'],'user_id'=>$params['parent_id'],'status' => StatusEnum::ENABLED]);
  91. if($params['parent_id'] && $type == RecommenEnum::RECOMMEND_TYPE_REG){
  92. // 如果是注册,那么就需要累加人数
  93. $is_new = 0;
  94. if(!$recommen_log){
  95. $recommen_log = new self();
  96. $recommen_log->loadDefaultValues();
  97. $recommen_log->user_id = $params['parent_id'];
  98. $parent_id = User::getOne([['id'=>$params['parent_id']]],true);
  99. $recommen_log->parent_id = $parent_id['parent_id'];
  100. $recommen_log->new_num = 1;
  101. $is_new = 1;
  102. }else{
  103. $recommen_log->new_num = $recommen_log->new_num + 1;
  104. }
  105. unset($params['parent_id'],$params['user_id']);
  106. $recommen_log->mall_id = $params['mall_id'];
  107. if(!$recommen_log->load($params,'') || !$recommen_log->save()){
  108. throw new Exception($recommen_log->getErrorMessage());
  109. }
  110. $update = Recommen::findOne(['id'=>$params['active_id']]);
  111. if($is_new == 1){
  112. $update->num = $update->num + 1;
  113. }
  114. $update->new_num = $update->new_num + 1;
  115. $update->save();
  116. } elseif($params['parent_id'] && $type == RecommenEnum::RECOMMEND_TYPE_ORDER_FINISH) {
  117. // 如果是下单,那么就可以直接计算上级是否参与活动
  118. if (!$recommen_log) { // 初始下单时上级的记录不存在
  119. $recommen_log = new self();
  120. $recommen_log->loadDefaultValues();
  121. $recommen_log->user_id = $params['parent_id'];
  122. $parent_id = User::getOne([['id'=>$params['parent_id']]],true);
  123. $recommen_log->parent_id = $parent_id['parent_id'];
  124. $recommen_log->new_num = 0;
  125. if(!$recommen_log->load($params,'') || !$recommen_log->save()){
  126. throw new Exception($recommen_log->getErrorMessage());
  127. }
  128. }
  129. }
  130. //记录被推荐人信息 结束
  131. return $recommen_log;
  132. }catch(\Exception $e){
  133. self::$static_error = $e->getMessage();
  134. Yii::error(FormatHelper::exception($e, __METHOD__));
  135. return false;
  136. }
  137. }
  138. /**
  139. * 关联用户信息
  140. * @Author:ltm
  141. * @DateTime: 2022/5/6 0022 17:13
  142. * @Copyright: copyright (c) 2021 广东七件事集团
  143. * @return \yii\db\ActiveQuery
  144. */
  145. public function getUser()
  146. {
  147. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,nickname,username,mobile,avatar_url');
  148. }
  149. /**
  150. * 关联活动信息
  151. * @Author:ltm
  152. * @DateTime: 2022/5/6 0022 17:13
  153. * @Copyright: copyright (c) 2021 广东七件事集团
  154. * @return \yii\db\ActiveQuery
  155. */
  156. public function getRecommen()
  157. {
  158. return $this->hasOne(Recommen::class, ['id' => 'active_id']);
  159. }
  160. }