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.

129 lines
3.0 KiB

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