MexPriceAdjustment.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Module\Mex\Models;
  3. use App\Module\GameItems\Models\Item;
  4. use App\Module\Mex\Enums\PriceAdjustmentType;
  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 $price_config_id 价格配置ID,关联mex_price_configs表
  13. * @property int $item_id 商品ID,关联物品表
  14. * @property int $admin_user_id 操作管理员用户ID
  15. * @property \App\Module\Mex\Enums\PriceAdjustmentType $adjustment_type 调整类型:MIN_PRICE最低价,MAX_PRICE最高价,PROTECTION_THRESHOLD保护阈值,STATUS启用状态,BATCH批量调整
  16. * @property float $old_min_price 调整前最低价
  17. * @property float $new_min_price 调整后最低价
  18. * @property float $old_max_price 调整前最高价
  19. * @property float $new_max_price 调整后最高价
  20. * @property int $old_protection_threshold 调整前保护阈值
  21. * @property int $new_protection_threshold 调整后保护阈值
  22. * @property bool $old_is_enabled 调整前启用状态
  23. * @property bool $new_is_enabled 调整后启用状态
  24. * @property string $adjustment_reason 调整原因
  25. * @property string $market_impact_note 市场影响说明
  26. * @property \Carbon\Carbon $created_at 调整时间
  27. * field end
  28. */
  29. class MexPriceAdjustment extends ModelCore
  30. {
  31. protected $table = 'mex_price_adjustments';
  32. // 禁用updated_at字段的自动管理,因为表中只有created_at字段
  33. public const UPDATED_AT = null;
  34. protected $fillable = [
  35. 'price_config_id',
  36. 'item_id',
  37. 'admin_user_id',
  38. 'adjustment_type',
  39. 'old_min_price',
  40. 'new_min_price',
  41. 'old_max_price',
  42. 'new_max_price',
  43. 'old_protection_threshold',
  44. 'new_protection_threshold',
  45. 'old_is_enabled',
  46. 'new_is_enabled',
  47. 'adjustment_reason',
  48. 'market_impact_note',
  49. ];
  50. protected $casts = [
  51. 'price_config_id' => 'integer',
  52. 'item_id' => 'integer',
  53. 'admin_user_id' => 'integer',
  54. 'adjustment_type' => PriceAdjustmentType::class,
  55. 'old_min_price' => 'decimal:5',
  56. 'new_min_price' => 'decimal:5',
  57. 'old_max_price' => 'decimal:5',
  58. 'new_max_price' => 'decimal:5',
  59. 'old_protection_threshold' => 'integer',
  60. 'new_protection_threshold' => 'integer',
  61. 'old_is_enabled' => 'boolean',
  62. 'new_is_enabled' => 'boolean',
  63. ];
  64. /**
  65. * 获取关联的价格配置
  66. */
  67. public function priceConfig(): BelongsTo
  68. {
  69. return $this->belongsTo(MexPriceConfig::class, 'price_config_id');
  70. }
  71. /**
  72. * 获取关联的商品信息
  73. */
  74. public function item(): BelongsTo
  75. {
  76. return $this->belongsTo(Item::class, 'item_id');
  77. }
  78. /**
  79. * 获取价格变化摘要
  80. */
  81. public function getPriceChangeSummaryAttribute(): string
  82. {
  83. $changes = [];
  84. if ($this->old_min_price !== $this->new_min_price) {
  85. $changes[] = "最低价: {$this->old_min_price} → {$this->new_min_price}";
  86. }
  87. if ($this->old_max_price !== $this->new_max_price) {
  88. $changes[] = "最高价: {$this->old_max_price} → {$this->new_max_price}";
  89. }
  90. if ($this->old_protection_threshold !== $this->new_protection_threshold) {
  91. $changes[] = "保护阈值: {$this->old_protection_threshold} → {$this->new_protection_threshold}";
  92. }
  93. if ($this->old_is_enabled !== $this->new_is_enabled) {
  94. $oldStatus = $this->old_is_enabled ? '启用' : '禁用';
  95. $newStatus = $this->new_is_enabled ? '启用' : '禁用';
  96. $changes[] = "状态: {$oldStatus} → {$newStatus}";
  97. }
  98. return implode('; ', $changes);
  99. }
  100. }