SocialFarmStealLog.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace App\Module\SocialFarm\Models;
  3. use UCore\ModelCore;
  4. use App\Module\SocialFarm\Enums\STEAL_STATUS;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. /**
  7. * 偷菜记录模型
  8. *
  9. * @property int $id 主键ID
  10. * @property int $stealer_id 偷菜者用户ID
  11. * @property int $owner_id 农场主用户ID
  12. * @property int $land_id 被偷的土地ID
  13. * @property int $crop_id 被偷的作物ID
  14. * @property int $item_id 偷到的物品ID
  15. * @property int $item_amount 偷到的物品数量
  16. * @property int $original_amount 作物原始数量
  17. * @property float $steal_ratio 偷菜比例
  18. * @property string $steal_time 偷菜时间
  19. * @property int $steal_status 偷菜状态
  20. * @property string|null $ip_address 偷菜者IP地址
  21. * @property string|null $user_agent 用户代理信息
  22. * @property \Carbon\Carbon $created_at 创建时间
  23. * @property \Carbon\Carbon $updated_at 更新时间
  24. */
  25. class SocialFarmStealLog extends ModelCore
  26. {
  27. /**
  28. * 数据表名
  29. */
  30. protected $table = 'social_farm_steal_logs';
  31. /**
  32. * 可批量赋值的属性
  33. */
  34. protected $fillable = [
  35. 'stealer_id',
  36. 'owner_id',
  37. 'land_id',
  38. 'crop_id',
  39. 'item_id',
  40. 'item_amount',
  41. 'original_amount',
  42. 'steal_ratio',
  43. 'steal_time',
  44. 'steal_status',
  45. 'ip_address',
  46. 'user_agent',
  47. ];
  48. /**
  49. * 属性类型转换
  50. */
  51. protected $casts = [
  52. 'stealer_id' => 'integer',
  53. 'owner_id' => 'integer',
  54. 'land_id' => 'integer',
  55. 'crop_id' => 'integer',
  56. 'item_id' => 'integer',
  57. 'item_amount' => 'integer',
  58. 'original_amount' => 'integer',
  59. 'steal_ratio' => 'decimal:4',
  60. 'steal_time' => 'datetime',
  61. 'steal_status' => 'integer',
  62. 'created_at' => 'datetime',
  63. 'updated_at' => 'datetime',
  64. ];
  65. /**
  66. * 日期字段
  67. */
  68. protected $dates = [
  69. 'steal_time',
  70. 'created_at',
  71. 'updated_at',
  72. ];
  73. /**
  74. * 获取偷菜者用户信息
  75. */
  76. public function stealer(): BelongsTo
  77. {
  78. return $this->belongsTo(\App\Module\User\Models\User::class, 'stealer_id');
  79. }
  80. /**
  81. * 获取农场主用户信息
  82. */
  83. public function owner(): BelongsTo
  84. {
  85. return $this->belongsTo(\App\Module\User\Models\User::class, 'owner_id');
  86. }
  87. /**
  88. * 获取土地信息
  89. */
  90. public function land(): BelongsTo
  91. {
  92. return $this->belongsTo(\App\Module\Farm\Models\FarmLandUser::class, 'land_id');
  93. }
  94. /**
  95. * 获取作物信息
  96. */
  97. public function crop(): BelongsTo
  98. {
  99. return $this->belongsTo(\App\Module\Farm\Models\FarmCropUser::class, 'crop_id');
  100. }
  101. /**
  102. * 获取物品信息
  103. */
  104. public function item(): BelongsTo
  105. {
  106. return $this->belongsTo(\App\Module\GameItems\Models\ItemConfig::class, 'item_id');
  107. }
  108. /**
  109. * 获取偷菜状态枚举
  110. */
  111. public function getStealStatusEnum(): STEAL_STATUS
  112. {
  113. return STEAL_STATUS::from($this->steal_status);
  114. }
  115. /**
  116. * 获取偷菜状态描述
  117. */
  118. public function getStealStatusDescAttribute(): string
  119. {
  120. return $this->getStealStatusEnum()->getDescription();
  121. }
  122. /**
  123. * 获取偷菜状态颜色
  124. */
  125. public function getStealStatusColorAttribute(): string
  126. {
  127. return $this->getStealStatusEnum()->getColor();
  128. }
  129. /**
  130. * 是否偷菜成功
  131. */
  132. public function isSuccess(): bool
  133. {
  134. return $this->getStealStatusEnum()->isSuccess();
  135. }
  136. /**
  137. * 是否偷菜失败
  138. */
  139. public function isFailed(): bool
  140. {
  141. return $this->getStealStatusEnum()->isFailed();
  142. }
  143. /**
  144. * 获取偷菜收益率
  145. */
  146. public function getYieldRateAttribute(): float
  147. {
  148. if ($this->original_amount <= 0) {
  149. return 0;
  150. }
  151. return round($this->item_amount / $this->original_amount, 4);
  152. }
  153. /**
  154. * 获取格式化的偷菜时间
  155. */
  156. public function getFormattedStealTimeAttribute(): string
  157. {
  158. return $this->steal_time->format('Y-m-d H:i:s');
  159. }
  160. /**
  161. * 获取偷菜时间距离现在的描述
  162. */
  163. public function getStealTimeHumanAttribute(): string
  164. {
  165. return $this->steal_time->diffForHumans();
  166. }
  167. /**
  168. * 作用域:按偷菜者查询
  169. */
  170. public function scopeByStealer($query, int $stealerId)
  171. {
  172. return $query->where('stealer_id', $stealerId);
  173. }
  174. /**
  175. * 作用域:按农场主查询
  176. */
  177. public function scopeByOwner($query, int $ownerId)
  178. {
  179. return $query->where('owner_id', $ownerId);
  180. }
  181. /**
  182. * 作用域:按偷菜状态查询
  183. */
  184. public function scopeByStatus($query, int $status)
  185. {
  186. return $query->where('steal_status', $status);
  187. }
  188. /**
  189. * 作用域:成功的偷菜记录
  190. */
  191. public function scopeSuccessful($query)
  192. {
  193. return $query->where('steal_status', STEAL_STATUS::SUCCESS->value);
  194. }
  195. /**
  196. * 作用域:失败的偷菜记录
  197. */
  198. public function scopeFailed($query)
  199. {
  200. return $query->where('steal_status', '!=', STEAL_STATUS::SUCCESS->value);
  201. }
  202. /**
  203. * 作用域:按时间范围查询
  204. */
  205. public function scopeByTimeRange($query, string $startTime, string $endTime)
  206. {
  207. return $query->whereBetween('steal_time', [$startTime, $endTime]);
  208. }
  209. /**
  210. * 作用域:今日记录
  211. */
  212. public function scopeToday($query)
  213. {
  214. return $query->whereDate('steal_time', today());
  215. }
  216. /**
  217. * 作用域:本周记录
  218. */
  219. public function scopeThisWeek($query)
  220. {
  221. return $query->whereBetween('steal_time', [
  222. now()->startOfWeek(),
  223. now()->endOfWeek()
  224. ]);
  225. }
  226. /**
  227. * 作用域:本月记录
  228. */
  229. public function scopeThisMonth($query)
  230. {
  231. return $query->whereMonth('steal_time', now()->month)
  232. ->whereYear('steal_time', now()->year);
  233. }
  234. /**
  235. * 获取用户今日偷菜次数
  236. */
  237. public static function getTodayStealCount(int $stealerId): int
  238. {
  239. return static::byStealer($stealerId)
  240. ->today()
  241. ->count();
  242. }
  243. /**
  244. * 获取用户今日被偷次数
  245. */
  246. public static function getTodayStolenCount(int $ownerId): int
  247. {
  248. return static::byOwner($ownerId)
  249. ->today()
  250. ->successful()
  251. ->count();
  252. }
  253. /**
  254. * 获取用户偷菜统计
  255. */
  256. public static function getStealStats(int $userId, string $date = null): array
  257. {
  258. $date = $date ?: today()->toDateString();
  259. $stealCount = static::byStealer($userId)
  260. ->whereDate('steal_time', $date)
  261. ->successful()
  262. ->count();
  263. $stolenCount = static::byOwner($userId)
  264. ->whereDate('steal_time', $date)
  265. ->successful()
  266. ->count();
  267. $itemsGained = static::byStealer($userId)
  268. ->whereDate('steal_time', $date)
  269. ->successful()
  270. ->sum('item_amount');
  271. $itemsLost = static::byOwner($userId)
  272. ->whereDate('steal_time', $date)
  273. ->successful()
  274. ->sum('item_amount');
  275. return [
  276. 'steal_count' => $stealCount,
  277. 'stolen_count' => $stolenCount,
  278. 'items_gained' => $itemsGained,
  279. 'items_lost' => $itemsLost,
  280. ];
  281. }
  282. }