| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- namespace App\Module\Team\Models;
- use App\Module\Team\Enums\REFERRAL_CODE_STATUS;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use UCore\ModelCore;
- /**
- * 推荐码
- *
- * field start
- * @property int $id 主键ID
- * @property int $user_id 用户ID
- * @property string $code 推荐码
- * @property int $usage_count 使用次数
- * @property \App\Module\Team\Enums\REFERRAL_CODE_STATUS $status 状态:1有效,0无效
- * @property string $expire_time 过期时间,NULL表示永不过期
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class TeamReferralCode extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'team_referral_codes';
- // attrlist start
- protected $fillable = [
- 'id',
- 'user_id',
- 'code',
- 'usage_count',
- 'status',
- 'expire_time',
- ];
- // attrlist end
- /**
- * 应该被转换为日期的属性
- *
- * @var array
- */
- protected $dates = [
- 'expire_time',
- 'created_at',
- 'updated_at',
- ];
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'status' => REFERRAL_CODE_STATUS::class,
- ];
- /**
- * 获取用户信息
- *
- * @return BelongsTo
- */
- public function user()
- {
- return $this->belongsTo('App\Models\User', 'user_id');
- }
- /**
- * 获取推荐码使用记录
- *
- * @return HasMany
- */
- public function usages()
- {
- return $this->hasMany(TeamReferralCodeUsage::class, 'code', 'code');
- }
- /**
- * 判断推荐码是否有效
- *
- * @return bool
- */
- public function isValid(): bool
- {
- if ($this->status != REFERRAL_CODE_STATUS::ACTIVE) {
- return false;
- }
- if ($this->expire_time && $this->expire_time->isPast()) {
- return false;
- }
- return true;
- }
- }
|