ItemTransactionLog.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 \Carbon\Carbon $updated_at 更新时间
  21. * @property string $ip_address 操作的IP地址(用于安全审计)
  22. * @property string $device_info 设备信息(用于安全审计)
  23. * field end
  24. */
  25. class ItemTransactionLog extends ModelCore
  26. {
  27. /**
  28. * 与模型关联的表名
  29. *
  30. * @var string
  31. */
  32. protected $table = 'item_transaction_logs';
  33. // attrlist start
  34. protected $fillable = [
  35. 'id',
  36. 'user_id',
  37. 'item_id',
  38. 'instance_id',
  39. 'quantity',
  40. 'transaction_type',
  41. 'source_type',
  42. 'source_id',
  43. 'attributes',
  44. 'expire_at',
  45. 'ip_address',
  46. 'device_info',
  47. ];
  48. // attrlist end
  49. /**
  50. * 应该被转换为日期的属性
  51. *
  52. * @var array
  53. */
  54. protected $dates = [
  55. 'expire_at',
  56. 'created_at',
  57. ];
  58. /**
  59. * 应该被转换为原生类型的属性
  60. *
  61. * @var array
  62. */
  63. protected $casts = [
  64. 'details' => \App\Module\GameItems\Casts\TransactionDetailsCast::class,
  65. 'quantity' => 'integer',
  66. 'transaction_type' => 'integer',
  67. ];
  68. /**
  69. * 交易类型常量
  70. */
  71. const TYPE_ACQUIRE = 1; // 获取
  72. const TYPE_CONSUME = 2; // 消耗
  73. const TYPE_TRADE_IN = 3; // 交易获得
  74. const TYPE_TRADE_OUT = 4; // 交易失去
  75. const TYPE_EXPIRE = 5; // 过期失效
  76. const TYPE_DELETE = 6; // 删除
  77. const TYPE_CRAFT = 7; // 合成
  78. const TYPE_DISMANTLE = 8; // 分解
  79. /**
  80. * 获取关联的物品
  81. *
  82. * @return BelongsTo
  83. */
  84. public function item(): BelongsTo
  85. {
  86. return $this->belongsTo(Item::class, 'item_id');
  87. }
  88. /**
  89. * 获取关联的物品实例(如果有)
  90. *
  91. * @return BelongsTo
  92. */
  93. public function instance(): BelongsTo
  94. {
  95. return $this->belongsTo(ItemInstance::class, 'instance_id');
  96. }
  97. /**
  98. * 获取交易类型名称
  99. *
  100. * @return string
  101. */
  102. public function getTransactionTypeName(): string
  103. {
  104. $types = [
  105. self::TYPE_ACQUIRE => '获取',
  106. self::TYPE_CONSUME => '消耗',
  107. self::TYPE_TRADE_IN => '交易获得',
  108. self::TYPE_TRADE_OUT => '交易失去',
  109. self::TYPE_EXPIRE => '过期失效',
  110. self::TYPE_DELETE => '删除',
  111. self::TYPE_CRAFT => '合成',
  112. self::TYPE_DISMANTLE => '分解',
  113. ];
  114. return $types[$this->transaction_type] ?? '未知';
  115. }
  116. }