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.

137 lines
3.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Model\v3;
  3. use App\Constants\v3\OrderState;
  4. use App\Constants\v3\Payment;
  5. use App\Constants\v3\Shipping;
  6. use App\Model\Model;
  7. use Hyperf\Database\Model\SoftDeletes;
  8. class OrderMain extends Model
  9. {
  10. use SoftDeletes;
  11. protected $table = 'lanzu_order_main';
  12. protected $guarded = [];
  13. // protected $fillable = [
  14. // 'market_id',
  15. // 'order_num',
  16. // 'global_order_id',
  17. // 'user_id',
  18. // 'pay_type',
  19. // 'type',
  20. // 'order_type',
  21. // 'shipping_type',
  22. // 'money',
  23. // 'total_money',
  24. // 'services_money',
  25. // 'coupon_money',
  26. // 'delivery_money',
  27. // 'state',
  28. // 'pay_time',
  29. // 'receive_time',
  30. // 'delivery_time',
  31. // 'complete_time',
  32. // 'cancel_time',
  33. // 'refund_time',
  34. // 'tel',
  35. // 'address',
  36. // 'area',
  37. // 'lat',
  38. // 'lng',
  39. // 'name',
  40. // 'print_num',
  41. // 'plat',
  42. // 'refuse_refund_note',
  43. // 'delivery_time_note',
  44. // 'total_refund_note',
  45. // 'note',
  46. // 'delivery_distance'
  47. // ];
  48. protected $appends = [
  49. 'created_at_text',
  50. 'state_text',
  51. 'pay_time_text',
  52. 'pay_type_text',
  53. 'shipping_type_text',
  54. ];
  55. protected $casts = [
  56. 'global_order_id' => 'string'
  57. ];
  58. public function getCreatedAtTextAttribute()
  59. {
  60. return date('Y-m-d H:i:s', $this->attributes['created_at']);
  61. }
  62. public function getPayTimeTextAttribute()
  63. {
  64. return date('Y-m-d H:i:s', $this->attributes['pay_time']);
  65. }
  66. public function getStateTextAttribute()
  67. {
  68. if ($this->attributes['state'] == 3) {
  69. if (!$this->attributes['horseman_id']) {
  70. return '已接单';
  71. }
  72. }
  73. return OrderState::getMessage($this->attributes['state']);
  74. }
  75. public function getPayTypeTextAttribute()
  76. {
  77. return Payment::getMessage($this->attributes['pay_type']);
  78. }
  79. public function getShippingTypeTextAttribute()
  80. {
  81. return Shipping::getMessage($this->attributes['shipping_type']);
  82. }
  83. public function getNoteAttribute($value)
  84. {
  85. if ($this->attributes['refuse_refund_note'] || $this->attributes['total_refund_note']) {
  86. return $this->attributes['refuse_refund_note'] ?: $this->attributes['total_refund_note'];
  87. }
  88. return $value;
  89. }
  90. public function market()
  91. {
  92. return $this->belongsTo(Market::class, 'market_id', 'id');
  93. }
  94. public function orders()
  95. {
  96. return $this->hasMany(Order::class, 'order_main_id', 'global_order_id');
  97. }
  98. public function orderGoods()
  99. {
  100. // firstKey是中间表联当前表的列,secondKey是远程表对应中间表的列,localKey是当前表关联中间表的列,secondLocalKey是中间表关联远程表的列
  101. return $this->hasManyThrough(
  102. OrderGoods::class,
  103. Order::class,
  104. 'order_main_id',
  105. 'order_id',
  106. 'global_order_id',
  107. 'id'
  108. );
  109. }
  110. public function user()
  111. {
  112. return $this->belongsTo(User::class, 'user_id', 'id');
  113. }
  114. public function employees()
  115. {
  116. return $this->belongsTo(Employees::class, 'horseman_id', 'id');
  117. }
  118. }