海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
2.9 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Models;
  3. use App\Common\OrderStatus;
  4. use App\Common\PayType;
  5. use App\Events\OrderUpdated;
  6. use Illuminate\Database\Eloquent\Factories\HasFactory;
  7. use Illuminate\Database\Eloquent\SoftDeletes;
  8. use App\Common\OrderStatus as Status;
  9. use Illuminate\Support\Facades\Storage;
  10. class Order extends BaseModel
  11. {
  12. use HasFactory, SoftDeletes;
  13. protected $guarded = ['created_at', 'updated_at']; //不可批量赋值的属性
  14. protected $dispatchesEvents = [
  15. 'updated' => OrderUpdated::class,
  16. ];
  17. public function scopeComplete($query)
  18. {
  19. return $query->where('status',OrderStatus::SUCCESS);
  20. }
  21. public function getCouponIdAttribute($value)
  22. {
  23. return explode(',', $value);
  24. }
  25. public function getStatusAttribute($value)
  26. {
  27. $this->append('status_text');
  28. return $value;
  29. }
  30. public function getStatusTextAttribute()
  31. {
  32. return Status::array()[$this->attributes['status']] ?? '未知';
  33. }
  34. public function getPayTypeAttribute($value)
  35. {
  36. $this->append('pay_type_text');
  37. return $value;
  38. }
  39. public function getPayTypeTextAttribute()
  40. {
  41. return PayType::array()[$this->attributes['pay_type']] ?? '未知';
  42. }
  43. //退款信息
  44. public function getRefundInfoAttribute($value): array
  45. {
  46. $value = $value ? json_decode($value, true) : [];
  47. if (!empty($value['pictures']) && is_array($value['pictures'])) {
  48. $prefix = Storage::disk('public')->url('');
  49. foreach ($value['pictures'] as &$v) {
  50. if ($v && strpos($v, $prefix) === false) {
  51. $v = $prefix . $v;
  52. }
  53. }
  54. }
  55. return $value ?? [];
  56. }
  57. public function getPictureAttribute($value): string
  58. {
  59. $prefix = Storage::disk('public')->url('');
  60. return $value && substr($value, 0, 4) != 'http' ? $prefix . $value : '';
  61. }
  62. public function setPictureAttribute($value)
  63. {
  64. //修改器里面$this->host变量为空
  65. $this->attributes['picture'] = str_replace(env('APP_URL'), '', $value);
  66. }
  67. public function agentProduct()
  68. {
  69. return $this->belongsTo(AgentProduct::class);
  70. }
  71. public function product()
  72. {
  73. return $this->belongsTo(Product::class);
  74. }
  75. public function coupon()
  76. {
  77. return $this->belongsTo(Coupon::class);
  78. }
  79. public function agent()
  80. {
  81. return $this->belongsTo(Agent::class);
  82. }
  83. public function supplier()
  84. {
  85. return $this->belongsTo(Supplier::class);
  86. }
  87. public function user()
  88. {
  89. return $this->belongsTo(User::class);
  90. }
  91. public function agentProductItem()
  92. {
  93. return $this->hasMany(AgentProductItem::class, 'agent_product_id', 'agent_product_id');
  94. }
  95. public function orderProductItem()
  96. {
  97. return $this->hasMany(OrderProductItem::class);
  98. }
  99. public function guide()
  100. {
  101. return $this->belongsTo(Guide::class);
  102. }
  103. //关联的组团云产品
  104. public function agentCloud()
  105. {
  106. return $this->belongsTo(AgentProduct::class, 'agent_cloud_pid', 'id');
  107. }
  108. public function statementAdmin()
  109. {
  110. return $this->hasMany(StatementAdmin::class,'order_id','id');
  111. }
  112. public function statement()
  113. {
  114. return $this->morphMany(Statement::class,'access');
  115. }
  116. }