ItemFreezeLog.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace App\Module\GameItems\Models;
  3. use App\Module\GameItems\Enums\FREEZE_ACTION_TYPE;
  4. use App\Module\GameItems\Enums\FREEZE_REASON_TYPE;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. use UCore\ModelCore;
  7. /**
  8. * 物品冻结记录
  9. *
  10. * field start
  11. * @property int $id 记录ID,主键
  12. * @property int $user_id 用户ID
  13. * @property int $item_id 物品ID
  14. * @property int $instance_id 物品实例ID(单独属性物品)
  15. * @property int $quantity 冻结数量
  16. * @property \App\Module\GameItems\Enums\FREEZE_ACTION_TYPE $action_type 操作类型(1:冻结, 2:解冻)
  17. * @property string $reason 操作原因
  18. * @property int $source_id 操作方记录ID
  19. * @property string $source_type 操作类型(如:order, admin, system等)
  20. * @property int $operator_id 操作员ID(系统操作为NULL)
  21. * @property \Carbon\Carbon $created_at 操作时间
  22. * @property \Carbon\Carbon $updated_at 更新时间
  23. * field end
  24. */
  25. class ItemFreezeLog extends ModelCore
  26. {
  27. /**
  28. * 与模型关联的表名
  29. *
  30. * @var string
  31. */
  32. protected $table = 'item_freeze_logs';
  33. // attrlist start
  34. protected $fillable = [
  35. 'id',
  36. 'user_id',
  37. 'item_id',
  38. 'instance_id',
  39. 'quantity',
  40. 'action_type',
  41. 'reason',
  42. 'source_id',
  43. 'source_type',
  44. 'operator_id',
  45. ];
  46. // attrlist end
  47. /**
  48. * 应该被转换为原生类型的属性
  49. *
  50. * @var array
  51. */
  52. protected $casts = [
  53. 'action_type' => FREEZE_ACTION_TYPE::class,
  54. 'quantity' => 'integer',
  55. 'user_id' => 'integer',
  56. 'item_id' => 'integer',
  57. 'instance_id' => 'integer',
  58. 'source_id' => 'integer',
  59. 'operator_id' => 'integer',
  60. ];
  61. /**
  62. * 获取关联的物品
  63. *
  64. * @return BelongsTo
  65. */
  66. public function item(): BelongsTo
  67. {
  68. return $this->belongsTo(Item::class, 'item_id');
  69. }
  70. /**
  71. * 获取关联的物品实例(如果有)
  72. *
  73. * @return BelongsTo
  74. */
  75. public function instance(): BelongsTo
  76. {
  77. return $this->belongsTo(ItemInstance::class, 'instance_id');
  78. }
  79. /**
  80. * 获取关联的用户物品记录
  81. *
  82. * @return BelongsTo
  83. */
  84. public function itemUser(): BelongsTo
  85. {
  86. return $this->hasOne(ItemUser::class, 'frozen_log_id', 'id');
  87. }
  88. /**
  89. * 检查是否为冻结操作
  90. *
  91. * @return bool
  92. */
  93. public function isFreeze(): bool
  94. {
  95. return $this->action_type === FREEZE_ACTION_TYPE::FREEZE;
  96. }
  97. /**
  98. * 检查是否为解冻操作
  99. *
  100. * @return bool
  101. */
  102. public function isUnfreeze(): bool
  103. {
  104. return $this->action_type === FREEZE_ACTION_TYPE::UNFREEZE;
  105. }
  106. /**
  107. * 获取操作类型名称
  108. *
  109. * @return string
  110. */
  111. public function getActionTypeName(): string
  112. {
  113. return $this->action_type->getName($this->action_type->value);
  114. }
  115. /**
  116. * 获取格式化的操作描述
  117. *
  118. * @return string
  119. */
  120. public function getOperationDescription(): string
  121. {
  122. $actionName = $this->getActionTypeName();
  123. $itemName = $this->item->name ?? "物品ID:{$this->item_id}";
  124. if ($this->instance_id) {
  125. return "{$actionName}单独属性物品: {$itemName} (实例ID: {$this->instance_id})";
  126. } else {
  127. return "{$actionName}统一属性物品: {$itemName} x{$this->quantity}";
  128. }
  129. }
  130. /**
  131. * 创建冻结记录
  132. *
  133. * @param int $userId 用户ID
  134. * @param int $itemId 物品ID
  135. * @param int|null $instanceId 物品实例ID
  136. * @param int $quantity 数量
  137. * @param FREEZE_ACTION_TYPE $actionType 操作类型
  138. * @param string $reason 操作原因
  139. * @param int|null $sourceId 来源ID
  140. * @param string|null $sourceType 来源类型
  141. * @param int|null $operatorId 操作员ID
  142. * @return static
  143. */
  144. public static function createLog(
  145. int $userId,
  146. int $itemId,
  147. ?int $instanceId,
  148. int $quantity,
  149. FREEZE_ACTION_TYPE $actionType,
  150. string $reason,
  151. ?int $sourceId = null,
  152. ?string $sourceType = null,
  153. ?int $operatorId = null
  154. ): self {
  155. return self::create([
  156. 'user_id' => $userId,
  157. 'item_id' => $itemId,
  158. 'instance_id' => $instanceId,
  159. 'quantity' => $quantity,
  160. 'action_type' => $actionType,
  161. 'reason' => $reason,
  162. 'source_id' => $sourceId,
  163. 'source_type' => $sourceType,
  164. 'operator_id' => $operatorId,
  165. ]);
  166. }
  167. }