| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace App\Module\Promotion\Models;
- use App\Module\Promotion\Enums\REFERRAL_CODE_STATUS;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use UCore\ModelCore;
- /**
- * 推荐码
- *
- * field start
- * field end
- */
- class PromotionReferralCode extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'promotion_referral_codes';
- // attrlist start
- protected $fillable = [
- ];
- // 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(PromotionReferralCodeUsage::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;
- }
- }
|