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.

157 lines
4.0 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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. 'updated_at_text',
  51. 'state_text',
  52. 'pay_time_text',
  53. 'pay_type_text',
  54. 'shipping_type_text',
  55. 'complete_time_text',
  56. 'delivery_time_text',
  57. ];
  58. protected $casts = [
  59. 'global_order_id' => 'string'
  60. ];
  61. public function getCreatedAtTextAttribute()
  62. {
  63. return date('Y-m-d H:i:s', $this->attributes['created_at']);
  64. }
  65. public function getUpdatedAtTextAttribute()
  66. {
  67. return date('Y-m-d H:i:s', $this->attributes['updated_at']);
  68. }
  69. public function getPayTimeTextAttribute()
  70. {
  71. return date('Y-m-d H:i:s', $this->attributes['pay_time']);
  72. }
  73. public function getCompleteTimeTextAttribute()
  74. {
  75. return date('Y-m-d H:i:s', $this->attributes['complete_time']);
  76. }
  77. public function getDeliveryTimeTextAttribute()
  78. {
  79. return date('Y-m-d H:i:s', $this->attributes['delivery_time']);
  80. }
  81. public function getStateTextAttribute()
  82. {
  83. if ($this->attributes['state'] == OrderState::DELIVERY) {
  84. if($this->attributes['shipping_type'] == 3) {
  85. return '待自提';
  86. }elseif ($this->attributes['shipping_type'] == 1 && empty($this->attributes['horseman_id'])){
  87. return '已接单';
  88. }
  89. }
  90. return OrderState::getMessage($this->attributes['state']);
  91. }
  92. public function getPayTypeTextAttribute()
  93. {
  94. return Payment::getMessage($this->attributes['pay_type']);
  95. }
  96. public function getShippingTypeTextAttribute()
  97. {
  98. return Shipping::getMessage($this->attributes['shipping_type']);
  99. }
  100. public function getNoteAttribute($value)
  101. {
  102. if ($this->attributes['refuse_refund_note'] || $this->attributes['total_refund_note']) {
  103. return $this->attributes['refuse_refund_note'] ?: $this->attributes['total_refund_note'];
  104. }
  105. return $value;
  106. }
  107. public function market()
  108. {
  109. return $this->belongsTo(Market::class, 'market_id', 'id');
  110. }
  111. public function orders()
  112. {
  113. return $this->hasMany(Order::class, 'order_main_id', 'global_order_id');
  114. }
  115. public function orderGoods()
  116. {
  117. // firstKey是中间表联当前表的列,secondKey是远程表对应中间表的列,localKey是当前表关联中间表的列,secondLocalKey是中间表关联远程表的列
  118. return $this->hasManyThrough(
  119. OrderGoods::class,
  120. Order::class,
  121. 'order_main_id',
  122. 'order_id',
  123. 'global_order_id',
  124. 'id'
  125. );
  126. }
  127. public function user()
  128. {
  129. return $this->belongsTo(User::class, 'user_id', 'id');
  130. }
  131. public function employees()
  132. {
  133. return $this->belongsTo(Employees::class, 'horseman_id', 'id');
  134. }
  135. }