ShopItem.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Module\Shop\Models;
  3. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  4. use Illuminate\Support\Facades\DB;
  5. use UCore\ModelCore;
  6. use App\Module\GameItems\Models\Item;
  7. /**
  8. * 商店物品模型
  9. *
  10. * field start
  11. * @property int $id 商品ID,主键
  12. * @property string $name 商品名称
  13. * @property string $description 商品描述
  14. * @property int $item_id 关联的物品ID,外键关联kku_item_items表
  15. * @property int $item_quantity 物品数量
  16. * @property int $price 价格
  17. * @property int $currency_id 货币类型ID
  18. * @property int $max_buy 最大购买数量(0表示无限制)
  19. * @property int $is_active 是否激活(0:否, 1:是)
  20. * @property int $sort_order 排序权重
  21. * @property string $start_time 上架时间
  22. * @property string $end_time 下架时间
  23. * @property \Carbon\Carbon $created_at 创建时间
  24. * @property \Carbon\Carbon $updated_at 更新时间
  25. * field end
  26. */
  27. class ShopItem extends ModelCore
  28. {
  29. /**
  30. * 与模型关联的表名
  31. *
  32. * @var string
  33. */
  34. protected $table = 'shop_items';
  35. /**
  36. * 可批量赋值的属性
  37. *
  38. * @var array
  39. */
  40. protected $fillable = [
  41. 'name',
  42. 'description',
  43. 'item_id',
  44. 'item_quantity',
  45. 'price',
  46. 'currency_id',
  47. 'max_buy',
  48. 'is_active',
  49. 'sort_order',
  50. 'start_time',
  51. 'end_time',
  52. ];
  53. /**
  54. * 应该被转换为日期的属性
  55. *
  56. * @var array
  57. */
  58. protected $dates = [
  59. 'start_time',
  60. 'end_time',
  61. 'created_at',
  62. 'updated_at',
  63. ];
  64. /**
  65. * 应该被转换为原生类型的属性
  66. *
  67. * @var array
  68. */
  69. protected $casts = [
  70. 'is_active' => 'boolean',
  71. ];
  72. /**
  73. * 获取关联的物品
  74. *
  75. * @return BelongsTo
  76. */
  77. public function item(): BelongsTo
  78. {
  79. return $this->belongsTo(Item::class, 'item_id');
  80. }
  81. /**
  82. * 获取用户已购买数量
  83. *
  84. * @param int $userId 用户ID
  85. * @return int 已购买数量
  86. */
  87. public function getUserBoughtCount(int $userId): int
  88. {
  89. return ShopPurchaseLog::where('user_id', $userId)
  90. ->where('shop_item_id', $this->id)
  91. ->sum('quantity');
  92. }
  93. /**
  94. * 记录购买记录
  95. *
  96. * @param int $userId 用户ID
  97. * @param int $quantity 购买数量
  98. * @param int $totalPrice 总价
  99. * @return ShopPurchaseLog 购买记录
  100. */
  101. public function recordPurchase(int $userId, int $quantity, int $totalPrice): ShopPurchaseLog
  102. {
  103. $log = new ShopPurchaseLog([
  104. 'user_id' => $userId,
  105. 'shop_item_id' => $this->id,
  106. 'item_id' => $this->item_id,
  107. 'quantity' => $quantity,
  108. 'price' => $this->price,
  109. 'total_price' => $totalPrice,
  110. 'currency_id' => $this->currency_id,
  111. 'purchase_time' => now(),
  112. 'ip_address' => request()->ip(),
  113. 'device_info' => request()->userAgent(),
  114. ]);
  115. $log->save();
  116. return $log;
  117. }
  118. }