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

126 lines
2.6 KiB

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