| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- namespace App\Module\Game\Models;
- use UCore\ModelCore;
- /**
- * 用户日志模型
- *
- * field start
- * @property int $id 主键ID
- * @property int $user_id 用户ID
- * @property string $message 日志消息内容
- * @property string $source_type 来源类型(fund, item, farm等)
- * @property int $source_id 来源记录ID
- * @property string $source_table 来源表名
- * @property \Carbon\Carbon $created_at 创建时间
- * field end
- */
- class UserLog extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'user_logs';
- /**
- * 指示模型是否应该被打上时间戳
- * 只需要创建时间,不需要更新时间
- *
- * @var bool
- */
- public $timestamps = true;
- /**
- * 更新时间戳字段名
- * 设置为null表示不使用updated_at字段
- *
- * @var string|null
- */
- const UPDATED_AT = null;
- // attrlist start
- protected $fillable = [
- 'id',
- 'user_id',
- 'message',
- 'source_type',
- 'source_id',
- 'source_table',
- ];
- // attrlist end
- /**
- * 应该被转换为日期的属性
- *
- * @var array
- */
- protected $dates = [
- 'created_at',
- ];
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'user_id' => 'integer',
- 'source_id' => 'integer',
- 'created_at' => 'datetime',
- ];
- /**
- * 关联用户模型
- *
- * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
- */
- public function user()
- {
- return $this->belongsTo(\App\Module\User\Models\User::class, 'user_id', 'id');
- }
- /**
- * 按用户ID查询日志
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @param int $userId
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeByUser($query, int $userId)
- {
- return $query->where('user_id', $userId);
- }
- /**
- * 按来源类型查询日志
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @param string $sourceType
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeBySourceType($query, string $sourceType)
- {
- return $query->where('source_type', $sourceType);
- }
- /**
- * 按时间范围查询日志
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @param string $startTime
- * @param string $endTime
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeByTimeRange($query, string $startTime, string $endTime)
- {
- return $query->whereBetween('created_at', [$startTime, $endTime]);
- }
- /**
- * 最新日志优先
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeLatest($query)
- {
- return $query->orderBy('created_at', 'desc');
- }
- /**
- * 获取格式化的创建时间
- *
- * @return string
- */
- public function getFormattedCreatedAtAttribute(): string
- {
- return $this->created_at ? $this->created_at->format('Y-m-d H:i:s') : '';
- }
- /**
- * 获取格式化的时间戳
- *
- * @return string
- */
- public function getTimeAttribute(): string
- {
- return $this->created_at ? $this->created_at->format('m-d H:i') : '';
- }
- }
|