海南旅游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.

142 lines
3.0 KiB

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