RedpackInvite.php 3.4 KB

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