ItemTransactionLog.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 $dates = [
  54. 'expire_at',
  55. 'created_at',
  56. ];
  57. /**
  58. * 应该被转换为原生类型的属性
  59. *
  60. * @var array
  61. */
  62. protected $casts = [
  63. 'details' => \App\Module\GameItems\Casts\TransactionDetailsCast::class,
  64. 'quantity' => 'integer',
  65. 'transaction_type' => 'integer',
  66. ];
  67. /**
  68. * 交易类型常量
  69. */
  70. const TYPE_ACQUIRE = 1; // 获取
  71. const TYPE_CONSUME = 2; // 消耗
  72. const TYPE_TRADE_IN = 3; // 交易获得
  73. const TYPE_TRADE_OUT = 4; // 交易失去
  74. const TYPE_EXPIRE = 5; // 过期失效
  75. const TYPE_DELETE = 6; // 删除
  76. const TYPE_CRAFT = 7; // 合成
  77. const TYPE_DISMANTLE = 8; // 分解
  78. /**
  79. * 获取关联的物品
  80. *
  81. * @return BelongsTo
  82. */
  83. public function item(): BelongsTo
  84. {
  85. return $this->belongsTo(Item::class, 'item_id');
  86. }
  87. /**
  88. * 获取关联的物品实例(如果有)
  89. *
  90. * @return BelongsTo
  91. */
  92. public function instance(): BelongsTo
  93. {
  94. return $this->belongsTo(ItemInstance::class, 'instance_id');
  95. }
  96. /**
  97. * 获取交易类型名称
  98. *
  99. * @return string
  100. */
  101. public function getTransactionTypeName(): string
  102. {
  103. $types = [
  104. self::TYPE_ACQUIRE => '获取',
  105. self::TYPE_CONSUME => '消耗',
  106. self::TYPE_TRADE_IN => '交易获得',
  107. self::TYPE_TRADE_OUT => '交易失去',
  108. self::TYPE_EXPIRE => '过期失效',
  109. self::TYPE_DELETE => '删除',
  110. self::TYPE_CRAFT => '合成',
  111. self::TYPE_DISMANTLE => '分解',
  112. ];
  113. return $types[$this->transaction_type] ?? '未知';
  114. }
  115. }