ItemTransactionLog.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace App\Module\GameItems\Models;
  3. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  4. use UCore\ModelCore;
  5. /**
  6. * 物品交易记录
  7. *
  8. * field start
  9. * @property int $id 记录ID,主键
  10. * @property int $user_id 用户ID
  11. * @property int $item_id 物品ID,外键关联kku_item_items表
  12. * @property int $instance_id 单独属性物品ID,外键关联kku_item_instances表(可为空)
  13. * @property int $quantity 数量(正数表示获取,负数表示消耗)
  14. * @property int $transaction_type 交易类型(1:获取, 2:消耗, 3:交易获得, 4:交易失去, 5:过期失效)
  15. * @property string $source_type 来源类型(如任务奖励、商店购买、宝箱开启等)
  16. * @property int $source_id 来源ID(如任务ID、订单ID、宝箱ID等)
  17. * @property object|array $attributes 额外属性,存储交易相关的详细信息
  18. * @property string $expire_at 物品过期时间(如果有)
  19. * @property \Carbon\Carbon $created_at 创建时间
  20. * @property string $ip_address 操作的IP地址(用于安全审计)
  21. * @property string $device_info 设备信息(用于安全审计)
  22. * field end
  23. */
  24. class ItemTransactionLog extends ModelCore
  25. {
  26. /**
  27. * 与模型关联的表名
  28. *
  29. * @var string
  30. */
  31. protected $table = 'item_transaction_logs';
  32. // attrlist start
  33. protected $fillable = [
  34. 'id',
  35. 'user_id',
  36. 'item_id',
  37. 'instance_id',
  38. 'quantity',
  39. 'transaction_type',
  40. 'source_type',
  41. 'source_id',
  42. 'attributes',
  43. 'expire_at',
  44. 'ip_address',
  45. 'device_info',
  46. ];
  47. // attrlist end
  48. /**
  49. * 可批量赋值的属性
  50. *
  51. * @var array
  52. */
  53. protected $fillable = [
  54. 'user_id',
  55. 'item_id',
  56. 'instance_id',
  57. 'quantity',
  58. 'transaction_type',
  59. 'source_type',
  60. 'source_id',
  61. 'details',
  62. 'expire_at',
  63. 'ip_address',
  64. 'device_info',
  65. ];
  66. /**
  67. * 应该被转换为日期的属性
  68. *
  69. * @var array
  70. */
  71. protected $dates = [
  72. 'expire_at',
  73. 'created_at',
  74. ];
  75. /**
  76. * 应该被转换为原生类型的属性
  77. *
  78. * @var array
  79. */
  80. protected $casts = [
  81. 'details' => \App\Module\GameItems\Casts\TransactionDetailsCast::class,
  82. 'quantity' => 'integer',
  83. 'transaction_type' => 'integer',
  84. ];
  85. /**
  86. * 交易类型常量
  87. */
  88. const TYPE_ACQUIRE = 1; // 获取
  89. const TYPE_CONSUME = 2; // 消耗
  90. const TYPE_TRADE_IN = 3; // 交易获得
  91. const TYPE_TRADE_OUT = 4; // 交易失去
  92. const TYPE_EXPIRE = 5; // 过期失效
  93. const TYPE_DELETE = 6; // 删除
  94. const TYPE_CRAFT = 7; // 合成
  95. const TYPE_DISMANTLE = 8; // 分解
  96. /**
  97. * 获取关联的物品
  98. *
  99. * @return BelongsTo
  100. */
  101. public function item(): BelongsTo
  102. {
  103. return $this->belongsTo(ItemItem::class, 'item_id');
  104. }
  105. /**
  106. * 获取关联的物品实例(如果有)
  107. *
  108. * @return BelongsTo
  109. */
  110. public function instance(): BelongsTo
  111. {
  112. return $this->belongsTo(ItemInstance::class, 'instance_id');
  113. }
  114. /**
  115. * 获取交易类型名称
  116. *
  117. * @return string
  118. */
  119. public function getTransactionTypeName(): string
  120. {
  121. $types = [
  122. self::TYPE_ACQUIRE => '获取',
  123. self::TYPE_CONSUME => '消耗',
  124. self::TYPE_TRADE_IN => '交易获得',
  125. self::TYPE_TRADE_OUT => '交易失去',
  126. self::TYPE_EXPIRE => '过期失效',
  127. self::TYPE_DELETE => '删除',
  128. self::TYPE_CRAFT => '合成',
  129. self::TYPE_DISMANTLE => '分解',
  130. ];
  131. return $types[$this->transaction_type] ?? '未知';
  132. }
  133. }