| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- namespace App\Module\Game\Models;
- use UCore\ModelCore;
- /**
- * 用户日志清理记录模型
- *
- * field start
- * @property int $id 主键ID
- * @property int $user_id 用户ID
- * @property \Carbon\Carbon $cleared_at 清理时间
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class UserLogClearRecord extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'user_log_clear_records';
- /**
- * 指示模型是否应该被打上时间戳
- *
- * @var bool
- */
- public $timestamps = true;
- // attrlist start
- protected $fillable = [
- 'id',
- 'user_id',
- 'cleared_at',
- ];
- // attrlist end
- /**
- * 应该被转换为日期的属性
- *
- * @var array
- */
- protected $casts = [
- 'cleared_at' => 'datetime',
- 'created_at' => 'datetime',
- 'updated_at' => 'datetime',
- ];
- /**
- * 按用户ID查询作用域
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @param int $userId 用户ID
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeByUser($query, int $userId)
- {
- return $query->where('user_id', $userId);
- }
- /**
- * 获取用户的最后清理时间
- *
- * @param int $userId 用户ID
- * @return \Carbon\Carbon|null
- */
- public static function getUserLastClearTime(int $userId): ?\Carbon\Carbon
- {
- $record = static::byUser($userId)->first();
- return $record ? $record->cleared_at : null;
- }
- /**
- * 设置用户的清理时间
- *
- * @param int $userId 用户ID
- * @param \Carbon\Carbon|null $clearTime 清理时间,默认为当前时间
- * @return static
- */
- public static function setUserClearTime(int $userId, ?\Carbon\Carbon $clearTime = null): static
- {
- $clearTime = $clearTime ?: now();
-
- return static::updateOrCreate(
- ['user_id' => $userId],
- ['cleared_at' => $clearTime]
- );
- }
- }
|