ScoreExpansionInvite.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace addons\ScoreExpansion\common\models;
  3. use addons\Redpack\common\models\RedpackUserCountDay;
  4. use common\enums\StatusEnum;
  5. use common\models\user\User;
  6. use Yii;
  7. use yii\db\Exception;
  8. /**
  9. * This is the model class for table "{{%addons_redpack_invite}}".
  10. *
  11. * @property int $id
  12. * @property int $mall_id 商城ID
  13. * @property int $rid qimall_addons_score_expansion.id
  14. * @property int $user_id 被分享人用户id
  15. * @property int $parent_id 上级用户id
  16. * @property int $is_valid 是否有效[0:无效,1有效]
  17. * @property int $status 状态[-1:删除;0:禁用;1启用]
  18. * @property int $created_at
  19. * @property int $updated_at
  20. */
  21. class ScoreExpansionInvite extends \common\models\base\BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%addons_score_expansion_invite}}';
  29. }
  30. public static function briefTableName()
  31. {
  32. return 'addons_score_expansion_invite';
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function rules()
  38. {
  39. return [
  40. [['mall_id'], 'required'],
  41. [['mall_id', 'rid', 'user_id', 'parent_id', 'is_valid', 'status', 'created_at', 'updated_at'], 'integer'],
  42. ];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'id' => 'ID',
  51. 'mall_id' => '商城ID',
  52. 'rid' => 'qimall_addons_score_expansion.id',
  53. 'user_id' => '被分享人用户id',
  54. 'parent_id' => '上级用户id',
  55. 'is_valid' => '是否有效[0:无效,1有效]',
  56. 'status' => '状态[-1:删除;0:禁用;1启用]',
  57. 'created_at' => 'Created At',
  58. 'updated_at' => 'Updated At',
  59. ];
  60. }
  61. public function getUser()
  62. {
  63. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,avatar_url,parent_id,username');
  64. }
  65. /**
  66. * 检查是否分享邀请用户
  67. * @Author: lun
  68. * @DateTime: 2022/3/21 0021 20:01
  69. * @Copyright: copyright (c) 2021 广东七件事集团
  70. * @param $mall_id
  71. * @param $rid
  72. * @param $user_id
  73. * @param $parent_id
  74. * @return bool
  75. */
  76. public static function checkRedpackInvite($mall_id, $rid, $user_id, $parent_id, $is_valid)
  77. {
  78. try {
  79. $model = self::findOne(['rid' => $rid, 'user_id' => $user_id, 'parent_id' => $parent_id, 'status' => StatusEnum::ENABLED]);
  80. if (empty($model)) {
  81. $model = new self();
  82. $model->mall_id = $mall_id;
  83. $model->rid = $rid;
  84. $model->user_id = $user_id;
  85. $model->parent_id = $parent_id;
  86. $model->is_valid = $is_valid;
  87. $model->loadDefaultValues();
  88. if (!$model->save()) throw new Exception($model->getErrorMessage());
  89. // 修改用户邀请数据,有效用户才会进行统计
  90. if ($is_valid) {
  91. $res = ScoreExpansionUserCountDay::setData([
  92. 'mall_id' => $mall_id,
  93. 'rid' => $rid,
  94. 'user_id' => $parent_id,
  95. 'is_inviter' => 1,
  96. ]);
  97. if ($res == false) throw new \Exception(ScoreExpansionUserCountDay::getStaticError());
  98. }
  99. }
  100. return true;
  101. } catch (\Exception $e) {
  102. self::setStaticError($e->getMessage());
  103. return false;
  104. }
  105. }
  106. }