| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- <?php
- namespace App\Module\SocialFarm\Models;
- use UCore\ModelCore;
- use App\Module\SocialFarm\Enums\STEAL_STATUS;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- /**
- * 偷菜记录模型
- *
- * @property int $id 主键ID
- * @property int $stealer_id 偷菜者用户ID
- * @property int $owner_id 农场主用户ID
- * @property int $land_id 被偷的土地ID
- * @property int $crop_id 被偷的作物ID
- * @property int $item_id 偷到的物品ID
- * @property int $item_amount 偷到的物品数量
- * @property int $original_amount 作物原始数量
- * @property float $steal_ratio 偷菜比例
- * @property string $steal_time 偷菜时间
- * @property int $steal_status 偷菜状态
- * @property string|null $ip_address 偷菜者IP地址
- * @property string|null $user_agent 用户代理信息
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- */
- class SocialFarmStealLog extends ModelCore
- {
- /**
- * 数据表名
- */
- protected $table = 'social_farm_steal_logs';
- /**
- * 可批量赋值的属性
- */
- protected $fillable = [
- 'stealer_id',
- 'owner_id',
- 'land_id',
- 'crop_id',
- 'item_id',
- 'item_amount',
- 'original_amount',
- 'steal_ratio',
- 'steal_time',
- 'steal_status',
- 'ip_address',
- 'user_agent',
- ];
- /**
- * 属性类型转换
- */
- protected $casts = [
- 'stealer_id' => 'integer',
- 'owner_id' => 'integer',
- 'land_id' => 'integer',
- 'crop_id' => 'integer',
- 'item_id' => 'integer',
- 'item_amount' => 'integer',
- 'original_amount' => 'integer',
- 'steal_ratio' => 'decimal:4',
- 'steal_time' => 'datetime',
- 'steal_status' => 'integer',
- 'created_at' => 'datetime',
- 'updated_at' => 'datetime',
- ];
- /**
- * 日期字段
- */
- protected $dates = [
- 'steal_time',
- 'created_at',
- 'updated_at',
- ];
- /**
- * 获取偷菜者用户信息
- */
- public function stealer(): BelongsTo
- {
- return $this->belongsTo(\App\Module\User\Models\User::class, 'stealer_id');
- }
- /**
- * 获取农场主用户信息
- */
- public function owner(): BelongsTo
- {
- return $this->belongsTo(\App\Module\User\Models\User::class, 'owner_id');
- }
- /**
- * 获取土地信息
- */
- public function land(): BelongsTo
- {
- return $this->belongsTo(\App\Module\Farm\Models\FarmLandUser::class, 'land_id');
- }
- /**
- * 获取作物信息
- */
- public function crop(): BelongsTo
- {
- return $this->belongsTo(\App\Module\Farm\Models\FarmCropUser::class, 'crop_id');
- }
- /**
- * 获取物品信息
- */
- public function item(): BelongsTo
- {
- return $this->belongsTo(\App\Module\GameItems\Models\ItemConfig::class, 'item_id');
- }
- /**
- * 获取偷菜状态枚举
- */
- public function getStealStatusEnum(): STEAL_STATUS
- {
- return STEAL_STATUS::from($this->steal_status);
- }
- /**
- * 获取偷菜状态描述
- */
- public function getStealStatusDescAttribute(): string
- {
- return $this->getStealStatusEnum()->getDescription();
- }
- /**
- * 获取偷菜状态颜色
- */
- public function getStealStatusColorAttribute(): string
- {
- return $this->getStealStatusEnum()->getColor();
- }
- /**
- * 是否偷菜成功
- */
- public function isSuccess(): bool
- {
- return $this->getStealStatusEnum()->isSuccess();
- }
- /**
- * 是否偷菜失败
- */
- public function isFailed(): bool
- {
- return $this->getStealStatusEnum()->isFailed();
- }
- /**
- * 获取偷菜收益率
- */
- public function getYieldRateAttribute(): float
- {
- if ($this->original_amount <= 0) {
- return 0;
- }
- return round($this->item_amount / $this->original_amount, 4);
- }
- /**
- * 获取格式化的偷菜时间
- */
- public function getFormattedStealTimeAttribute(): string
- {
- return $this->steal_time->format('Y-m-d H:i:s');
- }
- /**
- * 获取偷菜时间距离现在的描述
- */
- public function getStealTimeHumanAttribute(): string
- {
- return $this->steal_time->diffForHumans();
- }
- /**
- * 作用域:按偷菜者查询
- */
- public function scopeByStealer($query, int $stealerId)
- {
- return $query->where('stealer_id', $stealerId);
- }
- /**
- * 作用域:按农场主查询
- */
- public function scopeByOwner($query, int $ownerId)
- {
- return $query->where('owner_id', $ownerId);
- }
- /**
- * 作用域:按偷菜状态查询
- */
- public function scopeByStatus($query, int $status)
- {
- return $query->where('steal_status', $status);
- }
- /**
- * 作用域:成功的偷菜记录
- */
- public function scopeSuccessful($query)
- {
- return $query->where('steal_status', STEAL_STATUS::SUCCESS->value);
- }
- /**
- * 作用域:失败的偷菜记录
- */
- public function scopeFailed($query)
- {
- return $query->where('steal_status', '!=', STEAL_STATUS::SUCCESS->value);
- }
- /**
- * 作用域:按时间范围查询
- */
- public function scopeByTimeRange($query, string $startTime, string $endTime)
- {
- return $query->whereBetween('steal_time', [$startTime, $endTime]);
- }
- /**
- * 作用域:今日记录
- */
- public function scopeToday($query)
- {
- return $query->whereDate('steal_time', today());
- }
- /**
- * 作用域:本周记录
- */
- public function scopeThisWeek($query)
- {
- return $query->whereBetween('steal_time', [
- now()->startOfWeek(),
- now()->endOfWeek()
- ]);
- }
- /**
- * 作用域:本月记录
- */
- public function scopeThisMonth($query)
- {
- return $query->whereMonth('steal_time', now()->month)
- ->whereYear('steal_time', now()->year);
- }
- /**
- * 获取用户今日偷菜次数
- */
- public static function getTodayStealCount(int $stealerId): int
- {
- return static::byStealer($stealerId)
- ->today()
- ->count();
- }
- /**
- * 获取用户今日被偷次数
- */
- public static function getTodayStolenCount(int $ownerId): int
- {
- return static::byOwner($ownerId)
- ->today()
- ->successful()
- ->count();
- }
- /**
- * 获取用户偷菜统计
- */
- public static function getStealStats(int $userId, string $date = null): array
- {
- $date = $date ?: today()->toDateString();
-
- $stealCount = static::byStealer($userId)
- ->whereDate('steal_time', $date)
- ->successful()
- ->count();
-
- $stolenCount = static::byOwner($userId)
- ->whereDate('steal_time', $date)
- ->successful()
- ->count();
-
- $itemsGained = static::byStealer($userId)
- ->whereDate('steal_time', $date)
- ->successful()
- ->sum('item_amount');
-
- $itemsLost = static::byOwner($userId)
- ->whereDate('steal_time', $date)
- ->successful()
- ->sum('item_amount');
- return [
- 'steal_count' => $stealCount,
- 'stolen_count' => $stolenCount,
- 'items_gained' => $itemsGained,
- 'items_lost' => $itemsLost,
- ];
- }
- }
|